???????Java?????
???????????? ???????[ 2015/11/18 11:12:49 ] ??????????????????? ???????
?????嵥 5. ???? ASM ????????
????@Override
????public void visitTypeInsn(int opcode?? String type) {
????if(opcode==Opcodes.<em>NEW</em> && type.equals(className)){
????List<LocalVariableNode> variables = node.localVariables;
????String compileType = null;
????for(int i=0;i<variables.size();i++){
????LocalVariableNode localVariable = variables.get(i);
????compileType = <em>formType</em>(localVariable.desc);
????if(matchType(compileType)&&!valiableIndexUsed[i]){
????valiableIndexUsed[i] = true;
????break;
????}
????}
????mv.visitMethodInsn(Opcodes.<em>INVOKESTATIC</em>?? <em>CLASSLOAD_TYPE</em>??
????"getInstance"?? "()L"+<em>CLASSLOAD_TYPE</em>+";");
????mv.visitLdcInsn(type.replace("/"?? "."));
????mv.visitMethodInsn(Opcodes.<em>INVOKEVIRTUAL</em>?? <em>CLASSLOAD_TYPE</em>??
????"findClass"?? "(Ljava/lang/String;)Ljava/lang/Class;");
????mv.visitMethodInsn(Opcodes.<em>INVOKEVIRTUAL</em>?? "java/lang/Class"??
????"newInstance"?? "()Ljava/lang/Object;");
????mv.visitTypeInsn(Opcodes.<em>CHECKCAST</em>?? compileType);
????flag = true;
????} else {
????mv.visitTypeInsn(opcode?? type);
????}
????}
?????????????????????????????? Class.forName()?? ClassLoader.findClass()???????????????????????????????
??????? JavaAgent ?????????????????
????? ???????????????????????????????????????? JVM ????????????????????????????? classpath ?μ????? class ????????????????????ü???????????????????????????????????????????????? class???п??????? LinkageError ?? exception??????????????????????????滻???????? class??????? jdk1.4 ??????????????????????? jdk ?? classloader ???????????????????????????????????????? jdk5.0 ???????????????????????С????????? JavaAgent ??????JavaAgent ?????? JVM ???????????????????????????????????Щ??????????????????????????? JavaAgent ??????????????????????????????
????JavaAgent ???????????????????????棬??????? premain ???????
?????嵥 6. ??????? JavaAgent
????public class ReloadAgent {
????public static void premain(String agentArgs?? Instrumentation inst){
????GeneralTransformer trans = new GeneralTransformer();
????inst.addTransformer(trans);
????}
????}
????????д??? manifest ??????? Premain-Class???????ó?????????? premain?????????????ɡ?
?????????????????? manifest ????? jar ????
????manifest-Version: 1.0
????Premain-Class: com.example.ReloadAgent
????Can-Redefine-Classes: true
??????????????????????????? -javaagent???? ?? ??????? jar????????? Javaagent?????????????е?????????????? test project ??·?????????????????????????????? premain?????е??????????????????????? class??
????? 1. ??????в???
???????????? JavaAgent?滻????????????????? Java ????????????????? ??? ClassFileTransformer???????????????????? class ?滻??????
?????嵥 7. ?滻 class
????@Override
????public byte [] transform(ClassLoader paramClassLoader?? String paramString??
????Class<?> paramClass?? ProtectionDomain paramProtectionDomain??
????byte [] paramArrayOfByte) throws IllegalClassFormatException {
????String className = paramString.replace("/"?? ".");
????if(className.equals("com.example.Test")){
????MyClassLoader cl = MyClassLoader.<em>getInstance</em>();
????cl.defineReference(className?? "com.example.Greeter");
????return cl.getByteCode(className);
????}else if(className.equals("com.example.Greeter")){
????MyClassLoader cl = MyClassLoader.<em>getInstance</em>();
????cl.redefineClass(className);
????return cl.getByteCode(className);
????}
????return null;
????}
????????????е????????????????? hotswap ?????ɡ?
????? 2. Test ??н??
??????????
?????? ?? hotswap ??????????????????????????????????????????μ??????????????????????????????????? JVM ?????????? class ????????????????????????????? class??hotswap ???? Java ????????????????????? JVM ????н?????????????????????????????????????е??????????δ???? Jdk ????????????? Java ??????????????????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11