Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/beans/finder/FieldFinder.java
38923 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}56Field field = type.getField(name);57if (!Modifier.isPublic(field.getModifiers())) {58throw new NoSuchFieldException("Field '" + name + "' is not public");59}60type = field.getDeclaringClass();61if (!Modifier.isPublic(type.getModifiers()) || !isPackageAccessible(type)) {62throw new NoSuchFieldException("Field '" + name + "' is not accessible");63}64return field;65}6667/**68* Finds public non-static field69* that is declared in public class.70*71* @param type the class that can have field72* @param name the name of field to find73* @return object that represents found field74* @throws NoSuchFieldException if field is not found75* @see Class#getField76*/77public static Field findInstanceField(Class<?> type, String name) throws NoSuchFieldException {78Field field = findField(type, name);79if (Modifier.isStatic(field.getModifiers())) {80throw new NoSuchFieldException("Field '" + name + "' is static");81}82return field;83}8485/**86* Finds public static field87* that is declared in public class.88*89* @param type the class that can have field90* @param name the name of field to find91* @return object that represents found field92* @throws NoSuchFieldException if field is not found93* @see Class#getField94*/95public static Field findStaticField(Class<?> type, String name) throws NoSuchFieldException {96Field field = findField(type, name);97if (!Modifier.isStatic(field.getModifiers())) {98throw new NoSuchFieldException("Field '" + name + "' is not static");99}100return field;101}102103/**104* Disable instantiation.105*/106private FieldFinder() {107}108}109110111