Java反射丢失注解信息

标签 java class reflection multiple-projects loss

我遇到了反射问题。我决定要使用反射创建一个 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());
}

cls.getAnnotation(Storable.class) 在将以下类传递给它时丢失信息

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

}

动物类在 ojdbc 项目中,appendTableName 方法属于我的查询构建器。我尝试将 querybuilder 项目生成到 jar 中并使用 maven install 将其添加到我的存储库中,但仍然没有成功。

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

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();
}

关于Java反射丢失注解信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30331140/

相关文章:

java - 使用 Map 填充 Java 对象

java - 如何通过类引用访问静态方法

java - Switch语句: Random generator

java - 用同一索引中的不同字符替换特定索引字符

python - 将分数存储为 Rational Python

c++ - 类的 vector 不存储单独的纹理

c# - 为什么 GetMember(string) 返回 MemberInfo 数组?

java - 重力底部,文字不向上滚动

java - 更改 Android Studio 中的默认 LAUNCHER Activity

oop - 在 MATLAB 中根据名称实例化类