History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/com/sun/beans/decoder/ArrayElementHandler.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/beans/decoder/ArrayElementHandler.java+133−0
@@ -0,0 +1,133 @@
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 java.lang.reflect.Array;
28+
29+/**
30+ * This class is intended to handle <array> element,
31+ * that is used to array creation.
32+ * The {@code length} attribute specifies the length of the array.
33+ * The {@code class} attribute specifies the elements type.
34+ * The {@link Object} type is used by default.
35+ * For example:<pre>
36+ * &lt;array length="10"/&gt;</pre>
37+ * is equivalent to {@code new Component[10]} in Java code.
38+ * The {@code set} and {@code get} methods,
39+ * as defined in the {@link java.util.List} interface,
40+ * can be used as if they could be applied to array instances.
41+ * The {@code index} attribute can thus be used with arrays.
42+ * For example:<pre>
43+ * &lt;array length="3" class="java.lang.String"&gt;
44+ * &lt;void index="1"&gt;
45+ * &lt;string&gt;Hello, world&lt;/string&gt;
46+ * &lt;/void&gt;
47+ * &lt;/array&gt;</pre>
48+ * is equivalent to the following Java code:<pre>
49+ * String[] s = new String[3];
50+ * s[1] = "Hello, world";</pre>
51+ * It is possible to omit the {@code length} attribute and
52+ * specify the values directly, without using {@code void} tags.
53+ * The length of the array is equal to the number of values specified.
54+ * For example:<pre>
55+ * &lt;array id="array" class="int"&gt;
56+ * &lt;int&gt;123&lt;/int&gt;
57+ * &lt;int&gt;456&lt;/int&gt;
58+ * &lt;/array&gt;</pre>
59+ * is equivalent to {@code int[] array = {123, 456}} in Java code.
60+ * <p>The following atributes are supported:
61+ * <dl>
62+ * <dt>length
63+ * <dd>the array length
64+ * <dt>class
65+ * <dd>the type of object for instantiation
66+ * <dt>id
67+ * <dd>the identifier of the variable that is intended to store the result
68+ * </dl>
69+ *
70+ * @since 1.7
71+ *
72+ * @author Sergey A. Malenkov
73+ */
74+final class ArrayElementHandler extends NewElementHandler {
75+ private Integer length;
76+
77+ /**
78+ * Parses attributes of the element.
79+ * The following atributes are supported:
80+ * <dl>
81+ * <dt>length
82+ * <dd>the array length
83+ * <dt>class
84+ * <dd>the type of object for instantiation
85+ * <dt>id
86+ * <dd>the identifier of the variable that is intended to store the result
87+ * </dl>
88+ *
89+ * @param name the attribute name
90+ * @param value the attribute value
91+ */
92+ @Override
93+ public void addAttribute(String name, String value) {
94+ if (name.equals("length")) { // NON-NLS: the attribute name
95+ this.length = Integer.valueOf(value);
96+ } else {
97+ super.addAttribute(name, value);
98+ }
99+ }
100+
101+ /**
102+ * Calculates the value of this element
103+ * if the lentgh attribute is set.
104+ */
105+ @Override
106+ public void startElement() {
107+ if (this.length != null) {
108+ getValueObject();
109+ }
110+ }
111+
112+ /**
113+ * Creates an instance of the array.
114+ *
115+ * @param type the base class
116+ * @param args the array of arguments
117+ * @return the value of this element
118+ */
119+ @Override
120+ protected ValueObject getValueObject(Class<?> type, Object[] args) {
121+ if (type == null) {
122+ type = Object.class;
123+ }
124+ if (this.length != null) {
125+ return ValueObjectImpl.create(Array.newInstance(type, this.length));
126+ }
127+ Object array = Array.newInstance(type, args.length);
128+ for (int i = 0; i < args.length; i++) {
129+ Array.set(array, i, args[i]);
130+ }
131+ return ValueObjectImpl.create(array);
132+ }
133+}