author | malenkov |
Tue, 29 Oct 2013 17:01:06 +0400 | |
changeset 21278 | ef8a3a2a72f2 |
parent 13608 | a2effa5fb0c3 |
child 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
1844 | 1 |
/* |
13608
a2effa5fb0c3
7194567: Improve long term persistence of java.beans objects
malenkov
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved. |
1844 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
1844 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
1844 | 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 |
* |
|
5506 | 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. |
|
1844 | 24 |
*/ |
25 |
package com.sun.beans.decoder; |
|
26 |
||
27 |
import com.sun.beans.finder.MethodFinder; |
|
28 |
||
29 |
import java.lang.reflect.Method; |
|
30 |
||
13608
a2effa5fb0c3
7194567: Improve long term persistence of java.beans objects
malenkov
parents:
5506
diff
changeset
|
31 |
import sun.reflect.misc.MethodUtil; |
a2effa5fb0c3
7194567: Improve long term persistence of java.beans objects
malenkov
parents:
5506
diff
changeset
|
32 |
|
1844 | 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 |
* <method name="valueOf" class="java.lang.Long"> |
|
43 |
* <string>10</string> |
|
44 |
* </method></pre> |
|
45 |
* is equivalent to {@code Long.valueOf("10")} in Java code. |
|
21278 | 46 |
* <p>The following attributes are supported: |
1844 | 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. |
|
21278 | 65 |
* The following attributes are supported: |
1844 | 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 |
} |
|
13608
a2effa5fb0c3
7194567: Improve long term persistence of java.beans objects
malenkov
parents:
5506
diff
changeset
|
106 |
Object value = MethodUtil.invoke(method, bean, args); |
1844 | 107 |
return method.getReturnType().equals(void.class) |
108 |
? ValueObjectImpl.VOID |
|
109 |
: ValueObjectImpl.create(value); |
|
110 |
} |
|
111 |
} |