List of usage examples for org.objectweb.asm.commons Method getMethod
public static Method getMethod(final String method)
From source file:com.google.template.soy.jbcsrc.ExpressionTester.java
License:Apache License
private static ClassData createClass(Class<? extends Invoker> targetInterface, Expression expr) { java.lang.reflect.Method invokeMethod; try {// www .ja va 2s . c o m invokeMethod = targetInterface.getMethod("invoke"); } catch (NoSuchMethodException | SecurityException e) { throw new RuntimeException(e); } Class<?> returnType = invokeMethod.getReturnType(); if (!Type.getType(returnType).equals(expr.resultType())) { if (!returnType.equals(Object.class) || expr.resultType().getSort() != Type.OBJECT) { throw new IllegalArgumentException(targetInterface + " is not appropriate for this expression"); } } TypeInfo generatedType = TypeInfo.create( ExpressionTester.class.getPackage().getName() + "." + targetInterface.getSimpleName() + "Impl"); SoyClassWriter cw = SoyClassWriter.builder(generatedType) .setAccess(Opcodes.ACC_FINAL | Opcodes.ACC_SUPER | Opcodes.ACC_PUBLIC) .implementing(TypeInfo.create(targetInterface)).build(); BytecodeUtils.defineDefaultConstructor(cw, generatedType); Method invoke = Method.getMethod(invokeMethod); Statement.returnExpression(expr).writeMethod(Opcodes.ACC_PUBLIC, invoke, cw); Method voidInvoke; try { voidInvoke = Method.getMethod(Invoker.class.getMethod("voidInvoke")); } catch (NoSuchMethodException | SecurityException e) { throw new RuntimeException(e); // this method definitely exists } Statement.concat(LocalVariable.createThisVar(generatedType, new Label(), new Label()) .invoke(MethodRef.create(invokeMethod)).toStatement(), new Statement() { @Override void doGen(CodeBuilder adapter) { adapter.visitInsn(Opcodes.RETURN); } }).writeMethod(Opcodes.ACC_PUBLIC, voidInvoke, cw); ClassData data = cw.toClassData(); checkClassData(data); return data; }
From source file:com.google.template.soy.jbcsrc.restricted.ConstructorRef.java
License:Apache License
public static ConstructorRef create(Class<?> clazz, Class<?>... argTypes) { TypeInfo type = TypeInfo.create(clazz); Constructor<?> c;/*from www . j av a 2 s. c o m*/ try { c = clazz.getConstructor(argTypes); } catch (NoSuchMethodException | SecurityException e) { throw new RuntimeException(e); } Type constructorType = Type.getType(c); return new AutoValue_ConstructorRef(type, Method.getMethod(c), ImmutableList.copyOf(constructorType.getArgumentTypes())); }
From source file:com.google.template.soy.jbcsrc.restricted.MethodRef.java
License:Apache License
public static MethodRef create(java.lang.reflect.Method method) { Class<?> clazz = method.getDeclaringClass(); TypeInfo ownerType = TypeInfo.create(method.getDeclaringClass()); boolean isStatic = Modifier.isStatic(method.getModifiers()); ImmutableList<Type> argTypes; if (isStatic) { argTypes = ImmutableList.copyOf(Type.getArgumentTypes(method)); } else {/*from w w w . j a va2 s . co m*/ // for instance methods the first 'argument' is always an instance of the class. argTypes = ImmutableList.<Type>builder().add(ownerType.type()).add(Type.getArgumentTypes(method)) .build(); } return new AutoValue_MethodRef( clazz.isInterface() ? Opcodes.INVOKEINTERFACE : isStatic ? Opcodes.INVOKESTATIC : Opcodes.INVOKEVIRTUAL, ownerType, Method.getMethod(method), Type.getType(method.getReturnType()), argTypes, Features.of()); }
From source file:com.google.template.soy.jbcsrc.restricted.testing.ExpressionEvaluator.java
License:Apache License
public static ClassData createClass(Class<? extends Invoker> targetInterface, Expression expr) { java.lang.reflect.Method invokeMethod; try {//from w ww . j a va 2 s .c o m invokeMethod = targetInterface.getMethod("invoke"); } catch (NoSuchMethodException | SecurityException e) { throw new RuntimeException(e); } Class<?> returnType = invokeMethod.getReturnType(); if (!Type.getType(returnType).equals(expr.resultType())) { if (!returnType.equals(Object.class) || expr.resultType().getSort() != Type.OBJECT) { throw new IllegalArgumentException(targetInterface + " is not appropriate for this expression"); } } TypeInfo generatedType = TypeInfo.create(Names.CLASS_PREFIX + targetInterface.getSimpleName() + "Impl"); SoyClassWriter cw = SoyClassWriter.builder(generatedType) .setAccess(Opcodes.ACC_FINAL | Opcodes.ACC_SUPER | Opcodes.ACC_PUBLIC) .implementing(TypeInfo.create(targetInterface)).build(); BytecodeUtils.defineDefaultConstructor(cw, generatedType); Method invoke = Method.getMethod(invokeMethod); Statement.returnExpression(expr).writeMethod(Opcodes.ACC_PUBLIC, invoke, cw); Method voidInvoke; try { voidInvoke = Method.getMethod(Invoker.class.getMethod("voidInvoke")); } catch (NoSuchMethodException | SecurityException e) { throw new RuntimeException(e); // this method definitely exists } Statement.concat(LocalVariable.createThisVar(generatedType, new Label(), new Label()) .invoke(MethodRef.create(invokeMethod)).toStatement(), new Statement() { @Override protected void doGen(CodeBuilder adapter) { adapter.visitInsn(Opcodes.RETURN); } }).writeMethod(Opcodes.ACC_PUBLIC, voidInvoke, cw); ClassData data = cw.toClassData(); checkClassData(data); return data; }
From source file:com.lighters.asm.HelloWorld.java
License:Apache License
public static void main(final String args[]) throws Exception { // Generates the bytecode corresponding to the following Java class: ///*from ww w. j a v a2s. c o m*/ // public class Example { // public static void main (String[] args) { // System.out.println("Hello world!"); // } // } // creates a ClassWriter for the Example public class, // which inherits from Object ClassWriter cw = new ClassWriter(0); cw.visit(V1_1, ACC_PUBLIC, "Example", null, "java/lang/Object", null); // creates a MethodWriter for the (implicit) constructor MethodVisitor mw = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); // pushes the 'this' variable mw.visitVarInsn(ALOAD, 0); // invokes the super class constructor mw.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mw.visitInsn(RETURN); // this code uses a maximum of one stack element and one local variable mw.visitMaxs(1, 1); mw.visitEnd(); // creates a MethodWriter for the 'main' method mw = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); // pushes the 'out' field (of type PrintStream) of the System class mw.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); // pushes the "Hello World!" String constant mw.visitLdcInsn("Hello world!"); // invokes the 'println' method (defined in the PrintStream class) mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); mw.visitInsn(RETURN); // this code uses a maximum of two stack elements and two local // variables mw.visitMaxs(2, 2); mw.visitEnd(); // gets the bytecode of the Example class, and loads it dynamically byte[] code = cw.toByteArray(); FileOutputStream fos = new FileOutputStream("Example.class"); fos.write(code); fos.close(); HelloWorld loader = new HelloWorld(); Class<?> exampleClass = loader.defineClass("Example", code, 0, code.length); // uses the dynamically generated class to print 'Helloworld' exampleClass.getMethods()[0].invoke(null, new Object[] { null }); // ------------------------------------------------------------------------ // Same example with a GeneratorAdapter (more convenient but slower) // ------------------------------------------------------------------------ cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); cw.visit(V1_1, ACC_PUBLIC, "Example", null, "java/lang/Object", null); // creates a GeneratorAdapter for the (implicit) constructor Method m = Method.getMethod("void <init> ()"); GeneratorAdapter mg = new GeneratorAdapter(ACC_PUBLIC, m, null, null, cw); mg.loadThis(); mg.invokeConstructor(Type.getType(Object.class), m); mg.returnValue(); mg.endMethod(); // creates a GeneratorAdapter for the 'main' method m = Method.getMethod("void main (String[])"); mg = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC, m, null, null, cw); mg.getStatic(Type.getType(System.class), "out", Type.getType(PrintStream.class)); mg.push("Hello world!"); mg.invokeVirtual(Type.getType(PrintStream.class), Method.getMethod("void println (String)")); mg.returnValue(); mg.endMethod(); cw.visitEnd(); code = cw.toByteArray(); loader = new HelloWorld(); exampleClass = loader.defineClass("Example", code, 0, code.length); // uses the dynamically generated class to print 'Helloworld' exampleClass.getMethods()[0].invoke(null, new Object[] { null }); }
From source file:com.mogujie.instantrun.IncrementalVisitor.java
License:Apache License
protected static void trace(GeneratorAdapter mv, String s) { mv.push(s);/*w ww. java 2 s .c om*/ mv.invokeStatic(Type.getObjectType(PACKAGE + "/AndroidInstantRuntime"), Method.getMethod("void trace(String)")); }
From source file:com.mogujie.instantrun.IncrementalVisitor.java
License:Apache License
protected static void trace(GeneratorAdapter mv, int argsNumber) { StringBuilder methodSignature = new StringBuilder("void trace(String"); for (int i = 0; i < argsNumber - 1; i++) { methodSignature.append(", String"); }//from w ww .j a va2 s . com methodSignature.append(")"); mv.invokeStatic(Type.getObjectType(PACKAGE + "/AndroidInstantRuntime"), Method.getMethod(methodSignature.toString())); }
From source file:com.mogujie.instantrun.IntSwitch.java
License:Apache License
private void visitx(GeneratorAdapter mv, List<String> strings) { if (strings.size() == 1) { visitCase(strings.get(0));//from w ww . jav a 2 s . c o m return; } for (String string : strings) { Label label = new Label(); visitString(); mv.visitLdcInsn(string); mv.invokeVirtual(STRING_TYPE, Method.getMethod("boolean equals(Object)")); mv.visitJumpInsn(Opcodes.IFEQ, label); visitCase(string); mv.visitLabel(label); } visitDefault(); }
From source file:com.mogujie.instantrun.IntSwitch.java
License:Apache License
void writeMissingMessageWithHash(GeneratorAdapter mv, String visitedClassName) { mv.newInstance(INSTANT_RELOAD_EXCEPTION_TYPE); mv.dup();//from w w w .j av a 2 s . co m mv.push("int switch could not find %d in %s"); mv.push(3); mv.newArray(OBJECT_TYPE); mv.dup(); mv.push(0); visitInt(); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false); mv.arrayStore(OBJECT_TYPE); mv.dup(); mv.push(2); mv.push(visitedClassName); mv.arrayStore(OBJECT_TYPE); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "format", "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;", false); mv.invokeConstructor(INSTANT_RELOAD_EXCEPTION_TYPE, Method.getMethod("void <init> (String)")); mv.throwException(); }
From source file:com.mogujie.instantrun.MethodRedirection.java
License:Apache License
@Override protected void doRedirect(GeneratorAdapter mv, int change) { mv.loadLocal(change);/*from ww w. j a v a 2 s . c om*/ mv.push(AcesoProguardMap.instance().getMtdIndex(visitedClassName, IncrementalTool.getMtdSig(mtdName, mtdDesc))); ByteCodeUtils.newVariableArray(mv, ByteCodeUtils.toLocalVariables(types)); // now invoke the generic dispatch method. mv.invokeInterface(IncrementalVisitor.CHANGE_TYPE, Method.getMethod("Object access$dispatch(int, Object[])")); }