mercredi 16 septembre 2015

Getting class information from ElementHandle

I wanted to create a simple template wizard for NetBeans that would take an existing Java class from current user project and create a new Java class from it. To do so, I need to access field and annotation data from the selected class.

Now, I used the org.netbeans.api.java.source.ui.TypeElementFinder for finding and selecting the wanted class, but as a result I get an ElementHandle and I don't know what to do with it. How do I get class info from this?

I managed to get a TypeMirror (com.sun.tools.javac.code.Type$ClassType) using this code snippet:

Project project = ...; // from Templates.getProject(WizardDescriptor);
ClasspathInfo ci = ClasspathInfoFactory.infoFor(project, true, true, true);
final ElementHandle<TypeElement> element = TypeElementFinder.find(ci);
FileObject fo = SourceUtils.getFile(element, ci);
JavaSource.forFileObject(fo).runUserActionTask(new Task<CompilationController>() {
    @Override
    public void run(CompilationController p) throws Exception {
        p.toPhase(JavaSource.Phase.RESOLVED);
        TypeElement typeElement = element.resolve(p);
        TypeMirror typeMirror = typeElement.asType();
    }
}, true);

But what to do form here? Am I going around this the wrong way altogether?





Aucun commentaire:

Enregistrer un commentaire