001: package javaparser;
002:
003: import javaparser.javacc_gen.*;
004: import java.util.*;
005: import tide.syntaxtree.*;
006:
007: /** A type is a class, an interface an enum an annotation defined
008: at first level of a source.
009: Subclasses: ClassNode, ...
010: */
011: public abstract class TypeNode extends ParserTreeNode implements
012: TypeInterface, NodeWithMod {
013: // the limits of this type
014: public Token blokStart, blokEnd;
015: // protected SimplifiedSyntaxTree simplifiedSyntaxTree;
016: protected SimplifiedSyntaxTree2 simplifiedSyntaxTree2;
017: public TypeNode parentType;
018:
019: // not used in the tree
020: public List<TypeNode> directChildTypes = new ArrayList<TypeNode>();
021:
022: // raw tree reference. Used for real time later parsing
023: // also used during
024: // public ParserTreeNode rawDeclarationNode;
025: public RAWParserTreeNode rawDeclarationNode2;
026:
027: // TODO:
028: // interesting for completion: the ClassOrInterfaceBodyDeclaration tokens that are NOT subchilds type declarations
029: // but anonymous instanciations with body.
030: // for example new ActionListener() { ... }
031:
032: /*
033: public TypeNode(String descr, SimplifiedSyntaxTree sst, TypeNode parentType, ParserTreeNode rawDeclarationNode)
034: {
035: super(descr);
036:
037: this.sortPriority = 5;
038:
039: this.simplifiedSyntaxTree = sst;
040: this.parentType = parentType;
041: this.rawDeclarationNode = rawDeclarationNode;
042:
043: if(parentType!=null)
044: {
045: parentType.directChildTypes.add(this);
046: }
047: }*/
048:
049: public TypeNode(String descr, SimplifiedSyntaxTree2 sst,
050: TypeNode parentType, RAWParserTreeNode rawDeclarationNode2) {
051: super (descr);
052:
053: this .sortPriority = 5;
054:
055: this .simplifiedSyntaxTree2 = sst;
056: this .parentType = parentType;
057: this .rawDeclarationNode2 = rawDeclarationNode2;
058:
059: if (parentType != null) {
060: parentType.directChildTypes.add(this );
061: }
062: }
063:
064: /** Simple name, without the parent.
065: */
066: public abstract String getTypeSimpleName();
067:
068: public abstract boolean isPublic();
069:
070: /** With the package name.
071: */
072: public abstract String getJavaFullName();
073:
074: /** The name without the package. Constructed from the simple name and the parents chain.
075: */
076: public String getTypeRelativeName() {
077: StringBuilder sb = new StringBuilder();
078: sb.append(getTypeSimpleName());
079: TypeNode pn = this .parentType;
080: while (pn != null) {
081: sb.insert(0, ".");
082: sb.insert(0, pn.getTypeSimpleName());
083: pn = pn.parentType;
084: }
085: return sb.toString();
086: }
087:
088: /** To detect if public or static, use Utils.contains(mods, JavaParserConstants.PUBLIC)
089: */
090: public abstract int[] getModifiers();
091:
092: /** null if none
093: looks in the declared subchilds, but not actually in the anonymous declared classes
094: */
095: @edu.umd.cs.findbugs.annotations.CheckForNull
096: public TypeNode getDeepestContainingTypeAt(int line, int col) {
097: if (!TreeUtils.contains(this , line, col))
098: return null;
099:
100: // deepest => look in childs
101: for (TypeNode tn : this .directChildTypes) {
102: TypeNode found = tn.getDeepestContainingTypeAt(line, col);
103: if (found != null)
104: return found;
105: }
106:
107: // not found in childs => this is the deepest
108: return this ;
109: }
110:
111: /** Interesting for completion: the ClassOrInterfaceBodyDeclaration tokens that are NOT subchilds type declarations
112: but anonymous instanciations with body.
113: for example new ActionListener() { ... }
114:
115: this is called at completion time
116: */
117: public AnonymousType getDeepestContainingAnonymousClassBlocForLocation(
118: int line, int col) {
119: List<RAWParserTreeNode> found = new ArrayList<RAWParserTreeNode>();
120: if (rawDeclarationNode2 == null) {
121: System.out
122: .println("No raw tree found for deepest type search.");
123: return null;
124: }
125: CCTreeUtils.getAllChildsNamed(rawDeclarationNode2,
126: "ClassOrInterfaceBody", found); // TODO: annotations ?, enums ?
127: //System.out.println("\n"+found.size()+" declaration found:");
128:
129: for (RAWParserTreeNode tn : found) {
130: //System.out.println("\nDecl at line "+tn.getStartLinCol()[0]);
131: AnonymousType at = AnonymousType.parse(tn, this );
132: if (at != null) {
133: if (Utils.isInto(line, col, at.start, at.end)) {
134: //System.out.println("found anonymous type "+at.name);
135: //new Throwable().printStackTrace();
136: return at;
137: }
138: }
139: }
140:
141: return null;
142: }
143:
144: /** Call this to help GC !
145: */
146: @Override
147: public void terminate() {
148: super .terminate();
149:
150: simplifiedSyntaxTree2 = null;
151: parentType = null;
152: directChildTypes.clear();
153: //rawDeclarationNode = null;
154: rawDeclarationNode2 = null;
155: blokEnd = null;
156: blokStart = null;
157: }
158:
159: }
|