use of javassist.NotFoundException in project pinpoint by naver.
the class JavassistMethod method insertCatch.
private void insertCatch(int from, String src, CtClass exceptionType, String exceptionName) throws CannotCompileException {
CtClass cc = behavior.getDeclaringClass();
ConstPool cp = behavior.getMethodInfo().getConstPool();
CodeAttribute ca = behavior.getMethodInfo().getCodeAttribute();
CodeIterator iterator = ca.iterator();
Bytecode b = new Bytecode(cp, ca.getMaxStack(), ca.getMaxLocals());
b.setStackDepth(1);
Javac jv = new Javac(b, cc);
try {
jv.recordParams(behavior.getParameterTypes(), Modifier.isStatic(getModifiers()));
jv.recordLocalVariables(ca, from);
int var = jv.recordVariable(exceptionType, exceptionName);
b.addAstore(var);
jv.compileStmnt(src);
int stack = b.getMaxStack();
int locals = b.getMaxLocals();
if (stack > ca.getMaxStack())
ca.setMaxStack(stack);
if (locals > ca.getMaxLocals())
ca.setMaxLocals(locals);
int len = iterator.getCodeLength();
int pos = iterator.append(b.get());
ca.getExceptionTable().add(from, len, len, cp.addClassInfo(exceptionType));
iterator.append(b.getExceptionTable(), pos);
behavior.getMethodInfo().rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
} catch (NotFoundException e) {
throw new CannotCompileException(e);
} catch (CompileError e) {
throw new CannotCompileException(e);
} catch (BadBytecode e) {
throw new CannotCompileException(e);
}
}
use of javassist.NotFoundException in project pinpoint by naver.
the class JavassistMethod method insertBeforeConstructor.
private int insertBeforeConstructor(int pos, String src) throws CannotCompileException {
CtClass cc = behavior.getDeclaringClass();
CodeAttribute ca = behavior.getMethodInfo().getCodeAttribute();
CodeIterator iterator = ca.iterator();
Bytecode b = new Bytecode(behavior.getMethodInfo().getConstPool(), ca.getMaxStack(), ca.getMaxLocals());
b.setStackDepth(ca.getMaxStack());
Javac jv = new Javac(b, cc);
try {
jv.recordParams(behavior.getParameterTypes(), false);
jv.recordLocalVariables(ca, 0);
jv.compileStmnt(src);
ca.setMaxStack(b.getMaxStack());
ca.setMaxLocals(b.getMaxLocals());
iterator.skipConstructor();
if (pos != -1) {
iterator.insertEx(pos, b.get());
} else {
pos = iterator.insertEx(b.get());
}
iterator.insert(b.getExceptionTable(), pos);
behavior.getMethodInfo().rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
return pos + b.length();
} catch (NotFoundException e) {
throw new CannotCompileException(e);
} catch (CompileError e) {
throw new CannotCompileException(e);
} catch (BadBytecode e) {
throw new CannotCompileException(e);
}
}
use of javassist.NotFoundException in project pinpoint by naver.
the class JavassistMethod method insertBeforeMethod.
private int insertBeforeMethod(int pos, String src) throws CannotCompileException {
CtClass cc = behavior.getDeclaringClass();
CodeAttribute ca = behavior.getMethodInfo().getCodeAttribute();
if (ca == null)
throw new CannotCompileException("no method body");
CodeIterator iterator = ca.iterator();
Javac jv = new Javac(cc);
try {
int nvars = jv.recordParams(behavior.getParameterTypes(), Modifier.isStatic(getModifiers()));
jv.recordParamNames(ca, nvars);
jv.recordLocalVariables(ca, 0);
jv.recordType(getReturnType0());
jv.compileStmnt(src);
Bytecode b = jv.getBytecode();
int stack = b.getMaxStack();
int locals = b.getMaxLocals();
if (stack > ca.getMaxStack())
ca.setMaxStack(stack);
if (locals > ca.getMaxLocals())
ca.setMaxLocals(locals);
if (pos != -1) {
iterator.insertEx(pos, b.get());
} else {
pos = iterator.insertEx(b.get());
}
iterator.insert(b.getExceptionTable(), pos);
behavior.getMethodInfo().rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
return pos + b.length();
} catch (NotFoundException e) {
throw new CannotCompileException(e);
} catch (CompileError e) {
throw new CannotCompileException(e);
} catch (BadBytecode e) {
throw new CannotCompileException(e);
}
}
use of javassist.NotFoundException in project pinpoint by naver.
the class InterceptorTest method interceptor.
// @Deprecated
@Test
public void interceptor() throws NotFoundException, CannotCompileException, IllegalAccessException, InstantiationException, IOException, ClassNotFoundException, NoSuchMethodException {
AroundInterceptor aroundInterceptor = new AroundInterceptor() {
@Override
public void before(Object target, Object[] args) {
logger.info("BEFORE target:" + target + " args:" + Arrays.toString(args));
}
@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
logger.info("AFTER target: " + target + " args:" + Arrays.toString(args) + " result:" + result + " throwable:" + throwable);
}
};
int interceptorId = INTERCEPTOR_REGISTRY_ADAPTOR.addInterceptor(aroundInterceptor);
final ClassPool classPool = new ClassPool(true);
CtClass throwable = classPool.get(Throwable.class.getName());
CtClass ctClass = classPool.get("com.navercorp.pinpoint.profiler.interceptor.JavaAssistTestObject");
final CtMethod hello = ctClass.getMethod("hello", "(Ljava/lang/String;)Ljava/lang/String;");
logger.debug("longName:{}", hello.getLongName());
logger.debug("name:{}", hello.getName());
String interceptorClassName = AroundInterceptor.class.getName();
CtClass interceptor = classPool.get(interceptorClassName);
hello.addLocalVariable("interceptor", interceptor);
CtClass object = classPool.get(Object.class.getName());
hello.addLocalVariable("result", object);
// hello.insertBefore("{ System.out.println(\"BEFORE\"); }");
hello.insertBefore("{" + "interceptor = (" + interceptorClassName + ") " + InterceptorRegistry.class.getName() + ".getInterceptor(" + interceptorId + ");" + "interceptor.before(this, $args);" + "}");
// hello.addCatch("{" +
//// " interceptor.after(ctx);"+
//// " AroundInterceptor a = (AroundInterceptor) " + InterceptorRegistry.class.getName() + ".getStaticInterceptor(\"a\");"+
// " throw $e;" +
// "}", throwable);
// hello.insertAfter("{" +
// "interceptor.after(this, $args, ($w)$_, null); " +
// "}");
// hello.setBody(generatedAroundInterceptor("TestObject", "hello"));
// hello.setBody("{ System.out.println(\"ddd\"); }", ClassMap map );
// hello.insertBefore(" System.out.println(\" BEFORE + \");");
// hello.insertAfter(" System.out.println($_);");
// hello.insertAfter(" System.out.println($r);");
// hello.insertAfter(" System.out.println($w);");
// hello.insertAfter(" System.out.println($sig);");
// hello.insertAfter(" System.out.println($type);");
// hello.insertAfter(" System.out.println($class);");
// hello.instrument(new ExprEditor() {
// public void edit(MethodCall m)
// throws CannotCompileException
// {
// try {
// System.out.println("method call" + m.getMethod().getName());
// } catch (NotFoundException e) {
// e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
// }
// String code = generatedAroundInterceptor("TestObject", "hello");
// m.replace(code);
// }
// });
// hello.addCatch("System.out.println(\"catch\"); throw $e;", throwable);
// hello.setName("__hello");
// CtMethod method = CtNewMethod.make("public void hello() { try {__hello(); } catch(Throwable th){throw th;}}", ctClass);
// CtMethod method = CtNewMethod.make("public void hello() { System.out.println(\"ddd\"); } catch(Throwable th){throw th;}}", ctClass);
// ctClass.addMethod(method);
// ctClass.freeze();
// ctClass.writeFile("./debug");
// ctClass.debugWriteFile("./debug");
Loader loader = LoaderUtils.createLoader(classPool);
loader.delegateLoadingOf("com.navercorp.pinpoint.bootstrap.");
Class aClass = loader.loadClass(ctClass.getName());
Object testObject = aClass.newInstance();
Method helloMethod = testObject.getClass().getDeclaredMethod("hello", String.class);
try {
helloMethod.invoke(testObject, "hello~~");
} catch (Exception e) {
Assert.fail(e.getMessage());
}
// o.hello();
}
use of javassist.NotFoundException in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class FormsHelperStubber method createStub.
protected static void createStub() {
ClassPool classPool = ClassPool.getDefault();
CtClass ctClass;
try {
ctClass = classPool.get(CLASS_NAME);
//indicates the class has already been stubbed and loaded
if (ctClass.isFrozen()) {
return;
}
//to remove any dependencies on impl classes.
for (CtMethod ctMethod : ctClass.getDeclaredMethods()) {
ctMethod.setBody(null);
}
//remove the error causing static field declaration
ctClass.removeField(ctClass.getDeclaredField(ERROR_FIELD));
//remove the static initilizer block calling new on impl class.
ctClass.removeConstructor(ctClass.getClassInitializer());
//load the stubbed class
ctClass.toClass();
} catch (NotFoundException e) {
e.printStackTrace();
} catch (CannotCompileException e) {
e.printStackTrace();
}
}
Aggregations