标签:处理 type getc false odex 实例化 card const ado
Annotation的作用
不是程序本身,可以对程序作出解释
可以被其他程序(比如:编辑器等)读取
Annotation的格式
注解是以@注释名在代码中存在的,还可以添加一些参数值,例如:@SuppressWarnings(value="unchecked")
Annotation在哪里使用
可以附加在package,class,method,field等上面,相当于给它们添加了额外的 辅助信息,我们可以通过反射机制编程实现对这些元数据的访问
@Override:定义在java.lang.Override中,此注解只适用于修辞方法,表示一个方法声明打算重写超类中的另一个方法声明
@Deprecated:定义在java.lang.Deprecated中,此注解可以用于修辞方法,属性,类,表示不鼓励程序员使用这些元素,通常是因为他们很不安全或有更好的选择
@SuppressWarnings:定义在java.lang.SuppressWarnings中,用来抑制编译时的警告信息
和上面两个注释有所不同,你需要添加一个参数才能正确使用,这些参数都是已经定义好的,我们选择性的使用就好了
@SuppressWarnings("all")
@SuppressWarnings("unchecked")
@SuppressWarnings(value={"unchecked","deprecation"})
等...
元注解的作用就是负责注解其他注解,Java定义了4个标准的meta-annotation类型,他们被用来提供对其他annotation类型做说明
这些类型和他们所支持的类在java.lang.annotation包中可以找到(@Target,@Retention,@Documented,@Inherited)
@Target:用于描述注解的使用范围(即被描述的注解可以用在什么地方)
@Retention:表示需要在上面级别保存该注释信息,用于描述注解的生命周期
(SOURCE<CLASS<RUNTIME)
@Document:说明该注解将被包围在javadoc中
@Inherited:说明子类可以继承父类中的该注解
 package com.lili.annotation;
 ?
 import java.lang.annotation.*;
 ?
 //测试元注解
 
使用@interface自定义注解是,自动继承java.lang.annotation.Annotation接口
分析:
@interface用来声明一个注解,格式:public @interface 注解名{定义内容}
其中的每一个方法实际上是声明了一个配置参数
方法的名称就是参数的名称
返回值类型就是参数的类型(返回值只能是基本类型,class,String,enum)
可以通过default来声明参数的默认值
如果只有一个参数成员,一般参数名是value
注解元素必须要有值,我们定义注解元素时,经常使用空字符串,0作为默认值
 package com.lili.annotation;
 ?
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 ?
 public class Test02 {
     
Reflection(反射)是Java被视为动态语言的关键,反射机制允许程序在执行期借助于Reflection API取得任何类的内部信息,并能直接操作任意对象的内部属性及方法
 Class c = Class.forName("java.lang.String")
加载完类之后,在堆内存的方法区中就产生了一个Class类型的对象(一个类只有一个Class对象),这个对象就包含了完整的类的结构信息。我们可以通过这个对象看到类的结构。这个对象就像一面镜子。透过这个镜子看到类结构,所以,我们形象的称为 反射。
正常方式:引入需要的"包类"名称——>通过new实例化——>取得实例化对象
反射方式:实例化对象——>getClass()方法——>得到完整的“包类”名称
在运行时判断任意一个对象所属的类
在运行时构造任意一个类的对象
在运行时判断任意一个类所具有的成员变量和方法
在运行时获取泛型信息
在运行时调用任意一个对象的成员变量和方法
在运行时处理注解
生成动态代理
。。。
优点:
可以实现动态创建对象和编译,体现出很大的灵活性
缺点:
对性能有影响。使用反射基本上是一种解释操作,我们可以告诉JVM,我们希望做什么并且它满足我们的要求。这类操作总是慢于直接执行相同的操作。
 package com.lili.annotation;
 //什么叫反射
 public class Test03 {
     public static void main(String[] args) throws ClassNotFoundException {
         //通过反射获取类的Class对象
         Class c1 = Class.forName("com.lili.annotation.User");
         //一个类被加载以后,类的整体结构都被封装在class对象中
         System.out.println(c1);
 ?
     }
 }
 //实体类
 class User{
     private String name;
     private int id;
     private int age;
 ?
     public User(String name, int id, int age) {
         this.name = name;
         this.id = id;
         this.age = age;
     }
 ?
     public User() {
     }
 ?
     public String getName() {
         return name;
     }
 ?
     public void setName(String name) {
         this.name = name;
     }
 ?
     public int getId() {
         return id;
     }
 ?
     public void setId(int id) {
         this.id = id;
     }
 ?
     public int getAge() {
         return age;
     }
 ?
     public void setAge(int age) {
         this.age = age;
     }
 ?
     
 package com.lili.annotation;
 ?
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 ?
 public class Test02 {
     
class:外部类,成员(成员内部类,静态内部类),局部内部类,匿名内部类。
interface:接口
[]:数组
enum:枚举
annotation:注解@interface
primitive type:基本数据类型
void
 package com.lili.annotation;
 ?
 import java.lang.annotation.ElementType;
 ?
 //所有类型的Class
 public class Test05 {
     public static void main(String[] args) {
         Class c1 = Object.class;
         Class c2 = Comparable.class;
         Class c3 = String[].class;
         Class c4 = int[][].class;
         Class c5 = Override.class;
         Class c6 = ElementType.class;
         Class c7 = Integer.class;
         Class c8 = void.class;
         Class c9 = Class.class;
         System.out.println(c1);
         System.out.println(c2);
         System.out.println(c3);
         System.out.println(c4);
         System.out.println(c5);
         System.out.println(c6);
         System.out.println(c7);
         System.out.println(c8);
         System.out.println(c9);
         int[] a = new int[10];
         int[] b = new int[100];
         System.out.println(a.getClass().hashCode());
         System.out.println(b.getClass().hashCode());
     }
 }
 package com.lili.annotation;
 //类怎么加载的
 public class Test06 {
     public static void main(String[] args) {
         A a = new A();
         System.out.println(a.m);
     }
 }
 class A{
     static {
         System.out.println("A类静态初始化!");
         m=100;
     }
     static int m = 200;
 ?
     public A() {
         System.out.println("A类无参构造方法");
     }
 }
 package com.lili.annotation;
 ?
 public class Test07 {
     static {
         System.out.println("main类被加载");
     }
     public static void main(String[] args) throws ClassNotFoundException {
         //主动引用
         //Son son = new Son();
         //反射也会产生主动引用
         //Class.forName("com.lili.annotation.Son");
         //不会产生类的引用方法
         //System.out.println(Son.f);
         Son[] sons = new Son[10];
         System.out.println(Son.m);
     }
 }
 class Father{
     static int f=3;
     static {
         System.out.println("父类被加载");
     }
 }
 class Son extends Father{
     static {
         System.out.println("子类被加载");
     }
     static int s = 100;
     static final int m=50;
 }
 package com.lili.annotation;
 ?
 public class Test08 {
     public static void main(String[] args) throws ClassNotFoundException {
         //获取系统类的加载器
         ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
         System.out.println(systemClassLoader);
         //获取系统类加载器的父类加载器-->扩展类加载器
         ClassLoader parent = systemClassLoader.getParent();
         System.out.println(parent);
         //获取扩展类加载器的父类加载器-->根加载器(C/C++)
         ClassLoader parent1 = parent.getParent();
         System.out.println(parent1);
         //测试由哪个加载器加载
         ClassLoader classLoader = Class.forName("com.lili.annotation.Test08").getClassLoader();
         System.out.println(classLoader);
         ClassLoader classLoader1 = Class.forName("java.lang.Object").getClassLoader();
         System.out.println(classLoader1);
         //获得系统类加载器可以加载的路径
         System.out.println(System.getProperty("java.class.path"));
     }
 }
通过反射获取运行时类的完整结构
实现的全部接口
所继承的父类
全部的构造方法
全部的方法
全部的Field
注解
 package com.lili.annotation;
 ?
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 ?
 public class Test09 {
     public static void main(String[] args) {
         User user = new User();
         Class c1 = user.getClass();
         System.out.println(c1.getName());//包+类名
         System.out.println(c1.getSimpleName());//类名
         //获得类属性
         Field[] fields = c1.getFields();//只能找到public属性
         fields = c1.getDeclaredFields();//可以找到全部属性
         for (Field field:fields) {
             System.out.println(field);
         }
         System.out.println("=======================");
         //获得类的方法
         Method[] methods = c1.getMethods();
         for (Method method:methods) {
             System.out.println(method);
         }
         System.out.println("=======================");
         methods = c1.getDeclaredMethods();
         for (Method method:methods) {
             System.out.println(method);
         }
     }
 }
创建类的对象:调用Class对象的newInstance()方法
类必须有一个无参数的构造器
类的构造器的访问权限需要足够
难道没有无参的构造器就不能创建对象?
只有在操作的时候明确的调用类中的构造器,并将参数传递进去之后,才可以实例化操作
步骤如下:
通过Class类的getDeclaredConstructor(Class...parameterTypes)取得本类的指定形参类型的构造器
向构造器的形参中传递一个对象数组进去,里面包含了构造器中所需要的各个参数
通过Constructor实例化对象
通过反射,调用类中的方法,通过Method类完成
通过Class类的getMethod(String name,Class...parameterTypes)方法取得一个Method对象,并设置此方法操作时所需要的参数类型
之后使用Object invoke(Object obj,Object[] args)进行调用,并向方法中传递要设置的obj对象的参数信息
Object对应原方法的返回值,若原方法无返回值,此时返回null
若原方法若为静态方法,此时形参Object obj可为null
若原方法形参列表为空,则Object[] args为null
若原方法声明为private,则需要在调用此invoke()方法前,显示调用方法对象的setAccessible(true)方法,将可访问private的方法
Method和field、Constructor对象都有setAccessible()方法
setAccessible作用是启动和禁用访问安全检查的开关
参数值为true则是指示反射的对象在使用时应该取消Java语言访问检查
提高反射的效率。如果代码中必须用反射,而该句代码需要频繁的被调用,那么请设置为true
使得原本无法访问的私有成员也可以访问
参数值为false则指示反射的对象应该实施Java语言访问检查
 package com.lili.annotation;
  
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 ?
 //创建动态对象,通过反射
 public class Test10 {
     public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
         //获得Class对象
         Class c1 = Class.forName("com.lili.annotation.User");
         //构造一个对象
         User user = (User) c1.newInstance();
         System.out.println(user);
         //通过构造器创造对象
         Constructor constructors = c1.getDeclaredConstructor(String.class,int.class,int.class);
         User user1 = (User)constructors.newInstance("白眉", 11, 500);
         System.out.println(user1);
         //通过反射调用普通方法
         User user2 = (User)c1.newInstance();
         //通过反射获取一个方法
         Method setName = c1.getDeclaredMethod("setName", String.class);
         setName.invoke(user2,"黑莓");
         System.out.println(user2.getName());
         //通过反射操作属性
         User user3 = (User)c1.newInstance();
         Field field = c1.getDeclaredField("name");
         field.setAccessible(true);//不能直接操作私有属性,关闭安全检测
         field.set(user3,"蓝莓");
         System.out.println(user3.getName());
     }
 }
 package com.lili.annotation;
 ?
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 ?
 public class Test11 {
     //普通方式调用
     public static void test1(){
         User user = new User();
         long startTime = System.currentTimeMillis();
         for (int i = 0; i < 1000000000; i++) {
             user.getName();
         }
         long endTime = System.currentTimeMillis();
         System.out.println("普通方式执行10亿次:"+(endTime-startTime)+"ms");
     }
     //反射
     public static void test2() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
         User user = new User();
         Class c1 = user.getClass();
         Method getName = c1.getDeclaredMethod("getName", null);
         long startTime = System.currentTimeMillis();
         for (int i = 0; i < 1000000000; i++) {
             getName.invoke(user,null);
         }
         long endTime = System.currentTimeMillis();
         System.out.println("反射方式执行10亿次:"+(endTime-startTime)+"ms");
     }
     //反射  关闭安全检测
     public static void test3() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
         User user = new User();
         Class c1 = user.getClass();
         Method getName = c1.getDeclaredMethod("getName", null);
         getName.setAccessible(true);
         long startTime = System.currentTimeMillis();
         for (int i = 0; i < 1000000000; i++) {
             getName.invoke(user,null);
         }
         long endTime = System.currentTimeMillis();
         System.out.println("反射关闭检测方式执行10亿次:"+(endTime-startTime)+"ms");
     }
 ?
     public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
         test1();
         test2();
         test3();
     }
 }
Java采用泛型擦除的机制来引入泛型,Java中泛型仅仅时给编译器javac使用的,确保数据的安全性和免去强制类型转换问题,但是,一旦编译完成,所有和泛型有关的类型全部擦除
为了通过反射操作这些类型,Java新增了ParameterizedType,GenericArrayType,TypeVariable和WildcardType几种类型来代表不能被归一到Class类中的类型但是又和原始类型齐名的类型
ParameterizedType:表示一种参数化类型,比如Collection<String>
GenericArrayType:表示一种元素类型时参数化类型或者类型变量的数组类型
TypeVariable:是各种类型变量的公共父接口
WildcardType:代表一种通配符类型表达式
 package com.lili.annotation;
 ?
 import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.util.List;
 import java.util.Map;
 ?
 //通过反射获取泛型
 public class Test12 {
     public void test1(Map<String,User> map, List<User> list){
         System.out.println("test1");
     }
     public Map<String,User> test2(){
         System.out.println("test2");
         return null;
     }
 ?
     public static void main(String[] args) throws NoSuchMethodException {
         Method method = Test12.class.getMethod("test1", Map.class, List.class);
         Type[] genericParameterTypes = method.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);
                 }
             }
         }
         method = Test12.class.getMethod("test2", null);
         Type genericReturnType = method.getGenericReturnType();
         if (genericReturnType instanceof ParameterizedType){
             Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
             for (Type actualTypeArgument : actualTypeArguments) {
                 System.out.println(actualTypeArgument);
             }
         }
     }
 }
 package com.lili.annotation;
 ?
 import java.lang.annotation.*;
 import java.lang.reflect.Field;
 ?
 //练习反射操作注解
 public class Test13 {
     public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
         Class c1 = Class.forName("com.lili.annotation.Student1");
         //通过反射获得注解
         Annotation[] annotations = c1.getAnnotations();
         for (Annotation annotation : annotations) {
             System.out.println(annotation);
         }
         //获得注解的值
         TableMe tableMe = (TableMe) c1.getAnnotation(TableMe.class);
         String value = tableMe.value();
         System.out.println(value);
         //获得类指定的注解
         Field field = c1.getDeclaredField("name");
         FieldMe fa = field.getAnnotation(FieldMe.class);
         System.out.println(fa.columnName());
         System.out.println(fa.length());
         System.out.println(fa.type());
     }
 ?
 }