Path: blob/master/src/java.desktop/share/classes/com/sun/beans/decoder/MethodElementHandler.java
41171 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package com.sun.beans.decoder;2526import com.sun.beans.finder.MethodFinder;2728import java.lang.reflect.Method;2930import sun.reflect.misc.MethodUtil;3132/**33* This class is intended to handle <method> element.34* It describes invocation of the method.35* The {@code name} attribute denotes36* the name of the method to invoke.37* If the {@code class} attribute is specified38* this element invokes static method of specified class.39* The inner elements specifies the arguments of the method.40* For example:<pre>41* <method name="valueOf" class="java.lang.Long">42* <string>10</string>43* </method></pre>44* is equivalent to {@code Long.valueOf("10")} in Java code.45* <p>The following attributes are supported:46* <dl>47* <dt>name48* <dd>the method name49* <dt>class50* <dd>the type of object for instantiation51* <dt>id52* <dd>the identifier of the variable that is intended to store the result53* </dl>54*55* @since 1.756*57* @author Sergey A. Malenkov58*/59final class MethodElementHandler extends NewElementHandler {60private String name;6162/**63* Parses attributes of the element.64* The following attributes are supported:65* <dl>66* <dt>name67* <dd>the method name68* <dt>class69* <dd>the type of object for instantiation70* <dt>id71* <dd>the identifier of the variable that is intended to store the result72* </dl>73*74* @param name the attribute name75* @param value the attribute value76*/77@Override78public void addAttribute(String name, String value) {79if (name.equals("name")) { // NON-NLS: the attribute name80this.name = value;81} else {82super.addAttribute(name, value);83}84}8586/**87* Returns the result of method execution.88*89* @param type the base class90* @param args the array of arguments91* @return the value of this element92* @throws Exception if calculation is failed93*/94@Override95protected ValueObject getValueObject(Class<?> type, Object[] args) throws Exception {96Object bean = getContextBean();97Class<?>[] types = getArgumentTypes(args);98Method method = (type != null)99? MethodFinder.findStaticMethod(type, this.name, types)100: MethodFinder.findMethod(bean.getClass(), this.name, types);101102if (method.isVarArgs()) {103args = getArguments(args, method.getParameterTypes());104}105Object value = MethodUtil.invoke(method, bean, args);106return method.getReturnType().equals(void.class)107? ValueObjectImpl.VOID108: ValueObjectImpl.create(value);109}110}111112113