package com.Test.Demo.FanShe;
import java.lang.reflect.*;
public class FanShe {
public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, InvocationTargetException {
Class c1 = Class.forName("java.lang.String");
System.out.println(c1);
Class c2 = String.class;
System.out.println(c2);
String s1 = "12w";
Class c3 = s1.getClass();
System.out.println(c3);
Object obj = c1.newInstance();
System.out.println();
Class c4 = Class.forName("com.Test.Demo.FanShe.A");
Constructor[] constructors=c4.getConstructors();
for(Constructor t:constructors){
System.out.println("构造方法方法名为:"+t.getName());
System.out.println("构造方法修饰符为:"+Modifier.toString(t.getModifiers()));
Class[] temp = t.getParameterTypes();
for(Class temp1:temp)
System.out.println("构造方法的参数类型:"+temp1.getName());
}
System.out.println();
Method[] methods = c4.getDeclaredMethods();
Method method = c4.getDeclaredMethod("sum",int.class,int.class);
method.setAccessible(true);
System.out.print("sum方法执行结果:");
method.invoke(new A(),1,2);
for(Method m1:methods){
System.out.println("普通方法名称:"+m1.getName());
System.out.println("普通方法修饰符:"+Modifier.toString(m1.getModifiers()));
Class[] t1 = m1.getParameterTypes();
System.out.println("普通方法参数类型:");
for(Class temp:t1){
System.out.println("\t"+temp.getName());
}
System.out.println("方法的返回值类型:"+m1.getReturnType().getName());
}
System.out.println();
Field[] fields = c4.getDeclaredFields();
System.out.println("变量的数量:"+fields.length);
Field f = c4.getDeclaredField("c");
System.out.println("是否取消执行安全检查"+f.isAccessible());
f.setAccessible(true);
A a = new A();
f.set(a,666);
System.out.println("取得的变量为:"+f.getName()+" 值为:"+f.get(a));
for(Field f1: fields){
f1.setAccessible(true);
System.out.println("变量的名称:"+f1.getName());
System.out.println("变量修饰符:"+Modifier.toString(f1.getModifiers()));
System.out.println("变量的类型:"+f1.getType().getName());
System.out.println("变量的值:"+f1.get(a));
}
System.out.println();
Class[] c5 = c4.getInterfaces();
for(Class t1 :c5){
System.out.println("该类实现的接口:"+t1.getName());
}
System.out.println("该类的父类:"+c4.getSuperclass().getName());
System.out.println("该类所在的包:"+c4.getPackage().getName());
System.out.println("该类的名字:"+c4.getName());
System.out.println("该类的简称:"+c4.getSimpleName());
System.out.println("该类是否是接口:"+c4.isInterface());
}
}
class A implements Runnable {
public static String a="123";
public String b;
private int c;
public A() {
}
public A(String a) {
this.a = a;
}
public A(String a, String b) {
this.a = a;
this.b = b;
}
private void sum(int a,int b){
System.out.println(a+b);
}
@Override
public void run() {
}
}
package com.Test.Demo.Test;
import sun.reflect.generics.scope.MethodScope;
import java.lang.reflect.*;
public class ReflectTest {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
Class c1 = Class.forName("com.Test.Demo.Test.A");
Class c2 = A.class;
Class c3 = new A().getClass();
A a1 = (A) c1.newInstance();
Constructor[] constructor = c1.getDeclaredConstructors();
Constructor constructor1= c1.getConstructor(int.class,int.class);
Constructor constructor2=c1.getDeclaredConstructor(int.class);
constructor2.setAccessible(true);
A a3 = (A)constructor2.newInstance(3);
Method method1 = c1.getDeclaredMethod("get");
method1.setAccessible(true);
method1.invoke(a3);
A a2 = (A)constructor1.newInstance(2,5);
Method method = c1.getDeclaredMethod("get");
method.setAccessible(true);
method.invoke(a2);
System.out.println("------------------------------");
for(Constructor temp : constructor){
System.out.println("构造方法名:"+temp.getName());
System.out.println("构造方法修饰符:"+ Modifier.toString(temp.getModifiers()));
System.out.println("构造方法参数类型:");
Class[] t1=temp.getParameterTypes();
for(Class t2 : t1){
System.out.print(t2.getName()+" ");
}
System.out.println();
}
System.out.println("------------------------------");
Method[] methods = c1.getDeclaredMethods();
Method methodss= c1.getDeclaredMethod("get");
methodss.setAccessible(true);
methodss.invoke(a2);
for(Method method2 : methods){
System.out.println("方法名称:"+method2.getName());
System.out.println("方法修饰符:"+Modifier.toString(method2.getModifiers()));
System.out.println("方法的返回值类型:"+method2.getReturnType());
Class[] cc = method2.getParameterTypes();
System.out.println("方法的参数类型:");
for(Class ccc : cc){
System.out.print(ccc.getName()+" ");
}
System.out.println();
}
Field[] fields = c1.getDeclaredFields();
System.out.println(c1.getSimpleName()+"类中的变量数量:"+fields.length);
Field field = c1.getDeclaredField("b");
field.setAccessible(true);
field.set(a2,666);
System.out.println("变量名:"+field.getName()+"变量的值:"+field.get(a2));
System.out.println("变量的修饰符"+Modifier.toString(field.getModifiers()));
System.out.println("变量的类型:"+field.getType());
Class[] interfaces = c1.getInterfaces();
System.out.println("该类实现的接口:");
for(Class temp : interfaces){
System.out.println(temp.getSimpleName());
}
System.out.println("该类的父类:"+c1.getSuperclass().getName());
System.out.println("该类所在的包"+c1.getPackage());
System.out.println("该类名字:"+c1.getName());
System.out.println("该类名的简称:"+c1.getSimpleName());
}
}
class A implements Runnable{
public int a;
private int b;
public A() {
}
private A(int a){
this.a=a;
}
public A(int a, int b) {
this.a = a;
this.b = b;
}
public void setA(int a) {
this.a = a;
}
public void setB(int b) {
this.b = b;
}
public void math(int x, int y){
System.out.println(x+y);
}
private void get(){
System.out.println(this.b);
}
@Override
public void run() {
}
}