01: package javaparser;
02:
03: import javaparser.javacc_gen.JavaParserConstants;
04: import javaparser.javacc_gen.Token;
05: import java.util.*;
06:
07: public final class ImportNode extends ParserTreeNode {
08: String importName;
09: boolean isStatic = false;
10:
11: public ImportNode(RAWParserTreeNode importDeclarationNode) {
12: super ("import statement");
13:
14: List<Token> tokens = new ArrayList<Token>();
15: CCTreeUtils.collectChilds(importDeclarationNode, tokens);
16: this .importName = createImportText(tokens);
17: if (importName.startsWith("static ")) {
18: importName = importName.substring(7).trim(); // can be ameliorated...
19: }
20:
21: this .setStartPosFrom(tokens.get(0));
22: this .setEndPosFrom(tokens.get(tokens.size() - 1));
23: }
24:
25: @Override
26: public String toString() {
27: return "" + importName;
28: }
29:
30: /** The name, without static.
31: */
32: public String getImportName() {
33: return importName;
34: }
35:
36: /** @return true for "import static" imports
37: */
38: public boolean isStatic() {
39: return isStatic;
40: }
41:
42: /** "static java.lang.Math.*", "javax.swing.JFrame", ...
43: */
44: private String createImportText(List<Token> tokens) {
45: StringBuilder sb = new StringBuilder(50);
46: for (int i = 1; i < tokens.size() - 1; i++) // ignore the "import" token itself
47: {
48: Token ti = tokens.get(i);
49: sb.append(ti);
50: if (ti.kind == JavaParserConstants.STATIC) {
51: isStatic = true;
52: sb.append(" "); // import static special case
53: }
54: }
55: return sb.toString();
56: }
57:
58: /** Call this to help GC !
59: */
60: @Override
61: public void terminate() {
62: super.terminate();
63:
64: importName = null;
65: }
66:
67: }
|