KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > parser > ElementInfo


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 package org.netbeans.modules.javacore.parser;
20
21 import java.lang.ref.Reference JavaDoc;
22 import java.lang.ref.WeakReference JavaDoc;
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 /**
29  *
30  * @author Martin Matula
31  */

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 JavaDoc 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 JavaDoc name;
53
54     public static Reference JavaDoc createASTReference(ASTree tree) {
55         return new CachedReference(tree);
56     }
57
58     public ElementInfo(ASTree tree, int infoType, String JavaDoc 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 JavaDoc getASTree() {
79         return tree;
80     }
81
82     public ASTree refreshASTree() {
83         ASTree parentTree = resource.getASTree();
84         ASTProvider parser = (ASTProvider) parentTree.getASTContext();
85 // System.err.println(this.getClass().getName() + "." + name + ": refreshing AST for: " + parser);
86
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 JavaDoc {
105     private static final int CACHE_SIZE = Integer.getInteger("org.netbeans.javacore.ASTCache.size", new Integer JavaDoc(2)).intValue(); // NOI18N
106
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 JavaDoc 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         /** Creates a new instance of Cache */
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