打开zip文件或缺少JAR清单时出错:c:\ jat.ja

时间:2017-12-07 09:22:30

标签: jvm java-bytecode-asm javaagents

我想使用jvm参数" -javaagent"修改Account.class,使它可以计算执行时间,但有一个错误信息,我不知道为什么。 源代码:

Account.class

public class Account {

    public void operation(){
        System.out.println("operations.....");
        try{
            Thread.sleep(10);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}

TimeStat.class

public class TimeStat {

    static ThreadLocal<Long> t=new ThreadLocal<>();
    public static void start(){
        t.set(System.currentTimeMillis());
    }

    public static void end(){
        long time=System.currentTimeMillis();
        System.out.println(Thread.currentThread().getStackTrace()[2]+"speed:");
        System.out.println(time);
    }
}

主要课程

public class RunAccountMain {

    public static void main(String[] args){
        Account account=new Account();
        account.operation();
        System.out.println(Account.class.getName());
    }
}

我可以使用一些类来修改以下的Account.class:

PreMainTraceAgent .class

 import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;
import java.security.ProtectionDomain;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;

public class PreMainTraceAgent {

    public static void premain(String agentArgs,Instrumentation inst)throws ClassNotFoundException,UnmodifiableClassException{
        System.out.println("agentArgs:"+agentArgs);
        inst.addTransformer(new ClassFileTransformer() {
            @Override
            public byte[] transform(ClassLoader classLoader, String classname, 
                    Class<?> classbeing, ProtectionDomain protectionDomain,
                    byte[] classfilebuffer)
                    throws IllegalClassFormatException {
            if(classname.equals("asmtimer.Account")){
                System.out.println("meet asmtimer/Account");
                ClassReader cr=new ClassReader(classfilebuffer);
                ClassWriter cw=new ClassWriter(ClassWriter.COMPUTE_MAXS|
                        ClassWriter.COMPUTE_FRAMES);
                TimeStatClassAdapter adapter=new TimeStatClassAdapter(cw);
                cr.accept(adapter, ClassReader.SKIP_DEBUG);
                return cw.toByteArray();
            }else{
                System.out.println(classname);
                return classfilebuffer;
            }
            }
        },true);
    }
}

TimeStatClassAdapter.class

import org.objectweb.asm.ClassAdapter;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;

public class TimeStatClassAdapter extends ClassAdapter {

    public TimeStatClassAdapter(ClassVisitor cv){
        super(cv);
    }

    public MethodVisitor visitMethod(final int access,final String name,
            final String desc,final String signature,
            final String[] exceptions){
        MethodVisitor mv=cv.visitMethod(access, name, desc, signature, exceptions);
        MethodVisitor wrappedMv=mv;
        if(mv!=null){
            if(name.equals("operation")){
                wrappedMv=new TimeStatMethodAdapter(mv);
            }
        }
        return wrappedMv;
    }
}

TimeStatMethodAdapter.class

import org.objectweb.asm.MethodAdapter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public class TimeStatMethodAdapter extends MethodAdapter{

    public TimeStatMethodAdapter(MethodVisitor ss){
        super(ss);
    }

    @Override
    public void visitCode(){
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "asmtimer/TimeStat", 
                "start", "()V");
        super.visitCode();
    }

    @Override
    public void visitInsn(int opcode){
        if(opcode>=Opcodes.IRETURN&&opcode<=Opcodes.RETURN){
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, "asmtimer/TimeStat", 
                    "end", "()V");
        }
        mv.visitInsn(opcode);
    }[enter image description here][1]
}

MAINFEST.MF

Manfest-Version: 1.0 

Premain-Class: asmtimer.PreMainTraceAgent

Can-Redine-Classes: true

Can-Retransform-Classes: true

控制台错误信息:

enter image description here

jvm参数: enter image description here

Jar文件有&#34; C:\ jat.jar&#34;,如果你有任何空闲来帮助我,请保存你的话!!!

0 个答案:

没有答案