Bug 211201 - Enum in AST seems to fail
Summary: Enum in AST seems to fail
Status: RESOLVED FIXED
Alias: None
Product: AspectJ
Classification: Tools
Component: Compiler (show other bugs)
Version: 1.5.3   Edit
Hardware: PC Windows Vista
: P3 normal (vote)
Target Milestone: 1.6.0 M2   Edit
Assignee: aspectj inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-11-28 07:07 EST by Wojto CLA
Modified: 2008-02-20 18:24 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Wojto CLA 2007-11-28 07:07:03 EST
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.
Comment 1 Andrew Clement CLA 2008-02-20 18:24:00 EST
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.