public static void main(String[] args) {
ATest aTest = new ATest();
String packageName = aTest.getClass().getName();
System.out.println("通过对象获取完整包名:" + packageName);
try {
Class<?> aClass = Class.forName(packageName);
Class<?> bClass = new ATest().getClass();
Class<?> cClass = ATest.class;
System.out.println("实例化Class类对象");
Class<?> parentClass = aClass.getSuperclass();
System.out.println("aClass的父类为:" + parentClass.getName());
Class<?>[] aClassInterfaces = aClass.getInterfaces();
for (Class<?> anInterface : aClassInterfaces) {
System.out.println("aClass实现的接口有:" + anInterface.getName());
}
*
* 创建对象实例
* 1.实例化默认构造方法,调用set赋值
* 2.取得全部的构造函数 使用构造函数赋值
*
*/
Class<?> class1 = null;
class1 = Class.forName("reflect.User");
User user = (User) class1.newInstance();
user.setName("ZQiang94");
user.setPwd("******");
Constructor<?>[] constructors = class1.getConstructors();
System.out.println("\n---------查看每个构造方法所需参数------");
for (Constructor<?> constructor : constructors) {
Class<?>[] types = constructor.getParameterTypes();
for (Class<?> type : types) {
String name = type.getName();
System.out.println("参数为:" + name);
}
}
System.out.println("\n---------通过构造方法创建实例-----------");
user = (User) constructors[0].newInstance();
System.out.println(user.toString());
user = (User) constructors[1].newInstance("ZHANGQIANG", "######");
System.out.println(user.toString());
System.out.println("\n---------- 获取类中所有 fields ----------");
Field[] fields = class1.getDeclaredFields();
for (Field f : fields) {
int modifier = f.getModifiers();
System.out.println("修饰符为:" + Modifier.toString(modifier));
String name = f.getType().getName();
System.out.println("类型为:" + name);
}
System.out.println("\n---------- 获取类中public fields ---------");
* getFields()
Returns an array containing {@code Field} objects reflecting all
the accessible public fields of the class or interface represented by
this {@code Class} object.
*/
Field[] fields1 = class1.getFields();
if (fields1.length == 0) {
System.out.println("没有公开属性");
}
for (Field f : fields1) {
int modifier = f.getModifiers();
System.out.println("修饰符为:" + Modifier.toString(modifier));
String name = f.getType().getName();
System.out.println("类型为:" + name);
}
System.out.println("\n------------获取类中所有public methods ----------");
Method[] methods = class1.getMethods();
for (int i = 0; i < methods.length; i++) {
StringBuilder sb = new StringBuilder("");
Method m = methods[i];
Class<?> returnType = m.getReturnType();
String modifiers = Modifier.toString(m.getModifiers());
Class<?>[] types = m.getParameterTypes();
sb = new StringBuilder(modifiers)
.append(" ")
.append(returnType.getName() + " ")
.append(m.getName()).append("(");
for (int j = 0; j < types.length; j++) {
String name = types[j].getName();
sb.append(name).append(" param" + j + ",");
}
Class<?> exce[] = methods[i].getExceptionTypes();
sb.append(")");
System.out.println(sb);
}
System.out.println("\n------------获取类中所有methods ----------");
Method[] declaredMethods = class1.getDeclaredMethods();
for (Method method : declaredMethods) {
System.out.println(method.getName());
}
System.out.println("\n--------- 调用类中方法 ------------");
Method method1 = class1.getMethod("Methods_public");
method1.invoke(class1.newInstance());
* 调用private方法
* Exception: java.lang.NoSuchMethodException
*
* Method method2 = class1.getMethod("Methods_private");
* method2.invoke(class1.newInstance());
*/
System.out.println("通过getMethod()调用private方法\n" +
"Exception: java.lang.NoSuchMethodException");
System.out.println("允许通过getDeclaredMethod()方法调用私有方法");
Method method2 = class1.getDeclaredMethod("Methods_private");
method2.setAccessible(true);
method2.invoke(class1.newInstance());
System.out.println("\n------------修改属性--------------");
Field[] fs = ATest.class.getDeclaredFields();
System.out.println("private final String str0 = \"str0\"; //私有 常量\n" +
"public final String str1 = \"str1\"; //公有 常量\n" +
"public static final String str2 = \"str2\"; //公有 静态 常量\n" +
"private static final String str3 = \"str3\"; //私有 静态 常量\n" +
"private String str4 = \"str4\"; //私有 变量\n" +
"private static String str5 = \"str5\"; //私有 静态 变量\n" +
"private User user0 = new User();\n" +
"private static User user1 = new User();\n" +
"private static final User user2 = new User();");
for (Field f : fs) {
f.setAccessible(true);
switch (f.getName()) {
case "str0":
f.set(aTest, "change_str0");
System.out.println(aTest.getStr0());
break;
case "str1":
f.set(aTest, "change_str1");
System.out.println(aTest.getStr1());
break;
case "str2":
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.set(f, f.getModifiers() & ~Modifier.FINAL);
f.set(null, "change_str2");
System.out.println(aTest.getStr2());
break;
case "str3":
Field modifiersField1 = Field.class.getDeclaredField("modifiers");
modifiersField1.setAccessible(true);
modifiersField1.set(f, f.getModifiers() & ~Modifier.FINAL);
f.set(aTest, "change_str3");
System.out.println(aTest.getStr3());
System.out.println("************ str0/str1/str2/str3 是final类型,不可变(即便是可赋值,但最后还是输出初始值)");
break;
case "str4":
f.set(aTest, "change_str4");
System.out.println(aTest.getStr4());
break;
case "str5":
f.set(aTest, "change_str5");
System.out.println(aTest.getStr5());
System.out.println("************ str4/str5 是非final类型,可改变初始值");
break;
case "user0":
u.setName("user0");
u.setPwd("110");
System.out.println(aTest.getUser0().toString());*/
System.out.println("因为user0是非static类型的对象,故会报以下错:" +
"\njava.lang.IllegalArgumentException: Can not set final reflect.User field reflect.ATest.user1 to java.lang.String\n" +
"\tat sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)\n" +
"\tat sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)\n" +
"\tat sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)\n" +
"\tat sun.reflect.UnsafeQualifiedObjectFieldAccessorImpl.get(UnsafeQualifiedObjectFieldAccessorImpl.java:38)\n" +
"\tat java.lang.reflect.Field.get(Field.java:393)");
break;
case "user1":
User u1 = (User) f.get("user1");
u1.setName("user1");
u1.setPwd("110");
System.out.println(aTest.getUser1().toString());
break;
case "user2":
User u2 = (User) f.get("user2");
u2.setName("user2");
u2.setPwd("110");
System.out.println(aTest.getUser2());
break;
default:
break;
}
}
System.out.println("\n---------------- 动态代理 ----------");
System.out.println("在java中有三种类类加载器。\n" +
"\n" +
"1.Bootstrap ClassLoader 此加载器采用c++编写,一般开发中很少见。\n" +
"\n" +
"2.Extension ClassLoader 用来进行扩展类的加载,一般对应的是jrelibext目录中的类\n" +
"\n" +
"3.AppClassLoader 加载classpath指定的类,是最常用的加载器。同时也是java中默认的加载器。\n" +
"如果想要完成动态代理,首先需要定义一个InvocationHandler接口的子类,已完成代理的具体操作。\n");
System.out.println("类加载器: " + aTest.getClass().getClassLoader().getClass().getName());
MyInvocationHandler handler = new MyInvocationHandler();
Subject sub = (Subject) handler.bind(new SubjectImpl());
String info = sub.outInfo("TEST", "***");
System.out.println(info);
} catch (Exception e) {
e.printStackTrace();
}
}