001: package javaparser;
002:
003: import java.util.*;
004: import javaparser.javacc_gen.*;
005:
006: /** The parameter of a method or constructor of a for loop
007: * or a field in a class.
008: */
009: public class Parameter {
010: public final String type; // ex: "double" or double[] or "Vector<String>"
011: public final String name; // ex: "a"
012:
013: // declaration position (start)
014: public int line, col;
015:
016: // public Token validFrom = null; //=> just look in the method or class or constructor
017: // public Token validTo = null;
018:
019: public boolean isParameterAfterLine(int _line) {
020: if (line > _line)
021: return true;
022: return false;
023: }
024:
025: public Parameter(String type, String name) {
026: this .type = type;
027: this .name = name;
028: }
029:
030: /** Occurs in methods and constructors
031: parsed during simplified syntax tree construction
032: */
033: public static Parameter parseFromFormalParamNode(
034: RAWParserTreeNode formalParameterNode) {
035: String type = CCTreeUtils.getImageOfAllSubElements(CCTreeUtils
036: .getFirstSubchildNamed_ONLYInDirectChilds(
037: formalParameterNode, "Type"));
038: RAWParserTreeNode vidNode = CCTreeUtils
039: .getFirstSubchildNamed_ONLYInDirectChilds(
040: formalParameterNode, "VariableDeclaratorId");
041: Token t = CCTreeUtils.getFirstSubchild(vidNode);
042:
043: String name = CCTreeUtils.getImageOfAllSubElements(vidNode);
044: // [April2007]: varargs
045: if (CCTreeUtils.getFirstSubchildNamed_ONLYInDirectChilds(
046: formalParameterNode, "...") != null) {
047: type += "[]"; // really an array for the usage...
048: }
049:
050: Parameter p = new Parameter(type, name);
051:
052: p.line = t.beginLine;
053: p.col = t.beginColumn;
054:
055: return p;
056: }
057:
058: public static Parameter parseFromLocalVariableDeclaration(
059: RAWParserTreeNode localVariableDeclarationNode) {
060: String type = CCTreeUtils.getImageOfAllSubElements(CCTreeUtils
061: .getFirstSubchildNamed_ONLYInDirectChilds(
062: localVariableDeclarationNode, "Type"));
063: RAWParserTreeNode vidNode = CCTreeUtils
064: .getFirstSubchildNamed_ONLYInDirectChilds(
065: localVariableDeclarationNode,
066: "VariableDeclarator"); // was Id
067: vidNode = CCTreeUtils.getFirstSubchildNamed_ONLYInDirectChilds(
068: vidNode, "VariableDeclaratorId");
069:
070: Token t = CCTreeUtils.getFirstSubchild(vidNode);
071:
072: String name = CCTreeUtils.getImageOfAllSubElements(vidNode);
073: Parameter p = new Parameter(type, name);
074:
075: p.line = t.beginLine;
076: p.col = t.beginColumn;
077:
078: return p;
079:
080: }
081:
082: /** occurs in field declarations
083: */
084: public static Parameter parseFromFieldDeclaration(
085: RAWParserTreeNode fieldDeclarationNode) {
086: String type = CCTreeUtils.getImageOfAllSubElements(CCTreeUtils
087: .getFirstSubchildNamed_ONLYInDirectChilds(
088: fieldDeclarationNode, "Type"));
089: RAWParserTreeNode vidNode = CCTreeUtils
090: .getFirstSubchildNamed_ONLYInDirectChilds(
091: fieldDeclarationNode, "VariableDeclarator"); // was Id
092: vidNode = CCTreeUtils.getFirstSubchildNamed_ONLYInDirectChilds(
093: vidNode, "VariableDeclaratorId");
094:
095: Token t = CCTreeUtils.getFirstSubchild(vidNode);
096:
097: String name = CCTreeUtils.getImageOfAllSubElements(vidNode);
098: Parameter p = new Parameter(type, name);
099:
100: p.line = t.beginLine;
101: p.col = t.beginColumn;
102:
103: return p;
104:
105: }
106:
107: public static Parameter parseFromMethodDeclaration(
108: RAWParserTreeNode methodDeclarationNode) {
109: RAWParserTreeNode rtNode = CCTreeUtils
110: .getFirstSubchildNamed_ONLYInDirectChilds(
111: methodDeclarationNode, "ResultType");
112: if (rtNode == null) {
113: System.out
114: .println("ERROR: No ResultType node for method declaration");
115: return null;
116: }
117: String type = CCTreeUtils.getImageOfAllSubElements(rtNode);
118:
119: RAWParserTreeNode mdNode = CCTreeUtils
120: .getFirstSubchildNamed_ONLYInDirectChilds(
121: methodDeclarationNode, "MethodDeclarator");
122: if (mdNode == null) {
123: System.out
124: .println("ERROR: No MethodDeclarator for method declaration");
125: return null;
126: }
127:
128: String name = "" + mdNode.getChildNodeAt(0);
129: Parameter p = new Parameter(type, name);
130: return p;
131: }
132:
133: /** occurs in field declarations
134: * type 1: for(String s : args) {}
135: * type 2: for(int i=0; i<12; i++) {}
136: * variant: for(; i<12; i++) {} no ForInit
137: */
138: public static Parameter parseFromForStatementDeclaration(
139: RAWParserTreeNode forDeclarationNode) {
140: RAWParserTreeNode finitNode = CCTreeUtils
141: .getFirstSubchildNamed_ONLYInDirectChilds(
142: forDeclarationNode, "ForInit");
143: if (finitNode != null) {
144: // type 2:
145: RAWParserTreeNode lvdNode = CCTreeUtils
146: .getFirstSubchildNamed_ONLYInDirectChilds(
147: finitNode, "LocalVariableDeclaration");
148: if (lvdNode == null) {
149: System.out.println("No param2 in for decl found: "
150: + forDeclarationNode);
151: return null;
152: }
153: return parseFromLocalVariableDeclaration(lvdNode);
154: }
155:
156: // type 1:
157: RAWParserTreeNode typeNode = CCTreeUtils
158: .getFirstSubchildNamed_ONLYInDirectChilds(
159: forDeclarationNode, "Type");
160: if (typeNode == null) {
161: System.out.println("No param1 in for decl found: "
162: + forDeclarationNode);
163: return null;
164: }
165: String type = CCTreeUtils.getImageOfAllSubElements(typeNode);
166:
167: RAWParserTreeNode nameNode = typeNode.getNextSibling();
168:
169: String name = CCTreeUtils.getImageOfAllSubElements(nameNode);
170: Parameter p = new Parameter(type, name);
171:
172: // p.line = typeNode.beginLine;
173: // p.col = typeNode.beginColumn;
174:
175: return p;
176:
177: }
178:
179: /** Collects the paramters.
180: */
181: public static List<Parameter> getParameters(
182: RAWParserTreeNode formalParametersNode) {
183: List<Parameter> params = new ArrayList<Parameter>();
184: for (int i = 0; i < formalParametersNode.getChildCount(); i++) {
185: RAWParserTreeNode ci = formalParametersNode
186: .getChildNodeAt(i);
187: if (ci.toString().equals("FormalParameter")) {
188: params.add(Parameter.parseFromFormalParamNode(ci));
189: }
190: }
191: return params;
192: }
193:
194: /** appears in the symplified syntax tree !
195: */
196: @Override
197: public String toString() {
198: return type + " " + name;
199: }
200:
201: }
|