Path: blob/master/src/jdk.dynalink/share/classes/jdk/dynalink/beans/StaticClassLinker.java
41161 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file, and Oracle licenses the original version of this file under the BSD30* license:31*/32/*33Copyright 2009-2013 Attila Szegedi3435Redistribution and use in source and binary forms, with or without36modification, are permitted provided that the following conditions are37met:38* Redistributions of source code must retain the above copyright39notice, this list of conditions and the following disclaimer.40* Redistributions in binary form must reproduce the above copyright41notice, this list of conditions and the following disclaimer in the42documentation and/or other materials provided with the distribution.43* Neither the name of the copyright holder nor the names of44contributors may be used to endorse or promote products derived from45this software without specific prior written permission.4647THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS48IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED49TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A50PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER51BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR52CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF53SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR54BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,55WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR56OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF57ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.58*/5960package jdk.dynalink.beans;6162import java.lang.invoke.MethodHandle;63import java.lang.invoke.MethodHandles;64import java.lang.invoke.MethodType;65import java.lang.reflect.Array;66import java.util.Arrays;67import java.util.Set;68import jdk.dynalink.CallSiteDescriptor;69import jdk.dynalink.NamedOperation;70import jdk.dynalink.StandardNamespace;71import jdk.dynalink.StandardOperation;72import jdk.dynalink.beans.GuardedInvocationComponent.ValidationType;73import jdk.dynalink.linker.GuardedInvocation;74import jdk.dynalink.linker.LinkRequest;75import jdk.dynalink.linker.LinkerServices;76import jdk.dynalink.linker.TypeBasedGuardingDynamicLinker;77import jdk.dynalink.linker.support.Lookup;7879/**80* Provides a linker for the {@link StaticClass} objects.81*/82class StaticClassLinker implements TypeBasedGuardingDynamicLinker {83private static final ClassValue<SingleClassStaticsLinker> linkers = new ClassValue<>() {84@Override85protected SingleClassStaticsLinker computeValue(final Class<?> clazz) {86return new SingleClassStaticsLinker(clazz);87}88};8990private static class SingleClassStaticsLinker extends AbstractJavaLinker {91private final DynamicMethod constructor;9293SingleClassStaticsLinker(final Class<?> clazz) {94super(clazz, IS_CLASS.bindTo(clazz));95// Map "staticClassObject.class" to StaticClass.getRepresentedClass(). Some adventurous soul could subclass96// StaticClass, so we use INSTANCE_OF validation instead of EXACT_CLASS.97setPropertyGetter("class", GET_CLASS, ValidationType.INSTANCE_OF);98constructor = createConstructorMethod(clazz);99}100101/**102* Creates a dynamic method containing all overloads of a class' public constructor103* @param clazz the target class104* @return a dynamic method containing all overloads of a class' public constructor. If the class has no public105* constructors, returns null.106*/107private static DynamicMethod createConstructorMethod(final Class<?> clazz) {108if(clazz.isArray()) {109final MethodHandle boundArrayCtor = ARRAY_CTOR.bindTo(clazz.getComponentType());110return new SimpleDynamicMethod(StaticClassIntrospector.editConstructorMethodHandle(111boundArrayCtor.asType(boundArrayCtor.type().changeReturnType(clazz))), clazz, "<init>");112}113if(CheckRestrictedPackage.isRestrictedClass(clazz)) {114return null;115}116return createDynamicMethod(Arrays.asList(clazz.getConstructors()), clazz, "<init>");117}118119@Override120FacetIntrospector createFacetIntrospector() {121return new StaticClassIntrospector(clazz);122}123124@Override125public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices)126throws Exception {127final GuardedInvocation gi = super.getGuardedInvocation(request, linkerServices);128if(gi != null) {129return gi;130}131final CallSiteDescriptor desc = request.getCallSiteDescriptor();132if(NamedOperation.getBaseOperation(desc.getOperation()) == StandardOperation.NEW && constructor != null) {133final MethodHandle ctorInvocation = constructor.getInvocation(desc, linkerServices);134if(ctorInvocation != null) {135return new GuardedInvocation(ctorInvocation, getClassGuard(desc.getMethodType()));136}137}138return null;139}140141@Override142protected GuardedInvocationComponent getGuardedInvocationComponent(final ComponentLinkRequest req) throws Exception {143final GuardedInvocationComponent superGic = super.getGuardedInvocationComponent(req);144if (superGic != null) {145return superGic;146}147if (!req.namespaces.isEmpty()148&& req.namespaces.get(0) == StandardNamespace.ELEMENT149&& (req.baseOperation == StandardOperation.GET || req.baseOperation == StandardOperation.SET))150{151// StaticClass doesn't behave as a collection152return getNextComponent(req.popNamespace());153}154return null;155}156157@Override158SingleDynamicMethod getConstructorMethod(final String signature) {159return constructor != null? constructor.getMethodForExactParamTypes(signature) : null;160}161}162163static Object getConstructorMethod(final Class<?> clazz, final String signature) {164return linkers.get(clazz).getConstructorMethod(signature);165}166167static Set<String> getReadableStaticPropertyNames(final Class<?> clazz) {168return linkers.get(clazz).getReadablePropertyNames();169}170171static Set<String> getWritableStaticPropertyNames(final Class<?> clazz) {172return linkers.get(clazz).getWritablePropertyNames();173}174175static Set<String> getStaticMethodNames(final Class<?> clazz) {176return linkers.get(clazz).getMethodNames();177}178179@Override180public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception {181final Object receiver = request.getReceiver();182if(receiver instanceof StaticClass) {183return linkers.get(((StaticClass)receiver).getRepresentedClass()).getGuardedInvocation(request,184linkerServices);185}186return null;187}188189@Override190public boolean canLinkType(final Class<?> type) {191return type == StaticClass.class;192}193194/*private*/ static final MethodHandle GET_CLASS;195/*private*/ static final MethodHandle IS_CLASS;196/*private*/ static final MethodHandle ARRAY_CTOR = Lookup.PUBLIC.findStatic(Array.class, "newInstance",197MethodType.methodType(Object.class, Class.class, int.class));198199static {200final Lookup lookup = new Lookup(MethodHandles.lookup());201GET_CLASS = lookup.findVirtual(StaticClass.class, "getRepresentedClass", MethodType.methodType(Class.class));202IS_CLASS = lookup.findOwnStatic("isClass", Boolean.TYPE, Class.class, Object.class);203}204205@SuppressWarnings("unused")206private static boolean isClass(final Class<?> clazz, final Object obj) {207return obj instanceof StaticClass && ((StaticClass)obj).getRepresentedClass() == clazz;208}209}210211212