use of javassist.CtClass in project activejdbc by javalite.
the class Instrumentation method instrument.
public void instrument() {
if (outputDirectory == null) {
throw new RuntimeException("Property 'outputDirectory' must be provided");
}
try {
System.out.println("**************************** START INSTRUMENTATION ****************************");
System.out.println("Directory: " + outputDirectory);
InstrumentationModelFinder mf = new InstrumentationModelFinder();
File target = new File(outputDirectory);
mf.processDirectoryPath(target);
ModelInstrumentation mi = new ModelInstrumentation();
for (CtClass clazz : mf.getModels()) {
byte[] bytecode = mi.instrument(clazz);
String fileName = getFullFilePath(clazz);
FileOutputStream fout = new FileOutputStream(fileName);
fout.write(bytecode);
fout.flush();
fout.close();
System.out.println("Instrumented class: " + fileName);
}
generateModelsFile(mf.getModels(), target);
System.out.println("**************************** END INSTRUMENTATION ****************************");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of javassist.CtClass in project activejdbc by javalite.
the class JavaAgent method premain.
@SuppressWarnings("unchecked")
public static void premain(String args, java.lang.instrument.Instrumentation inst) {
Instrumentation.log("You are using dynamic instrumentation...");
try {
modelFinder = new InstrumentationModelFinder();
modelInstrumentation = new ModelInstrumentation();
// calling this via reflection because we do not want AJ dependency on instrumentation project
Class finderClass = Class.forName("org.javalite.activejdbc.ModelFinder");
modelFound = finderClass.getDeclaredMethod("modelFound", String.class);
} catch (Exception e) {
throw new InstrumentationException(e);
}
inst.addTransformer(new ClassFileTransformer() {
@Override
public synchronized byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
try {
CtClass clazz = modelFinder.getClazz(className.replace('/', '.'));
if (modelFinder.isModel(clazz)) {
if (!loaders.contains(loader) && loader instanceof URLClassLoader) {
scanLoader(loader);
loaders.add(loader);
List<CtClass> models = modelFinder.getModels();
for (CtClass ctClass : models) {
modelFound.invoke(null, ctClass.getName());
}
}
byte[] bytecode = modelInstrumentation.instrument(clazz);
Instrumentation.log("Instrumented model: " + clazz.getName());
return bytecode;
} else {
return null;
}
} catch (Exception e) {
throw new InstrumentationException(e);
}
}
});
}
use of javassist.CtClass in project powermock by powermock.
the class InstrumentMockTransformerTest method should_ignore_call_to_synthetic_field_when_instrument_call_to_method.
@Test
public void should_ignore_call_to_synthetic_field_when_instrument_call_to_method() throws Throwable {
final ClassPool classPool = new ClassPool(true);
CtClass ctClass = prepareClassesForFieldTests(classPool);
mockTransformerChain.transform(wrap(ctClass));
runTestWithNewClassLoader(classPool, ShouldIgnoreCallToSyntheticField.class.getName());
}
use of javassist.CtClass in project powermock by powermock.
the class InstrumentMockTransformerTest method prepareClassesForFieldTests.
private CtClass prepareClassesForFieldTests(ClassPool classPool) throws NotFoundException, CannotCompileException {
CtClass ctClass = classPool.getCtClass(SuperClassWithObjectMethod.class.getName());
addSyntheticField(classPool, ctClass);
insertCallSyntheticField(ctClass);
return ctClass;
}
use of javassist.CtClass in project powermock by powermock.
the class MethodsMockTransformerTest method should_ignore_call_to_synthetic_non_bridge_methods.
@Test
public void should_ignore_call_to_synthetic_non_bridge_methods() throws Throwable {
final ClassPool classPool = new ClassPool(true);
CtClass ctClass = prepareClassesForTest(classPool, "syntheticMethodIsCalled = true;");
final Class<?> clazz = loadWithMockClassLoader(ctClass);
final Object instance = WhiteboxImpl.newInstance(clazz);
clazz.getMethod("doSomething", Object.class).invoke(instance, new Object());
assertThat(methodCalls()).isNot(registered().forMethod(SYNTHETIC_METHOD_NAME));
assertThat(WhiteboxImpl.getInternalState(clazz, "syntheticMethodIsCalled")).isEqualTo(true);
}
Aggregations