Path: blob/master/src/java.desktop/share/classes/com/sun/beans/decoder/ArrayElementHandler.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.lang.reflect.Array;2728/**29* This class is intended to handle <array> element,30* that is used to array creation.31* The {@code length} attribute specifies the length of the array.32* The {@code class} attribute specifies the elements type.33* The {@link Object} type is used by default.34* For example:<pre>35* <array length="10"/></pre>36* is equivalent to {@code new Component[10]} in Java code.37* The {@code set} and {@code get} methods,38* as defined in the {@link java.util.List} interface,39* can be used as if they could be applied to array instances.40* The {@code index} attribute can thus be used with arrays.41* For example:<pre>42* <array length="3" class="java.lang.String">43* <void index="1">44* <string>Hello, world</string>45* </void>46* </array></pre>47* is equivalent to the following Java code:<pre>48* String[] s = new String[3];49* s[1] = "Hello, world";</pre>50* It is possible to omit the {@code length} attribute and51* specify the values directly, without using {@code void} tags.52* The length of the array is equal to the number of values specified.53* For example:<pre>54* <array id="array" class="int">55* <int>123</int>56* <int>456</int>57* </array></pre>58* is equivalent to {@code int[] array = {123, 456}} in Java code.59* <p>The following attributes are supported:60* <dl>61* <dt>length62* <dd>the array length63* <dt>class64* <dd>the type of object for instantiation65* <dt>id66* <dd>the identifier of the variable that is intended to store the result67* </dl>68*69* @since 1.770*71* @author Sergey A. Malenkov72*/73final class ArrayElementHandler extends NewElementHandler {74private Integer length;7576/**77* Parses attributes of the element.78* The following attributes are supported:79* <dl>80* <dt>length81* <dd>the array length82* <dt>class83* <dd>the type of object for instantiation84* <dt>id85* <dd>the identifier of the variable that is intended to store the result86* </dl>87*88* @param name the attribute name89* @param value the attribute value90*/91@Override92public void addAttribute(String name, String value) {93if (name.equals("length")) { // NON-NLS: the attribute name94this.length = Integer.valueOf(value);95} else {96super.addAttribute(name, value);97}98}99100/**101* Calculates the value of this element102* if the lentgh attribute is set.103*/104@Override105public void startElement() {106if (this.length != null) {107getValueObject();108}109}110111/**112* Tests whether the value of this element can be used113* as an argument of the element that contained in this one.114*115* @return {@code true} if the value of this element can be used116* as an argument of the element that contained in this one,117* {@code false} otherwise118*/119@Override120protected boolean isArgument() {121return true; // hack for compatibility122}123124125/**126* Creates an instance of the array.127*128* @param type the base class129* @param args the array of arguments130* @return the value of this element131*/132@Override133protected ValueObject getValueObject(Class<?> type, Object[] args) {134if (type == null) {135type = Object.class;136}137if (this.length != null) {138return ValueObjectImpl.create(Array.newInstance(type, this.length));139}140Object array = Array.newInstance(type, args.length);141for (int i = 0; i < args.length; i++) {142Array.set(array, i, args[i]);143}144return ValueObjectImpl.create(array);145}146}147148149