Java注解和Java反射机制

我的基本类:

package com.example.a51044.zhujie.bean;

public class MyData {
    private String name;
    private String sex;

    public void setData(String name,String sex){
        this.name = name;
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "MyData{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

package com.example.a51044.zhujie.bean;

import android.util.Log;

public class Student {
    private String name;
    private String sex;
    private int age;
    private String lick = "lvxx 帅";

    public Student(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public Student() {

    }

    private Student(String name) {
        this.name = name;
    }


    public void getInfo(String name) {
        this.name = name;
    }

    private void getInfo(String name, String sex,int age) {
        Log.e("name和sex", name + ",sex=" + sex+",age="+age);

    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", lick='" + lick + '\'' +
                '}';
    }
}

我定义的注解:

package com.example.a51044.zhujie.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//运行时的执行
@Retention(RetentionPolicy.RUNTIME)
//给属性的注解
@Target(ElementType.FIELD)
public @interface AutoObject {
}

package com.example.a51044.zhujie.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TestRetention {
    String value();
}

我的工具类:

package com.example.a51044.zhujie.utils;

import com.example.a51044.zhujie.annotation.AutoObject;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

public class AnnotationUtils {

    public static void bind(Object object)
    {
        //得到字节码文件
        Class  aClass = object.getClass();
        //得到所有的成员变量
        Field[] fields = aClass.getDeclaredFields();

        for(Field f:fields)
        {
            //判断这个变量,是不是这个注解里的(就是f)
            AutoObject annotation = f.getAnnotation(AutoObject.class);
            if(annotation!=null)
            {
                f.setAccessible(true);
                //找到这个变量  因为反射机制提供了一个方法让你通过成员变量就可以获取到一个字节码对象
                Class type = f.getType();
                try {
                    Constructor constructor = type.getConstructor();
                    f.set(object,constructor.newInstance());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

用注解赋值反射获取值:

package com.example.a51044.zhujie.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.example.a51044.zhujie.annotation.AutoObject;
import com.example.a51044.zhujie.annotation.TestRetention;
import com.example.a51044.zhujie.bean.MyData;
import com.example.a51044.zhujie.bean.Student;
import com.example.a51044.zhujie.utils.AnnotationUtils;

public class AnnotationActivity extends AppCompatActivity {
    @TestRetention("李小强")
    String mStr;
    @AutoObject
    Student mstu;
    @AutoObject
    MyData data;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //在上方已经给mStr赋过值了,接下来就是要得到里面的值
        AnnotationUtils.bind(this);
        mstu.getInfo("小强");
        Toast.makeText(this, mstu.toString(), Toast.LENGTH_SHORT).show();
        data.setData("lxq","帅炸");
        Toast.makeText(this
                , data.toString(), Toast.LENGTH_SHORT).show();
    }


}

猜你喜欢

转载自blog.csdn.net/weixin_43747497/article/details/85253765