通常某个类的Class对象被载入内存,它就用来建立这个类的全部对象。 java
1、如何获得Class的对象呢?有三种方法能够的获取
一、调用Object类的getClass()方法来获得Class对象,这也是最多见的产生Class对象的方法。例如:
MyObject x; spring
Class c1 = x.getClass(); 数组
二、使用Class类的中静态forName()方法得到与字符串对应的Class对象。例如:
三、获取Class类型对象的第三个方法很是简单。若是T是一个Java类型,那么T.class就表明了匹配的类对象。例如
Class cl1 = Manager.class;
Class cl2 = int.class;
Class cl3 = Double[].class; ui
2、经常使用方法及用法详解 this
package test.uitls; import java.lang.reflect.Constructor; import javax.persistence.Entity; import org.hibernate.annotations.AccessType; import org.springframework.transaction.annotation.Transactional; import test.model.AbsEntity; import test.model.Role; public class ReflectionUtils { public static void main(String args[]) throws NoSuchMethodException, SecurityException, NoSuchFieldException, InstantiationException, IllegalAccessException, ClassNotFoundException { System.out.println("getName() : " + Role.class.getName());//返回class或interface的包名+类名。 System.out.println("getClassLoader() : " + Role.class.getClassLoader()); System.out.println("getConstructors : " + Role.class.getConstructors());//返回这个类全部的public构造器 System.out.println("getConstructor : " + Role.class.getConstructor());//返回含有某个特定参数类型的public修饰的构造器。 Constructor[] Constructors = Role.class.getDeclaredConstructors();//返回这个类全部由 public, protected, default (package) access, private修饰的构造器。 for (int i = 0; i < Constructors.length; i++) { System.out.println("getDeclaredConstructors " + (i + 1) + " : " + Constructors[i]); } System.out.println("getDeclaredConstructor : " + Role.class.getDeclaredConstructor(String.class));//返回某个含有特定参数类型的public, protected, default //(package) access, private修饰的构造器。 System.out.println("getFields : " + Role.class.getFields().length);//返回全部这个类全部Public修饰属性, 包括继承来的属性. System.out.println("getField : " + Role.class.getField("id"));//返回全部这个类特定名称public修饰的某个属性, 包括继承来的属性(返回Field类型)。 System.out.println("getDeclaredFields : " + Role.class.getDeclaredFields().length);//返回这个类全部 public, protected, default //(package) access, private修饰的属性, 但不包括继承来的属性。 System.out.println("getDeclaredField : " + Role.class.getDeclaredField("roleName"));//返回这个类含有某个特定名称的,并包括由public, protected, default //(package) access, private修饰的属性。 System.out.println("getMethods : " + Role.class.getMethods().length);//返回全部这个类全部Public修饰属性, 包括继承来的方法. System.out.println("getMethod : " + Role.class.getMethod("getId"));//返回全部这个类特定名称public修饰的某个方法, 包括继承来的方法(返回Method类型)。 System.out.println("getDeclaredMethods : " + Role.class.getDeclaredMethods().length);//返回这个类全部 public, protected, default //(package) access, private修饰的方法, 但不包括继承来的方法。 System.out.println("getDeclaredMethod : " + Role.class.getDeclaredMethod("getRoleName"));//返回这个类含有某个特定名称的,并包括由public, protected, default //(package) access, private修饰的方法。 System.out.println("getSuperclass : " + Role.class.getSuperclass());//返回父类的Class对象。一个对象只有一个superclass. System.out.println("getSuperclass : " + AbsEntity.class.getSuperclass()); // System.out.println(IEntity.class.getSuperclass());//最上层的接口的超类竟然不是object? System.out.println("getInterfaces : " + Role.class.getInterfaces()[0]);//返回这个类实现的接口。 System.out.println("Role is Interface : " + Role.class.isInterface());//判断这个Class对象是不是一个接口。 System.out.println("Role is Annotation : " + Role.class.isAnnotation());//判断这个Class对象是不是一个注释。 System.out.println("Transactional is Annotation : " + Transactional.class.isAnnotation()); System.out.println("get Role's Entity Annotation : " + Role.class.getAnnotation(Entity.class));//获取这个类的的注释Entity,Entity没有注释这个类返回null。 System.out.println("get Role's AccessType Annotation : " + Role.class.getAnnotation(AccessType.class)); System.out.println("get Role's Transactional Annotation : " + Role.class.getAnnotation(Transactional.class)); Role myrole = new Role(); System.out.println("Role is Instance Of myrole : " + Role.class.isInstance(myrole));//是不是某个对象的实例。 Class roleClass = Class.forName("test.model.Role"); Role role = (Role)roleClass.newInstance();//这个类必定要有一个默认构造器或public无参构造器。 role.getDisplayString(); } }
getName() : test.model.Role getClassLoader() : sun.misc.Launcher$AppClassLoader@3feef1eb getConstructors : [Ljava.lang.reflect.Constructor;@6411c21b getConstructor : public test.model.Role() getDeclaredConstructors 1 : public test.model.Role() getDeclaredConstructors 2 : private test.model.Role(java.lang.String) getDeclaredConstructor : private test.model.Role(java.lang.String) getFields : 3 getField : public java.lang.String test.model.AbsEntity.id getDeclaredFields : 2 getDeclaredField : public java.lang.String test.model.Role.roleName getMethods : 16 getMethod : public java.lang.String test.model.AbsEntity.getId() getDeclaredMethods : 5 getDeclaredMethod : public java.lang.String test.model.Role.getRoleName() getSuperclass : class test.model.AbsEntity getSuperclass : class java.lang.Object getInterfaces : interface test.model.IEntity Role is Interface : false Role is Annotation : false Transactional is Annotation : true get Role's Entity Annotation : @javax.persistence.Entity(name=) get Role's AccessType Annotation : null get Role's Transactional Annotation : null Role is Instance Of myrole : true I am a Role
package test.model; public interface IEntity { public String getDisplayString(); }
package test.model; import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.Id; import org.hibernate.annotations.AccessType; import org.hibernate.annotations.GenericGenerator; public abstract class AbsEntity { @Id @GeneratedValue(generator = "system-uuid") @GenericGenerator(name = "system-uuid", strategy = "uuid.hex") @Column(length = 40) @AccessType("property") public String id; public String getId() { return id; } public void setId(String id) { this.id = id; } }
package test.model; import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(schema = "public") public class Role extends AbsEntity implements IEntity{ public String roleName; public String userId; public Role() {} private Role(String roleName) { this.roleName = roleName; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getDisplayString() { System.out.println("I am a Role"); return "Role"; } }
5、不长用方法 spa
1.isAssignableFrom(class<?> cls) 判断这个类是不是cls的父类或与cls相同的类。 hibernate