package hu.advancedweb.scott.instrumentation.transformation;

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

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;

import hu.advancedweb.scott.instrumentation.transformation.param.DiscoveryClassVisitor;
import hu.advancedweb.scott.instrumentation.transformation.param.TransformationParameters;


/**
 * Transform classes for detailed failure test reports.
 * 
 * @author David Csakvari
 */
public class TestClassTransformer implements ClassFileTransformer {

	public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
		TransformationParameters transformationParameters = calculateTransformationParameters(classfileBuffer);
	    return transform(classfileBuffer, transformationParameters);
	}

	private TransformationParameters calculateTransformationParameters(byte[] classfileBuffer) {
		TransformationParameters.Builder transformationParameters = new TransformationParameters.Builder();
	    ClassVisitor discoveryClassVisitor = new DiscoveryClassVisitor(transformationParameters);
	    new ClassReader(classfileBuffer).accept(discoveryClassVisitor, 0);
		return transformationParameters.build();
	}
	
	private byte[] transform(byte[] classfileBuffer, TransformationParameters transformationParameters) {
		ClassReader classReader = new ClassReader(classfileBuffer);
	    ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_FRAMES);
	    ClassVisitor localVariableStateEmitterTestClassVisitor = new LocalVariableStateEmitterTestClassVisitor(classWriter, transformationParameters);
	    classReader.accept(localVariableStateEmitterTestClassVisitor, 0);
	    return classWriter.toByteArray();
	}

}

@@ -851,16 +851,25 @@
 ption %7B%0A
+%09%09try %7B%0A%09
 %09%09Transf
@@ -962,24 +962,25 @@
 ileBuffer);%0A
+%09
 %09    return 
@@ -1033,16 +1033,166 @@
 eters);%0A
+%09%09%7D catch (Exception e) %7B%0A%09%09%09System.err.println(%22Scott: test instrumentation failed for %22 + className + %22!%22);%0A%09%09%09e.printStackTrace();%0A%09%09%09throw e;%0A%09%09%7D%0A
 %09%7D%0A%0A%09pri
@@ -1760,28 +1760,25 @@
 leBuffer);%0A%09
-    
+%09
 ClassWriter 
@@ -1846,28 +1846,25 @@
 E_FRAMES);%0A%09
-    
+%09
 ClassVisitor
@@ -1995,20 +1995,17 @@
 ters);%0A%09
-    
+%09
 classRea
@@ -2063,20 +2063,17 @@
 r, 0);%0A%09
-    
+%09
 return c

Print errors, when the instrumentation fails. Fix issue #16.