001: package javaparser;
002:
003: import java.util.*;
004: import tide.syntaxtree.*;
005: import javaparser.javacc_gen.JavaParserConstants;
006:
007: /** represents an enum in the simplified syntax tree
008: */
009: public class EnumNode extends TypeNode {
010: public String name = "", fullName;
011: public int[] modifiers;
012: public String modifiersShort;
013: public List<String> items = new ArrayList<String>();
014:
015: public EnumNode(SimplifiedSyntaxTree2 sst,
016: RAWParserTreeNode enumDeclarationNode,
017: RAWParserTreeNode modNode, TypeNode parentType) {
018: super ("enum", sst, parentType, enumDeclarationNode);
019:
020: this .sortPriority = 3;
021: this .expandInView = false;
022:
023: this .modifiers = CCTreeUtils.getAllModifiers(modNode);
024: modifiersShort = Utils.getModifiersShortString(modifiers);
025:
026: if (modNode.getChildCount() > 0) {
027: this .setStartPosFrom(CCTreeUtils.getFirstSubchild(modNode));
028: } else {
029: this .setStartPosFrom(CCTreeUtils
030: .getFirstSubchild(enumDeclarationNode));
031: }
032:
033: this .setEndPosFrom(CCTreeUtils
034: .getLastSubchild(enumDeclarationNode));
035:
036: // {enum, name, EnumBody}
037: this .name = enumDeclarationNode.childs.get(1).toString();
038: parseEnumBody(enumDeclarationNode.childs.get(2));
039:
040: StringBuilder fn = new StringBuilder();
041: if (parentType != null) {
042: fn.append(parentType.getJavaFullName());
043: fn.append("$"); // inner classes
044: } else {
045: fn.append(sst.packageNode.toString());
046: if (fn.length() > 0)
047: fn.append("."); // may be the root package (no name)
048: }
049: fn.append(name);
050: fullName = fn.toString();
051:
052: RAWParserTreeNode bodyNode = CCTreeUtils
053: .getFirstSubchildNamed_ONLYInDirectChilds(
054: enumDeclarationNode, "EnumBody");
055: if (bodyNode != null) {
056: for (RAWParserTreeNode ci : bodyNode.childs) {
057: if (ci.toString().equals("EnumConstant")) {
058: addConstant(ci);
059: }
060: }
061: }
062: } // Constructor
063: /*
064: private void addConstant(ParserTreeNode enumConstNode)
065: {
066: EnumConstantNode en = new EnumConstantNode(enumConstNode);
067: items.add( ""+en.name );
068: this.add(en);
069: }*/
070:
071: private void addConstant(RAWParserTreeNode enumConstNode) {
072: EnumConstantNode en = new EnumConstantNode(enumConstNode);
073: items.add("" + en.name);
074: this .add(en);
075: }
076:
077: /** call this to help GC !
078: */
079: @Override
080: public void terminate() {
081: super .terminate();
082:
083: items.clear();
084: fullName = null;
085: name = null;
086: modifiersShort = null;
087: modifiers = null;
088: }
089:
090: /** Have {EnumConstant and ClassOrInterfaceBodyDeclaration (with Constructor, Method, Field)}
091: */
092: private void parseEnumBody(RAWParserTreeNode body) {
093: for (RAWParserTreeNode ci : body.childs) {
094: if (ci.toString().equals("EnumConstant")) {
095: items.add(ci.childs.get(1).toString());
096: } else if (ci.toString().equals(
097: "ClassOrInterfaceBodyDeclaration")) {
098: // Simplified !, no public, private nodes...
099: ClassNode.addClassOrInterfaceBodyDeclaration(
100: this .simplifiedSyntaxTree2, ci, this , this );
101: }
102: }
103: //test: System.out.println(""+items);
104: }
105:
106: @Override
107: public String toString() {
108: return "" + name;
109: }
110:
111: @Override
112: public String getTypeSimpleName() {
113: return name;
114: }
115:
116: /** With the package name.
117: */
118: @Override
119: public String getJavaFullName() {
120: return fullName;
121: }
122:
123: @Override
124: public int[] getModifiers() {
125: return this .modifiers;
126: }
127:
128: @Override
129: public boolean isPublic() {
130: return Utils.isPublic(modifiers);
131: }
132:
133: public boolean isPrivate() {
134: return Utils.isPrivate(modifiers);
135: }
136:
137: public boolean isProtected() {
138: return Utils.isProtected(modifiers);
139: }
140:
141: public boolean isStatic() {
142: return Utils.isStatic(modifiers);
143: }
144:
145: }
|