question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't use existing annotation processor

See original GitHub issue

I’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:

nbtest.zip

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:closed
  • Created 7 years ago
  • Comments:74 (74 by maintainers)

github_iconTop GitHub Comments

1reaction
FroMagecommented, Oct 17, 2016

OK I got it to work. Now I need to clean up the changes in the .car generation.

0reactions
FroMagecommented, Oct 20, 2016

Done.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Cannot load resources in Annotation Processor (Not on ...
Now I have only the resources within the jar where the processor resides available. I did try to define -classpath and/or -sourcepath via...
Read more >
Configure annotation processors | IntelliJ IDEA Documentation
Press Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment | Compiler | Annotation Processors. · On the Annotation ...
Read more >
Builder not available to other Annotation Processors #1538
I use Builder annotation in Target model. but Mapstruct annotation processor cannot find accessor of target model. It cannot detect Builder class in...
Read more >
Annotation Processing 101 - Hannes Dorfmann
First, I am going to explain to you what annotation processing is, what you can do with that powerful tool and finally what...
Read more >
All About Annotations and Annotation Processors - Medium
One cool thing about annotation processors, besident code generation, is that they run in their own JVM. Though you can not access source...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found