javaee / jaxb2-commons

JAXB Implementation project has been contributed to Eclipse Foundation. This repository is for legacy review only. Please refer to the Eclipse EE4J Metro project for the very latest
https://eclipse-ee4j.github.io/jaxb-ri/
Other
17 stars 15 forks source link

Annotation plugin does not include the project's classpath when annotating generated code #32

Closed glassfishrobot closed 8 years ago

glassfishrobot commented 12 years ago

Given the following annotation (defined in the same project along with ExistingCustomerValidator class )

package com.tktserver.constraints;

@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = { ExistingCustomerValidator.class })
@Documented
public @interface ExistingCustomerMatch {
    String message() default "{customer.notfound}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    /**
     * @return The field
     */
    String field();
}

and the following jxb customisation

<jaxb:bindings node="xsd:complexType[@name='customer']">
    <annox:annotate>
        <annox:annotate
            annox:class="com.tktserver.constraints.ExistingCustomerMatch"
            field="electronicUserId" />
    </annox:annotate>
</jaxb:bindings>

I get this when I generate my sources ( it's a Maven project)

Caused by: org.jvnet.annox.annotation.AnnotationClassNotFoundException: Annotation class [com.tktserver.constraints.ExistingCustomerMatch] could not be found.
    ... 32 more
Caused by: java.lang.ClassNotFoundException: com.tktserver.constraints.ExistingCustomerMatch
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at org.jvnet.annox.parser.XAnnotationParser.parse(XAnnotationParser.java:76)
    ... 31 more

Other JSR-303 annotations work fine. I believe that the problem lies in the fact that the xjc plugin does not consider the project's classpath and therefore won't compile for Annotations that have class references in the main project. A workaround is to create a separate maven project and use it as a dependency but this is Ugly.

glassfishrobot commented 12 years ago

Reported by ioannism

glassfishrobot commented 12 years ago

lexi said: Thanks for the report.

glassfishrobot commented 12 years ago

ioannism said: You're welcome, let me know if you need any more info.

glassfishrobot commented 11 years ago

jesseplymale said: I had a similar issue, but the annotations that I was trying to annotate on the JAXB class were not custom annotations that I had defined, but rather just annotations that were from a library (Jackson) that was not part of the Java API. Turns out that Maven was not using the project dependencies (of which Jackson was a part) in the classpath when running XJC via the maven JAXB plugin. When I added a element underneath the for the JAXB maven plugin, and added Jackson element to that, it worked.

Definitely seems like a shortcoming of the Maven JAXB plugin, that it does not put the project dependencies on its classpath during code generation.

glassfishrobot commented 10 years ago

lexi said: http://jira.highsource.org/browse/JIIB-55

glassfishrobot commented 8 years ago

lexi said: Continued in https://github.com/highsource/jaxb2-annotate-plugin/issues/20.

glassfishrobot commented 7 years ago

This issue was imported from java.net JIRA JAXB2_COMMONS-32

glassfishrobot commented 8 years ago

Marked as duplicate on Thursday, December 24th 2015, 1:08:34 am