List of usage examples for org.objectweb.asm.commons GeneratorAdapter getStatic
public void getStatic(final Type owner, final String name, final Type type)
From source file:co.cask.cdap.internal.app.runtime.batch.distributed.ContainerLauncherGenerator.java
License:Apache License
/** * Generates the bytecode for a main class and writes to the given {@link JarOutputStream}. * The generated class looks like this:/*from www. ja v a 2s .c o m*/ * * <pre>{@code * class className { * public static void main(String[] args) { * MapReduceContainerLauncher.launch(launcherClassPath, classLoaderName, className, args); * } * } * } * </pre> * * The {@code launcherClassPath}, {@code classLoaderName} and {@code className} are represented as * string literals in the generated class. */ private static void generateLauncherClass(String launcherClassPath, String classLoaderName, String className, JarOutputStream output) throws IOException { String internalName = className.replace('.', '/'); ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES); classWriter.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, internalName, null, Type.getInternalName(Object.class), null); Method constructor = Methods.getMethod(void.class, "<init>"); // Constructor // MRAppMaster() GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, constructor, null, null, classWriter); mg.loadThis(); mg.invokeConstructor(Type.getType(Object.class), constructor); mg.returnValue(); mg.endMethod(); // Main method. // public static void main(String[] args) { // MapReduceContainerLauncher.launch(launcherClassPath, classLoaderName, className, args); // } Method mainMethod = Methods.getMethod(void.class, "main", String[].class); mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, mainMethod, null, new Type[] { Type.getType(Exception.class) }, classWriter); mg.getStatic(Type.getType(System.class), "out", Type.getType(PrintStream.class)); mg.visitLdcInsn("Launch class " + className); mg.invokeVirtual(Type.getType(PrintStream.class), Methods.getMethod(void.class, "println", String.class)); // The Launcher classpath, classloader name and main classname are stored as string literal in the generated class mg.visitLdcInsn(launcherClassPath); mg.visitLdcInsn(classLoaderName); mg.visitLdcInsn(className); mg.loadArg(0); mg.invokeStatic(Type.getType(MapReduceContainerLauncher.class), Methods.getMethod(void.class, "launch", String.class, String.class, String.class, String[].class)); mg.returnValue(); mg.endMethod(); classWriter.visitEnd(); output.putNextEntry(new JarEntry(internalName + ".class")); output.write(classWriter.toByteArray()); }
From source file:co.cask.cdap.internal.app.runtime.batch.distributed.ContainerLauncherGenerator.java
License:Apache License
/** * Generates a class that has a static main method which delegates the call to a static method in the given delegator * class with method signature {@code public static void launch(String className, String[] args)} * * @param className the classname of the generated class * @param mainDelegator the class to delegate the main call to * @param output for writing the generated bytes *///from w ww . jav a2s .com private static void generateMainClass(String className, Type mainDelegator, JarOutputStream output) throws IOException { String internalName = className.replace('.', '/'); ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES); classWriter.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, internalName, null, Type.getInternalName(Object.class), null); // Generate the default constructor, which just call super() Method constructor = Methods.getMethod(void.class, "<init>"); GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, constructor, null, null, classWriter); mg.loadThis(); mg.invokeConstructor(Type.getType(Object.class), constructor); mg.returnValue(); mg.endMethod(); // Generate the main method // public static void main(String[] args) { // System.out.println("Launch class ....."); // <MainDelegator>.launch(<className>, args); // } Method mainMethod = Methods.getMethod(void.class, "main", String[].class); mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, mainMethod, null, new Type[] { Type.getType(Exception.class) }, classWriter); mg.getStatic(Type.getType(System.class), "out", Type.getType(PrintStream.class)); mg.visitLdcInsn("Launch class " + className + " by calling " + mainDelegator.getClassName() + ".launch"); mg.invokeVirtual(Type.getType(PrintStream.class), Methods.getMethod(void.class, "println", String.class)); // The main classname is stored as string literal in the generated class mg.visitLdcInsn(className); mg.loadArg(0); mg.invokeStatic(mainDelegator, Methods.getMethod(void.class, "launch", String.class, String[].class)); mg.returnValue(); mg.endMethod(); classWriter.visitEnd(); output.putNextEntry(new JarEntry(internalName + ".class")); output.write(classWriter.toByteArray()); }
From source file:co.cask.cdap.internal.io.DatumWriterGenerator.java
License:Apache License
/** * Generates the constructor. The constructor generated has signature {@code (Schema, FieldAccessorFactory)}. *//*from w w w . java 2 s . c om*/ private void generateConstructor() { Method constructor = getMethod(void.class, "<init>", Schema.class, FieldAccessorFactory.class); // Constructor(Schema schema, FieldAccessorFactory accessorFactory) GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, constructor, null, null, classWriter); // super(); // Calling Object constructor mg.loadThis(); mg.invokeConstructor(Type.getType(Object.class), getMethod(void.class, "<init>")); // if (!SCHEMA_HASH.equals(schema.getSchemaHash().toString())) { throw IllegalArgumentException } mg.getStatic(classType, "SCHEMA_HASH", Type.getType(String.class)); mg.loadArg(0); mg.invokeVirtual(Type.getType(Schema.class), getMethod(SchemaHash.class, "getSchemaHash")); mg.invokeVirtual(Type.getType(SchemaHash.class), getMethod(String.class, "toString")); mg.invokeVirtual(Type.getType(String.class), getMethod(boolean.class, "equals", Object.class)); Label hashEquals = mg.newLabel(); mg.ifZCmp(GeneratorAdapter.NE, hashEquals); mg.throwException(Type.getType(IllegalArgumentException.class), "Schema not match."); mg.mark(hashEquals); // this.schema = schema; mg.loadThis(); mg.loadArg(0); mg.putField(classType, "schema", Type.getType(Schema.class)); // For each record field that needs an accessor, get the accessor and store it in field. for (Map.Entry<TypeToken<?>, String> entry : fieldAccessorRequests.entries()) { String fieldAccessorName = getFieldAccessorName(entry.getKey(), entry.getValue()); classWriter.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, fieldAccessorName, Type.getDescriptor(FieldAccessor.class), null, null); // this.fieldAccessorName // = accessorFactory.getFieldAccessor(TypeToken.of(Class.forName("className")), "fieldName"); mg.loadThis(); mg.loadArg(1); mg.push(entry.getKey().getRawType().getName()); mg.invokeStatic(Type.getType(Class.class), getMethod(Class.class, "forName", String.class)); mg.invokeStatic(Type.getType(TypeToken.class), getMethod(TypeToken.class, "of", Class.class)); mg.push(entry.getValue()); mg.invokeInterface(Type.getType(FieldAccessorFactory.class), getMethod(FieldAccessor.class, "getFieldAccessor", TypeToken.class, String.class)); mg.putField(classType, fieldAccessorName, Type.getType(FieldAccessor.class)); } mg.returnValue(); mg.endMethod(); }
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: ///* www.j ava 2s . 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.mulberry.athena.asm.ASMTest.java
License:Open Source License
@Test public void generateClassUseClassVisitor() throws Exception { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); ClassVisitor cv = new TraceClassVisitor(cw, new PrintWriter(System.out)); cv.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "Hello", null, "java/lang/Object", null); {// w w w . jav a 2 s . c o m GeneratorAdapter clinitgen = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC, clinit, null, null, cv); clinitgen.visitCode(); clinitgen.visitLineNumber(1, clinitgen.mark()); clinitgen.returnValue(); clinitgen.endMethod(); } { GeneratorAdapter ctorgen = new GeneratorAdapter(ACC_PUBLIC, voidctor, null, null, cv); Label start = ctorgen.newLabel(); Label end = ctorgen.newLabel(); ctorgen.visitCode(); ctorgen.visitLineNumber(2, ctorgen.mark()); ctorgen.visitLabel(start); ctorgen.loadThis(); ctorgen.invokeConstructor(Type.getObjectType("java/lang/Object"), voidctor); ctorgen.visitLabel(end); ctorgen.returnValue(); ctorgen.endMethod(); } { GeneratorAdapter gen = new GeneratorAdapter(ACC_PUBLIC, Method.getMethod("int getRequiredArity()"), null, null, cv); gen.visitCode(); gen.push(3); gen.returnValue(); gen.endMethod(); } { Method method = Method.getMethod("void main(java/lang/String[])", true); GeneratorAdapter maingen = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC, method, null, null, cv); Label start = maingen.newLabel(); maingen.visitCode(); maingen.visitLineNumber(3, maingen.mark()); maingen.visitLabel(start); maingen.getStatic(Type.getType("java/lang/System"), "out", Type.getType(PrintStream.class)); maingen.push("IT WORKS!"); maingen.invokeVirtual(Type.getType("java/io/PrintStream"), Method.getMethod("void println(java/lang/String)", true)); Label end = maingen.newLabel(); maingen.visitLabel(end); maingen.returnValue(); maingen.endMethod(); } cv.visitEnd(); Class<?> clazz = new DynamicClassLoader().defineClass("Hello", cw.toByteArray()); java.lang.reflect.Method method = clazz.getMethod("main", String[].class); final String[] arguments = new String[] { "hello", "world" }; method.invoke(clazz, (Object) arguments); }
From source file:com.xruby.compiler.codegen.RubyIDClassGenerator.java
License:BSD License
public static void getField(GeneratorAdapter mg, String s) { String id = idMap.get(s);/*from w w w . jav a 2 s. c o m*/ if (id == null) { if (isValidJavaID(s)) { id = s + "ID"; } else { id = nextID(); } idMap.put(s, id); } mg.getStatic(Type.getType("L" + RubyIDClassName + ";"), id, Types.RUBY_ID_TYPE); }
From source file:com.xruby.runtime.lang.util.RubyClassFactory.java
License:BSD License
protected int createRubyType(GeneratorAdapter mg, Annotation annotation) { RubyLevelClass klassAnnotation = (RubyLevelClass) annotation; mg.push(klassAnnotation.name());/*ww w .j ava 2 s. com*/ loadRubyClass(mg, klassAnnotation.superclass()); mg.invokeStatic(Types.RUBY_API_TYPE, RubyAPIDefineClassMethod); int rubyTypeIdx = mg.newLocal(Types.RUBY_CLASS_TYPE); mg.storeLocal(rubyTypeIdx); for (String moduleName : klassAnnotation.modules()) { includeModule(mg, rubyTypeIdx, moduleName); } for (UndefMethod method : klassAnnotation.undef()) { undefMethod(mg, rubyTypeIdx, method); } for (com.xruby.runtime.lang.annotation.DummyMethod dummy : klassAnnotation.dummy()) { mg.loadLocal(rubyTypeIdx); mg.push(dummy.name()); Type type = Type.getType(DummyMethod.class); mg.getStatic(type, "INSTANCE", type); if (dummy.privateMethod()) { mg.invokeVirtual(Types.RUBY_MODULE_TYPE, CgUtil.getMethod("definePrivateMethod", Types.RUBY_VALUE_TYPE, Type.getType(String.class), Types.RUBY_METHOD_TYPE)); } else { mg.invokeVirtual(Types.RUBY_MODULE_TYPE, CgUtil.getMethod("defineMethod", Types.RUBY_VALUE_TYPE, Type.getType(String.class), Types.RUBY_VALUE_TYPE)); } } return rubyTypeIdx; }
From source file:com.xruby.runtime.lang.util.RubyClassFactory.java
License:BSD License
private void loadModule(GeneratorAdapter mg, String module, int rubyTypeIdx) { if (Types.isBuiltinModule(module)) { mg.getStatic(Types.RUBY_RUNTIME_TYPE, module + "Module", Types.RUBY_MODULE_TYPE); } else {// w w w .j a v a 2s.co m mg.loadLocal(rubyTypeIdx); mg.push(module); mg.invokeStatic(Types.RUBY_API_TYPE, CgUtil.getMethod("getCurrentNamespaceConstant", Types.RUBY_VALUE_TYPE, Types.RUBY_MODULE_TYPE, Type.getType(String.class))); mg.checkCast(Types.RUBY_MODULE_TYPE); } }
From source file:com.xruby.runtime.lang.util.RubyTypeFactory.java
License:BSD License
protected static void loadRubyClass(GeneratorAdapter mg, String superclass) { if (superclass == null || superclass.length() == 0) { mg.push((String) null);// w ww . ja v a 2s .c om } if (Types.isBuiltinClass(superclass)) { mg.getStatic(Types.RUBY_RUNTIME_TYPE, superclass + "Class", Types.RUBY_CLASS_TYPE); } else { // FIXME: } }
From source file:com.xruby.runtime.lang.util.RubyTypeFactory.java
License:BSD License
private void defineRubyConstant(GeneratorAdapter mg, Field field, RubyLevelConstant constant, int rubyTypeIdx) { mg.loadLocal(rubyTypeIdx);/*w ww.j a v a 2 s . c om*/ mg.push(constant.name()); mg.getStatic(Type.getType(this.klass), field.getName(), Type.getType(field.getType())); mg.invokeVirtual(Types.RUBY_MODULE_TYPE, CgUtil.getMethod("setConstant", Types.RUBY_VALUE_TYPE, Type.getType(String.class), Types.RUBY_VALUE_TYPE)); }