1 /*
2  * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package com.sun.beans.finder;
26 
27 import java.lang.reflect.Field;
28 import java.lang.reflect.Modifier;
29 
30 import static sun.reflect.misc.ReflectUtil.isPackageAccessible;
31 
32 /**
33  * This utility class provides {@code static} methods
34  * to find a public field with specified name
35  * in specified class.
36  *
37  * @since 1.7
38  *
39  * @author Sergey A. Malenkov
40  */
41 public final class FieldFinder {
42 
43     /**
44      * Finds public field (static or non-static)
45      * that is declared in public class.
46      *
47      * @param type  the class that can have field
48      * @param name  the name of field to find
49      * @return object that represents found field
50      * @throws NoSuchFieldException if field is not found
51      * @see Class#getField
52      */
findField(Class<?> type, String name)53     public static Field findField(Class<?> type, String name) throws NoSuchFieldException {
54         if (name == null) {
55             throw new IllegalArgumentException("Field name is not set");
56         }
57         Field field = type.getField(name);
58         if (!Modifier.isPublic(field.getModifiers())) {
59             throw new NoSuchFieldException("Field '" + name + "' is not public");
60         }
61         type = field.getDeclaringClass();
62         if (!Modifier.isPublic(type.getModifiers()) || !isPackageAccessible(type)) {
63             throw new NoSuchFieldException("Field '" + name + "' is not accessible");
64         }
65         return field;
66     }
67 
68     /**
69      * Finds public non-static field
70      * that is declared in public class.
71      *
72      * @param type  the class that can have field
73      * @param name  the name of field to find
74      * @return object that represents found field
75      * @throws NoSuchFieldException if field is not found
76      * @see Class#getField
77      */
findInstanceField(Class<?> type, String name)78     public static Field findInstanceField(Class<?> type, String name) throws NoSuchFieldException {
79         Field field = findField(type, name);
80         if (Modifier.isStatic(field.getModifiers())) {
81             throw new NoSuchFieldException("Field '" + name + "' is static");
82         }
83         return field;
84     }
85 
86     /**
87      * Finds public static field
88      * that is declared in public class.
89      *
90      * @param type  the class that can have field
91      * @param name  the name of field to find
92      * @return object that represents found field
93      * @throws NoSuchFieldException if field is not found
94      * @see Class#getField
95      */
findStaticField(Class<?> type, String name)96     public static Field findStaticField(Class<?> type, String name) throws NoSuchFieldException {
97         Field field = findField(type, name);
98         if (!Modifier.isStatic(field.getModifiers())) {
99             throw new NoSuchFieldException("Field '" + name + "' is not static");
100         }
101         return field;
102     }
103 
104     /**
105      * Disable instantiation.
106      */
FieldFinder()107     private FieldFinder() {
108     }
109 }
110