1 19 package org.netbeans.modules.javacore.parser; 20 21 import java.lang.ref.Reference ; 22 import java.lang.ref.WeakReference ; 23 import org.netbeans.lib.java.parser.ASTree; 24 import org.netbeans.lib.java.parser.ASTreeTypes; 25 import org.netbeans.modules.javacore.jmiimpl.javamodel.ResourceImpl; 26 import org.netbeans.modules.javacore.jmiimpl.javamodel.SemiPersistentElement; 27 28 32 public class ElementInfo { 33 public static final int SINGLE_IMPORT_TYPE = ASTreeTypes.SINGLE_TYPE_IMPORT; 34 public static final int IMPORT_ON_DEMAND_TYPE = ASTreeTypes.TYPE_IMPORT_ON_DEMAND; 35 36 public static final TypeRef[] EMPTY_TYPEREFS = new TypeRef[0]; 37 public static final NameRef[] EMPTY_NAMEREFS = new NameRef[0]; 38 public static final TypeParamRef[] EMPTY_TPREFS = new TypeParamRef[0]; 39 40 public static final ClassInfo[] EMPTY_FEATURES = new ClassInfo[0]; 41 public static final ParameterInfo[] EMPTY_PARAMETERS = new ParameterInfo[0]; 42 public static final TypeParamInfo[] EMPTY_TYPE_PARAMS = new TypeParamInfo[0]; 43 public static final AnnotationInfo[] EMPTY_ANNOTATIONS = new AnnotationInfo[0]; 44 public static final AnnotationValueInfo[] EMPTY_ANNOTATION_VALUES = new AnnotationValueInfo[0]; 45 46 protected Reference tree; 47 protected final ResourceImpl resource; 48 private final int firstToken, lastToken; 49 private final int astType; 50 51 public final int infoType; 52 public final String name; 53 54 public static Reference createASTReference(ASTree tree) { 55 return new CachedReference(tree); 56 } 57 58 public ElementInfo(ASTree tree, int infoType, String name) { 59 if (tree == null) { 60 this.tree = null; 61 this.astType = this.firstToken = this.lastToken = 0; 62 this.resource = null; 63 } else { 64 this.resource = (ResourceImpl) ((MDRParser) tree.getASTContext()).getResource(); 65 this.tree = createASTReference(tree); 66 this.firstToken = tree.getFirstToken(); 67 this.lastToken = tree.getLastToken(); 68 this.astType = tree.getType(); 69 } 70 this.infoType = infoType; 71 this.name = name; 72 } 73 74 public void hardRefAST() { 75 resource.getElementInfo().hardRefAST(); 76 } 77 78 public final Reference getASTree() { 79 return tree; 80 } 81 82 public ASTree refreshASTree() { 83 ASTree parentTree = resource.getASTree(); 84 ASTProvider parser = (ASTProvider) parentTree.getASTContext(); 85 ASTree result = parser.findTree(parentTree, firstToken, lastToken, astType); 87 this.tree = createASTReference(result); 88 return result; 89 } 90 91 public ASTree getTypeAST(SemiPersistentElement owner) { 92 ASTree tree = owner.getASTree(); 93 if (tree != null) { 94 int type=tree.getType(); 95 ASTree parts[]=tree.getSubTrees(); 96 97 if (type==ASTreeTypes.SINGLE_TYPE_IMPORT || type==ASTreeTypes.TYPE_IMPORT_ON_DEMAND) 98 return parts[1]; 99 return parts[0]; 100 } 101 return null; 102 } 103 104 static class CachedReference extends WeakReference { 105 private static final int CACHE_SIZE = Integer.getInteger("org.netbeans.javacore.ASTCache.size", new Integer (2)).intValue(); private static final Cache CACHE = new Cache(CACHE_SIZE); 107 private static int requests = 0; 108 109 CachedReference(ASTree tree) { 110 super(tree); 111 MDRParser parser = (MDRParser) tree.getASTContext(); 112 CACHE.put(parser); 113 } 114 115 public Object get() { 116 ASTree result = (ASTree) super.get(); 117 if (result != null) { 118 MDRParser parser = (MDRParser) result.getASTContext(); 119 CACHE.put(parser); 120 } 121 return result; 122 } 123 } 124 125 126 static class Cache { 127 private final MDRParser[] cachedInstances; 128 129 130 public Cache(int size) { 131 cachedInstances = new MDRParser[size]; 132 } 133 134 public synchronized void put(MDRParser instance) { 135 MDRParser ce = instance; 136 for (int i = 0; i < cachedInstances.length; i++) { 137 MDRParser temp = cachedInstances[i]; 138 cachedInstances[i] = ce; 139 ce = temp; 140 if (temp == null || temp == instance || temp.getFileObject().equals(instance.getFileObject())) { 141 break; 142 } 143 } 144 } 145 146 public void dump() { 147 System.err.println("******** Cache dump:"); 148 for (int i = 0; i < cachedInstances.length; i++) { 149 System.err.println(cachedInstances[i].toString()); 150 } 151 System.err.println("*********"); 152 } 153 } 154 } 155 | Popular Tags |