mardi 19 mai 2015

Java reflection loss of annotation information

I'm having an issue with reflection. I decided I wanted to create a SQL Query Generator using reflection. I created my own annotations to determine what classes can be used, what attributes can be stored ect. The code works as I want it to, however the issue lyes in using this project as a dependency in others.

I have another project using OJDBC and im trying to use my library to generate the Queries based on class. However when I pass a class from my ojdbc project there is a loss of all class information, the class is appearing as java.lang.Class and even the annotaion 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());
}

The cls.getAnnotation(Storable.class) is losing the information when the following 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 have tried generating the querybuilder project into a jar and use maven install to add it to my repository and still no luck.

Thanks in advance





Aucun commentaire:

Enregistrer un commentaire