001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.mobility.e2e.classdata;
043:
044: import org.netbeans.modules.mobility.javon.JavonSerializer;
045:
046: import java.util.ArrayList;
047: import java.util.Collections;
048: import java.util.Iterator;
049: import java.util.List;
050:
051: /**
052: *
053: * @author Michal Skvor, Jirka Prazak
054: */
055: public class ClassData {
056:
057: private String packageName;
058: private String className;
059: private boolean primitive;
060: private boolean array;
061: private boolean generics;
062:
063: private ClassData parent;
064: private JavonSerializer supportingSerializer = null;
065:
066: private List<FieldData> fields = new ArrayList<FieldData>();
067: private List<MethodData> methods = new ArrayList<MethodData>();
068:
069: private ClassData componentType;
070: private List<ClassData> typeParameters = new ArrayList<ClassData>();
071:
072: public static final ClassData java_lang_Object = new ClassData(
073: "java.lang", "Object", false, false);
074:
075: private ClassData(String packageName, String className,
076: boolean primitive, boolean array) {
077: this .packageName = packageName;
078: this .className = className;
079: this .primitive = primitive;
080: this .array = array;
081:
082: parent = java_lang_Object;
083: }
084:
085: public ClassData(ClassData cd) {
086: this .packageName = cd.getPackage();
087: this .className = cd.getName();
088: this .primitive = cd.isPrimitive();
089: this .array = cd.isArray();
090: this .generics = cd.getParameterTypes().isEmpty() ? true : false;
091: this .componentType = cd.getComponentType();
092: this .parent = cd.getParent();
093: this .supportingSerializer = cd.getSerializer();
094: this .fields = cd.getFields();
095: this .methods = cd.getMethods();
096: this .typeParameters = cd.getParameterTypes();
097: }
098:
099: public ClassData(String packageName, String className,
100: boolean primitive, boolean array, JavonSerializer serializer) {
101: this (packageName, className, primitive, array);
102:
103: this .generics = false;
104: this .supportingSerializer = serializer;
105: }
106:
107: public ClassData(String packageName, String className,
108: boolean array, List<FieldData> fields,
109: List<MethodData> methods, JavonSerializer serializer) {
110: this (packageName, className, false, array, serializer);
111: this .generics = false;
112: this .fields = fields;
113: this .methods = methods;
114: }
115:
116: public ClassData(String packageName, String className,
117: boolean array, List<ClassData> typeParams,
118: JavonSerializer serializer) {
119: this (packageName, className, false, array, serializer);
120: this .generics = true;
121: this .typeParameters = typeParams;
122: }
123:
124: public ClassData(String packageName, String className,
125: boolean array, List<FieldData> fields,
126: List<MethodData> methods, List<ClassData> typeParams,
127: JavonSerializer serializer) {
128: this (packageName, className, array, fields, methods, serializer);
129: this .generics = true;
130: this .typeParameters = typeParams;
131: }
132:
133: public String getPackage() {
134: return packageName;
135: }
136:
137: public String getClassName() {
138: return className;
139: }
140:
141: public String getName() {
142: int arrayDepth = 0;
143: ClassData t = this ;
144: while (t.isArray()) {
145: t = t.getComponentType();
146: arrayDepth++;
147: }
148: String arrayBrackets = "";
149: if (arrayDepth > 0) {
150: for (int i = 0; i < arrayDepth; i++) {
151: arrayBrackets += "[]";
152: }
153: }
154: return className + arrayBrackets;
155: }
156:
157: /**
158: * Return fully qualified name of the ClassData
159: *
160: * @return fully qualified name
161: */
162: public String getFullyQualifiedName() {
163: if (packageName == "") {
164: return getName();
165: }
166: return packageName + "." + getName();
167: }
168:
169: public void setParent(ClassData parent) {
170: this .parent = parent;
171: }
172:
173: public ClassData getParent() {
174: return parent;
175: }
176:
177: /**
178: * Returns true when the ClassData structure represents
179: * primitive type
180: *
181: * @return true when the ClassData structure represents primitive type
182: */
183: public boolean isPrimitive() {
184: return primitive;
185: }
186:
187: public boolean isArray() {
188: return array;
189: }
190:
191: public void setComponentType(ClassData type) {
192: this .componentType = type;
193: }
194:
195: public ClassData getComponentType() {
196: return componentType;
197: }
198:
199: public void addField(FieldData field) {
200: fields.add(field);
201: }
202:
203: public List<FieldData> getFields() {
204: return Collections.unmodifiableList(fields);
205: }
206:
207: public List<FieldData> getAllFields() {
208: List<FieldData> f = new ArrayList<FieldData>();
209: ClassData p = this ;
210: while (p != null) {
211: f.addAll(p.getFields());
212: p = p.getParent();
213: }
214: return Collections.unmodifiableList(f);
215: }
216:
217: public void addMethod(MethodData method) {
218: methods.add(method);
219: }
220:
221: public List<MethodData> getMethods() {
222: return Collections.unmodifiableList(methods);
223: }
224:
225: /**
226: * Returns all parameter type specified for this class
227: *
228: * @return List of all parameter types, EmptyList if no parameter types are specified
229: */
230: public List<ClassData> getParameterTypes() {
231: return Collections.unmodifiableList(typeParameters);
232: }
233:
234: public void setParameterTypes(List<ClassData> parameters) {
235: typeParameters = parameters;
236: }
237:
238: public String toString() {
239: String result = getName();
240: if (typeParameters.size() > 0) {
241: result += "<";
242: for (Iterator<ClassData> it = typeParameters.iterator(); it
243: .hasNext();) {
244: result += it.next().toString();
245: if (it.hasNext())
246: result += ", ";
247: }
248: result += ">";
249: }
250: if (fields.size() > 0) {
251: result += "[";
252: for (FieldData field : fields) {
253: result += field.getType().toString() + ", ";
254: }
255: result += "]";
256: }
257: return result;
258: }
259:
260: public static enum Modifier {
261: PUBLIC, PRIVATE
262: }
263:
264: public JavonSerializer getSerializer() {
265: return this .supportingSerializer;
266: }
267:
268: @Override
269: public boolean equals(Object o) {
270: if (o instanceof ClassData) {
271: ClassData cd = (ClassData) o;
272: if (!getFullyQualifiedName().equals(
273: cd.getFullyQualifiedName()))
274: return false;
275: if (primitive != cd.isPrimitive())
276: return false;
277: if (array != cd.isArray())
278: return false;
279: if (typeParameters.size() != cd.getParameterTypes().size())
280: return false;
281: for (int i = 0; i < typeParameters.size(); i++) {
282: if (!typeParameters.get(i).equals(
283: cd.getParameterTypes().get(i)))
284: return false;
285: }
286: return true;
287: }
288: return false;
289: }
290:
291: @Override
292: public int hashCode() {
293: return packageName.hashCode() * 37 + className.hashCode() * 37
294: + (primitive ? 7 : 3) + (array ? 7 : 3);
295: }
296: }
|