How can I find a TypeDeclaration
in an IProject
with the JDT search engine?
I want to use the package and the name (e.g. "my.package.ClassName"
) of a class to find its TypeDeclaration
in a specific IProject
.
I know I could use the Java AST and a AST visitor which compares the current package and the node name, but I would prefer to use a string search.
If you already hold the qualified name of the class and have a project, there's no need to employ the search engine.
Step 1: get an instance of IJavaProject
representing your project:
IJavaProject jProj = JavaCore.create(proj);
Step 2: find the IType
representing your class:
IType type = jProj.findType(fullyQualifiedname);
Step 3: get the containing compilation unit:
ICompilationUnit cu = type.getCompilationUnit();
Step 4: pass this to the ASTParser
that will create your TypeDeclaration
(as part of a CompilationUnit
):
astParser.setSource(cu);
(This assumes you already have an ASTParser
, know how to invoke parsing and inspect the resulting AST).