use of javassist.bytecode.ConstPool in project hibernate-orm by hibernate.
the class BulkAccessorFactory method addDefaultConstructor.
/**
* Declares a constructor that takes no parameter.
*
* @param classfile The class descriptor
*
* @throws CannotCompileException Indicates trouble with the underlying Javassist calls
*/
private void addDefaultConstructor(ClassFile classfile) throws CannotCompileException {
final ConstPool constPool = classfile.getConstPool();
final String constructorSignature = "()V";
final MethodInfo constructorMethodInfo = new MethodInfo(constPool, MethodInfo.nameInit, constructorSignature);
final Bytecode code = new Bytecode(constPool, 0, 1);
// aload_0
code.addAload(0);
// invokespecial
code.addInvokespecial(BulkAccessor.class.getName(), MethodInfo.nameInit, constructorSignature);
// return
code.addOpcode(Opcode.RETURN);
constructorMethodInfo.setCodeAttribute(code.toCodeAttribute());
constructorMethodInfo.setAccessFlags(AccessFlag.PUBLIC);
classfile.addMethod(constructorMethodInfo);
}
use of javassist.bytecode.ConstPool 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.bytecode.ConstPool in project pinpoint by naver.
the class JavassistEngineTest method getTransformByteCode.
public byte[] getTransformByteCode() {
try {
final ClassPool pool = new ClassPool(true);
final CtClass ctClass = pool.get(mock);
final ConstPool constPool = ctClass.getClassFile2().getConstPool();
MethodInfo info = new MethodInfo(constPool, "transformMethod", "()V");
final CtMethod newMethod = CtMethod.make(info, ctClass);
ctClass.addMethod(newMethod);
return ctClass.toBytecode();
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
Aggregations