1 19 20 package org.netbeans.modules.java.bridge; 21 22 import java.util.*; 23 import java.lang.reflect.Modifier ; 24 import org.netbeans.api.mdr.MDRepository; 25 import org.netbeans.jmi.javamodel.JavaClass; 26 import org.netbeans.jmi.javamodel.JavaClassClass; 27 import org.netbeans.jmi.javamodel.FieldClass; 28 import org.netbeans.jmi.javamodel.MethodClass; 29 import org.netbeans.jmi.javamodel.ConstructorClass; 30 import org.netbeans.jmi.javamodel.InitializerClass; 31 import org.netbeans.jmi.javamodel.ParameterClass; 32 import org.netbeans.jmi.javamodel.ImportClass; 33 import org.netbeans.jmi.javamodel.Feature; 34 import org.netbeans.jmi.javamodel.Initializer; 35 import org.netbeans.jmi.javamodel.Field; 36 import org.netbeans.jmi.javamodel.Constructor; 37 import org.netbeans.jmi.javamodel.Method; 38 import org.netbeans.jmi.javamodel.Parameter; 39 import org.netbeans.jmi.javamodel.Resource; 40 41 import org.openide.src.*; 42 43 public class FeaturesCollection { 44 private static final Element[] NO_ELEMENTS = new Element[0]; 45 static final Type[] NO_TYPES = new Type[0]; 46 47 ElementImpl parentImpl; 48 ClassElement parentClass; 49 50 MDRepository repository; 51 JavaClass javaClass; 52 Resource resource; 53 DefaultLangModel model; 54 55 JavaClassClass classProxy; 56 FieldClass fieldProxy; 57 MethodClass methodProxy; 58 ConstructorClass constructorProxy; 59 InitializerClass initializerProxy; 60 ParameterClass parameterProxy; 61 ImportClass importProxy; 62 63 65 private Feature currentElement = null; 66 private boolean insertBefore; 67 68 70 public FeaturesCollection(ElementImpl parentImpl) { 71 this.parentImpl = parentImpl; 72 73 repository = parentImpl.repository; 74 if (parentImpl instanceof ClassElementImpl) { 75 javaClass = (JavaClass) parentImpl.getJavaElement (); 76 resource = javaClass.getResource(); 77 } 78 model = parentImpl.getModelImpl (); 79 80 if (parentImpl.javaModelPackage != null) { 81 try { 82 classProxy = parentImpl.javaModelPackage.getJavaClass (); 83 fieldProxy = parentImpl.javaModelPackage.getField (); 84 methodProxy = parentImpl.javaModelPackage.getMethod (); 85 constructorProxy = parentImpl.javaModelPackage.getConstructor (); 86 initializerProxy = parentImpl.javaModelPackage.getInitializer (); 87 parameterProxy = parentImpl.javaModelPackage.getParameter (); 88 importProxy = parentImpl.javaModelPackage.getImport (); 89 } catch (javax.jmi.reflect.InvalidObjectException e) { 90 } 91 } 92 } 93 94 96 public ClassElement getParentClass () { 97 if (parentClass == null) 98 parentClass = (ClassElement) parentImpl.getElement (); 99 return parentClass; 100 } 101 102 104 public List typesToDescriptors (Type[] types) { 105 List result = new LinkedList (); 106 for (int x = 0; x < types.length; x++) { 107 result.add (parentImpl.typeToDescr (types [x])); 108 } 109 return result; 110 } 111 112 public List convertParameters (MethodParameter[] params) { 113 List result = new LinkedList (); 114 for (int x = 0; x < params.length; x++) { 115 Parameter par = parameterProxy.createParameter ( 116 params [x].getName (), 117 null, 118 params [x].isFinal (), 119 null, 120 0, 121 false 122 ); 123 Type type = params[x].getType(); 124 Type t = type; 125 while (t.isArray()) { 126 t = t.getElementType(); 127 } 128 if (t.isPrimitive()) { 129 par.setType(parentImpl.typeToDescr(type)); 130 } else { 131 par.setTypeName(parentImpl.typeToTypeReference(type)); 132 } 133 result.add (par); 134 } 135 return result; 136 } 137 138 public List convertIdentifiers (Identifier [] ids) { 139 List result = new LinkedList (); 140 for (int x = 0; x < ids.length; x++) { 141 result.add (parentImpl.typeToDescr (Type.createClass (ids[x]))); 142 } 143 return result; 144 } 145 146 148 public JavaClass createClass (ClassElement cls) { 149 int x; 150 List features = new ArrayList(); 151 152 FieldElement [] fields = cls.getFields (); 153 for (x = 0; x < fields.length; x++) { 154 features.add(createField (fields [x])); 155 } 156 InitializerElement [] initializers = cls.getInitializers (); 157 for (x = 0; x < initializers.length; x++) { 158 features.add(createInitializer (initializers [x])); 159 } 160 ConstructorElement [] constrs = cls.getConstructors (); 161 for (x = 0; x < constrs.length; x++) { 162 features.add(createConstructor (constrs [x])); 163 } 164 MethodElement [] methods = cls.getMethods (); 165 for (x = 0; x < methods.length; x++) { 166 features.add(createMethod (methods [x])); 167 } 168 ClassElement [] classes = cls.getClasses (); 169 for (x = 0; x < classes.length; x++) { 170 features.add(createClass (classes [x])); 171 } 172 173 String simpleName = cls.getName().getName(); 174 Identifier parentName = null; 175 if (parentImpl instanceof ClassElementImpl) { 176 parentName = ((ClassElementImpl) parentImpl).getName(); 177 } 178 else { 179 SourceElementImpl sei = (SourceElementImpl) parentImpl; 180 parentName = sei.getPackage(); 181 } 182 String fullName = simpleName; 183 if (parentName !=null) { 184 String parentFqn=parentName.getFullName(); 185 186 if (parentFqn.length()>0) 187 fullName=parentName+"."+simpleName; } 189 Identifier className = Identifier.create(fullName, simpleName); 190 Identifier superclass = cls.getSuperclass (); 191 int modifier = cls.getModifiers(); 192 193 if (!cls.isClassOrInterface()) 194 modifier |= Modifier.INTERFACE; 195 JavaClass res = classProxy.createJavaClass ( 196 className.getFullName (), 197 null, modifier, 199 cls.getJavaDoc().getRawText (), 200 null, features, 202 null, 203 null, 204 null ); 206 207 if (superclass != null) { 208 res.setSuperClass ((JavaClass) parentImpl.javaModelPackage.getType().resolve(superclass.getFullName())); 209 } 210 211 Identifier [] interfaces = cls.getInterfaces (); 212 List inters = new ArrayList(interfaces.length); 213 for (int i = 0; i < interfaces.length; i++) { 214 inters.add(parentImpl.javaModelPackage.getMultipartId().createMultipartId(interfaces[i].getFullName(), null, null)); 215 } 216 res.getInterfaceNames().addAll(inters); 217 return res; 218 } 219 220 public Method createMethod (MethodElement method) { 221 Method res = methodProxy.createMethod ( 222 method.getName ().getName (), 223 null, method.getModifiers (), 225 method.getJavaDoc().getRawText (), 226 null, null, method.getBody (), 229 null, convertParameters (method.getParameters ()), 231 null, 232 null, 233 0 234 ); 235 Type retType = method.getReturn(); 236 Type t = retType; 237 while (t.isArray()) { 238 t = t.getElementType(); 239 } 240 if (t.isPrimitive()) { 241 res.setType(parentImpl.typeToDescr(retType)); 242 } else { 243 res.setTypeName(parentImpl.typeToTypeReference(retType)); 244 } 245 Identifier [] exs = method.getExceptions (); 246 List list = new ArrayList (exs.length); 247 for (int x = 0; x < exs.length; x++) { 248 String excName = exs[x].getSourceName(); 249 if (excName == null) 250 excName = exs[x].getFullName(); 251 list.add (parentImpl.javaModelPackage.getMultipartId().createMultipartId(excName, null, null)); 252 } 253 res.getExceptionNames().addAll(list); 254 return res; 255 } 256 257 public Constructor createConstructor (ConstructorElement constr) { 258 Constructor res = constructorProxy.createConstructor( 259 null, 260 null, constr.getModifiers (), 262 constr.getJavaDoc ().getRawText (), 263 null, null, constr.getBody (), null, convertParameters (constr.getParameters ()), 268 null ); 270 Identifier [] exs = constr.getExceptions (); 271 List list = new ArrayList (exs.length); 272 for (int x = 0; x < exs.length; x++) { 273 list.add (parentImpl.javaModelPackage.getMultipartId().createMultipartId(exs[x].getFullName(), null, null)); 274 } 275 res.getExceptionNames().addAll(list); 276 return res; 277 } 278 279 public Field createField (FieldElement field) { 280 String initValue = field.getInitValue(); 281 if ((initValue != null) && (initValue.length() == 0)) 282 initValue = null; 283 Field res = fieldProxy.createField ( 284 field.getName ().getName (), 285 null, field.getModifiers (), 287 field.getJavaDoc().getRawText (), 288 null, (field.getModifiers () & Modifier.FINAL) > 0, null, 0, null, initValue ); 295 res.setType(parentImpl.typeToDescr(field.getType ())); 296 return res; 297 } 298 299 public Initializer createInitializer (InitializerElement initializer) { 300 JavaDoc javaDoc = initializer.getJavaDoc (); 301 Initializer res = initializerProxy.createInitializer ( 302 "<clinit>", null, initializer.isStatic () ? Modifier.STATIC : 0, javaDoc != null ? javaDoc.getRawText() : null, null, null, initializer.getBody () ); 310 return res; 311 } 312 313 public org.netbeans.jmi.javamodel.Import createImport (ImportElement elem) { 314 Import imp = elem.getImport (); 315 String name = imp.getIdentifier ().getFullName (); 316 org.netbeans.jmi.javamodel.Import res = importProxy.createImport ( 317 name, 318 null, 319 false, 320 imp.isPackage() 321 ); 322 return res; 323 } 324 325 } 326 | Popular Tags |