001: package tide.syntaxtree;
002:
003: import java.util.*;
004: import java.io.File;
005: import japa.parser.JavaParser;
006: import japa.parser.ast.body.*;
007: import japa.parser.ast.stmt.*;
008: import japa.parser.ast.*;
009:
010: public final class ASTUtils {
011: private ASTUtils() {
012: }
013:
014: public static class TypeDecl {
015: String fullName; // "full relative name", for example ASTUtils or ASTUtils.SomeInnerClass
016: int startLine = 0;
017: int endLine = 0; // :-( not precise, missing bloc end "}" info from parser... TODO
018:
019: @Override
020: public final String toString() {
021: StringBuilder sb = new StringBuilder();
022: sb.append(fullName + ":" + startLine + "-" + endLine);
023: return sb.toString();
024: }
025: }
026:
027: public static TypeDecl getDeepestTypeAt(final CompilationUnit cu,
028: int line, int col) {
029: List<TypeDecl> allTypes = getAllDeclaredTypes(cu, false);
030: System.out.println("All types: " + allTypes);
031:
032: // TODO
033: return null;
034: }
035:
036: public static List<TypeDecl> getAllDeclaredTypes(
037: final CompilationUnit cu, boolean includingAnonymous) {
038: final List<TypeDecl> allTypes = new ArrayList<TypeDecl>();
039:
040: // toplevel types
041: for (final TypeDeclaration ti : cu.types) {
042: if (ti.name == null)
043: continue; // empty type ";"
044: //System.out.println("=== toplevel type "+ti.name);
045: collectTypes(allTypes, ti, null);
046: }
047: return allTypes;
048: }
049:
050: // Todo: also collect anonymous types as in
051: // new ActionListener() { public void hello() {} }
052: public static void collectTypes(List<TypeDecl> allTypes,
053: final TypeDeclaration ti, final String enclosingTypeName) {
054: if (ti.name == null)
055: return; // empty type ";"
056:
057: final TypeDecl act = new TypeDecl();
058: act.fullName = (enclosingTypeName != null ? enclosingTypeName
059: + "." : "")
060: + ti.name;
061: act.startLine = act.endLine = ti.getLine();
062: allTypes.add(act);
063:
064: JapaVisitor jv = new JapaVisitor();
065: ti.accept(jv, null);
066:
067: if (ti.members != null) {
068: for (final BodyDeclaration bi : ti.members) {
069: bi.accept(jv, null);
070: act.endLine = Math.max(bi.getLine(), act.endLine);
071:
072: if (bi instanceof AnnotationMemberDeclaration) {
073: } else if (bi instanceof ConstructorDeclaration) {
074: } else if (bi instanceof EmptyMemberDeclaration) {
075: } else if (bi instanceof EnumConstantDeclaration) {
076: } else if (bi instanceof FieldDeclaration) {
077:
078: } else if (bi instanceof InitializerDeclaration) {
079: } else if (bi instanceof MethodDeclaration) {
080: MethodDeclaration md = (MethodDeclaration) bi;
081: //System.out.println("Method "+md.name);
082: md.accept(jv, null);
083: if (md.block != null && md.block.stmts != null) {
084: for (Statement sti : md.block.stmts) {
085: if (sti instanceof ExpressionStmt) {
086: ExpressionStmt est = (ExpressionStmt) sti;
087: //System.out.println(""+ est.expr);
088: //est.expr.
089: //System.out.println("Expr of type "+est.expr.getClass());
090: } else {
091: // foreach, ...
092: //System.out.println("Statement of type "+sti.getClass());
093: }
094: }
095: }
096:
097: } else if (bi instanceof TypeDeclaration) {
098: // recurse !
099: TypeDeclaration td = (TypeDeclaration) bi;
100: collectTypes(allTypes, td, act.fullName);
101: //System.out.println(" enclosed type: "+td.name);
102: } else {
103: System.out.println("Unknown " + bi.getClass());
104: }
105: }
106: }
107: }
108:
109: public static void main(final String[] args) throws Exception {
110:
111: final CompilationUnit cun = JavaParser.parse(new File(
112: "c:/proj/tide/src/japa/dev/Test.java"));
113: getDeepestTypeAt(cun, 0, 0);
114: //cun.accept(new JapaStructVisitor(), null);
115:
116: }
117:
118: }
119:
120: class SomeStupidInnerClass {
121: class AnotherStupidOne {
122: class AntLastButNotLeast {
123: }
124: }
125: }
126: /*
127: class ActionsForNode
128: {
129: }*/
|