Path: blob/master/src/jdk.dynalink/share/classes/jdk/dynalink/beans/FacetIntrospector.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.reflect.Field;64import java.lang.reflect.Member;65import java.lang.reflect.Method;66import java.lang.reflect.Modifier;67import java.util.ArrayList;68import java.util.Collection;69import java.util.Collections;70import java.util.Map;71import jdk.dynalink.linker.support.Lookup;7273/**74* Base for classes that expose class field and method information to an {@link AbstractJavaLinker}. There are75* subclasses for instance (bean) and static facet of a class.76*/77abstract class FacetIntrospector {78private final Class<?> clazz;79private final boolean instance;80private final boolean isRestricted;8182protected final AccessibleMembersLookup membersLookup;8384FacetIntrospector(final Class<?> clazz, final boolean instance) {85this.clazz = clazz;86this.instance = instance;87isRestricted = CheckRestrictedPackage.isRestrictedClass(clazz);88membersLookup = new AccessibleMembersLookup(clazz, instance);89}9091/**92* Returns getters for inner classes.93* @return getters for inner classes.94*/95abstract Map<String, MethodHandle> getInnerClassGetters();9697/**98* Returns getter methods for record components.99* @return getter methods for record components.100*/101abstract Collection<Method> getRecordComponentGetters();102103/**104* Returns the fields for the class facet.105* @return the fields for the class facet.106*/107Collection<Field> getFields() {108if(isRestricted) {109// NOTE: we can't do anything here. Unlike with methods in AccessibleMethodsLookup, we can't just return110// the fields from a public superclass, because this class might define same-named fields which will shadow111// the superclass fields, and we have no way to know if they do, since we're denied invocation of112// getFields(). Therefore, the only correct course of action is to not expose any public fields from a class113// defined in a restricted package.114return Collections.emptySet();115}116117final Field[] fields = clazz.getFields();118final Collection<Field> cfields = new ArrayList<>(fields.length);119for(final Field field: fields) {120final boolean isStatic = Modifier.isStatic(field.getModifiers());121if(isStatic && clazz != field.getDeclaringClass()) {122// ignore inherited static fields123continue;124}125126if(instance != isStatic && isAccessible(field)) {127cfields.add(field);128}129}130return cfields;131}132133boolean isAccessible(final Member m) {134final Class<?> declaring = m.getDeclaringClass();135// (declaring == clazz) is just an optimization - we're calling this only from code that operates on a136// non-restricted class, so if the declaring class is identical to the class being inspected, then forego137// a potentially expensive restricted-package check.138return declaring == clazz || !CheckRestrictedPackage.isRestrictedClass(declaring);139}140141/**142* Returns all the methods in the facet.143* @return all the methods in the facet.144*/145Collection<Method> getMethods() {146return membersLookup.getMethods();147}148149MethodHandle unreflectGetter(final Field field) {150return editMethodHandle(Lookup.PUBLIC.unreflectGetter(field));151}152153MethodHandle unreflectSetter(final Field field) {154return editMethodHandle(Lookup.PUBLIC.unreflectSetter(field));155}156157/**158* Returns an edited method handle. A facet might need to edit an unreflected method handle before it is usable with159* the facet. By default, returns the passed method handle unchanged. The class' static facet will introduce a160* dropArguments.161* @param mh the method handle to edit.162* @return the edited method handle.163*/164abstract MethodHandle editMethodHandle(MethodHandle mh);165}166167168