Java反射丢失注释信息

Java反射丢失注释信息,java,class,reflection,multiple-projects,loss,Java,Class,Reflection,Multiple Projects,Loss,我对反思有意见。我决定使用反射创建一个SQL查询生成器。我创建了自己的注释,以确定可以使用哪些类,可以存储哪些属性。 代码按我所希望的方式工作,但是问题在于将此项目用作其他项目的依赖项 我有另一个使用OJDBC的项目,我试图使用我的库根据类生成查询。但是,当我从ojdbc项目传递一个类时,会丢失所有的类信息,该类显示为java.lang.class,甚至注释信息也会丢失。 有人知道为什么会这样吗 private static <T> void appendTableName(Clas

我对反思有意见。我决定使用反射创建一个SQL查询生成器。我创建了自己的注释,以确定可以使用哪些类,可以存储哪些属性。 代码按我所希望的方式工作,但是问题在于将此项目用作其他项目的依赖项

我有另一个使用OJDBC的项目,我试图使用我的库根据类生成查询。但是,当我从ojdbc项目传递一个类时,会丢失所有的类信息,该类显示为java.lang.class,甚至注释信息也会丢失。 有人知道为什么会这样吗

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());
}
animal类位于ojdbc项目中,appendTableName方法属于我的查询生成器。我曾尝试将querybuilder项目生成到jar中,并使用maven安装将其添加到我的存储库中,但仍然没有成功

感谢您的快速回复,不过这并不是问题,因为我创建的注释已将保留设置为运行时,请参见下文

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 "";
}

“我的批注”已设置为“运行时”,但类信息仍在丢失。

如果希望批注在运行时可用,则需要向其添加批注

@Retention(RetentionPolicy.RUNTIME)
public @interface Storable {
否则,注释在运行时不可见

有关详细信息,请参见此注释的源代码

/**
 * 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();
}
/**
*指示带注释类型的注释将保留多长时间
*保留。如果上没有保留批注
*作为注释类型声明,保留策略默认为
*{@code RetentionPolicy.CLASS}。
*
*保留元注释只有在
*元注释类型直接用于注释。它没有
*如果元注释类型用作
*另一种注释类型。
*
*@作者约书亚·布洛赫
*@自1.5
*@jls 9.6.3.2@Retention
*/
@记录
@保留(RetentionPolicy.RUNTIME)
@目标(ElementType.ANNOTATION\U类型)
公共@接口保留{
/**
*返回保留策略。
*@返回保留策略
*/
保留策略值();
}

您发布的代码应该有效。也就是说,原因在别处。您是否绝对确定要传递的类对象对应于具有此注释的类?(例如,不是某个运行时生成的子类吗?)?(您运行的类的旧版本还没有注释,是吗?)不,它运行的是正确版本的注释,目前我没有子类型。该类不会抛出编译错误并正确引用注释。注释打包在一个jar中,该jar通过maven依赖项添加到项目中。。。。您确定注释类在运行时类路径上可用吗?(您可以通过检查
Class.forName(“com.whatever.Storable”)
是否引发异常来验证这一点)我不完全确定我出了什么问题,因为我没有对代码进行任何更改,现在它似乎运行良好。谢谢你的意见。
/**
 * 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();
}