KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > ui > nodes > BridgeUtils


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
20 package org.netbeans.modules.java.ui.nodes;
21
22 import org.openide.src.*;
23 import org.openide.src.Element;
24 import org.openide.loaders.DataObject;
25 import org.openide.cookies.SourceCookie;
26 import org.openide.ErrorManager;
27 import org.netbeans.modules.java.bridge.ElementImpl;
28 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
29 import org.netbeans.jmi.javamodel.*;
30
31 import javax.jmi.reflect.RefObject;
32 import javax.jmi.reflect.JmiException;
33
34 /**
35  * Helper for bridging between source hierarchy and jmi
36  */

37 public final class BridgeUtils {
38     /**
39      * maps source hierarchy element to jmi object
40      * @param element source hierarchy element
41      * @return jmi object
42      */

43     public static RefObject getJavaObject(Element element) {
44         ElementImpl impl = (ElementImpl) element.getCookie(Element.Impl.class);
45         return impl.getJavaElement();
46     }
47
48     /**
49      * maps jmi object to class element that contains the object.
50      * @param member jmi object
51      * @return source hierarchy element or null
52      */

53     private static ClassElement getClassElement(ClassMember member) {
54         JavaClass jc = (JavaClass) member.getDeclaringClass();
55         if (jc == null) {
56             throwIllegalState(member, "none declaring class"); // NOI18N
57
}
58         return getElement(jc);
59     }
60
61     public static Element getElement(ClassMember member) {
62         Element e = null;
63         if (member instanceof JavaClass)
64             e = getElement((JavaClass) member);
65         else if (member instanceof Constructor)
66             e = getElement((Constructor) member);
67         else if (member instanceof Field)
68             e = getElement((Field) member);
69         else if (member instanceof Initializer)
70             e = getElement((Initializer) member);
71         else if (member instanceof Method)
72             e = getElement((Method) member);
73         else
74             throw new IllegalArgumentException JavaDoc("Unknown member: " + member); //NOI18N
75

76         return e;
77     }
78     
79     /**
80      * @see #getElementForCookieSet
81      */

82     private static final ThreadLocal JavaDoc isForCookieSet = new ThreadLocal JavaDoc();
83     
84     /**
85      * workaround for issue #48997; see also issue #48595. It is replacement for
86      * {@link #getElement(ClassMember)} in case you would invoke it
87      * inside Node.getCookie
88      */

89     static Element getElementForCookieSet(ClassMember member) {
90         try {
91             isForCookieSet.set(Boolean.TRUE);
92             return getElement(member);
93         } finally {
94             isForCookieSet.set(Boolean.FALSE);
95         }
96     }
97     
98     /**
99      * maps JavaClass to ClassElement
100      * @param member jmi object
101      * @return source hierarchy element
102      */

103     public static ClassElement getElement(JavaClass member) {
104         Resource r = member.getResource();
105         if (r == null) {
106             throwIllegalState(member, "missing resource"); // NOI18N
107
}
108         DataObject d = JavaMetamodel.getManager().getDataObject(r);
109         if (d == null) {
110             throwIllegalState(member, "missing data object"); // NOI18N
111
}
112         SourceCookie sc = (SourceCookie) d.getCookie(SourceCookie.class);
113         if (sc == null) {
114             if (Boolean.TRUE.equals(isForCookieSet.get())) {
115                 return null;
116             } else {
117                 throwIllegalState(member, "missing source cookie"); // NOI18N
118
}
119         }
120         SourceElement source = sc.getSource();
121         ClassElement[] els;
122        
123         if (member.refImmediateComposite() instanceof JavaClass)
124             els = source.getAllClasses();
125         else
126             els = source.getClasses();
127         return (ClassElement) chooseElement(els, member);
128     }
129
130     /**
131      * maps Constructor to ConstructorElement
132      * @param member jmi object
133      * @return source hierarchy element
134      */

135     public static ConstructorElement getElement(Constructor member) {
136         ClassElement ce = getClassElement(member);
137         if (ce == null) // #48997
138
return null;
139         ConstructorElement[] els = ce.getConstructors();
140         return (ConstructorElement) chooseElement(els, member);
141     }
142
143     /**
144      * maps Field to FieldElement
145      * @param member jmi object
146      * @return source hierarchy element
147      */

148     public static FieldElement getElement(Field member) {
149         ClassElement ce = getClassElement(member);
150         if (ce == null) // #48997
151
return null;
152         FieldElement[] els = ce.getFields();
153         return (FieldElement) chooseElement(els, member);
154     }
155
156     /**
157      * maps Method to MethodElement
158      * @param member jmi object
159      * @return source hierarchy element
160      */

161     public static MethodElement getElement(Method member) {
162         ClassElement ce = getClassElement(member);
163         if (ce == null) // #48997
164
return null;
165         MethodElement[] els = ce.getMethods();
166         return (MethodElement) chooseElement(els, member);
167     }
168
169     /**
170      * maps Initializer to FieldElement
171      * @param member jmi object
172      * @return source hierarchy element
173      */

174     public static InitializerElement getElement(Initializer member) {
175         ClassElement ce = getClassElement(member);
176         if (ce == null) // #48997
177
return null;
178         InitializerElement[] els = ce.getInitializers();
179         return (InitializerElement) chooseElement(els, member);
180     }
181
182     private static Element chooseElement(Element[] els, ClassMember member) {
183         for (int i = 0; i < els.length; i++) {
184             Element e = els[i];
185             RefObject ref = getJavaObject(e);
186             if (member.equals(ref)) {
187                 return e;
188             }
189         }
190         throwIllegalState(member, "cannot map class member to source hierarchy element"); // NOI18N
191
return null;
192     }
193     
194     /**
195      * throws annotated illegal state exception
196      * @param member
197      * @param message
198      * @throws IllegalStateException
199      */

200     private static void throwIllegalState(ClassMember member, String JavaDoc message) throws IllegalStateException JavaDoc {
201         IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc(message);
202         ErrorManager.getDefault().annotate(ise, ErrorManager.INFORMATIONAL, member.getClass().toString(), null, null, null);
203         try {
204             ErrorManager.getDefault().annotate(
205                     ise, ErrorManager.INFORMATIONAL, "name: " + member.getName(), null, null, null); // NOI18N
206
ErrorManager.getDefault().annotate(
207                     ise, ErrorManager.INFORMATIONAL, "member: " + member.toString(), null, null, null); // NOI18N
208
} catch (JmiException e) {
209             ErrorManager.getDefault().annotate(
210                     ise, ErrorManager.INFORMATIONAL, null, null, e, null); // NOI18N
211
}
212         throw ise;
213     }
214     
215 }
216
Popular Tags