aboutsummaryrefslogtreecommitdiff
path: root/src/java.desktop/share/classes/com/sun/beans/decoder/ArrayElementHandler.java
blob: ce0aeb31a7a7a5016f25723e2b6835ddbe851472 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.sun.beans.decoder;

import java.lang.reflect.Array;

/**
 * This class is intended to handle <array> element,
 * that is used to array creation.
 * The {@code length} attribute specifies the length of the array.
 * The {@code class} attribute specifies the elements type.
 * The {@link Object} type is used by default.
 * For example:<pre>
 * &lt;array length="10"/&gt;</pre>
 * is equivalent to {@code new Component[10]} in Java code.
 * The {@code set} and {@code get} methods,
 * as defined in the {@link java.util.List} interface,
 * can be used as if they could be applied to array instances.
 * The {@code index} attribute can thus be used with arrays.
 * For example:<pre>
 * &lt;array length="3" class="java.lang.String"&gt;
 *     &lt;void index="1"&gt;
 *         &lt;string&gt;Hello, world&lt;/string&gt;
 *     &lt;/void&gt;
 * &lt;/array&gt;</pre>
 * is equivalent to the following Java code:<pre>
 * String[] s = new String[3];
 * s[1] = "Hello, world";</pre>
 * It is possible to omit the {@code length} attribute and
 * specify the values directly, without using {@code void} tags.
 * The length of the array is equal to the number of values specified.
 * For example:<pre>
 * &lt;array id="array" class="int"&gt;
 *     &lt;int&gt;123&lt;/int&gt;
 *     &lt;int&gt;456&lt;/int&gt;
 * &lt;/array&gt;</pre>
 * is equivalent to {@code int[] array = {123, 456}} in Java code.
 * <p>The following attributes are supported:
 * <dl>
 * <dt>length
 * <dd>the array length
 * <dt>class
 * <dd>the type of object for instantiation
 * <dt>id
 * <dd>the identifier of the variable that is intended to store the result
 * </dl>
 *
 * @since 1.7
 *
 * @author Sergey A. Malenkov
 */
final class ArrayElementHandler extends NewElementHandler {
    private Integer length;

    /**
     * Parses attributes of the element.
     * The following attributes are supported:
     * <dl>
     * <dt>length
     * <dd>the array length
     * <dt>class
     * <dd>the type of object for instantiation
     * <dt>id
     * <dd>the identifier of the variable that is intended to store the result
     * </dl>
     *
     * @param name   the attribute name
     * @param value  the attribute value
     */
    @Override
    public void addAttribute(String name, String value) {
        if (name.equals("length")) { // NON-NLS: the attribute name
            this.length = Integer.valueOf(value);
        } else {
            super.addAttribute(name, value);
        }
    }

    /**
     * Calculates the value of this element
     * if the lentgh attribute is set.
     */
    @Override
    public void startElement() {
        if (this.length != null) {
            getValueObject();
        }
    }

    /**
     * Tests whether the value of this element can be used
     * as an argument of the element that contained in this one.
     *
     * @return {@code true} if the value of this element can be used
     *         as an argument of the element that contained in this one,
     *         {@code false} otherwise
     */
    @Override
    protected boolean isArgument() {
        return true; // hack for compatibility
    }


    /**
     * Creates an instance of the array.
     *
     * @param type  the base class
     * @param args  the array of arguments
     * @return the value of this element
     */
    @Override
    protected ValueObject getValueObject(Class<?> type, Object[] args) {
        if (type == null) {
            type = Object.class;
        }
        if (this.length != null) {
            return ValueObjectImpl.create(Array.newInstance(type, this.length));
        }
        Object array = Array.newInstance(type, args.length);
        for (int i = 0; i < args.length; i++) {
            Array.set(array, i, args[i]);
        }
        return ValueObjectImpl.create(array);
    }
}