use of org.objectweb.asm.ClassWriter in project felix by apache.
the class Manipulator method manipulate.
/**
* Manipulate the given byte array.
* @param origin : original class.
* @return the manipulated class, if the class is already manipulated, the original class.
* @throws IOException : if an error occurs during the manipulation.
*/
public byte[] manipulate(byte[] origin) throws IOException {
if (!m_alreadyManipulated) {
InputStream is2 = new ByteArrayInputStream(origin);
ClassReader reader = new ClassReader(is2);
ClassWriter writer = new ClassLoaderAwareClassWriter(ClassWriter.COMPUTE_FRAMES, m_className, m_superClass, m_classLoader);
ClassManipulator process = new ClassManipulator(new CheckClassAdapter(writer, false), this);
if (m_version >= Opcodes.V1_6) {
reader.accept(process, ClassReader.EXPAND_FRAMES);
} else {
reader.accept(process, 0);
}
is2.close();
return writer.toByteArray();
} else {
return origin;
}
}
use of org.objectweb.asm.ClassWriter in project cdap by caskdata.
the class HttpHandlerGenerator method generate.
/**
* Generates a new class that implements {@link HttpHandler} by copying methods signatures from the given
* {@link HttpServiceHandler} class. Calls to {@link HttpServiceHandler} methods are transactional unless
* the method is annotated with {@link TransactionPolicy(TransactionControl)}.
*
* @param delegateType type of the {@link HttpServiceHandler}
* @param pathPrefix prefix for all {@code @PATH} annotation
* @return A {@link ClassDefinition} containing information of the newly generated class.
* @throws IOException if failed to generate the class.
*/
ClassDefinition generate(TypeToken<? extends HttpServiceHandler> delegateType, String pathPrefix) throws IOException {
Class<?> rawType = delegateType.getRawType();
List<Class<?>> preservedClasses = Lists.newArrayList();
preservedClasses.add(rawType);
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
String internalName = Type.getInternalName(rawType);
String className = internalName + Hashing.md5().hashString(internalName);
// Generate the class
Type classType = Type.getObjectType(className);
classWriter.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, className, getClassSignature(delegateType), Type.getInternalName(AbstractHttpHandlerDelegator.class), null);
// Inspect the delegate class hierarchy to generate public handler methods.
for (TypeToken<?> type : delegateType.getTypes().classes()) {
if (!Object.class.equals(type.getRawType())) {
inspectHandler(delegateType, type, pathPrefix, classType, classWriter, preservedClasses);
}
}
generateConstructor(delegateType, classWriter);
generateLogger(classType, classWriter);
ClassDefinition classDefinition = new ClassDefinition(classWriter.toByteArray(), className, preservedClasses);
// End DEBUG block
return classDefinition;
}
use of org.objectweb.asm.ClassWriter in project Bookshelf by Darkhax-Minecraft.
the class ASMUtils method createByteArrayFromClass.
/**
* Converts a ClassNode into a byte array which can then be returned by your transformer.
*
* @param classNode: An instance of the ClassNode you wish to convert into a byte array.
* @param flags: The flags to use when converting the ClassNode. These are generally
* COMPUTE_FRAMES and COMPUTE_MAXS.
* @return byte[]: A byte array representation of the ClassNode.
*/
public static byte[] createByteArrayFromClass(ClassNode classNode, int flags) {
final ClassWriter classWriter = new ClassWriter(flags);
classNode.accept(classWriter);
return classWriter.toByteArray();
}
use of org.objectweb.asm.ClassWriter in project android_frameworks_base by crdroidandroid.
the class DelegateClassAdapterTest method testDelegateNative.
@Test
public void testDelegateNative() throws Throwable {
ClassWriter cw = new ClassWriter(0);
String internalClassName = NATIVE_CLASS_NAME.replace('.', '/');
HashSet<String> delegateMethods = new HashSet<>();
delegateMethods.add(DelegateClassAdapter.ALL_NATIVES);
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
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);
// Use reflection to access inner methods
assertEquals(42, callAdd(i2, 20, 22));
Object[] objResult = new Object[] { null };
int result = callCallNativeInstance(i2, 10, 3.1415, objResult);
assertEquals((int) (10 + 3.1415), result);
assertSame(i2, objResult[0]);
// Check that the native method now has the new annotation and is not native
Method[] m = clazz2.getDeclaredMethods();
Method nativeInstanceMethod = null;
for (Method method : m) {
if ("native_instance".equals(method.getName())) {
nativeInstanceMethod = method;
break;
}
}
assertNotNull(nativeInstanceMethod);
assertFalse(Modifier.isNative(nativeInstanceMethod.getModifiers()));
Annotation[] a = nativeInstanceMethod.getAnnotations();
assertEquals("LayoutlibDelegate", a[0].annotationType().getSimpleName());
}
};
cl2.add(NATIVE_CLASS_NAME, cw);
cl2.testModifiedInstance();
} catch (Throwable t) {
throw dumpGeneratedClass(t, cl2);
}
}
use of org.objectweb.asm.ClassWriter in project android_frameworks_base by crdroidandroid.
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<>();
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,
// so by default we can't access it.
boolean gotIllegalAccessException = false;
try {
callMethod(o2, "privateMethod", false);
} catch (IllegalAccessException e) {
gotIllegalAccessException = true;
}
assertTrue(gotIllegalAccessException);
// The private method from original Outer has been
// delegated. The delegate generated should have the
// same access.
gotIllegalAccessException = false;
try {
assertEquals("outerPrivateMethod", callMethod(o2, "privateMethod_Original", false));
} catch (IllegalAccessException e) {
gotIllegalAccessException = true;
}
assertTrue(gotIllegalAccessException);
// 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(outerClazz2);
Object i2 = innerCons.newInstance(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);
}
}
Aggregations