use of org.objectweb.asm.ClassWriter in project MinecraftForge by MinecraftForge.
the class SideTransformer method transform.
@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
if (bytes == null) {
return null;
}
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(bytes);
classReader.accept(classNode, 0);
if (remove(classNode.visibleAnnotations, SIDE)) {
if (DEBUG) {
System.out.println(String.format("Attempted to load class %s for invalid side %s", classNode.name, SIDE));
}
throw new RuntimeException(String.format("Attempted to load class %s for invalid side %s", classNode.name, SIDE));
}
Iterator<FieldNode> fields = classNode.fields.iterator();
while (fields.hasNext()) {
FieldNode field = fields.next();
if (remove(field.visibleAnnotations, SIDE)) {
if (DEBUG) {
System.out.println(String.format("Removing Field: %s.%s", classNode.name, field.name));
}
fields.remove();
}
}
LambdaGatherer lambdaGatherer = new LambdaGatherer();
Iterator<MethodNode> methods = classNode.methods.iterator();
while (methods.hasNext()) {
MethodNode method = methods.next();
if (remove(method.visibleAnnotations, SIDE)) {
if (DEBUG) {
System.out.println(String.format("Removing Method: %s.%s%s", classNode.name, method.name, method.desc));
}
methods.remove();
lambdaGatherer.accept(method);
}
}
// remove dynamic lambda methods that are inside of removed methods
List<Handle> dynamicLambdaHandles = lambdaGatherer.getDynamicLambdaHandles();
if (!dynamicLambdaHandles.isEmpty()) {
methods = classNode.methods.iterator();
while (methods.hasNext()) {
MethodNode method = methods.next();
for (Handle dynamicLambdaHandle : dynamicLambdaHandles) {
if (method.name.equals(dynamicLambdaHandle.getName()) && method.desc.equals(dynamicLambdaHandle.getDesc())) {
if (DEBUG) {
System.out.println(String.format("Removing Method: %s.%s%s", classNode.name, method.name, method.desc));
}
methods.remove();
}
}
}
}
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
classNode.accept(writer);
return writer.toByteArray();
}
use of org.objectweb.asm.ClassWriter in project android_frameworks_base by ParanoidAndroid.
the class DelegateClassAdapterTest method testConstructorsNotSupported.
/**
* {@link DelegateMethodAdapter2} does not support overriding constructors yet,
* so this should fail with an {@link UnsupportedOperationException}.
*
* Although not tested here, the message of the exception should contain the
* constructor signature.
*/
@Test(expected = UnsupportedOperationException.class)
public void testConstructorsNotSupported() throws IOException {
ClassWriter cw = new ClassWriter(0);
String internalClassName = NATIVE_CLASS_NAME.replace('.', '/');
HashSet<String> delegateMethods = new HashSet<String>();
delegateMethods.add("<init>");
DelegateClassAdapter cv = new DelegateClassAdapter(mLog, cw, internalClassName, delegateMethods);
ClassReader cr = new ClassReader(NATIVE_CLASS_NAME);
cr.accept(cv, 0);
}
use of org.objectweb.asm.ClassWriter in project android_frameworks_base by ParanoidAndroid.
the class DelegateClassAdapterTest method testDelegateInner.
@Test
public void testDelegateInner() throws Throwable {
// We'll delegate the "get" method of both the inner and outer class.
HashSet<String> delegateMethods = new HashSet<String>();
delegateMethods.add("get");
delegateMethods.add("privateMethod");
// Generate the delegate for the outer class.
ClassWriter cwOuter = new ClassWriter(0);
String outerClassName = OUTER_CLASS_NAME.replace('.', '/');
DelegateClassAdapter cvOuter = new DelegateClassAdapter(mLog, cwOuter, outerClassName, delegateMethods);
ClassReader cr = new ClassReader(OUTER_CLASS_NAME);
cr.accept(cvOuter, 0);
// Generate the delegate for the inner class.
ClassWriter cwInner = new ClassWriter(0);
String innerClassName = INNER_CLASS_NAME.replace('.', '/');
DelegateClassAdapter cvInner = new DelegateClassAdapter(mLog, cwInner, innerClassName, delegateMethods);
cr = new ClassReader(INNER_CLASS_NAME);
cr.accept(cvInner, 0);
// Load the generated classes in a different class loader and try them
ClassLoader2 cl2 = null;
try {
cl2 = new ClassLoader2() {
@Override
public void testModifiedInstance() throws Exception {
// Check the outer class
Class<?> outerClazz2 = loadClass(OUTER_CLASS_NAME);
Object o2 = outerClazz2.newInstance();
assertNotNull(o2);
// The original Outer.get returns 1+10+20,
// but the delegate makes it return 4+10+20
assertEquals(4 + 10 + 20, callGet(o2, 10, 20));
assertEquals(1 + 10 + 20, callGet_Original(o2, 10, 20));
// The original Outer has a private method that is
// delegated. We should be able to call both the delegate
// and the original (which is now public).
assertEquals("outerPrivateMethod", callMethod(o2, "privateMethod_Original", false));
// The original method is private, so by default we can't access it
boolean gotIllegalAccessException = false;
try {
callMethod(o2, "privateMethod", false);
} catch (IllegalAccessException e) {
gotIllegalAccessException = true;
}
assertTrue(gotIllegalAccessException);
// Try again, but now making it accessible
assertEquals("outerPrivate_Delegate", callMethod(o2, "privateMethod", true));
// Check the inner class. Since it's not a static inner class, we need
// to use the hidden constructor that takes the outer class as first parameter.
Class<?> innerClazz2 = loadClass(INNER_CLASS_NAME);
Constructor<?> innerCons = innerClazz2.getConstructor(new Class<?>[] { outerClazz2 });
Object i2 = innerCons.newInstance(new Object[] { o2 });
assertNotNull(i2);
// The original Inner.get returns 3+10+20,
// but the delegate makes it return 6+10+20
assertEquals(6 + 10 + 20, callGet(i2, 10, 20));
assertEquals(3 + 10 + 20, callGet_Original(i2, 10, 20));
}
};
cl2.add(OUTER_CLASS_NAME, cwOuter.toByteArray());
cl2.add(INNER_CLASS_NAME, cwInner.toByteArray());
cl2.testModifiedInstance();
} catch (Throwable t) {
throw dumpGeneratedClass(t, cl2);
}
}
use of org.objectweb.asm.ClassWriter in project android_frameworks_base by ParanoidAndroid.
the class DelegateClassAdapterTest method testNoOp.
/**
* Tests that a class not being modified still works.
*/
@SuppressWarnings("unchecked")
@Test
public void testNoOp() throws Throwable {
// create an instance of the class that will be modified
// (load the class in a distinct class loader so that we can trash its definition later)
ClassLoader cl1 = new ClassLoader(this.getClass().getClassLoader()) {
};
Class<ClassWithNative> clazz1 = (Class<ClassWithNative>) cl1.loadClass(NATIVE_CLASS_NAME);
ClassWithNative instance1 = clazz1.newInstance();
assertEquals(42, instance1.add(20, 22));
try {
instance1.callNativeInstance(10, 3.1415, new Object[0]);
fail("Test should have failed to invoke callTheNativeMethod [1]");
} catch (UnsatisfiedLinkError e) {
// This is expected to fail since the native method is not implemented.
}
// Now process it but tell the delegate to not modify any method
ClassWriter cw = new ClassWriter(0);
HashSet<String> delegateMethods = new HashSet<String>();
String internalClassName = NATIVE_CLASS_NAME.replace('.', '/');
DelegateClassAdapter cv = new DelegateClassAdapter(mLog, cw, internalClassName, delegateMethods);
ClassReader cr = new ClassReader(NATIVE_CLASS_NAME);
cr.accept(cv, 0);
// Load the generated class in a different class loader and try it again
ClassLoader2 cl2 = null;
try {
cl2 = new ClassLoader2() {
@Override
public void testModifiedInstance() throws Exception {
Class<?> clazz2 = loadClass(NATIVE_CLASS_NAME);
Object i2 = clazz2.newInstance();
assertNotNull(i2);
assertEquals(42, callAdd(i2, 20, 22));
try {
callCallNativeInstance(i2, 10, 3.1415, new Object[0]);
fail("Test should have failed to invoke callTheNativeMethod [2]");
} catch (InvocationTargetException e) {
// This is expected to fail since the native method has NOT been
// overridden here.
assertEquals(UnsatisfiedLinkError.class, e.getCause().getClass());
}
// Check that the native method does NOT have the new annotation
Method[] m = clazz2.getDeclaredMethods();
assertEquals("native_instance", m[2].getName());
assertTrue(Modifier.isNative(m[2].getModifiers()));
Annotation[] a = m[2].getAnnotations();
assertEquals(0, a.length);
}
};
cl2.add(NATIVE_CLASS_NAME, cw);
cl2.testModifiedInstance();
} catch (Throwable t) {
throw dumpGeneratedClass(t, cl2);
}
}
use of org.objectweb.asm.ClassWriter in project pinpoint by naver.
the class JavassistVerifyErrorTest method asm_stackmapframe_check.
@Test
public void asm_stackmapframe_check() throws Exception {
CustomURLClassLoader classLoader = new CustomURLClassLoader(new URL[] {}, Thread.currentThread().getContextClassLoader());
final InputStream stream = classLoader.getResourceAsStream(JavaAssistUtils.javaNameToJvmName(INVALID_STACK_MAP_FRAME) + ".class");
ClassReader cr = new ClassReader(stream);
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
ClassVisitor cv = new BytecodeVerifyTestClassVisitor(cw);
cr.accept(cv, ClassReader.EXPAND_FRAMES | ClassReader.SKIP_DEBUG);
byte[] bytecode = cw.toByteArray();
classLoader.defineClass0(INVALID_STACK_MAP_FRAME, bytecode);
final Class<?> aClass = Class.forName(INVALID_STACK_MAP_FRAME, true, classLoader);
Assert.assertSame(aClass.getClassLoader(), classLoader);
final ASMBytecodeDisassembler bytecodeDisassembler = new ASMBytecodeDisassembler();
final String dumpBytecode = bytecodeDisassembler.dumpBytecode(bytecode);
logger.debug("dumpBytecode:{}", dumpBytecode);
final String verify = bytecodeDisassembler.dumpVerify(bytecode, classLoader);
logger.debug("dumpVerify:{}", verify);
// final String dumpAsm = bytecodeDisassembler.dumpASM(bytecode);
// logger.debug("dumpAsm :{}", dumpAsm);
}
Aggregations