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

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

问题描述

我当前的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;

....

我的注释界面:

pu

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天全站免登陆