827N/A/*
5252N/A * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
827N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
827N/A *
827N/A * This code is free software; you can redistribute it and/or modify it
827N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
827N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
827N/A *
827N/A * This code is distributed in the hope that it will be useful, but WITHOUT
827N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
827N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
827N/A * version 2 for more details (a copy is included in the LICENSE file that
827N/A * accompanied this code).
827N/A *
827N/A * You should have received a copy of the GNU General Public License version
827N/A * 2 along with this work; if not, write to the Free Software Foundation,
827N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
827N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
827N/A */
827N/Apackage com.sun.beans.finder;
827N/A
827N/Aimport java.lang.reflect.Field;
827N/Aimport java.lang.reflect.Modifier;
827N/A
5252N/Aimport static sun.reflect.misc.ReflectUtil.isPackageAccessible;
5252N/A
827N/A/**
827N/A * This utility class provides {@code static} methods
827N/A * to find a public field with specified name
827N/A * in specified class.
827N/A *
827N/A * @since 1.7
827N/A *
827N/A * @author Sergey A. Malenkov
827N/A */
827N/Apublic final class FieldFinder {
827N/A
827N/A /**
827N/A * Finds public field (static or non-static)
827N/A * that is declared in public class.
827N/A *
827N/A * @param type the class that can have field
827N/A * @param name the name of field to find
827N/A * @return object that represents found field
827N/A * @throws NoSuchFieldException if field is not found
827N/A * @see Class#getField
827N/A */
827N/A public static Field findField(Class<?> type, String name) throws NoSuchFieldException {
827N/A if (name == null) {
827N/A throw new IllegalArgumentException("Field name is not set");
827N/A }
827N/A Field field = type.getField(name);
827N/A if (!Modifier.isPublic(field.getModifiers())) {
827N/A throw new NoSuchFieldException("Field '" + name + "' is not public");
827N/A }
5252N/A type = field.getDeclaringClass();
5252N/A if (!Modifier.isPublic(type.getModifiers()) || !isPackageAccessible(type)) {
827N/A throw new NoSuchFieldException("Field '" + name + "' is not accessible");
827N/A }
827N/A return field;
827N/A }
827N/A
827N/A /**
827N/A * Finds public non-static field
827N/A * that is declared in public class.
827N/A *
827N/A * @param type the class that can have field
827N/A * @param name the name of field to find
827N/A * @return object that represents found field
827N/A * @throws NoSuchFieldException if field is not found
827N/A * @see Class#getField
827N/A */
827N/A public static Field findInstanceField(Class<?> type, String name) throws NoSuchFieldException {
827N/A Field field = findField(type, name);
827N/A if (Modifier.isStatic(field.getModifiers())) {
827N/A throw new NoSuchFieldException("Field '" + name + "' is static");
827N/A }
827N/A return field;
827N/A }
827N/A
827N/A /**
827N/A * Finds public static field
827N/A * that is declared in public class.
827N/A *
827N/A * @param type the class that can have field
827N/A * @param name the name of field to find
827N/A * @return object that represents found field
827N/A * @throws NoSuchFieldException if field is not found
827N/A * @see Class#getField
827N/A */
827N/A public static Field findStaticField(Class<?> type, String name) throws NoSuchFieldException {
827N/A Field field = findField(type, name);
827N/A if (!Modifier.isStatic(field.getModifiers())) {
827N/A throw new NoSuchFieldException("Field '" + name + "' is not static");
827N/A }
827N/A return field;
827N/A }
827N/A
827N/A /**
827N/A * Disable instantiation.
827N/A */
827N/A private FieldFinder() {
827N/A }
827N/A}