001: package javaparser;
002:
003: import javaparser.javacc_gen.JavaParserConstants;
004: import snow.utils.ArrayUtils;
005:
006: public class FieldNode extends ParserTreeNode implements NodeWithMod {
007: public int[] modifiers;
008: public String modifiersShort;
009: public String type = "";
010: public String name = ""; //
011:
012: /** May add several FieldNodes if several fields declared on the same statement
013: i.e. public final int a,b,c=3, d;
014: */
015: public FieldNode(RAWParserTreeNode fieldDeclarationNode,
016: RAWParserTreeNode modNode, ParserTreeNode destination) {
017: super ("field");
018: this .sortPriority = 0;
019:
020: this .modifiers = CCTreeUtils.getAllModifiers(modNode);
021: this .modifiersShort = Utils
022: .getModifiersShortString(this .modifiers);
023:
024: if (modNode.getChildCount() > 0) {
025: this .setStartPosFrom(CCTreeUtils.getFirstSubchild(modNode));
026: } else {
027: this .setStartPosFrom(CCTreeUtils
028: .getFirstSubchild(fieldDeclarationNode));
029: }
030: this .setEndPosFrom(CCTreeUtils
031: .getLastSubchild(fieldDeclarationNode));
032:
033: // childs: {Type, VariableDeclarator [,VariableDeclarator]* }
034: RAWParserTreeNode tn = fieldDeclarationNode.getChildNodeAt(0);
035: this .type = CCTreeUtils.getImageOfAllSubElements(tn);
036:
037: for (int i = 1; i < fieldDeclarationNode.getChildCount(); i++) // start at 1
038: {
039: RAWParserTreeNode vdn = fieldDeclarationNode
040: .getChildNodeAt(i); // a=1
041: if (vdn.toString().equals("VariableDeclarator")) {
042: RAWParserTreeNode vdidn = vdn.getChildNodeAt(0); // VariableDeclaratorId
043: if (i == 1) {
044: this .name = CCTreeUtils
045: .getImageOfAllSubElements(vdidn);
046: } else {
047: //System.out.println("Additional variable in multiple field declaration found:");
048: String n = CCTreeUtils
049: .getImageOfAllSubElements(vdidn);
050: //System.out.println(" "+n);
051: FieldNode fn = new FieldNode(n, type, modifiers,
052: modifiersShort);
053: Utils.sortedInsert(fn, destination);
054:
055: // set precise start and end
056: fn.setStartPosFrom(CCTreeUtils
057: .getFirstSubchild(vdidn)); // vdidn : without initializer, // vdn: with
058: fn
059: .setEndPosFrom(CCTreeUtils
060: .getLastSubchild(vdidn));
061:
062: }
063: }
064: }
065:
066: // add to the destination
067: Utils.sortedInsert(this , destination);
068: }
069:
070: /** Special case when multiple declaration encountered.
071: overtakes the main modifiers and type.
072: Causes a java.lang.verify error when optimized/shrinked with Proguard4beta4 when private.
073: */
074: public FieldNode(String name, String type, int[] modifiers,
075: String modifiersShort) {
076: super ("field");
077:
078: this .sortPriority = 0;
079: this .name = name;
080: this .type = type;
081: this .modifiers = modifiers;
082: this .modifiersShort = modifiersShort;
083: }
084:
085: @Override
086: public String toString() {
087: return name + " " + type;
088: }
089:
090: public final int[] getModifiers() {
091: return modifiers;
092: }
093:
094: public boolean isStatic() {
095: return Utils.isStatic(modifiers);
096: }
097:
098: public boolean isPublic() {
099: return Utils.isPublic(modifiers);
100: }
101:
102: public boolean isPrivate() {
103: return Utils.isPrivate(modifiers);
104: }
105:
106: public boolean isProtected() {
107: return Utils.isProtected(modifiers);
108: }
109:
110: /** Call this to help GC !
111: */
112: @Override
113: public void terminate() {
114: super.terminate();
115:
116: modifiers = null;
117: modifiersShort = null;
118: name = null;
119: type = null;
120: }
121:
122: }
|