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/decoder/FieldElementHandler.java
41171 views
1
/*
2
* Copyright (c) 2008, 2013, 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.decoder;
26
27
import com.sun.beans.finder.FieldFinder;
28
29
import java.lang.reflect.Field;
30
31
/**
32
* This class is intended to handle <field> element.
33
* This element simplifies access to the fields.
34
* If the {@code class} attribute is specified
35
* this element accesses static field of specified class.
36
* This element defines getter if it contains no argument.
37
* It returns the value of the field in this case.
38
* For example:<pre>
39
* &lt;field name="TYPE" class="java.lang.Long"/&gt;</pre>
40
* is equivalent to {@code Long.TYPE} in Java code.
41
* This element defines setter if it contains one argument.
42
* It does not return the value of the field in this case.
43
* For example:<pre>
44
* &lt;field name="id"&gt;&lt;int&gt;0&lt;/int&gt;&lt;/field&gt;</pre>
45
* is equivalent to {@code id = 0} in Java code.
46
* <p>The following attributes are supported:
47
* <dl>
48
* <dt>name
49
* <dd>the field name
50
* <dt>class
51
* <dd>the type is used for static fields only
52
* <dt>id
53
* <dd>the identifier of the variable that is intended to store the result
54
* </dl>
55
*
56
* @since 1.7
57
*
58
* @author Sergey A. Malenkov
59
*/
60
final class FieldElementHandler extends AccessorElementHandler {
61
private Class<?> type;
62
63
/**
64
* Parses attributes of the element.
65
* The following attributes are supported:
66
* <dl>
67
* <dt>name
68
* <dd>the field name
69
* <dt>class
70
* <dd>the type is used for static fields only
71
* <dt>id
72
* <dd>the identifier of the variable that is intended to store the result
73
* </dl>
74
*
75
* @param name the attribute name
76
* @param value the attribute value
77
*/
78
@Override
79
public void addAttribute(String name, String value) {
80
if (name.equals("class")) { // NON-NLS: the attribute name
81
this.type = getOwner().findClass(value);
82
} else {
83
super.addAttribute(name, value);
84
}
85
}
86
87
/**
88
* Tests whether the value of this element can be used
89
* as an argument of the element that contained in this one.
90
*
91
* @return {@code true} if the value of this element should be used
92
* as an argument of the element that contained in this one,
93
* {@code false} otherwise
94
*/
95
@Override
96
protected boolean isArgument() {
97
return super.isArgument() && (this.type != null); // only static accessor can be used an argument
98
}
99
100
/**
101
* Returns the context of the field.
102
* The context of the static field is the class object.
103
* The context of the non-static field is the value of the parent element.
104
*
105
* @return the context of the field
106
*/
107
@Override
108
protected Object getContextBean() {
109
return (this.type != null)
110
? this.type
111
: super.getContextBean();
112
}
113
114
/**
115
* Returns the value of the field with specified {@code name}.
116
*
117
* @param name the name of the field
118
* @return the value of the specified field
119
*/
120
@Override
121
protected Object getValue(String name) {
122
try {
123
return getFieldValue(getContextBean(), name);
124
}
125
catch (Exception exception) {
126
getOwner().handleException(exception);
127
}
128
return null;
129
}
130
131
/**
132
* Sets the new value for the field with specified {@code name}.
133
*
134
* @param name the name of the field
135
* @param value the new value for the specified field
136
*/
137
@Override
138
protected void setValue(String name, Object value) {
139
try {
140
setFieldValue(getContextBean(), name, value);
141
}
142
catch (Exception exception) {
143
getOwner().handleException(exception);
144
}
145
}
146
147
/**
148
* Performs the search of the field with specified {@code name}
149
* in specified context and returns its value.
150
*
151
* @param bean the context bean that contains field
152
* @param name the name of the field
153
* @return the value of the field
154
* @throws IllegalAccessException if the field is not accesible
155
* @throws NoSuchFieldException if the field is not found
156
*/
157
static Object getFieldValue(Object bean, String name) throws IllegalAccessException, NoSuchFieldException {
158
return findField(bean, name).get(bean);
159
}
160
161
/**
162
* Performs the search of the field with specified {@code name}
163
* in specified context and updates its value.
164
*
165
* @param bean the context bean that contains field
166
* @param name the name of the field
167
* @param value the new value for the field
168
* @throws IllegalAccessException if the field is not accesible
169
* @throws NoSuchFieldException if the field is not found
170
*/
171
private static void setFieldValue(Object bean, String name, Object value) throws IllegalAccessException, NoSuchFieldException {
172
findField(bean, name).set(bean, value);
173
}
174
175
/**
176
* Performs the search of the field
177
* with specified {@code name} in specified context.
178
*
179
* @param bean the context bean that contains field
180
* @param name the name of the field
181
* @return field object that represents found field
182
* @throws NoSuchFieldException if the field is not found
183
*/
184
private static Field findField(Object bean, String name) throws NoSuchFieldException {
185
return (bean instanceof Class<?>)
186
? FieldFinder.findStaticField((Class<?>) bean, name)
187
: FieldFinder.findField(bean.getClass(), name);
188
}
189
}
190
191