ArrayType.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 1998, 2007, 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
26/*
27 * Licensed Materials - Property of IBM
28 * RMI-IIOP v1.0
29 * Copyright IBM Corp. 1998 1999  All Rights Reserved
30 *
31 */
32
33package sun.rmi.rmic.iiop;
34
35import java.util.Vector;
36import java.util.HashSet;
37import sun.tools.java.CompilerError;
38import sun.tools.java.Identifier;
39import sun.tools.java.ClassDefinition;
40import java.lang.reflect.Array;
41
42/**
43 * ArrayType is a wrapper for any of the other types. The getElementType()
44 * method can be used to get the array element type.  The getArrayDimension()
45 * method can be used to get the array dimension.
46 *
47 * @author      Bryan Atsatt
48 */
49public class ArrayType extends Type {
50
51    private Type type;
52    private int arrayDimension;
53    private String brackets;
54    private String bracketsSig;
55
56    //_____________________________________________________________________
57    // Public Interfaces
58    //_____________________________________________________________________
59
60    /**
61     * Create an ArrayType object for the given type.
62     *
63     * If the class is not a properly formed or if some other error occurs, the
64     * return value will be null, and errors will have been reported to the
65     * supplied BatchEnvironment.
66     */
67    public static ArrayType forArray(   sun.tools.java.Type theType,
68                                        ContextStack stack) {
69
70
71        ArrayType result = null;
72        sun.tools.java.Type arrayType = theType;
73
74        if (arrayType.getTypeCode() == TC_ARRAY) {
75
76            // Find real type...
77
78            while (arrayType.getTypeCode() == TC_ARRAY) {
79                arrayType = arrayType.getElementType();
80            }
81
82            // Do we already have it?
83
84            Type existing = getType(theType,stack);
85            if (existing != null) {
86
87                if (!(existing instanceof ArrayType)) return null; // False hit.
88
89                                // Yep, so return it...
90
91                return (ArrayType) existing;
92            }
93
94            // Now try to make a Type from it...
95
96            Type temp = CompoundType.makeType(arrayType,null,stack);
97
98            if (temp != null) {
99
100                                // Got a valid one. Make an array type...
101
102                result = new ArrayType(stack,temp,theType.getArrayDimension());
103
104                                // Add it...
105
106                putType(theType,result,stack);
107
108                // Do the stack thing in case tracing on...
109
110                stack.push(result);
111                stack.pop(true);
112            }
113        }
114
115        return result;
116    }
117
118    /**
119     * Return signature for this type  (e.g. com.acme.Dynamite
120     * would return "com.acme.Dynamite", byte = "B")
121     */
122    public String getSignature() {
123        return bracketsSig + type.getSignature();
124    }
125
126    /**
127     * Get element type. Returns null if not an array.
128     */
129    public Type getElementType () {
130        return type;
131    }
132
133    /**
134     * Get array dimension. Returns zero if not an array.
135     */
136    public int getArrayDimension () {
137        return arrayDimension;
138    }
139
140    /**
141     * Get brackets string. Returns "" if not an array.
142     */
143    public String getArrayBrackets () {
144        return brackets;
145    }
146
147    /**
148     * Return a string representation of this type.
149     */
150    public String toString () {
151        return getQualifiedName() + brackets;
152    }
153
154    /**
155     * Return a string describing this type.
156     */
157    public String getTypeDescription () {
158        return "Array of " + type.getTypeDescription();
159    }
160
161
162    /**
163     * Return the name of this type. For arrays, will include "[]" if useIDLNames == false.
164     * @param useQualifiedNames If true, print qualified names; otherwise, print unqualified names.
165     * @param useIDLNames If true, print IDL names; otherwise, print java names.
166     * @param globalIDLNames If true and useIDLNames true, prepends "::".
167     */
168    public String getTypeName ( boolean useQualifiedNames,
169                                boolean useIDLNames,
170                                boolean globalIDLNames) {
171        if (useIDLNames) {
172            return super.getTypeName(useQualifiedNames,useIDLNames,globalIDLNames);
173        } else {
174            return super.getTypeName(useQualifiedNames,useIDLNames,globalIDLNames) + brackets;
175        }
176    }
177
178    //_____________________________________________________________________
179    // Subclass/Internal Interfaces
180    //_____________________________________________________________________
181
182
183    /**
184     * Convert all invalid types to valid ones.
185     */
186    protected void swapInvalidTypes () {
187        if (type.getStatus() != STATUS_VALID) {
188            type = getValidType(type);
189        }
190    }
191
192    /*
193     * Add matching types to list. Return true if this type has not
194     * been previously checked, false otherwise.
195     */
196    protected boolean addTypes (int typeCodeFilter,
197                                HashSet checked,
198                                Vector matching) {
199
200        // Check self.
201
202        boolean result = super.addTypes(typeCodeFilter,checked,matching);
203
204        // Have we been checked before?
205
206        if (result) {
207
208            // No, so add element type...
209
210            getElementType().addTypes(typeCodeFilter,checked,matching);
211        }
212
213        return result;
214    }
215
216    /**
217     * Create an ArrayType instance for the given type.  The resulting
218     * object is not yet completely initialized.
219     */
220    private ArrayType(ContextStack stack, Type type, int arrayDimension) {
221        super(stack,TYPE_ARRAY);
222        this.type = type;
223        this.arrayDimension = arrayDimension;
224
225        // Create our brackets string...
226
227        brackets = "";
228        bracketsSig = "";
229        for (int i = 0; i < arrayDimension; i ++) {
230            brackets += "[]";
231            bracketsSig += "[";
232        }
233
234        // Now set our names...
235
236        String idlName = IDLNames.getArrayName(type,arrayDimension);
237        String[] module = IDLNames.getArrayModuleNames(type);
238        setNames(type.getIdentifier(),module,idlName);
239
240        // Set our repositoryID...
241
242        setRepositoryID();
243    }
244
245
246    /*
247     * Load a Class instance. Return null if fail.
248     */
249    protected Class loadClass() {
250        Class result = null;
251        Class elementClass = type.getClassInstance();
252        if (elementClass != null) {
253            result = Array.newInstance(elementClass, new int[arrayDimension]).getClass();
254        }
255        return result;
256    }
257
258    /**
259     * Release all resources
260     */
261    protected void destroy () {
262        super.destroy();
263        if (type != null) {
264            type.destroy();
265            type = null;
266        }
267        brackets = null;
268        bracketsSig = null;
269    }
270}
271