Path: blob/master/src/java.desktop/share/classes/com/sun/beans/finder/FieldFinder.java
41161 views
/*1* Copyright (c) 2008, 2012, 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*/24package com.sun.beans.finder;2526import java.lang.reflect.Field;27import java.lang.reflect.Modifier;2829import static sun.reflect.misc.ReflectUtil.isPackageAccessible;3031/**32* This utility class provides {@code static} methods33* to find a public field with specified name34* in specified class.35*36* @since 1.737*38* @author Sergey A. Malenkov39*/40public final class FieldFinder {4142/**43* Finds public field (static or non-static)44* that is declared in public class.45*46* @param type the class that can have field47* @param name the name of field to find48* @return object that represents found field49* @throws NoSuchFieldException if field is not found50* @see Class#getField51*/52public static Field findField(Class<?> type, String name) throws NoSuchFieldException {53if (name == null) {54throw new IllegalArgumentException("Field name is not set");55}56if (!FinderUtils.isExported(type)) {57throw new NoSuchFieldException("Field '" + name + "' is not accessible");58}59Field field = type.getField(name);60if (!Modifier.isPublic(field.getModifiers())) {61throw new NoSuchFieldException("Field '" + name + "' is not public");62}63type = field.getDeclaringClass();64if (!Modifier.isPublic(type.getModifiers()) || !isPackageAccessible(type)) {65throw new NoSuchFieldException("Field '" + name + "' is not accessible");66}67return field;68}6970/**71* Finds public non-static field72* that is declared in public class.73*74* @param type the class that can have field75* @param name the name of field to find76* @return object that represents found field77* @throws NoSuchFieldException if field is not found78* @see Class#getField79*/80public static Field findInstanceField(Class<?> type, String name) throws NoSuchFieldException {81Field field = findField(type, name);82if (Modifier.isStatic(field.getModifiers())) {83throw new NoSuchFieldException("Field '" + name + "' is static");84}85return field;86}8788/**89* Finds public static field90* that is declared in public class.91*92* @param type the class that can have field93* @param name the name of field to find94* @return object that represents found field95* @throws NoSuchFieldException if field is not found96* @see Class#getField97*/98public static Field findStaticField(Class<?> type, String name) throws NoSuchFieldException {99Field field = findField(type, name);100if (!Modifier.isStatic(field.getModifiers())) {101throw new NoSuchFieldException("Field '" + name + "' is not static");102}103return field;104}105106/**107* Disable instantiation.108*/109private FieldFinder() {110}111}112113114