Showing the changes to java/pkg/com/sun/beans/decoder/MethodElementHandler.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,109 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2008, 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 | /** | ||
| 32 | * This class is intended to handle <method> element. | ||
| 33 | * It describes invocation of the method. | ||
| 34 | * The {@code name} attribute denotes | ||
| 35 | * the name of the method to invoke. | ||
| 36 | * If the {@code class} attribute is specified | ||
| 37 | * this element invokes static method of specified class. | ||
| 38 | * The inner elements specifies the arguments of the method. | ||
| 39 | * For example:<pre> | ||
| 40 | * <method name="valueOf" class="java.lang.Long"> | ||
| 41 | * <string>10</string> | ||
| 42 | * </method></pre> | ||
| 43 | * is equivalent to {@code Long.valueOf("10")} in Java code. | ||
| 44 | * <p>The following atributes are supported: | ||
| 45 | * <dl> | ||
| 46 | * <dt>name | ||
| 47 | * <dd>the method name | ||
| 48 | * <dt>class | ||
| 49 | * <dd>the type of object for instantiation | ||
| 50 | * <dt>id | ||
| 51 | * <dd>the identifier of the variable that is intended to store the result | ||
| 52 | * </dl> | ||
| 53 | * | ||
| 54 | * @since 1.7 | ||
| 55 | * | ||
| 56 | * @author Sergey A. Malenkov | ||
| 57 | */ | ||
| 58 | final class MethodElementHandler extends NewElementHandler { | ||
| 59 | private String name; | ||
| 60 | |||
| 61 | /** | ||
| 62 | * Parses attributes of the element. | ||
| 63 | * The following atributes are supported: | ||
| 64 | * <dl> | ||
| 65 | * <dt>name | ||
| 66 | * <dd>the method name | ||
| 67 | * <dt>class | ||
| 68 | * <dd>the type of object for instantiation | ||
| 69 | * <dt>id | ||
| 70 | * <dd>the identifier of the variable that is intended to store the result | ||
| 71 | * </dl> | ||
| 72 | * | ||
| 73 | * @param name the attribute name | ||
| 74 | * @param value the attribute value | ||
| 75 | */ | ||
| 76 | @Override | ||
| 77 | public void addAttribute(String name, String value) { | ||
| 78 | if (name.equals("name")) { // NON-NLS: the attribute name | ||
| 79 | this.name = value; | ||
| 80 | } else { | ||
| 81 | super.addAttribute(name, value); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | /** | ||
| 86 | * Returns the result of method execution. | ||
| 87 | * | ||
| 88 | * @param type the base class | ||
| 89 | * @param args the array of arguments | ||
| 90 | * @return the value of this element | ||
| 91 | * @throws Exception if calculation is failed | ||
| 92 | */ | ||
| 93 | @Override | ||
| 94 | protected ValueObject getValueObject(Class<?> type, Object[] args) throws Exception { | ||
| 95 | Object bean = getContextBean(); | ||
| 96 | Class<?>[] types = getArgumentTypes(args); | ||
| 97 | Method method = (type != null) | ||
| 98 | ? MethodFinder.findStaticMethod(type, this.name, types) | ||
| 99 | : MethodFinder.findMethod(bean.getClass(), this.name, types); | ||
| 100 | |||
| 101 | if (method.isVarArgs()) { | ||
| 102 | args = getArguments(args, method.getParameterTypes()); | ||
| 103 | } | ||
| 104 | Object value = method.invoke(bean, args); | ||
| 105 | return method.getReturnType().equals(void.class) | ||
| 106 | ? ValueObjectImpl.VOID | ||
| 107 | : ValueObjectImpl.create(value); | ||
| 108 | } | ||
| 109 | } | ||