注解
什么是注解
内置注解
元注解
自定义注解
//自定义注解
public class CustomAnnotation {
public static void main(String[] args) {
new CustomAnnotation().test01();
}
@myAnnotation(id = 1)
//当注解属性有默认值时,可以省略也可以自己显示定义
void test01(){
System.out.println(111);
}
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface myAnnotation{
String name() default "";//注解属性有默认值
int id();
double time() default 0;
}
}
反射机制
Java反射机制概述
Class类
package com.reflection;
public class test01 {
//测试Class类的创建方式
public static void main(String[] args) throws ClassNotFoundException {
Person person = new Student();
System.out.println(person.name);
//方式一
Class s1 = person.getClass();
System.out.println(s1.hashCode());
//方式二
Class s2 = Class.forName("com.reflection.Student");
System.out.println(s2.hashCode());
//方式三
Class s3 = Student.class;
System.out.println(s3.hashCode());
//方式四:基本内置类型的包装类的Type属性
Class type = Long.TYPE;
System.out.println(type);
//获取父类类型
Class s1Superclass = s1.getSuperclass();
System.out.println(s1Superclass);
}
}
class Person {
public String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
'}';
}
}
class Student extends Person {
public Student() {
this.name = "学生";
}
}
Java内存分析
//通过反射获取类的所有结构
public class test03 {
public static void main(String[] args) throws NoSuchMethodException {
Person person = new Person();
Class c1 = person.getClass();
//类的名称
String simpleName = c1.getSimpleName();
String name = c1.getName();
System.out.println(simpleName);
System.out.println(name);
System.out.println("================");
//获取类的属性
Field[] field = c1.getFields();//获取Public属性
Field[] declaredFields = c1.getDeclaredFields();//获取全部属性
for (Field declaredField : declaredFields) {
System.out.println(declaredField);
}
//获取类的方法
System.out.println("=============");
Method[] methods = c1.getMethods();//获得本类和父类的所有Public方法
for (Method method : methods) {
System.out.println(method);
}
for (Method declaredMethod : c1.getDeclaredMethods()) { //获得本类的所有方法
System.out.println(declaredMethod);
}
System.out.println("=================");
//获得指定方法
Method method = c1.getMethod("getName");
System.out.println(method);
System.out.println("=================");
//获得指定构造器
Constructor[] constructors = c1.getConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
}
}
性能对比
//性能对比
public class test04 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
//普通调用
Person person = new Person();
long startTime = System.currentTimeMillis();
for (int i = 0; i < 1000000000; i++) {
person.getName();
}
long endTime = System.currentTimeMillis();
System.out.println("普通调用用时"+(endTime-startTime)+"ms");
//反射调用
Class c1 = Class.forName("com.reflection.Person");
Person person1 = (Person) c1.newInstance();
Method getName = c1.getDeclaredMethod("getName");
long startTime1 = System.currentTimeMillis();
for (int i = 0; i < 1000000000; i++) {
getName.invoke(person1,null);
}
long endTime1 = System.currentTimeMillis();
System.out.println("反射调用用时"+(endTime1-startTime1)+"ms");
//关闭检测使用反射调用
Class c2 = Class.forName("com.reflection.Person");
Person person2 = (Person) c2.newInstance();
Method getName1 = c1.getDeclaredMethod("getName");
getName1.setAccessible(true);
long startTime2 = System.currentTimeMillis();
for (int i = 0; i < 1000000000; i++) {
getName1.invoke(person2,null);
}
long endTime2 = System.currentTimeMillis();
System.out.println("关闭检测反射调用用时"+(endTime2-startTime2)+"ms");
}
}
结果:
普通调用用时3ms
反射调用用时2009ms
关闭检测反射调用用时1282ms
获取泛型信息
//反射获取泛型
public class test05 {
public static void main(String[] args) throws NoSuchMethodException {
Method method2 = test05.class.getDeclaredMethod("method2", Map.class, List.class);
Type[] genericParameterTypes = method2.getGenericParameterTypes();//获取泛型参数
for (Type genericParameterType : genericParameterTypes) {
System.out.println(genericParameterType);
if (genericParameterType instanceof ParameterizedType){ //判断是否是参数化类型
Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
for (Type actualTypeArgument : actualTypeArguments) {
System.out.println(actualTypeArgument);
}
}
}
System.out.println("======================");
Method method1 = test05.class.getDeclaredMethod("method1");
Type genericReturnType = method1.getGenericReturnType(); //获取返回值类型
if (genericReturnType instanceof ParameterizedType){
Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
for (Type actualTypeArgument : actualTypeArguments) {
System.out.println(actualTypeArgument);
}
}
}
public TreeMap<String,Integer> method1(){
return null;
}
public void method2(Map<String,Integer> map,List<Double> list){
}
}
反射操作注解
//反射获取注解
public class test06 {
public static void main(String[] args) {
Cat cat = new Cat();
Class<? extends Cat> c1 = cat.getClass();
//获取类的注解
for (Annotation annotation : c1.getAnnotations()) {
System.out.println(annotation);
}
//获取value的值
myAnnotation1 annotation = c1.getAnnotation(myAnnotation1.class);
System.out.println(annotation);
String value = annotation.value();
System.out.println(value);
//获取字段的注解
try {
Field id = c1.getDeclaredField("id");
myAnnotation2 annotation1 = id.getAnnotation(myAnnotation2.class);
String columname = annotation1.columname();
String type = annotation1.type();
int length = annotation1.length();
System.out.println(columname);
System.out.println(type);
System.out.println(length);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
}
@myAnnotation1("Cat") //注解表示存储在数据库Cat表中
class Cat{
@myAnnotation2(columname = "id",type = "int",length = 10)
private int id;
@myAnnotation2(columname = "姓名",type = "varchar",length = 5)
private String name;
@myAnnotation2(columname = "年龄",type = "int",length = 10)
private int age;
public Cat(){
}
public Cat(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Cat{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
//类的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface myAnnotation1{
String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface myAnnotation2{
String columname();
String type();
int length();
}