KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Set JavaDoc;
24 import org.netbeans.jmi.javamodel.Resource;
25 import org.netbeans.lib.java.parser.ASTree;
26 import org.netbeans.lib.java.parser.ASTreeTypes;
27 import org.netbeans.modules.javacore.jmiimpl.javamodel.MetadataElement;
28 import org.netbeans.modules.javacore.jmiimpl.javamodel.ResourceImpl;
29 import org.netbeans.modules.javacore.jmiimpl.javamodel.SemiPersistentElement;
30
31 /**
32  *
33  * @author Martin Matula
34  */

35 public final class ResourceInfo extends ElementInfo {
36     public static final int RESOURCE_TYPE = ASTreeTypes.COMPILATION_UNIT;
37
38     public final ClassInfo[] classes;
39     public final ElementInfo[] imports;
40     
41     private MDRParser hardRef;
42     private Set JavaDoc attributed;
43     private static final boolean DEBUG = false;
44
45     public ResourceInfo(ASTree tree, int infoType, Resource resource, ClassInfo[] classes, ElementInfo[] imports) {
46         super(tree, infoType, resource == null ? null : resource.getName());
47         this.classes = classes == null ? EMPTY_FEATURES : classes;
48         this.imports = imports == null ? EMPTY_FEATURES : imports;
49     }
50
51     public void hardRefAST() {
52         hardRef = resource.getParser();
53     }
54
55     public void doAttribution(MetadataElement parent) {
56         ASTree tree = parent.getASTree();
57         if (tree != null) {
58             if (attributed == null) {
59                 attributed = new HashSet JavaDoc();
60             }
61             if (attributed.add(parent)) {
62                 MDRParser parser = (MDRParser) tree.getASTContext();
63                 parser.prepareForAttribution(parent, tree);
64             }
65         }
66     }
67
68     public ASTree refreshASTree() {
69         MDRParser mdrParser = ((ResourceImpl) resource).createMDRParser(null, false);
70         if (DEBUG) System.err.println("Recreating AST for: " + mdrParser);
71         try {
72             resource.alreadyChecking = true;
73             resource.alreadyCheckingStackTrace = new RuntimeException JavaDoc();
74             ResourceInfo resInfo = mdrParser.enterMembers();
75             this.tree = resInfo.tree;
76         } finally {
77             resource.alreadyChecking = false;
78         }
79         
80         if (attributed != null) {
81             for (Iterator JavaDoc it = attributed.iterator(); it.hasNext();) {
82                 MetadataElement entry = (MetadataElement) it.next();
83                 mdrParser.prepareForAttribution(entry, entry.getASTree());
84             }
85         }
86         
87         return mdrParser.getASTree();
88     }
89
90     public ASTree getTypeAST(SemiPersistentElement owner) {
91         ASTree tree = owner.getASTree();
92         if (tree != null) {
93             ASTree pkg = tree.getSubTrees()[0];
94             return pkg == null ? null : pkg.getSubTrees()[1];
95         }
96         return null;
97     }
98 }
99
Popular Tags