Path: blob/master/src/java.desktop/share/classes/com/sun/beans/decoder/ObjectElementHandler.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 java.beans.Expression;2728import static java.util.Locale.ENGLISH;2930/**31* This class is intended to handle <object> element.32* This element looks like <void> element,33* but its value is always used as an argument for element34* that contains this one.35* <p>The following attributes are supported:36* <dl>37* <dt>class38* <dd>the type is used for static methods and fields39* <dt>method40* <dd>the method name41* <dt>property42* <dd>the property name43* <dt>index44* <dd>the property index45* <dt>field46* <dd>the field name47* <dt>idref48* <dd>the identifier to refer to the variable49* <dt>id50* <dd>the identifier of the variable that is intended to store the result51* </dl>52*53* @since 1.754*55* @author Sergey A. Malenkov56*/57class ObjectElementHandler extends NewElementHandler {58private String idref;59private String field;60private Integer index;61private String property;62private String method;6364/**65* Parses attributes of the element.66* The following attributes are supported:67* <dl>68* <dt>class69* <dd>the type is used for static methods and fields70* <dt>method71* <dd>the method name72* <dt>property73* <dd>the property name74* <dt>index75* <dd>the property index76* <dt>field77* <dd>the field name78* <dt>idref79* <dd>the identifier to refer to the variable80* <dt>id81* <dd>the identifier of the variable that is intended to store the result82* </dl>83*84* @param name the attribute name85* @param value the attribute value86*/87@Override88public final void addAttribute(String name, String value) {89if (name.equals("idref")) { // NON-NLS: the attribute name90this.idref = value;91} else if (name.equals("field")) { // NON-NLS: the attribute name92this.field = value;93} else if (name.equals("index")) { // NON-NLS: the attribute name94this.index = Integer.valueOf(value);95addArgument(this.index); // hack for compatibility96} else if (name.equals("property")) { // NON-NLS: the attribute name97this.property = value;98} else if (name.equals("method")) { // NON-NLS: the attribute name99this.method = value;100} else {101super.addAttribute(name, value);102}103}104105/**106* Calculates the value of this element107* if the field attribute or the idref attribute is set.108*/109@Override110public final void startElement() {111if ((this.field != null) || (this.idref != null)) {112getValueObject();113}114}115116/**117* Tests whether the value of this element can be used118* as an argument of the element that contained in this one.119*120* @return {@code true} if the value of this element can be used121* as an argument of the element that contained in this one,122* {@code false} otherwise123*/124@Override125protected boolean isArgument() {126return true; // hack for compatibility127}128129/**130* Creates the value of this element.131*132* @param type the base class133* @param args the array of arguments134* @return the value of this element135* @throws Exception if calculation is failed136*/137@Override138protected final ValueObject getValueObject(Class<?> type, Object[] args) throws Exception {139if (this.field != null) {140return ValueObjectImpl.create(FieldElementHandler.getFieldValue(getContextBean(), this.field));141}142if (this.idref != null) {143return ValueObjectImpl.create(getVariable(this.idref));144}145Object bean = getContextBean();146String name;147if (this.index != null) {148name = (args.length == 2)149? PropertyElementHandler.SETTER150: PropertyElementHandler.GETTER;151} else if (this.property != null) {152name = (args.length == 1)153? PropertyElementHandler.SETTER154: PropertyElementHandler.GETTER;155156if (0 < this.property.length()) {157name += this.property.substring(0, 1).toUpperCase(ENGLISH) + this.property.substring(1);158}159} else {160name = (this.method != null) && (0 < this.method.length())161? this.method162: "new"; // NON-NLS: the constructor marker163}164Expression expression = new Expression(bean, name, args);165return ValueObjectImpl.create(expression.getValue());166}167}168169170