package ch.usi.dag.disl.test.agent;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;

import ch.usi.dag.disl.DiSL;

public class Transformer implements ClassFileTransformer {
    
    @Override
    public byte [] transform (ClassLoader loader, String className,
        Class <?> classBeingRedefined, ProtectionDomain protectionDomain,
        byte [] classfileBuffer)
        throws IllegalClassFormatException {
    	
    	byte[] instrumentedClass = null;
    	
    	try {
    	
	    	ClassReader cr = new ClassReader(classfileBuffer);
			ClassNode classNode = new ClassNode();
			cr.accept(classNode, ClassReader.SKIP_DEBUG | ClassReader.EXPAND_FRAMES);
	    	
			DiSL disl = new DiSL();
			disl.initialize();
			disl.instrument(classNode);
	
			
			ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
			classNode.accept(cw);
	
			instrumentedClass = cw.toByteArray();
			
			// TODO Remove the two-round computing
			cr = new ClassReader(instrumentedClass);
			cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
			cr.accept(cw, ClassReader.SKIP_DEBUG | ClassReader.EXPAND_FRAMES);
			instrumentedClass = cw.toByteArray();
			
			/* now does for every user class - unusable
			 * unable to track wheter is modified or not
			 */
			/*
			// print class 
			TraceClassVisitor tcv = new TraceClassVisitor(new PrintWriter(System.out));
			classNode.accept(tcv);
			/**/
			
			/*
			// output class
			try {
				File f = new File("ModifiedClass.class");
				FileOutputStream fos = new FileOutputStream(f);
				fos.write(cw.toByteArray());
				fos.flush();
				fos.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			/**/
			
    	} catch(Throwable e) {
    		e.printStackTrace();
    	}
    	
        return instrumentedClass;
    }

}

@@ -954,17 +954,16 @@
 Node);%0A%09
-%0A
 %09%09%09%0A%09%09%09C
@@ -1097,262 +1097,8 @@
 %09%09%09%0A
-%09%09%09// TODO Remove the two-round computing%0A%09%09%09cr = new ClassReader(instrumentedClass);%0A%09%09%09cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);%0A%09%09%09cr.accept(cw, ClassReader.SKIP_DEBUG %7C ClassReader.EXPAND_FRAMES);%0A%09%09%09instrumentedClass = cw.toByteArray();%0A%09%09%09%0A
 %09%09%09/

Remove the second-round computing