如何传递/访问注解信息? [英] How to pass / access annotation information?

查看:19
本文介绍了如何传递/访问注解信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试应用注释的当前 Book 类的示例:

Example of my current Book class which I am trying to apply annotations to:

@DatabaseTable(tableName = "books")
public class Book {

    @DatabaseField(generatedId = true)
    private int mId;

    @DatabaseField(columnName = "book_name", canBeNull = false)
    private String mBookName;

    @DatabaseField(canBeNull = false)
    private String mAuthorName;

....

我的注解接口:

blic @interface DatabaseTable {
    String tableName() default "";
}

public @interface DatabaseField {
    String columnName() default "";
    boolean generatedId() default false;
    boolean canBeNull() default true;
}

然后在我的 MainActivity 中尝试:

And then in my MainActivity I tried:

public class MainActivity extends AppCompatActivity {
    private final String LOG_TAG = getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Book book = new Book("The Book of Kittens", "Mr. Smith");

        outputInfoAboutClass((Class<? extends DatabaseTable>) book.getClass());
    }

    private void outputInfoAboutClass(Class<? extends DatabaseTable> aClass) {
        Log.i(LOG_TAG, "Class simple name: " + aClass.getSimpleName());

        for (Field field : aClass.getFields()) {
            Log.i(LOG_TAG, "Field name: " + field.getName());
        }

        for (Annotation annotation : aClass.getAnnotations()) {
            Log.i(LOG_TAG, "Annotation name: " + annotation.toString());
        }

        Log.i(LOG_TAG, "End of logging!");
    }
}

我想做的是创建一个通用方法,它接受一个类并让它能够发现底层注释及其值/属性,但我认为我的做法完全错误.例如,我将如何参加这个课程(可能是一本书,也可能是其他东西),并能够确定这是 tableName,这些是我们定义为 DatabaseFields 的成员变量,这些是我们设置的设置对于那些字段,"等

What I am trying to do is make a general method that accepts a class and have it be able to uncover the underlying annotations and their values/attributes, but I think I am going about it all wrong. For example how would I take in this class (might be a Book, might be something else), and be able to determine "This is the tableName, these are the member variables we've defined as DatabaseFields, these are the settings we set for those fields," etc.

推荐答案

Oracle 在这方面有一些很好的文档:https://docs.oracle.com/javase/tutorial/java/annotations/http://www.oracle.com/technetwork/articles/hunter-meta-2-098036.html

Oracle has some good documentation on this: https://docs.oracle.com/javase/tutorial/java/annotations/ http://www.oracle.com/technetwork/articles/hunter-meta-2-098036.html

题目复杂,步骤较多,不适合在此快速总结.您使用 @interface TypeName 声明一种特殊类型的接口并填充一些逻辑,通常使用反射.

The topic is complex and there are a lot of steps to it, so it is not suitable for a quick summary here. You declare a special kind of interface using @interface TypeName and fill in some logic, typically using reflection.

在向客户发布代码之前,您应该彻底研究并实践这一点.

It is something you should study thoroughly and practice before releasing code to clients.

这篇关于如何传递/访问注解信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆