1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.lang.reflect.Modifier ; 22 import java.util.*; 23 import org.netbeans.lib.java.parser.ASTree; 24 import org.netbeans.lib.java.parser.Token; 25 import org.netbeans.mdr.storagemodel.StorableObject; 26 import org.netbeans.modules.javacore.parser.ElementInfo; 27 import org.netbeans.modules.javacore.parser.ASTProvider; 28 import org.openide.util.Utilities; 29 import org.netbeans.jmi.javamodel.*; 30 import org.netbeans.modules.javacore.ClassIndex; 31 import org.netbeans.modules.javacore.parser.MDRParser; 32 33 39 public abstract class ImportImpl extends SemiPersistentElement implements Import { 40 private static final ElementInfo DEFAULT_INFO = new ElementInfo(null, ElementInfo.SINGLE_IMPORT_TYPE, null); 41 42 private String name = null; 43 private boolean isStatic = false; 44 private boolean isOnDemand = false; 45 46 private MultipartId identifier = null; 47 48 49 public ImportImpl(StorableObject s) { 50 super(s); 51 } 52 53 58 protected void matchPersistent(ElementInfo info) { 59 } 61 62 protected ElementInfo getDefaultInfo() { 63 return DEFAULT_INFO; 64 } 65 66 68 protected void matchElementInfo(ElementInfo newInfo) { 69 super.matchElementInfo(newInfo); 70 71 ElementInfo refInfo=getElementInfo(); 72 73 if (!Utilities.compareObjects(refInfo.name, newInfo.name)) 74 setName(newInfo.name); 75 resetChildren(); 76 } 77 78 protected void resetChildren() { 79 super.resetChildren(); 80 if (identifier != null) { 81 MultipartId temp = identifier; 82 identifier = null; 83 temp.refDelete(); 84 childrenInited = false; 85 } 86 } 87 88 public String getName() { 89 if (isChanged(CHANGED_NAME)) { 90 return name; 91 } else { 92 return getElementInfo().name; 93 } 94 } 95 96 public void setName(String name) { 97 objectChanged(CHANGED_NAME); 98 this.name = name; 99 MultipartId newValue = createIdentifier(); 100 changeChild(getIdentifier(), newValue); 101 this.identifier = newValue; 102 } 103 104 private MultipartId createIdentifier() { 105 if (name == null) return null; 106 MultipartIdClass proxy = ((JavaModelPackage) refImmediatePackage()).getMultipartId(); 107 return proxy.createMultipartId(name, null, null); 108 } 109 110 void setData(String name, boolean onDemand, boolean isStatic, MultipartId identifier) { 111 this.name = name; 112 if (identifier == null) { 113 this.identifier = createIdentifier(); 114 } else { 115 this.identifier = identifier; 116 if (name == null) { 117 name = identifier.getName(); 118 } 119 } 120 changeChild(null, this.identifier); 121 this.isOnDemand = onDemand; 122 this.isStatic = isStatic; 123 childrenInited = true; 124 } 125 126 public boolean isStatic() { 127 if (isChanged(CHANGED_IS_STATIC)) { 128 return isStatic; 129 } else { 130 ASTree tree=getASTree(); 131 132 if (tree==null) 133 return false; 134 return tree.getSubTrees()[0]!=null; 135 } 136 } 137 138 public void setStatic(boolean s) { 139 objectChanged(CHANGED_IS_STATIC); 140 isStatic = s; 141 } 142 143 public boolean isOnDemand() { 144 if (isChanged(CHANGED_IS_ON_DEMAND)) { 145 return isOnDemand; 146 } else { 147 return getElementInfo().infoType == ElementInfo.IMPORT_ON_DEMAND_TYPE; 148 } 149 } 150 151 public void setOnDemand(boolean s) { 152 objectChanged(CHANGED_IS_ON_DEMAND); 153 isOnDemand = s; 154 } 155 156 protected ASTree getPartTree(ElementPartKind part) { 157 if (ElementPartKindEnum.NAME.equals(part)) { 158 return getASTree().getSubTrees()[1]; 159 } 160 throw new IllegalArgumentException ("Invalid part for this element: " + part); } 162 163 String getRawText() { 164 StringBuffer buf = new StringBuffer (); 165 buf.append("import "); if (isStatic()) 167 buf.append("static "); MetadataElement id = (MetadataElement)getIdentifier(); 169 buf.append(id == null ? getName() : id.getSourceText()); 170 if (isOnDemand()) { 171 buf.append(".*"); } 173 buf.append(';'); 174 return buf.toString(); 175 } 176 177 public void getDiff(List diff) { 178 ASTProvider parser = getParser(); 179 ASTree tree = getASTree(); 180 ASTree[] children = tree.getSubTrees(); 181 182 if (isChanged(CHANGED_IS_STATIC)) { 183 int startOffset, endOffset = parser.getToken(children[1].getFirstToken()).getStartOffset(); 184 if (isStatic) { 185 if (children[0] == null) { 186 startOffset = endOffset; 187 diff.add(new DiffElement(startOffset, endOffset, "static ")); } 189 } 190 else { 191 if (children[0] != null) { 192 startOffset = parser.getToken(children[0].getFirstToken()).getStartOffset(); 193 diff.add(new DiffElement(startOffset, endOffset, "")); 194 } 195 } 196 } 197 getChildDiff(diff, parser, children[1], (MetadataElement) getIdentifier(), CHANGED_NAME); 198 199 if (isChanged(CHANGED_IS_ON_DEMAND)) { 200 int startOffset = parser.getToken(children[1].getLastToken()).getEndOffset(); 201 int endOffset = parser.getToken(tree.getLastToken()).getStartOffset(); 202 diff.add(new DiffElement(startOffset, endOffset, isOnDemand() ? ".*" : "")); } 204 } 205 206 public void replaceChild(Element oldElement, Element newElement) { 207 if (childrenInited && oldElement.equals(identifier)) { 208 setIdentifier((MultipartId) newElement); 209 return; 210 } 211 } 212 213 public MultipartId getIdentifier() { 214 checkUpToDate(); 215 if (!childrenInited) { 216 initChildren(); 217 } 218 return identifier; 219 } 220 221 public void setIdentifier(MultipartId newValue) { 222 objectChanged(CHANGED_NAME); 223 changeChild(getIdentifier(), newValue); 224 this.identifier = newValue; 225 this.name = newValue != null ? ((MultipartIdImpl)newValue).getSourceText() : ""; } 227 228 protected void initChildren() { 229 childrenInited = false; 230 ElementInfo info = getElementInfo(); 231 ASTree tree = info.getTypeAST(this); 232 if (identifier != null) { 233 ((TransientElement) identifier).init(tree); 234 } else { 235 identifier = (MultipartId) createElement(tree, ((JavaModelPackage) refImmediatePackage()).getMultipartId()); 236 } 237 childrenInited = true; 238 } 239 240 public List getChildren() { 241 List l = new ArrayList(1); 242 addIfNotNull(l, getIdentifier()); 243 return l; 244 } 245 246 public NamedElement getImportedNamespace() { 247 JavaModelPackage model = (JavaModelPackage) refImmediatePackage(); 248 MultipartId id = getIdentifier(); 249 if (id == null) return null; 250 return id.getElement(); 251 } 252 253 public Collection getReferences() { 254 return Collections.EMPTY_LIST; 255 } 256 257 private String getSignature(Feature feature) { 258 if (feature instanceof Method) { 259 StringBuffer sig=new StringBuffer (); 260 Object [] pars=((Method)feature).getParameters().toArray(); 261 262 sig.append(feature.getName()); 263 for(int i=0;i<pars.length;i++) { 264 Parameter par=(Parameter)pars[i]; 265 266 sig.append(par.getType().getName()); 267 sig.append(';'); 268 } 269 sig.append('.'); 270 return sig.toString(); 271 } 272 return feature.getName(); 273 } 274 275 private void getAllStaticWithName(JavaClass jcls,String name,Map fMap, Set visited) { 276 if (!visited.add(jcls)) return; 277 JavaClass superJavaClass=jcls.getSuperClass(); 278 Iterator ifaceIt=jcls.getInterfaces().iterator(); 279 Iterator fIt; 280 281 if (superJavaClass!=null) 282 getAllStaticWithName(superJavaClass, name, fMap, visited); 283 while (ifaceIt.hasNext()) { 284 JavaClass ifaceClass=(JavaClass)ifaceIt.next(); 285 286 if (ifaceClass!=null) 287 getAllStaticWithName(ifaceClass, name, fMap, visited); 288 } 289 Object [] features=jcls.getFeatures().toArray(); 290 for(int i=0;i<features.length;i++) { 291 Feature feature=(Feature)features[i]; 292 293 if (!Modifier.isStatic(feature.getModifiers())) 294 continue; 295 if (name!=null && !name.equals(feature.getName())) 296 continue; 297 if (feature instanceof Field || feature instanceof Method) { 298 fMap.put(getSignature(feature), feature); 299 } 300 } 301 } 302 303 public Collection getImportedElements() { 304 if (isStatic()) { 305 Map imported=new HashMap(); 306 307 if (isOnDemand()) { 308 getAllStaticWithName((JavaClass)getImportedNamespace(),null,imported, new HashSet()); 309 } else { 310 MDRParser parser=getParser(); 311 ASTree tree=getPartTree(ElementPartKindEnum.NAME); 312 ASTree parts[]=tree.getSubTrees(); 313 Object symbol=parser.getSemanticInfo(parts[0], this); 314 315 if (symbol instanceof JavaClass) { 316 String name=(String )((Token)parts[1]).getValue(); 317 318 getAllStaticWithName((JavaClass)symbol, name, imported, new HashSet()); 319 } 320 } 321 return imported.values(); 322 } else if (isOnDemand()) { 323 NamedElement el=getImportedNamespace(); 324 Collection classes=new ArrayList(); 325 326 if (el instanceof JavaPackage) { 327 Iterator rIt=((JavaPackage)el).getResources().iterator(); 328 329 while(rIt.hasNext()) { 330 Iterator topIt=((Resource)rIt.next()).getClassifiers().iterator(); 331 332 while(topIt.hasNext()) { 333 JavaClass jcls=(JavaClass)topIt.next(); 334 335 if (Modifier.isPublic(jcls.getModifiers())) { 336 classes.add(jcls); 337 } 338 } 339 } 340 } else if ((el instanceof JavaClass) && !(el instanceof UnresolvedClass)) { 341 JavaClass jcls=(JavaClass)el; 342 ClassIndex index=ClassIndex.getIndex((JavaModelPackage)jcls.refImmediatePackage()); 343 Iterator innerIt=index.getClassesByFQNPrefix(jcls.getName().concat(".")).iterator(); 345 while(innerIt.hasNext()) { 346 JavaClass inner=(JavaClass)innerIt.next(); 347 348 if (inner.getDeclaringClass().equals(jcls)) { 349 classes.add(inner); 350 } 351 } 352 } 353 return classes; 354 } else { 355 return Collections.singletonList(getImportedNamespace()); 356 } 357 } 358 359 protected void hardRefParent(boolean enabled) { 360 } 362 363 protected void parentChanged() { 364 } 366 367 public Element duplicate(JavaModelPackage targetExtent) { 368 return targetExtent.getImport().createImport( 369 getName(), 370 (MultipartId) duplicateElement(getIdentifier(), targetExtent), 371 isStatic(), 372 isOnDemand() 373 ); 374 } 375 376 protected void _delete() { 377 if (childrenInited) { 379 deleteChild(identifier); 380 } 381 super._delete(); 386 } 387 } 388 | Popular Tags |