dimanche 4 mars 2018

Why reflection mtehod getDeclaredAnnotations() doesn't work?

There is a class:

import com.github.reinert.jjschema.Attributes;

@Attributes(title = "root", additionalProperties = false)
public class SampleDto {
    @Attributes(required = true, title = "stringParam")
    private String stringParam;

    public String getStringParam() {
        return stringParam;
    }

    public void setStringParam(String stringParam) {
        this.stringParam = stringParam;
    }
}

@Attributes - is an annotation from jjschema library, which puprose is json schema generation from POJO. How it looks:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.TYPE})
public @interface Attributes {
    String $ref() default "";
...

...and other fileds below.

So if I run the following code I face the problem that getDeclaredAnnotations() and getDeclaredAnnotationsByType() methods don't work

String classPath = getClasspathFromPath(classFile); //path to SampleDto
Class cls = classLoader.loadClass(classPath); //class have been loaded
Field[] fields = cls.getDeclaredFields(); //length = 1 - ok
Method[] methods = cls.getDeclaredMethods(); //length = 2 - ok
Annotation[] annotations = cls.getDeclaredAnnotations(); //length = 0
Attributes[] attributes = (Attributes[])targetClass.
                          getDeclaredAnnotationsByType(Attributes.class); //length = 0

Other annotation-related methods not work too. What is wrong? Annotation has a RUNTIME retention - so it should be possible to get it at runtime.





Aucun commentaire:

Enregistrer un commentaire