use of org.objectweb.asm.ClassWriter in project Lucee by lucee.
the class SourceLastModifiedClassAdapter method setSourceLastModified.
public static byte[] setSourceLastModified(byte[] barr, long lastModified) {
ClassReader cr = new ClassReader(barr);
ClassWriter cw = ASMUtil.getClassWriter();
ClassVisitor ca = new SourceLastModifiedClassAdapter(cw, lastModified);
cr.accept(ca, 0);
return cw.toByteArray();
}
use of org.objectweb.asm.ClassWriter in project SpongeCommon by SpongePowered.
the class ClassEventListenerFactory method generateClass.
private static byte[] generateClass(String name, Class<?> handle, Method method, Class<?> eventClass, Class<? extends EventFilter> filter) {
name = name.replace('.', '/');
final String handleName = Type.getInternalName(handle);
final String handleDescriptor = Type.getDescriptor(handle);
final String filterName = Type.getInternalName(filter);
String eventDescriptor = "(";
for (int i = 0; i < method.getParameterCount(); i++) {
eventDescriptor += Type.getDescriptor(method.getParameterTypes()[i]);
}
eventDescriptor += ")V";
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
MethodVisitor mv;
FieldVisitor fv;
cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, name, null, BASE_HANDLER, null);
{
fv = cw.visitField(ACC_PRIVATE + ACC_STATIC, "FILTER", "L" + filterName + ";", null, null);
fv.visitEnd();
}
{
mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
mv.visitCode();
mv.visitTypeInsn(NEW, filterName);
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, filterName, "<init>", "()V", false);
mv.visitFieldInsn(PUTSTATIC, name, "FILTER", "L" + filterName + ";");
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", '(' + handleDescriptor + ")V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKESPECIAL, BASE_HANDLER, "<init>", "(Ljava/lang/Object;)V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "handle", HANDLE_METHOD_DESCRIPTOR, null, new String[] { "java/lang/Exception" });
mv.visitCode();
mv.visitFieldInsn(GETSTATIC, name, "FILTER", "L" + filterName + ";");
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(EventFilter.class), "filter", FILTER_DESCRIPTOR, true);
mv.visitVarInsn(ASTORE, 2);
mv.visitVarInsn(ALOAD, 2);
Label l2 = new Label();
mv.visitJumpInsn(IFNULL, l2);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, name, "handle", "Ljava/lang/Object;");
mv.visitTypeInsn(CHECKCAST, handleName);
for (int i = 0; i < method.getParameterCount(); i++) {
mv.visitVarInsn(ALOAD, 2);
mv.visitIntInsn(BIPUSH, i);
mv.visitInsn(AALOAD);
Type paramType = Type.getType(method.getParameterTypes()[i]);
GeneratorUtils.visitUnboxingMethod(mv, paramType);
}
mv.visitMethodInsn(INVOKEVIRTUAL, handleName, method.getName(), eventDescriptor, false);
mv.visitLabel(l2);
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
use of org.objectweb.asm.ClassWriter in project jacoco by jacoco.
the class BootstrapMethodReferenceTest method test.
@Test
public void test() throws Exception {
final String className = "Example";
final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
cw.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", null);
final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "run", "()I", null, null);
mv.visitCode();
addCauseOfResizeInstructions(mv);
final MethodType methodType = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class);
final Handle handle = new Handle(Opcodes.H_INVOKESTATIC, this.getClass().getCanonicalName().replace('.', '/'), "bootstrap", methodType.toMethodDescriptorString(), false);
mv.visitInvokeDynamicInsn("invoke", "()I", handle);
mv.visitInsn(Opcodes.IRETURN);
mv.visitMaxs(1, 0);
mv.visitEnd();
cw.visitEnd();
final byte[] original = cw.toByteArray();
assertEquals(42, run(className, original));
final byte[] instrumented = instrumenter.instrument(original, className);
assertEquals(42, run(className, instrumented));
}
use of org.objectweb.asm.ClassWriter in project jacoco by jacoco.
the class ProbeArrayStrategyFactoryTest method testModule.
@Test
public void testModule() {
final ClassWriter writer = new ClassWriter(0);
writer.visit(Opcodes.V9, Opcodes.ACC_MODULE, "module-info", null, null, null);
writer.visitModule("module", 0, null).visitEnd();
writer.visitEnd();
final IProbeArrayStrategy strategy = ProbeArrayStrategyFactory.createFor(0, new ClassReader(writer.toByteArray()), generator);
assertEquals(NoneProbeArrayStrategy.class, strategy.getClass());
}
use of org.objectweb.asm.ClassWriter in project jacoco by jacoco.
the class ModifiedSystemClassRuntimeTest method createClass.
private static byte[] createClass(final int version) {
final ClassWriter cw = new ClassWriter(0);
cw.visit(version, 0, "Foo", null, "java/lang/Object", null);
cw.visitEnd();
return cw.toByteArray();
}
Aggregations