Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/com/sun/beans/finder/FieldFinder.java
41161 views
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
*/
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
if (!FinderUtils.isExported(type)) {
58
throw new NoSuchFieldException("Field '" + name + "' is not accessible");
59
}
60
Field field = type.getField(name);
61
if (!Modifier.isPublic(field.getModifiers())) {
62
throw new NoSuchFieldException("Field '" + name + "' is not public");
63
}
64
type = field.getDeclaringClass();
65
if (!Modifier.isPublic(type.getModifiers()) || !isPackageAccessible(type)) {
66
throw new NoSuchFieldException("Field '" + name + "' is not accessible");
67
}
68
return field;
69
}
70
71
/**
72
* Finds public non-static field
73
* that is declared in public class.
74
*
75
* @param type the class that can have field
76
* @param name the name of field to find
77
* @return object that represents found field
78
* @throws NoSuchFieldException if field is not found
79
* @see Class#getField
80
*/
81
public static Field findInstanceField(Class<?> type, String name) throws NoSuchFieldException {
82
Field field = findField(type, name);
83
if (Modifier.isStatic(field.getModifiers())) {
84
throw new NoSuchFieldException("Field '" + name + "' is static");
85
}
86
return field;
87
}
88
89
/**
90
* Finds public static field
91
* that is declared in public class.
92
*
93
* @param type the class that can have field
94
* @param name the name of field to find
95
* @return object that represents found field
96
* @throws NoSuchFieldException if field is not found
97
* @see Class#getField
98
*/
99
public static Field findStaticField(Class<?> type, String name) throws NoSuchFieldException {
100
Field field = findField(type, name);
101
if (!Modifier.isStatic(field.getModifiers())) {
102
throw new NoSuchFieldException("Field '" + name + "' is not static");
103
}
104
return field;
105
}
106
107
/**
108
* Disable instantiation.
109
*/
110
private FieldFinder() {
111
}
112
}
113
114