The apparent loss of reflection from the annotation

I have a problem with thinking. I decided to create a SQL Query Generator using reflection. I created my own annotations to determine which classes can be used, which attributes can be stored, etc. The code works the way I want, but the problem is using this project as a dependency in others.

I have another project using OJDBC and I'm trying to use my library to create queries based on a class. However, when I pass a class from my ojdbc project, all the class information is lost, the class appears as java.lang.Class, and even annotation information is lost. Does anyone know why this is happening?

private static <T> void appendTableName(Class<T> cls) throws NotStorableException { Storable storable = cls.getAnnotation(Storable.class); String tableName = null; if (storable != null) { if ((tableName = storable.tableName()).isEmpty()) tableName = cls.getSimpleName(); } else { throw new NotStorableException( "The class you are trying to persist must declare the Storable annotaion"); } createStatement.append(tableName.toUpperCase()); } 

cls.getAnnotation(Storable.class) loses information when the next class is passed to it

 package com.fdm.ojdbc; import com.fdm.QueryBuilder.annotations.PrimaryKey; import com.fdm.QueryBuilder.annotations.Storable; @Storable(tableName="ANIMALS") public class Animal { @PrimaryKey private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } 

The animal class is in the ojdbc project, and the appendTableName method belongs to my query builder. I tried to create a querybuilder project in the bank and use maven install to add it to my repository and still have no luck.

Thanks for the quick response, however this is not a problem since the generated annotation was saved in Runtime below.

 package com.fdm.QueryBuilder.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(value = { ElementType.TYPE }) @Retention(value = RetentionPolicy.RUNTIME) public @interface Storable { public String tableName() default ""; } 

My annotation is indicated at runtime, but class information is still lost.

+5
source share
1 answer

If you want to use an annotation that can be used at run time, you need to add an annotation to it.

 @Retention(RetentionPolicy.RUNTIME) public @interface Storable { 

Without this, the annotation is not displayed at run time.

For more information, here is the source of this annotation.

 /** * Indicates how long annotations with the annotated type are to * be retained. If no Retention annotation is present on * an annotation type declaration, the retention policy defaults to * {@code RetentionPolicy.CLASS}. * * <p>A Retention meta-annotation has effect only if the * meta-annotated type is used directly for annotation. It has no * effect if the meta-annotated type is used as a member type in * another annotation type. * * @author Joshua Bloch * @since 1.5 * @jls 9.6.3.2 @Retention */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention { /** * Returns the retention policy. * @return the retention policy */ RetentionPolicy value(); } 
+5
source

All Articles