use of javassist.bytecode.BadBytecode 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.bytecode.BadBytecode in project tutorials by eugenp.
the class JavasisstUnitTest method givenLoadedClass_whenAddConstructorToClass_shouldCreateClassWithConstructor.
@Test
public void givenLoadedClass_whenAddConstructorToClass_shouldCreateClassWithConstructor() throws NotFoundException, CannotCompileException, BadBytecode {
// given
ClassFile cf = ClassPool.getDefault().get("com.baeldung.javasisst.Point").getClassFile();
Bytecode code = new Bytecode(cf.getConstPool());
code.addAload(0);
code.addInvokespecial("java/lang/Object", MethodInfo.nameInit, "()V");
code.addReturn(null);
// when
MethodInfo minfo = new MethodInfo(cf.getConstPool(), MethodInfo.nameInit, "()V");
minfo.setCodeAttribute(code.toCodeAttribute());
cf.addMethod(minfo);
// then
CodeIterator ci = code.toCodeAttribute().iterator();
List<String> operations = new LinkedList<>();
while (ci.hasNext()) {
int index = ci.next();
int op = ci.byteAt(index);
operations.add(Mnemonic.OPCODE[op]);
}
assertEquals(operations, Arrays.asList("aload_0", "invokespecial", "return"));
}
use of javassist.bytecode.BadBytecode in project fakereplace by fakereplace.
the class ClassInfoTransformer method transform.
@Override
public boolean transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, ClassFile file, Set<Class<?>> classesToRetransform, ChangedClassImpl changedClass, Set<MethodInfo> modifiedMethods) throws IllegalClassFormatException, BadBytecode, DuplicateMemberException {
if (className.equals("com/sun/beans/introspect/ClassInfo")) {
file.addInterface(Runnable.class.getName());
MethodInfo run = new MethodInfo(file.getConstPool(), "run", "()V");
run.setAccessFlags(AccessFlag.PUBLIC);
Bytecode b = new Bytecode(file.getConstPool(), 1, 1);
b.addGetstatic(file.getName(), "CACHE", "Lcom/sun/beans/util/Cache;");
b.addInvokevirtual("com/sun/beans/util/Cache", "clear", "()V");
b.add(Opcode.RETURN);
run.setCodeAttribute(b.toCodeAttribute());
file.addMethod(run);
MethodInfo m = file.getMethod("<init>");
CodeIterator iterator = m.getCodeAttribute().iterator();
int pos = 0;
while (iterator.hasNext()) {
pos = iterator.next();
}
b = new Bytecode(file.getConstPool(), 1, 0);
b.add(Bytecode.ALOAD_0);
b.addPutstatic(ClassInfoTransformer.class.getName(), "clearAction", "Ljava/lang/Runnable;");
iterator.insert(pos, b.get());
m.getCodeAttribute().computeMaxStack();
return true;
}
return false;
}
use of javassist.bytecode.BadBytecode in project fakereplace by fakereplace.
the class ResteasyTransformer method transform.
@Override
public boolean transform(final ClassLoader loader, final String className, final Class<?> classBeingRedefined, final ProtectionDomain protectionDomain, final ClassFile file, Set<Class<?>> classesToRetransform, ChangedClassImpl changedClass, Set<MethodInfo> modifiedMethods) throws IllegalClassFormatException, BadBytecode {
try {
if (file.getName().equals(ResteasyExtension.FILTER_DISPATCHER)) {
FieldInfo field = new FieldInfo(file.getConstPool(), FIELD_NAME, FILTER_FIELD_TYPE);
field.setAccessFlags(Modifier.PUBLIC);
file.addField(field);
field = new FieldInfo(file.getConstPool(), PARAMETER_FIELD_NAME, SET_TYPE);
field.setAccessFlags(Modifier.PUBLIC);
file.addField(field);
for (final MethodInfo method : (List<MethodInfo>) file.getMethods()) {
if (method.getName().equals("init") && method.getDescriptor().equals("(Ljavax/servlet/FilterConfig;)V")) {
method.setAccessFlags(method.getAccessFlags() | AccessFlag.SYNCHRONIZED);
modifiedMethods.add(method);
final Bytecode b = new Bytecode(file.getConstPool());
b.addAload(0);
b.addNew(RESTEASY_FILTER_CONFIG);
b.add(Opcode.DUP);
b.addAload(1);
b.addInvokespecial(RESTEASY_FILTER_CONFIG, "<init>", "(Ljavax/servlet/FilterConfig;)V");
b.addPutfield(ResteasyExtension.FILTER_DISPATCHER, FIELD_NAME, FILTER_FIELD_TYPE);
b.addAload(1);
b.addInvokeinterface("javax/servlet/FilterConfig", "getServletContext", "()Ljavax/servlet/ServletContext;", 1);
b.addAload(0);
b.addGetfield(ResteasyExtension.FILTER_DISPATCHER, PARAMETER_FIELD_NAME, SET_TYPE);
b.addInvokestatic(CONTEXT_PARAMS, "init", INIT_METHOD_DESC);
b.addAload(0);
b.add(Opcode.SWAP);
b.addPutfield(ResteasyExtension.FILTER_DISPATCHER, PARAMETER_FIELD_NAME, SET_TYPE);
method.getCodeAttribute().iterator().insert(b.get());
method.getCodeAttribute().computeMaxStack();
} else if (method.getName().equals("<init>")) {
// no idea why this is needed
method.getCodeAttribute().setMaxStack(1);
} else if (method.getName().equals("getDispatcher")) {
method.setAccessFlags(method.getAccessFlags() | AccessFlag.SYNCHRONIZED);
modifiedMethods.add(method);
}
}
return true;
} else if (file.getName().equals(ResteasyExtension.SERVLET_DISPATCHER)) {
FieldInfo field = new FieldInfo(file.getConstPool(), FIELD_NAME, SERVLET_FIELD_TYPE);
field.setAccessFlags(Modifier.PUBLIC);
file.addField(field);
field = new FieldInfo(file.getConstPool(), PARAMETER_FIELD_NAME, SET_TYPE);
field.setAccessFlags(Modifier.PUBLIC);
file.addField(field);
for (final MethodInfo method : (List<MethodInfo>) file.getMethods()) {
if (method.getName().equals("init") && method.getDescriptor().equals("(Ljavax/servlet/ServletConfig;)V")) {
method.setAccessFlags(method.getAccessFlags() | AccessFlag.SYNCHRONIZED);
modifiedMethods.add(method);
final Bytecode b = new Bytecode(file.getConstPool());
b.addAload(0);
b.addNew(RESTEASY_SERVLET_CONFIG);
b.add(Opcode.DUP);
b.addAload(1);
b.addInvokespecial(RESTEASY_SERVLET_CONFIG, "<init>", "(Ljavax/servlet/ServletConfig;)V");
b.addPutfield(ResteasyExtension.SERVLET_DISPATCHER, FIELD_NAME, SERVLET_FIELD_TYPE);
b.addAload(1);
b.addInvokeinterface("javax/servlet/ServletConfig", "getServletContext", "()Ljavax/servlet/ServletContext;", 1);
b.addAload(0);
b.addGetfield(ResteasyExtension.SERVLET_DISPATCHER, PARAMETER_FIELD_NAME, SET_TYPE);
b.addInvokestatic(CONTEXT_PARAMS, "init", INIT_METHOD_DESC);
b.addAload(0);
b.add(Opcode.SWAP);
b.addPutfield(ResteasyExtension.SERVLET_DISPATCHER, PARAMETER_FIELD_NAME, SET_TYPE);
method.getCodeAttribute().iterator().insert(b.get());
method.getCodeAttribute().computeMaxStack();
} else if (method.getName().equals("<init>")) {
method.getCodeAttribute().setMaxStack(1);
} else if (method.getName().equals("getDispatcher")) {
method.setAccessFlags(method.getAccessFlags() | AccessFlag.SYNCHRONIZED);
modifiedMethods.add(method);
}
}
return true;
}
} catch (DuplicateMemberException e) {
throw new RuntimeException(e);
}
return false;
}
use of javassist.bytecode.BadBytecode in project fakereplace by fakereplace.
the class WildflyHibernate5ClassTransformer method transform.
@Override
public boolean transform(final ClassLoader loader, final String className, final Class<?> classBeingRedefined, final ProtectionDomain protectionDomain, final ClassFile file, Set<Class<?>> classesToRetransform, ChangedClassImpl changedClass, Set<MethodInfo> modifiedMethods) throws IllegalClassFormatException, BadBytecode, DuplicateMemberException {
if (file.getName().equals("org.jboss.as.jpa.service.PersistenceUnitServiceImpl")) {
for (MethodInfo method : (List<MethodInfo>) file.getMethods()) {
if (method.getName().equals("getEntityManagerFactory")) {
modifiedMethods.add(method);
// need to save the method params so we can re-use them when we re-create our EMF
Bytecode s = new Bytecode(file.getConstPool());
// we need to interceptor the return value
// and add in our own bytecode fragment.
// first lets create our proxy creation code
final Bytecode b = new Bytecode(file.getConstPool());
b.addNew(PROXY_NAME);
b.add(Opcode.DUP);
b.addAload(0);
b.addInvokespecial(PROXY_NAME, "<init>", "(Lorg/jipijapa/plugin/spi/PersistenceUnitService;)V");
insertBeforeReturn(method, s, b);
}
}
file.addInterface(HackPersistenceUnitService.class.getName());
Bytecode bc = new Bytecode(file.getConstPool(), 1, 1);
bc.addAload(0);
bc.addGetfield("org.jboss.as.jpa.service.PersistenceUnitServiceImpl", "entityManagerFactory", "Ljavax/persistence/EntityManagerFactory;");
bc.addOpcode(Bytecode.ARETURN);
MethodInfo methodInfo = new MethodInfo(file.getConstPool(), "emf", "()Ljavax/persistence/EntityManagerFactory;");
methodInfo.setAccessFlags(AccessFlag.PUBLIC);
methodInfo.setCodeAttribute(bc.toCodeAttribute());
file.addMethod(methodInfo);
modifiedMethods.add(methodInfo);
return true;
} else {
return false;
}
}
Aggregations