反射
1、获取Calss实例的四种方式
//方式一:调用运行时类的属性
Class clzz = Person.calss;
//方式二:通过运行时类的对象
Person p = new Person;
Class clzz = p.getClass();
//方式三:调用Class的静态方法:forName(String classPath)
Class.forName("com.longge.java.Person");
//方式四:使用类的加载器:
ClassLoader classLoader = TestClass.class.getClassLoader();
Class clzz = classLoader.loadClass("com.longge.java.Person");
2、读取配置文件的两种方式
Properties pros = new Properties ;
FileInputStream fis = new FileInputStream ( "jdbc.properties" ) ;
pros. load ( fis) ;
ClassLoader calssloder = ClassLoderTest . class . getClassLoader ( ) ;
InputStream is = classloader. getResourceAsStream ( "jdbc.properties" ) ;
pros. load ( is) ;
String user = pros. getProperty ( "user" ) ;
3、通过反射创建运行时类的对象
Class < Person > clazz = Person . class ;
Person obj = clazz. newInstance ( ) ;
4、获取运行时类的完整结构
4.1、获取属性
Class < Person > clazz = Person . class ;
Field [ ] fields = clazz. getFields ( ) ;
Field [ ] fields = clazz. getDeclaredFields ( ) ;
int modifier = fields[ i] . getModifiders
Class type = fields[ i] . getType ( ) ;
String name = fields[ i] . getName ( ) ;
4.2、获取方法
Method [ ] methods = clazz. getMethods ( ) ;
Method [ ] methods = clazz. getDeclaredMethods ( ) ;
for ( Method m : declaredMethods) {
Annotation [ ] annos = m. getAnnotations ( ) ;
for ( Annotation a : annos) {
System . out. println ( a + "KKKK" ) ;
}
System . out. print ( Modifier . toString ( m. getModifiers ( ) ) + "\t" ) ;
System . out. print ( m. getReturnType ( ) . getName ( ) + "\t" ) ;
System . out. print ( m. getName ( ) ) ;
System . out. print ( "(" ) ;
Class [ ] pTs = m. getParameterTypes ( ) ;
if ( ! ( pTs == null && pTs. length == 0 ) ) {
for ( int i = 0 ; i < pTs. length; i++ ) {
if ( i == pTs. length - 1 ) {
System . out. print ( pTs[ i] . getName ( ) + " args_" + i) ;
break ;
}
System . out. print ( pTs[ i] . getName ( ) + " args_" + i + "," ) ;
}
}
System . out. print ( ")" ) ;
Class [ ] eTs = m. getExceptionTypes ( ) ;
if ( eTs. length > 0 ) {
System . out. print ( "throws " ) ;
for ( int i = 0 ; i < eTs. length; i++ ) {
if ( i == eTs. length - 1 ) {
System . out. print ( eTs[ i] . getName ( ) ) ;
break ;
}
System . out. print ( eTs[ i] . getName ( ) + "," ) ;
}
}
System . out. println ( "TQA" ) ;
}
4.3、获取构造器
@Test
public void test ( ) {
Class clazz = Person . class ;
Constructor [ ] constructors = clazz. getConstructors ( ) ;
for ( Constructor c : constructors) {
System . out. println ( c) ;
}
System . out. println ( "************************" ) ;
Constructor [ ] declaredConstructors = clazz. getDeclaredConstructors ( ) ;
for ( Constructor c : declaredConstructors) {
System . out. println ( c) ;
}
}
4.4、获取父类以及父类的泛型
@Test
public void test2 ( ) {
Class clazz = Person . class ;
Class superclass = clazz. getSuperclass ( ) ;
System . out. println ( superclass) ;
}
@Test
public void test3 ( ) {
Class clazz = Person . class ;
Type genericSuperclass = clazz. getGenericSuperclass ( ) ;
System . out. println ( genericSuperclass) ;
}
@Test
public void test4 ( ) {
Class clazz = Person . class ;
Type genericSuperclass = clazz. getGenericSuperclass ( ) ;
ParameterizedType paramType = ( ParameterizedType ) genericSuperclass;
Type [ ] actualTypeArguments = paramType. getActualTypeArguments ( ) ;
System . out. println ( ( ( Class ) actualTypeArguments[ 0 ] ) . getName ( ) ) ;
}
4.5、获取接口、所在包、注解等
@Test
public void test5 ( ) {
Class clazz = Person . class ;
Class [ ] interfaces = clazz. getInterfaces ( ) ;
for ( Class c : interfaces) {
System . out. println ( c) ;
}
System . out. println ( "++++++++++++++++++++++" ) ;
Class [ ] interfaces1 = clazz. getSuperclass ( ) . getInterfaces ( ) ;
for ( Class c : interfaces1) {
System . out. println ( c) ;
}
}
@Test
public void test6 ( ) {
Class clazz = Person . class ;
Package pack = clazz. getPackage ( ) ;
System . out. println ( pack) ;
}
@Test
public void test7 ( ) {
Class clazz = Person . class ;
Annotation [ ] annotations = clazz. getAnnotations ( ) ;
for ( Annotation annos : annotations) {
System . out. println ( annos) ;
}
}
5、调用运行时类的指定结构
5.1、调用指定的属性
public class ReflectionTest {
@Test
public void testField ( ) throws Exception {
Class clazz = Person . class ;
Person p = ( Person ) clazz. newInstance ( ) ;
Field id = clazz. getField ( "id" ) ;
id. set ( p, 1001 ) ;
int pId = ( int ) id. get ( p) ;
System . out. println ( pId) ;
}
@Test
public void testField1 ( ) throws Exception {
Class clazz = Person . class ;
Person p = ( Person ) clazz. newInstance ( ) ;
Field name = clazz. getDeclaredField ( "name" ) ;
name. setAccessible ( true ) ;
name. set ( p, "Jam" ) ;
System . out. println ( name. get ( p) ) ;
}
}
5.2、调用指定的方法
public class ReflectionTest {
@Test
public void testMethod ( ) throws Exception {
Class clazz = Person . class ;
Person p = ( Person ) clazz. newInstance ( ) ;
Method show = clazz. getDeclaredMethod ( "show" , String . class ) ;
show. setAccessible ( true ) ;
Object returnValue = show. invoke ( p, "CCA" ) ;
System . out. println ( returnValue) ;
System . out. println ( "+++++++++如何调用静态方法+++++++++++" ) ;
Method showDesc = clazz. getDeclaredMethod ( "showDown" ) ;
showDesc. setAccessible ( true ) ;
Object returnVal = showDesc. invoke ( Person . class ) ;
System . out. println ( returnVal) ;
}
5.3、调用指定的构造器
@Test
public void testConstructor ( ) throws Exception {
Class clazz = Person . class ;
Constructor constructor = clazz. getDeclaredConstructor ( String . class ) ;
constructor. setAccessible ( true ) ;
Person per = ( Person ) constructor. newInstance ( "Tom" ) ;
System . out. println ( per) ;
}