Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

289
Vistas
How to get annotation parameter in annotation processor

I'm writing my own annotation processor and I'm trying to get parameter of my annotation like in the code below in process method:

roundEnv.getElementsAnnotatedWith(annotation).forEach {
        val annotation = it.getAnnotation(annotation)
        annotation.interfaces
}

What I get is An exception occurred: javax.lang.model.type.MirroredTypesException: Attempt to access Class objects for TypeMirrors [] during build. Anyone know how to get annotation data?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

The documentation on the getAnnotation method explains why Class<?> objects are problematic for an annotation processor:

The annotation returned by this method could contain an element whose value is of type Class. This value cannot be returned directly: information necessary to locate and load a class (such as the class loader to use) is not available, and the class might not be loadable at all. Attempting to read a Class object by invoking the relevant method on the returned annotation will result in a MirroredTypeException, from which the corresponding TypeMirror may be extracted. Similarly, attempting to read a Class[]-valued element will result in a MirroredTypesException.

To access annotation elements like classes you need to instead use Element.getAnnotationMirrors() and manually find the annotation of interest. These annotation mirrors will contain elements representing the actual values but without requiring the presence of the classes in question.

over 4 years ago · Santiago Trujillo Denunciar

0

This blog post seems to be the canonical source for how to do this. It references a Sun forum discussion and is referenced in a number of annotation processors.

For the following annotation:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Action {
    Class<?> value();
}

The field of type Class<?> can be accessed through this code:

for (ExecutableElement ee : ElementFilter.methodsIn(te.getEnclosedElements())) {
    Action action = ee.getAnnotation(Action.class);
    if (action == null) {
        // Look for the overridden method
        ExecutableElement oe = getExecutableElement(te, ee.getSimpleName());
        if (oe != null) {
            action = oe.getAnnotation(Action.class);
        }
    }

    TypeMirror value = null;
    if (action != null) {
        try {
            action.value();
        } catch (MirroredTypeException mte) {
            value = mte.getTypeMirror();
        }
    }

    System.out.printf(“ % s Action value = %s\n”,ee.getSimpleName(), value);
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda