use of javassist.bytecode.BadBytecode in project fakereplace by fakereplace.
the class WeldClassTransformer method transform.
@Override
public boolean transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, ClassFile file, Set<Class<?>> classesToRetransform, ChangedClassImpl changedClass, Set<MethodInfo> modifiedMethods, boolean replaceable) throws IllegalClassFormatException, BadBytecode {
// Hack up the proxy factory so it stores the proxy ClassFile. We need this to regenerate proxies.
if (file.getName().equals(ORG_JBOSS_WELD_BEAN_PROXY_PROXY_FACTORY)) {
for (final MethodInfo method : (List<MethodInfo>) file.getMethods()) {
if (method.getName().equals("createProxyClass")) {
modifiedMethods.add(method);
final VirtualToStaticManipulator virtualToStaticManipulator = new VirtualToStaticManipulator();
virtualToStaticManipulator.replaceVirtualMethodInvokationWithStatic(ClassLoader.class.getName(), WELD_PROXY_CLASS_LOADING_DELEGATE, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;", "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;", loader);
virtualToStaticManipulator.replaceVirtualMethodInvokationWithStatic("org.jboss.weld.util.bytecode.ClassFileUtils", WELD_PROXY_CLASS_LOADING_DELEGATE, "toClass", "(Lorg/jboss/classfilewriter/ClassFile;Ljava/lang/ClassLoader;Ljava/security/ProtectionDomain;)Ljava/lang/Class;", "(Lorg/jboss/classfilewriter/ClassFile;Ljava/lang/ClassLoader;Ljava/security/ProtectionDomain;)Ljava/lang/Class;", loader);
virtualToStaticManipulator.transformClass(file, loader, true, modifiedMethods, replaceable);
return true;
} else if (method.getName().equals("<init>")) {
modifiedMethods.add(method);
Integer beanArgument = null;
int count = 1;
for (final String paramType : DescriptorUtils.descriptorStringToParameterArray(method.getDescriptor())) {
if (paramType.equals("Ljavax/enterprise/inject/spi/Bean")) {
beanArgument = count;
break;
} else if (paramType.equals("D") || paramType.equals("J")) {
count += 2;
} else {
count++;
}
}
if (beanArgument == null) {
log.error("Constructor org.jboss.weld.bean.proxy.ProxyFactory.<init>" + method.getDescriptor() + " does not have a bean parameter, proxies produced by this factory will not be reloadable");
continue;
}
// similar to other tracked instances
// but we need a strong ref
Bytecode code = new Bytecode(file.getConstPool());
code.addAload(0);
code.addAload(beanArgument);
code.addInvokestatic(WeldClassChangeAware.class.getName(), "addProxyFactory", "(Ljava/lang/Object;Ljava/lang/Object;)V");
CodeIterator it = method.getCodeAttribute().iterator();
it.skipConstructor();
it.insert(code.get());
}
}
}
return false;
}
use of javassist.bytecode.BadBytecode in project fakereplace by fakereplace.
the class WildflyClassTransformer method transform.
@Override
public boolean transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, ClassFile file, Set<Class<?>> classesToRetransform, ChangedClassImpl changedClass, Set<MethodInfo> modifiedMethods, boolean replaceable) throws IllegalClassFormatException, BadBytecode, DuplicateMemberException {
if (!file.getName().equals("org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService")) {
return false;
}
for (MethodInfo method : (List<MethodInfo>) file.getMethods()) {
if (method.getName().equals("createServletConfig")) {
CodeAttribute code = method.getCodeAttribute();
code.setMaxStack(code.getMaxStack() + 1);
CodeIterator it = code.iterator();
modifiedMethods.add(method);
while (it.hasNext()) {
int pos = it.next();
int inst = it.byteAt(pos);
if (inst == CodeAttribute.ARETURN) {
Bytecode b = new Bytecode(method.getConstPool());
b.addGetstatic("org.fakereplace.integration.wildfly.autoupdate.WebUpdateHandlerWrapper", "INSTANCE", "Lio/undertow/server/HandlerWrapper;");
b.addInvokevirtual("io.undertow.servlet.api.DeploymentInfo", "addInnerHandlerChainWrapper", "(Lio/undertow/server/HandlerWrapper;)Lio/undertow/servlet/api/DeploymentInfo;");
it.insert(pos, b.get());
}
}
}
}
return true;
}
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, boolean replaceable) 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;
}
}
use of javassist.bytecode.BadBytecode in project javaparser by javaparser.
the class JavassistUtils method getMethodUsage.
static Optional<MethodUsage> getMethodUsage(CtClass ctClass, String name, List<Type> argumentsTypes, TypeSolver typeSolver, Context invokationContext) {
// TODO avoid bridge and synthetic methods
for (CtMethod method : ctClass.getDeclaredMethods()) {
if (method.getName().equals(name)) {
// TODO check typeParametersValues
MethodUsage methodUsage = new MethodUsage(new JavassistMethodDeclaration(method, typeSolver));
if (argumentsTypes.size() < methodUsage.getNoParams()) {
// this method cannot be a good candidate (except if variadic ?)
continue;
}
try {
if (method.getGenericSignature() != null) {
SignatureAttribute.MethodSignature classSignature = SignatureAttribute.toMethodSignature(method.getGenericSignature());
List<Type> parametersOfReturnType = parseTypeParameters(classSignature.getReturnType().toString(), typeSolver, invokationContext);
Type newReturnType = methodUsage.returnType();
// consume one parametersOfReturnType at the time
if (!(newReturnType instanceof VoidType)) {
newReturnType = newReturnType.asReferenceType().transformTypeParameters(tp -> parametersOfReturnType.remove(0));
}
methodUsage = methodUsage.replaceReturnType(newReturnType);
}
return Optional.of(methodUsage);
} catch (BadBytecode e) {
throw new RuntimeException(e);
}
}
}
try {
CtClass superClass = ctClass.getSuperclass();
if (superClass != null) {
Optional<MethodUsage> ref = new JavassistClassDeclaration(superClass, typeSolver).solveMethodAsUsage(name, argumentsTypes, typeSolver, invokationContext, null);
if (ref.isPresent()) {
return ref;
}
}
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
try {
for (CtClass interfaze : ctClass.getInterfaces()) {
Optional<MethodUsage> ref = new JavassistInterfaceDeclaration(interfaze, typeSolver).solveMethodAsUsage(name, argumentsTypes, typeSolver, invokationContext, null);
if (ref.isPresent()) {
return ref;
}
}
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
return Optional.empty();
}
use of javassist.bytecode.BadBytecode in project UniverseCore by EB-wilson.
the class Analyzer method mergeRet.
private void mergeRet(IntQueue queue, CodeIterator iter, int pos, Frame frame, Subroutine subroutine) throws BadBytecode {
if (subroutine == null)
throw new BadBytecode("Ret on no subroutine! [pos = " + pos + "]");
for (int caller : subroutine.callers()) {
int returnLoc = getNext(iter, caller, pos);
boolean changed = false;
Frame old = frames[returnLoc];
if (old == null) {
old = frames[returnLoc] = frame.copyStack();
changed = true;
} else {
changed = old.mergeStack(frame);
}
for (int index : subroutine.accessed()) {
Type oldType = old.getLocal(index);
Type newType = frame.getLocal(index);
if (oldType != newType) {
old.setLocal(index, newType);
changed = true;
}
}
if (!old.isRetMerged()) {
old.setRetMerged(true);
changed = true;
}
if (changed && old.isJsrMerged())
queue.add(returnLoc);
}
}
Aggregations