Can't use existing annotation processor
See original GitHub issueI’m trying to use annotation processors provided by the NetBeans platform. They are supposed to read annotations to register components automatically in an XML file. For example:
import org.netbeans.api.editor.mimelookup {
mimeRegistration
}
mimeRegistration {
mimeType = "text/x-java";
service = `class Test`;
}
class Test() { }
The first thing I noticed is that although the processor is called, it does nothing:
TypeElement mimeRegistration = processingEnv.getElementUtils().getTypeElement("org.netbeans.api.editor.mimelookup.MimeRegistration");
for (Element el : roundEnv.getElementsAnnotatedWith(mimeRegistration)) {
for (AnnotationMirror am : el.getAnnotationMirrors()) {
if (!mimeRegistration.equals(am.getAnnotationType().asElement())) {
continue;
}
process(el, am);
}
}
mimeRegistration.equals(am.getAnnotationType().asElement())
is always false
, because both compared objects are instances of com.redhat.ceylon.langtools.tools.javac.processing.wrappers.TypeElementFacade
that doesn’t define a proper equals
method. I fixed this problem by adding this code to com.redhat.ceylon.langtools.tools.javac.processing.wrappers.ElementFacade
:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o instanceof ElementFacade) {
ElementFacade that = (ElementFacade) o;
return f.equals(that.f);
}
return false;
}
@Override
public int hashCode() {
return f != null ? f.hashCode() : super.hashCode();
}
(I didn’t push the modification because I’m not 100% sure it is correct, but at least it allowed me to go further in my debugging.)
Now that the annotation is correctly recognized by the processor, I get another error:
java.lang.ClassCastException: com.redhat.ceylon.langtools.tools.javac.code.Type$ClassType cannot be cast to javax.lang.model.type.TypeMirror
at org.netbeans.modules.editor.mimelookup.CreateRegistrationProcessor.process(CreateRegistrationProcessor.java:131)
at org.netbeans.modules.editor.mimelookup.CreateRegistrationProcessor.handleProcess(CreateRegistrationProcessor.java:87)
at org.openide.filesystems.annotations.LayerGeneratingProcessor.process(LayerGeneratingProcessor.java:122)
at com.redhat.ceylon.langtools.tools.javac.processing.wrappers.ProcessorWrapper.process(ProcessorWrapper.java:46)
Type$ClassType
is a com.redhat.ceylon.javax.lang.model.type.TypeMirror
instead of a javax.lang.model.type.TypeMirror
. I’m wondering if this is a limitation of our processor wrapper, or if it’s something that can be worked around.
Here’s my test project:
Due to https://github.com/ceylon/ceylon-ide-eclipse/issues/1828, you’ll have to download the sources for the annotation processor manually here, and put them in your .m2
repository, then reopen the project in Eclipse. The processor is named org.netbeans.modules.editor.mimelookup.CreateRegistrationProcessor.class
.
Issue Analytics
- State:
- Created 7 years ago
- Comments:74 (74 by maintainers)
Top GitHub Comments
OK I got it to work. Now I need to clean up the changes in the
.car
generation.Done.