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/MethodElementHandler.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.MethodFinder;
28
29
import java.lang.reflect.Method;
30
31
import sun.reflect.misc.MethodUtil;
32
33
/**
34
* This class is intended to handle <method> element.
35
* It describes invocation of the method.
36
* The {@code name} attribute denotes
37
* the name of the method to invoke.
38
* If the {@code class} attribute is specified
39
* this element invokes static method of specified class.
40
* The inner elements specifies the arguments of the method.
41
* For example:<pre>
42
* &lt;method name="valueOf" class="java.lang.Long"&gt;
43
* &lt;string&gt;10&lt;/string&gt;
44
* &lt;/method&gt;</pre>
45
* is equivalent to {@code Long.valueOf("10")} in Java code.
46
* <p>The following attributes are supported:
47
* <dl>
48
* <dt>name
49
* <dd>the method name
50
* <dt>class
51
* <dd>the type of object for instantiation
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 MethodElementHandler extends NewElementHandler {
61
private String name;
62
63
/**
64
* Parses attributes of the element.
65
* The following attributes are supported:
66
* <dl>
67
* <dt>name
68
* <dd>the method name
69
* <dt>class
70
* <dd>the type of object for instantiation
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("name")) { // NON-NLS: the attribute name
81
this.name = value;
82
} else {
83
super.addAttribute(name, value);
84
}
85
}
86
87
/**
88
* Returns the result of method execution.
89
*
90
* @param type the base class
91
* @param args the array of arguments
92
* @return the value of this element
93
* @throws Exception if calculation is failed
94
*/
95
@Override
96
protected ValueObject getValueObject(Class<?> type, Object[] args) throws Exception {
97
Object bean = getContextBean();
98
Class<?>[] types = getArgumentTypes(args);
99
Method method = (type != null)
100
? MethodFinder.findStaticMethod(type, this.name, types)
101
: MethodFinder.findMethod(bean.getClass(), this.name, types);
102
103
if (method.isVarArgs()) {
104
args = getArguments(args, method.getParameterTypes());
105
}
106
Object value = MethodUtil.invoke(method, bean, args);
107
return method.getReturnType().equals(void.class)
108
? ValueObjectImpl.VOID
109
: ValueObjectImpl.create(value);
110
}
111
}
112
113