KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
22 import org.netbeans.api.java.classpath.ClassPath;
23 import org.netbeans.jmi.javamodel.*;
24 import org.netbeans.jmi.javamodel.JavaModelPackage;
25 import org.netbeans.mdr.handlers.ClassProxyHandler;
26 import org.netbeans.mdr.storagemodel.StorableClass;
27 import org.netbeans.mdr.storagemodel.StorableBaseObject;
28 import org.netbeans.mdr.persistence.StorageException;
29 import org.netbeans.modules.javacore.ClassIndex;
30 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
31 import java.util.List JavaDoc;
32 import org.netbeans.modules.javacore.parser.NameRef;
33
34 /**
35  *
36  * @author Martin Matula
37  */

38 public abstract class JavaClassClassImpl extends ClassProxyHandler implements JavaClassClass {
39 // [TODO] this class should probably extend SemiPersistentClass, since not all JavaClasses
40
// are persistent (local classes are not)
41

42     /** Creates a new instance of JavaClassClassImpl */
43     public JavaClassClassImpl(StorableClass s) {
44         super(s);
45     }
46     
47     /**
48      * The default factory operation used to create an instance object.
49      * @return The created instance object.
50      */

51     public JavaClass createJavaClass() {
52         return create(null, null, 0, null, null, null, null, null, null);
53     }
54     
55     public JavaClass createJavaClass(java.lang.String JavaDoc name, List JavaDoc annotations, int modifiers, java.lang.String JavaDoc javadocText, JavaDoc javadoc, List JavaDoc features, MultipartId superClassName, List JavaDoc interfaceNames, List JavaDoc typeArguments) {
56         return create(name, annotations, modifiers, javadocText, javadoc, features, superClassName, interfaceNames, typeArguments);
57     }
58     
59     protected abstract JavaClass super_createJavaClass(java.lang.String JavaDoc name, List JavaDoc annotations, int modifiers, java.lang.String JavaDoc javadocText, JavaDoc javadoc, List JavaDoc features, MultipartId superClassName, List JavaDoc interfaceNames, List JavaDoc typeArguments);
60     protected abstract JavaClass super_createJavaClass();
61     
62     private JavaClassImpl create(java.lang.String JavaDoc name, List JavaDoc annotations, int modifiers, java.lang.String JavaDoc javadocText, JavaDoc javadoc, List JavaDoc features, MultipartId superClassName, List JavaDoc interfaceNames, List JavaDoc typeArguments) {
63         boolean fail = true;
64         _lock(true);
65         try {
66             JavaClassImpl result = create(name, modifiers, (NameRef) SemiPersistentElement.typeReferenceToTypeRef(superClassName, 0), interfaceNames == null ? null : (NameRef[]) SemiPersistentElement.typeReferencesToTypeRef(interfaceNames).toArray(new NameRef[interfaceNames.size()]), false);
67             result.setNew();
68             result.setData(annotations, javadocText, javadoc, features, superClassName, interfaceNames, typeArguments);
69             fail = false;
70             return result;
71         } finally {
72             _unlock(fail);
73         }
74     }
75
76     protected SemiPersistentElement createTransient() {
77         try {
78             StorableBaseObject s = _getDelegate();
79             DeferredObject o = new DeferredObject(null, s.getMdrStorage(), s.getImmediatePackageId(), s.getOutermostPackageId(), s.getMetaObject(), (StorableClass) s, null, true);
80             SemiPersistentElement result = (SemiPersistentElement) _getRepository().getHandler(o);
81             return result;
82         } catch (StorageException e) {
83             throw new RuntimeException JavaDoc();
84         }
85     }
86
87     public JavaClassImpl create(String JavaDoc name, int modifiers, NameRef superclass, NameRef[] interfaces, boolean isTransient) {
88         JavaClassImpl result;
89         if (isTransient) {
90             result = (JavaClassImpl) createTransient();
91             boolean changes = result.disableChanges;
92             result.disableChanges = true;
93             try {
94                 result.setName(name);
95                 result.setModifiers(modifiers);
96             } finally {
97                 result.disableChanges = changes;
98             }
99         } else {
100             result = (JavaClassImpl) super_createJavaClass(name, null, modifiers, null, null, null, null, null, null);
101             if (name != null) {
102                 JavaModelPackage srcExtent = (JavaModelPackage) refOutermostPackage();
103                 ClassIndex index = ClassIndex.getIndex(srcExtent);
104                 index.addClass(result, name, result.getSimpleName());
105                 //System.out.println("Class created: " + name + " MOFID: " + result.refMofId());
106
//Thread.dumpStack();
107
}
108         }
109         result.setSuperclassRef(superclass);
110         result.setInterfaceRefs(interfaces == null ? null : Arrays.asList(interfaces));
111         return result;
112     }
113
114     /**
115      *
116      * @param name
117      * @param onlyResolved If true, then this method will return null (instead of UnresolvedClass) if the classname is not resolved.
118      * @return
119      */

120     public JavaClass resolveClass(String JavaDoc name, boolean onlyResolved) {
121         if (name == null) return null;
122         _lock(false);
123         try {
124             ClassPath cp = JavaMetamodel.getManager().getClassPath();
125             JavaClass result = ClassIndex.getClassByFqn(name,cp);
126             if (result == null && !onlyResolved) {
127                 // class was not found - create UnresolvedClass
128
UnresolvedClassClassImpl cls = (UnresolvedClassClassImpl) JavaMetamodel.getManager().getDefaultExtent().getUnresolvedClass();
129                 result = cls.resolveUnresolved(name);
130             }
131             return result;
132         } finally {
133             _unlock();
134         }
135     }
136     
137     public org.netbeans.jmi.javamodel.Type resolve(java.lang.String JavaDoc name) {
138         JavaModelPackage pkg = (JavaModelPackage) refImmediatePackage();
139         return pkg.getType().resolve(name);
140     }
141 }
142
Popular Tags