Skip to content

Instantly share code, notes, and snippets.

@oguna
Created March 31, 2015 08:53
Show Gist options
  • Select an option

  • Save oguna/b60a2c099095aa2ba7dd to your computer and use it in GitHub Desktop.

Select an option

Save oguna/b60a2c099095aa2ba7dd to your computer and use it in GitHub Desktop.
JDTによるASTの取得(依存パッケージなし版)
package org.eclipse.jdt.core;
import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
import org.eclipse.jdt.internal.compiler.IProblemFactory;
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.parser.Parser;
import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
import org.eclipse.jdt.internal.compiler.batch.CompilationUnit;
public class JdtTest {
public static void main(String[] args) {
IErrorHandlingPolicy policy = new IErrorHandlingPolicy() {
public boolean proceedOnErrors() {
return false;
}
public boolean stopOnFirstError() {
return false;
}
public boolean ignoreAllErrors() {
return false;
}
};
CompilerOptions co = new CompilerOptions();
IProblemFactory pf = new DefaultProblemFactory();
ProblemReporter pr = new ProblemReporter(policy, co, pf);
Parser parser = new Parser(pr, false);
String input = "pathToSource";
String output = null;
CompilationUnit unit = new CompilationUnit(null, input, null, output, false);
CompilationResult result = new CompilationResult(unit, 0, 1, 10);
CompilationUnitDeclaration parsedUnit = parser.parse(unit, result);
for(TypeDeclaration td : parsedUnit.types) {
System.out.println(td.name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment