001: /*
002: * Author: Chris Seguin
003: *
004: * This software has been developed under the copyleft
005: * rules of the GNU General Public License. Please
006: * consult the GNU General Public License for more
007: * details about use and distribution of this software.
008: */
009: package org.acm.seguin.pretty;
010:
011: import net.sourceforge.jrefactory.ast.ASTBlockStatement;
012: import net.sourceforge.jrefactory.ast.ASTLocalVariableDeclaration;
013: import net.sourceforge.jrefactory.ast.ASTName;
014: import net.sourceforge.jrefactory.ast.ASTPrimitiveType;
015: import net.sourceforge.jrefactory.ast.ASTReferenceType;
016: import net.sourceforge.jrefactory.ast.ASTClassOrInterfaceType;
017: import net.sourceforge.jrefactory.ast.ASTTypeArguments;
018: import net.sourceforge.jrefactory.ast.ASTType;
019: import net.sourceforge.jrefactory.ast.ASTVariableDeclaratorId;
020: import net.sourceforge.jrefactory.ast.ASTAnnotation;
021: import net.sourceforge.jrefactory.ast.SimpleNode;
022:
023: /**
024: * Helps determine the size of a local variable for spacing purposes
025: *
026: *@author Chris Seguin
027: *@author Mike Atkinson
028: */
029: class LocalVariableLookAhead {
030: private FieldSize size;
031:
032: /**
033: * Constructor for the LocalVariableLookAhead object
034: */
035: public LocalVariableLookAhead() {
036: size = new FieldSize();
037: }
038:
039: /**
040: * Main processing method for the LocalVariableLookAhead object
041: *
042: *@param body Description of Parameter
043: *@return Description of the Returned Value
044: */
045: public FieldSize run(SimpleNode body) {
046: int last = body.jjtGetNumChildren();
047: for (int ndx = 0; ndx < last; ndx++) {
048: SimpleNode child = (SimpleNode) body.jjtGetChild(ndx);
049: if ((child instanceof ASTBlockStatement)
050: && (child.jjtGetFirstChild() instanceof ASTLocalVariableDeclaration)) {
051: ASTLocalVariableDeclaration localVariable = (ASTLocalVariableDeclaration) child
052: .jjtGetFirstChild();
053: int equalsLength = computeEqualsLength(localVariable);
054: size.setMinimumEquals(equalsLength);
055: }
056: }
057:
058: return size;
059: }
060:
061: /**
062: * Compute the length of the equals
063: *
064: *@param localVariable the local variable
065: *@return the length
066: */
067: public int computeEqualsLength(
068: ASTLocalVariableDeclaration localVariable) {
069: int modifierLength = computeModifierLength(localVariable);
070: int typeLength = computeTypeLength(localVariable);
071: int nameLength = computeNameLength(localVariable);
072: int equalsLength = modifierLength + typeLength + nameLength;
073: return equalsLength;
074: }
075:
076: /**
077: * Compute the length of the type
078: *
079: *@param localVariable the local variable
080: *@return the type length
081: */
082: public int computeTypeLength(
083: ASTLocalVariableDeclaration localVariable) {
084: int childNo = localVariable.skipAnnotations();
085: ASTType typeNode = (ASTType) localVariable.jjtGetChild(childNo);
086: int typeLength = 0; //2 * typeNode.getArrayCount();
087: if (typeNode.jjtGetFirstChild() instanceof ASTPrimitiveType) {
088: ASTPrimitiveType primitive = (ASTPrimitiveType) typeNode
089: .jjtGetFirstChild();
090: typeLength += primitive.getName().length();
091: } else if (typeNode.jjtGetFirstChild() instanceof ASTReferenceType) {
092: typeLength += computeReferenceTypeLength((ASTReferenceType) typeNode
093: .jjtGetFirstChild());
094: } else {
095: ASTName name = (ASTName) typeNode.jjtGetFirstChild();
096: typeLength += name.getName().length();
097: }
098: size.setTypeLength(typeLength);
099: return typeLength;
100: }
101:
102: /**
103: * Compute the length of the type
104: *
105: *@param reference the reference type
106: *@return the type length
107: *@since JRefactory 2.7.00
108: */
109: public int computeReferenceTypeLength(ASTReferenceType reference) {
110: int typeLength = 0;
111: if (reference.jjtGetFirstChild() instanceof ASTPrimitiveType) {
112: ASTPrimitiveType primitive = (ASTPrimitiveType) reference
113: .jjtGetFirstChild();
114: typeLength += primitive.getName().length();
115: } else if (reference.jjtGetFirstChild() instanceof ASTClassOrInterfaceType) {
116: ASTClassOrInterfaceType name = (ASTClassOrInterfaceType) reference
117: .jjtGetFirstChild();
118: typeLength += name.getName().length();
119: } else {
120: // FIXME: sometimes the child is a CompilationUnit which should not be a child of a ASTReferenceType
121: //System.out.println("LocalVariableLookAhead.computeReferenceTypeLength: Error: reference.jjtGetFirstChild()="+reference.jjtGetFirstChild());
122: }
123: typeLength += reference.getArrayCount() * 2;
124: return typeLength;
125: }
126:
127: /**
128: * Compute the length of the modifier
129: *
130: *@param localVariable the local variable
131: *@return the length of the modifier
132: */
133: private int computeModifierLength(
134: ASTLocalVariableDeclaration localVariable) {
135: int modifierLength = localVariable.isUsingFinal() ? 6 : 0;
136: size.setModifierLength(modifierLength);
137: return modifierLength;
138: }
139:
140: /**
141: * Compute the length of the name
142: *
143: *@param localVariable the local variable
144: *@return the length
145: */
146: private int computeNameLength(
147: ASTLocalVariableDeclaration localVariable) {
148: int childNo = localVariable.skipAnnotations();
149: ASTVariableDeclaratorId id = (ASTVariableDeclaratorId) localVariable
150: .jjtGetChild(childNo + 1).jjtGetFirstChild();
151: int nameLength = id.getName().length();
152: size.setNameLength(nameLength);
153: return nameLength;
154: }
155: }
|