ASTParser ignore embedded inner class

When using Eclipse ASTParser, if you want to get all methods and fields of the outer class and ignore the embedded inner class(es), you may try to visit all methods and fields and try to differentiate outer class and inter class.

Each class is a TypeDeclaration in AST, and Inner class is another TypeDeclaration inside the TypeDeclaration in the AST. The key question is: how to know if stuff is in a root TypeDeclaration or a inner TypeDeclaration?

Actually, we do not need to traverse all method and field node, we can only visit the outermost class which is a TypeDeclaration type and return false to disable traversing further down; then get the children we need.

Here is the code to do that.

final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
 
cu.accept(new ASTVisitor() {
 
	public boolean visit(TypeDeclaration typeNote) {
		System.out.println("------------------parent-----------");
		System.out.println(typeNote.superInterfaceTypes());
		System.out.println(typeNote.getSuperclassType().getClass());
		List list = typeNote.superInterfaceTypes();
		for(Object o: list){
			System.out.println(((Type) o).resolveBinding());
		}
 
		System.out.println("------------------methods-----------");
		MethodDeclaration[] notes = typeNote.getMethods();
		for(MethodDeclaration note: notes){					
			if(note.getModifiers() > 0){
				System.out.println("modifier: "+note.getModifiers()); // 9 = public static,  1=public, 
				System.out.println("name: "+note.getName());
				System.out.println("parameter: " + note.parameters());
				System.out.println("return type: "+note.getReturnType2());
 
				System.out.println("-------");
			}										
		}
 
		System.out.println("------------------fields-----------");
		FieldDeclaration[] nodes = typeNote.getFields();
		for(FieldDeclaration node: nodes){										
			System.out.println(node.getModifiers());//25 = public static final 
			System.out.println(node.fragments());
 
			for(Object f: node.fragments()){
				System.out.println(f);
 
				String s = f.toString();
				if(s.contains("=")){
					String[] arr = s.split("=");
					s = arr[0];
				}else{
 
				}
			}
		}				
		return false;
	}				
});

I assume you know how to use ASTParser to parse a java file, if you don’t, you can read the “How to parse a java file by using JDT ASTParser” tutorial.

Here is the complete code.

package ASTParser;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
 
public class TestParsing {
 
	//use ASTParse to parse string
	public static void parse(char[] str) {
		ASTParser parser = ASTParser.newParser(AST.JLS3);
		parser.setSource(str);
		parser.setKind(ASTParser.K_COMPILATION_UNIT);
 
		final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
 
		cu.accept(new ASTVisitor() {
 
			public boolean visit(TypeDeclaration typeNote) {
				System.out.println("------------------parent-----------");
				System.out.println(typeNote.superInterfaceTypes());
				System.out.println(typeNote.getSuperclassType().getClass());
				List list = typeNote.superInterfaceTypes();
				for(Object o: list){
					System.out.println(((Type) o).resolveBinding());
				}
 
				System.out.println("------------------methods-----------");
				MethodDeclaration[] notes = typeNote.getMethods();
				for(MethodDeclaration note: notes){
 
					if(note.getModifiers() > 0){
						System.out.println("modifier: "+note.getModifiers()); // 9 = public static,  1=public, 
						System.out.println("name: "+note.getName());
						System.out.println("parameter: " + note.parameters());
						System.out.println("return type: "+note.getReturnType2());
 
						System.out.println("-------");
					}
 
 
				}
 
				System.out.println("------------------fields-----------");
				FieldDeclaration[] nodes = typeNote.getFields();
				for(FieldDeclaration node: nodes){
 
 
					System.out.println(node.getModifiers());//25 = public static final 
					System.out.println(node.fragments());
 
					for(Object f: node.fragments()){
						System.out.println(f);
 
						String s = f.toString();
						if(s.contains("=")){
							String[] arr = s.split("=");
							s = arr[0];
						}else{
 
						}
					}
				}
 
				return false;
			}	
 
		});
 
	}
 
	//read file content into a string
	public static char[] ReadFileToCharArray(String filePath) throws IOException {
		StringBuilder fileData = new StringBuilder(1000);
		BufferedReader reader = new BufferedReader(new FileReader(filePath));
 
		char[] buf = new char[10];
		int numRead = 0;
		while ((numRead = reader.read(buf)) != -1) {
			//System.out.println(numRead);
			String readData = String.valueOf(buf, 0, numRead);
			fileData.append(readData);
			buf = new char[1024];
		}
 
		reader.close();
 
		return  fileData.toString().toCharArray();	
	}
 
	//loop directory to get file list
	public static void ParseFilesInDir() throws IOException{
 
		parse(ReadFileToCharArray("/home/workspace/UltimateTest/src/ASTParser/StyleSheet.java"));		
	}
 
	public static void main(String[] args) throws IOException {
		ParseFilesInDir();
	}
}
Category >> Java Development Tools (JDT)  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>