Community
Participate
Working Groups
Such visitor: import java.util.ArrayList; import org.aspectj.org.eclipse.jdt.core.dom.ASTVisitor; import org.aspectj.org.eclipse.jdt.core.dom.AnonymousClassDeclaration; import org.aspectj.org.eclipse.jdt.core.dom.CompilationUnit; import org.aspectj.org.eclipse.jdt.core.dom.EnumDeclaration; import org.aspectj.org.eclipse.jdt.core.dom.MethodDeclaration; import pl.wirp.user23.logic.metrics.beans.MetricResult; public class NLOCVisitor extends ASTVisitor { private ArrayList<MetricResult> result = new ArrayList<MetricResult>(); public ArrayList<MetricResult> getResult() { return result; } @Override public boolean visit(CompilationUnit cu) { MetricResult bean = new MetricResult(); bean.setNLOC(countNewlines(source)); result.add(bean); return true; } @Override public boolean visit(MethodDeclaration md) { MetricResult bean = new MetricResult(); bean.setNLOC(countNewlines(source)); result.add(bean); return true; } @Override public boolean visit(EnumDeclaration ed) { System.out.println(ed.toString()); return true; } @Override public boolean visit(AnonymousClassDeclaration acd) { System.out.println(acd); return true; } private int countNewlines(String source) { int result = 0; for (char c : source.toCharArray()) { if (c == '\n') { result++; } } return result; } } isn't invoked on any enum declaration. On classes it works fine. I tested it on such enum class: package metrics; public enum BasicEnum { A = 1, B = 2, C = 3; public int getNumberOfEnumerators(){ return 3; } } All writen in pure Java 1.5 (no aspects). Tool I'm writing is about to count some basic code metrics and I'm using the aspectJ's AST.
You didn't include the code you were using to actually build the Ast, but I vaguely remember it was based on the wiki contents at the eclipse.org AJDT wiki page. The code that actually works fine is: import java.util.*; import org.aspectj.org.eclipse.jdt.core.dom.*; import org.aspectj.org.eclipse.jdt.internal.compiler.impl.*; public class Program { public static void main(String []argv) { ASTParser parser = ASTParser.newParser(AST.JLS3); Map options = new HashMap(); options.put(CompilerOptions.OPTION_Source, "1.5"); parser.setCompilerOptions(options); parser.setSource(argv[0].toCharArray()); CompilationUnit cu2 = (CompilationUnit) parser.createAST(null); AjNaiveASTFlattener visitor = new AjNaiveASTFlattener(); cu2.accept(visitor); System.err.println(visitor.getResult()); } } It must: use AST.JLS3 *and* pass a Source level of 1.5, otherwise the callbacks for the enum type will not occur. Using that code I can run: ast\>java Program "public enum E { A=1,B=2,C=3; }}" public enum E {A, B, C} whereas before (with JLS2 and not specifying the source level) it would print nothing.