KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > TypeClassImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.javacore.jmiimpl.javamodel;
20
21 import java.util.Iterator JavaDoc;
22 import org.netbeans.jmi.javamodel.ArrayClass;
23 import org.netbeans.jmi.javamodel.JavaClass;
24 import org.netbeans.jmi.javamodel.JavaModelPackage;
25 import org.netbeans.jmi.javamodel.ParameterizedType;
26 import org.netbeans.jmi.javamodel.Type;
27 import org.netbeans.jmi.javamodel.Array;
28 import org.netbeans.jmi.javamodel.TypeClass;
29 import org.netbeans.jmi.javamodel.TypeParameter;
30 import org.netbeans.mdr.storagemodel.StorableClass;
31
32 /**
33  *
34  * @author Martin Matula
35  */

36 public abstract class TypeClassImpl extends SemiPersistentClass implements TypeClass {
37     
38     /** Creates a new instance of JavaClassClassImpl */
39     public TypeClassImpl(StorableClass s) {
40         super(s);
41     }
42
43     public Type resolve(String JavaDoc name) {
44         if (name == null) return null; // throw new NullPointerException();
45

46         _lock(false);
47         try {
48             int dimension = 0;
49             while (name.endsWith("[]")) { // NOI18N
50
dimension++;
51                 name = name.substring(0, name.length() - 2);
52             }
53
54             JavaModelPackage pkg = (JavaModelPackage) refImmediatePackage();
55             PrimitiveTypeClassImpl pt = (PrimitiveTypeClassImpl) pkg.getPrimitiveType();
56             Type result = pt.resolveType(name);
57             if (result == null) {
58                 JavaClassClassImpl cls = (JavaClassClassImpl) pkg.getJavaClass();
59                 result = cls.resolveClass(name, true);
60             }
61             if (result == null) {
62                 // class was not found - create UnresolvedClass
63
UnresolvedClassClassImpl cls = (UnresolvedClassClassImpl) pkg.getUnresolvedClass();
64                 result = cls.resolveUnresolved(name);
65             }
66             if (dimension > 0) {
67                 ArrayClass ar = ((JavaModelPackage)result.refImmediatePackage()).getArray();
68                 for (int i = 0; i < dimension; i++) {
69                     result = ar.resolveArray(result);
70                 }
71             }
72             return result;
73         } finally {
74             _unlock();
75         }
76     }
77     
78     public static Type getRawType(Type type) {
79         if (type instanceof ParameterizedType) {
80             return ((ParameterizedType) type).getDefinition();
81         } else if (type instanceof Array) {
82             int dimCount = 1;
83             Type elemType = ((Array) type).getType();
84             while (elemType instanceof Array) {
85                 elemType = ((Array) elemType).getType();
86                 dimCount++;
87             }
88             Type rawElemType=getRawType(elemType);
89             if (!elemType.equals(rawElemType)) {
90                 ArrayClass proxy = ((JavaModelPackage)type.refOutermostPackage()).getArray();
91                 type = rawElemType;
92                 for (; dimCount > 0; dimCount--) {
93                     type = proxy.resolveArray(type);
94                 }
95             }
96             return type;
97         } else if (type instanceof TypeParameter) {
98             TypeParameter typePar = (TypeParameter) type;
99             JavaClass superClass = typePar.getSuperClass();
100             
101             if ("java.lang.Object".equals(superClass.getName())) {
102                 Iterator JavaDoc intIt = superClass.getInterfaces().iterator();
103                 
104                 if (intIt.hasNext()) {
105                     superClass=(JavaClass) intIt.next();
106                 }
107             }
108             return getRawType(superClass);
109         }
110         return type;
111     }
112 }
113
Popular Tags