1 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 37 public final class BridgeUtils { 38 43 public static RefObject getJavaObject(Element element) { 44 ElementImpl impl = (ElementImpl) element.getCookie(Element.Impl.class); 45 return impl.getJavaElement(); 46 } 47 48 53 private static ClassElement getClassElement(ClassMember member) { 54 JavaClass jc = (JavaClass) member.getDeclaringClass(); 55 if (jc == null) { 56 throwIllegalState(member, "none declaring class"); } 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 ("Unknown member: " + member); 76 return e; 77 } 78 79 82 private static final ThreadLocal isForCookieSet = new ThreadLocal (); 83 84 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 103 public static ClassElement getElement(JavaClass member) { 104 Resource r = member.getResource(); 105 if (r == null) { 106 throwIllegalState(member, "missing resource"); } 108 DataObject d = JavaMetamodel.getManager().getDataObject(r); 109 if (d == null) { 110 throwIllegalState(member, "missing data object"); } 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"); } 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 135 public static ConstructorElement getElement(Constructor member) { 136 ClassElement ce = getClassElement(member); 137 if (ce == null) return null; 139 ConstructorElement[] els = ce.getConstructors(); 140 return (ConstructorElement) chooseElement(els, member); 141 } 142 143 148 public static FieldElement getElement(Field member) { 149 ClassElement ce = getClassElement(member); 150 if (ce == null) return null; 152 FieldElement[] els = ce.getFields(); 153 return (FieldElement) chooseElement(els, member); 154 } 155 156 161 public static MethodElement getElement(Method member) { 162 ClassElement ce = getClassElement(member); 163 if (ce == null) return null; 165 MethodElement[] els = ce.getMethods(); 166 return (MethodElement) chooseElement(els, member); 167 } 168 169 174 public static InitializerElement getElement(Initializer member) { 175 ClassElement ce = getClassElement(member); 176 if (ce == null) 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"); return null; 192 } 193 194 200 private static void throwIllegalState(ClassMember member, String message) throws IllegalStateException { 201 IllegalStateException ise = new IllegalStateException (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); ErrorManager.getDefault().annotate( 207 ise, ErrorManager.INFORMATIONAL, "member: " + member.toString(), null, null, null); } catch (JmiException e) { 209 ErrorManager.getDefault().annotate( 210 ise, ErrorManager.INFORMATIONAL, null, null, e, null); } 212 throw ise; 213 } 214 215 } 216 | Popular Tags |