一.读取字节码文件的三种方式
package com.project.Reflect;
import java.util.Date;
public class ReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class<?> aClass = Class.forName("java.lang.String");
System.out.println(aClass);
Class<?> aClass1 = Class.forName("java.util.Date");
System.out.println(aClass1);
Class<?> aClass2 = Class.forName("java.lang.Integer");
System.out.println(aClass2);
String s="asd";
Class<? extends String> aClass3 = s.getClass();
System.out.println(aClass3==aClass);
Date time=new Date();
System.out.println(time.getClass());
Class<String> stringClass = String.class;
Class<Date> dateClass = Date.class;
Class<Integer> integerClass = int.class;
Class<Double> doubleClass = double.class;
System.out.println(stringClass);
}
}
二.通过反射机制来创建一个对象
package com.project.Reflect;
import com.project.bean.User;
public class ReflectDemo2 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
User user = new User();
Class<User> aClass = (Class<User>) Class.forName("com.project.bean.User");
User user1 = aClass.newInstance();
System.out.println(user1);
}
四.把这个字节码的类名写在这个properties文件中如何去读取
package com.project.Reflect;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class ReflectDemo3 {
public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
FileReader reader=new FileReader("Classinfo.properties");
Properties properties = new Properties();
properties.load(reader);
reader.close();
String className = properties.getProperty("ClassName");
System.out.println(className);
Class<?> aClass = Class.forName(className);
Object o = aClass.newInstance();
System.out.println(o.toString());
}
}
五.获取这个字节码文件当中的字段属性
package com.project.Reflect;
import com.project.bean.Student;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
public class ReflectDemo5 {
public static void main(String[] args) {
try {
Class<?> aClass = Class.forName("com.project.bean.Student");
System.out.println(aClass.getSimpleName());
Field[] fields = aClass.getDeclaredFields();
Arrays.stream(fields).forEach(field -> {
int modifiers = field.getModifiers();
System.out.println(Modifier.toString(modifiers));
});
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
六.通过这个反射机制给这个属性进行赋值并且完整的把这个类给拼接出来(自己设置一个类直接把这个类名修改尝试即可)
package com.project.Reflect;
import com.project.bean.Student;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
public class ReflectDemo7 {
public static void main(String[] args) throws Exception {
Class<?> aClass = Class.forName("com.project.bean.Student");
Object student = aClass.newInstance();
int modifiers1 = aClass.getModifiers();
String s1 = Modifier.toString(modifiers1);
String simpleName = aClass.getSimpleName();
String join = s1 + " " + "class" + " " + simpleName + "{\t\n";
Field[] fields = aClass.getDeclaredFields();
StringBuilder sbr = new StringBuilder();
sbr.append(join);
Arrays.stream(fields).forEach(field -> {
field.setAccessible(true);
int modifiers = field.getModifiers();
String s2 = Modifier.toString(modifiers);
sbr.append(s2);
String simpleName1 = field.getType().getSimpleName();
sbr.append(" " + simpleName1 + "\t");
sbr.append(field.getName());
if (!s2.contains("final")) {
String simpleNames = field.getType().getSimpleName();
switch (simpleNames) {
case "String":
try {
String stringValue = (String) field.get(student);
if (stringValue != null) {
sbr.append(" = ");
sbr.append(stringValue);
}
System.out.println("原始String值: " + stringValue);
field.set(student, "新的值");
stringValue = (String) field.get(student);
System.out.println("修改后的String值: " + stringValue);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
break;
case "int":
try {
int intValue = field.getInt(student);
System.out.println("原始int值: " + intValue);
if (intValue != 0) {
sbr.append(" = ");
sbr.append(intValue);
}
field.set(student, 12);
intValue = field.getInt(student);
System.out.println("修改后的int值: " + intValue);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
break;
case "boolean":
try {
boolean booleanValue = field.getBoolean(student);
System.out.println("原始boolean值: " + booleanValue);
if (booleanValue) {
sbr.append(" = true");
}
field.setBoolean(student, !booleanValue);
booleanValue = field.getBoolean(student);
System.out.println("修改后的boolean值: " + booleanValue);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
break;
case "long":
try {
long longValue = field.getLong(student);
System.out.println("原始long值: " + longValue);
field.setLong(student, 12L);
longValue = field.getLong(student);
System.out.println("修改后的long值: " + longValue);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
break;
case "char":
try {
char charValue = field.getChar(student);
System.out.println("原始char值: " + charValue);
field.setChar(student, 'A');
charValue = field.getChar(student);
System.out.println("修改后的char值: " + charValue);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
break;
default:
System.out.println("其他类型: " + simpleName);
}
} else {
try {
Object value = field.get(student);
sbr.append(" = " + value);
System.out.println("final修饰的字段值: " + value);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
sbr.append(";\n");
});
sbr.append("}");
System.out.println(sbr.toString());
}
}
七.通过反射机制来读取方法这个只是介绍了一下里面的方法但是并没有具体的进行拼接这个方法
package com.project.Reflect;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class ReflectDemo9 {
public static void main(String[] args) throws Exception{
Class<?> aClass = Class.forName("com.project.Service.UserService");
Method[] declaredMethods = aClass.getDeclaredMethods();
for (Method method : declaredMethods) {
String name = method.getName();
Class<?> returnType = method.getReturnType();
String simpleName = returnType.getSimpleName();
int modifiers = method.getModifiers();
String s = Modifier.toString(modifiers);
Class<?>[] parameterTypes = method.getParameterTypes();
for (Class<?> parameterType : parameterTypes) {
System.out.println(parameterType.getSimpleName());
}
}
}
}
八拼接所有的方法
package com.project.Reflect;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class ReflectDemo10 {
public static void main(String[] args) throws Exception{
StringBuilder sbr = new StringBuilder();
Class<?> aClass = Class.forName("java.lang.String");
sbr.append(Modifier.toString(aClass.getModifiers())+" class "+aClass.getSimpleName()+"\t\n"+"{\n");
Method[] methods = aClass.getDeclaredMethods();
for (Method method : methods) {
sbr.append(Modifier.toString(method.getModifiers())+"\t ");
sbr.append(method.getReturnType().getSimpleName()+"\t ");
sbr.append(method.getName()+"\t ");
sbr.append("(");
Class<?>[] parameterTypes = method.getParameterTypes();
for (int i = 0; i < parameterTypes.length; i++) {
sbr.append(parameterTypes[i].getSimpleName()+" arg"+i);
if (i!=parameterTypes.length-1)
{
sbr.append(",");
}
}
sbr.append("System.out.println(\"你好\")");
sbr.append("\n");
}
sbr.append("}");
System.out.println(sbr);
}
}
九.通过这个反射机制去调用一个普通的方法
package com.project.Reflect;
import java.lang.reflect.Method;
public class ReflectDemo11 {
public static void main(String[] args) throws Exception{
Class<?> aClass = Class.forName("com.project.Service.UserService");
Object object = aClass.newInstance();
Method[] declaredMethods = aClass.getDeclaredMethods();
for (Method method : declaredMethods) {
String name = method.getName();
if (name.equals("login")) {
Method methodss = aClass.getDeclaredMethod(name, String[].class);
String[] names = {"admin", "123456"};
Object invoke = methodss.invoke(object, (Object) names);
System.out.println(invoke.toString());
}else{
System.out.println("退出方法暂时不要去执行");
}
}
}
}
十拼接一个完整的含有构造方法的方法
package com.project.Reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class ReflectDemo12 {
public static void main(String[] args) throws Exception {
StringBuilder sbr = new StringBuilder();
Class<?> aClass = Class.forName("com.project.bean.Vip");
sbr.append("package; ");
String[] s2 = aClass.getName().split("\\.Vip");
sbr.append(s2[0]+"\n");
Map<String,String> Fieldmap=new HashMap<>();
Field[] fields = aClass.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Fieldmap.put(fields[i].getType().getSimpleName(),fields[i].getName());
}
System.out.println(Fieldmap);
int modifiers = aClass.getModifiers();
String s = Modifier.toString(modifiers);
sbr.append(s);
sbr.append(" class ");
sbr.append(aClass.getSimpleName());
sbr.append("{\n");
for (Field field : fields) {
int modifiers1 = field.getModifiers();
String s1 = Modifier.toString(modifiers1);
sbr.append(s1);
String simpleName = field.getType().getSimpleName();
sbr.append("\t"+simpleName);
sbr.append(" "+field.getName()+";");
sbr.append("\n");
}
Constructor<?>[] constructors = aClass.getConstructors();
for (Constructor<?> constructor : constructors) {
int modifiers1 = constructor.getModifiers();
String s1 = Modifier.toString(modifiers1);
sbr.append(" "+s1);
String simpleName = aClass.getSimpleName();
sbr.append(" "+simpleName);
sbr.append("(");
Class<?>[] parameterTypes = constructor.getParameterTypes();
for (int i = 0; i < parameterTypes.length; i++) {
sbr.append(parameterTypes[i].getSimpleName());
sbr.append(" "+"args"+i);
if (i != parameterTypes.length-1) {
sbr.append(",");
}
}
sbr.append("){\n");
for (int i = 0; i < parameterTypes.length; i++) {
sbr.append("\tthis.");
sbr.append("=");
sbr.append("args"+i+";");
sbr.append("\n");
}
sbr.append(" }\n");
}
sbr.append("}");
System.out.println(sbr.toString());
}
}
十一通过有参数的构造方法和这个无参数的构造方法来创建这个对象
package com.project.Reflect;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
public class ReflectDemo13 {
public static void main(String[] args) throws Exception {
Class<?> aClass = Class.forName("com.project.bean.Vip");
Constructor<?>[] constructors = aClass.getDeclaredConstructors();
for (Constructor<?> constructor : constructors) {
List<Class> list=new ArrayList<>();
Class<?>[] parameterTypes = constructor.getParameterTypes();
for (Class<?> parameterType : parameterTypes) {
list.add(parameterType);
}
}
Constructor<?> con = aClass.getDeclaredConstructor(int.class, String.class, String.class);
Object o = con.newInstance(1, "23", "234");
System.out.println(o.toString());
Constructor<?> constructor = aClass.getDeclaredConstructor();
Object o1 = constructor.newInstance();
System.out.println(o1.toString());
}
}
十二.最后这个地方还有一个可变参数的问题
package com.project.Reflect;
import java.util.Arrays;
public class ArgsTest {
public static void main(String[] args) {
m();
m2("aSD",12,12,3213,3213);
m3("12","323","232");
m3("1122","RER","GFGD");
}
public static void m(int... args)
{
System.out.println("方法别执行了");
}
public static void m2(String a,int ... args){
Arrays.stream(args).forEach(value -> {
System.out.println(value);
});
System.out.println("可变长度参数只能有一个并且");
}
public static void m3(String ... args)
{
Arrays.stream(args).forEach(s -> {
System.out.println(s);
});
}
}