List of usage examples for org.objectweb.asm.commons GeneratorAdapter invokeStatic
public void invokeStatic(final Type owner, final Method method)
From source file:co.cask.cdap.app.runtime.spark.SparkRunnerClassLoader.java
License:Apache License
/** * Defines the class by rewriting the constructor to call * SparkRuntimeEnv#setContext(SparkContext) or SparkRuntimeEnv#setContext(StreamingContext), * depending on the class type.//w w w. j av a 2 s . c o m */ private Class<?> defineContext(final Type contextType, InputStream byteCodeStream) throws IOException { return rewriteConstructorAndDefineClass(contextType, byteCodeStream, new ConstructorRewriter() { @Override public void onMethodExit(GeneratorAdapter generatorAdapter) { generatorAdapter.loadThis(); generatorAdapter.invokeStatic(SPARK_RUNTIME_ENV_TYPE, new Method("setContext", Type.VOID_TYPE, new Type[] { contextType })); } }); }
From source file:co.cask.cdap.app.runtime.spark.SparkRunnerClassLoader.java
License:Apache License
/** * Defines the SparkConf class by rewriting the constructor to call SparkRuntimeEnv#setupSparkConf(SparkConf). *///from w w w .j a va 2 s.c o m private Class<?> defineSparkConf(final Type sparkConfType, InputStream byteCodeStream) throws IOException { return rewriteConstructorAndDefineClass(sparkConfType, byteCodeStream, new ConstructorRewriter() { @Override public void onMethodExit(GeneratorAdapter generatorAdapter) { generatorAdapter.loadThis(); generatorAdapter.invokeStatic(SPARK_RUNTIME_ENV_TYPE, new Method("setupSparkConf", Type.VOID_TYPE, new Type[] { sparkConfType })); } }); }
From source file:co.cask.cdap.app.runtime.spark.SparkRunnerClassLoader.java
License:Apache License
/** * Defines the org.apache.spark.deploy.yarn.Client class with rewriting of the createConfArchive method to * workaround the SPARK-13441 bug.//from ww w .j ava2 s . c o m */ private Class<?> defineClient(String name, InputStream createConfArchive) throws IOException, ClassNotFoundException { // We only need to rewrite if listing either HADOOP_CONF_DIR or YARN_CONF_DIR return null. boolean needRewrite = false; for (String env : ImmutableList.of("HADOOP_CONF_DIR", "YARN_CONF_DIR")) { String value = System.getenv(env); if (value != null) { File path = new File(value); if (path.isDirectory() && path.listFiles() == null) { needRewrite = true; break; } } } // If rewrite is not needed if (!needRewrite) { return findClass(name); } ClassReader cr = new ClassReader(createConfArchive); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); cr.accept(new ClassVisitor(Opcodes.ASM5, cw) { @Override public MethodVisitor visitMethod(final int access, final String name, final String desc, String signature, String[] exceptions) { MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); // Only rewrite the createConfArchive method if (!"createConfArchive".equals(name)) { return mv; } // Check if it's a recognizable return type. // Spark 1.5+ return type is File boolean isReturnFile = Type.getReturnType(desc).equals(Type.getType(File.class)); Type optionType = Type.getObjectType("scala/Option"); if (!isReturnFile) { // Spark 1.4 return type is Option<File> if (!Type.getReturnType(desc).equals(optionType)) { // Unknown type. Not going to modify the code. return mv; } } // Generate this for Spark 1.5+ // return SparkRuntimeUtils.createConfArchive(this.sparkConf, SPARK_CONF_FILE, // LOCALIZED_CONF_DIR, LOCALIZED_CONF_DIR_ZIP); // Generate this for Spark 1.4 // return Option.apply(SparkRuntimeUtils.createConfArchive(this.sparkConf, SPARK_CONF_FILE, // LOCALIZED_CONF_DIR, LOCALIZED_CONF_DIR_ZIP)); GeneratorAdapter mg = new GeneratorAdapter(mv, access, name, desc); // load this.sparkConf to the stack mg.loadThis(); mg.getField(Type.getObjectType("org/apache/spark/deploy/yarn/Client"), "sparkConf", SPARK_CONF_TYPE); // push three constants to the stack mg.visitLdcInsn(SPARK_CONF_FILE); mg.visitLdcInsn(LOCALIZED_CONF_DIR); mg.visitLdcInsn(LOCALIZED_CONF_DIR_ZIP); // call SparkRuntimeUtils.createConfArchive, return a File and leave it in stack Type stringType = Type.getType(String.class); mg.invokeStatic(SPARK_RUNTIME_UTILS_TYPE, new Method("createConfArchive", Type.getType(File.class), new Type[] { SPARK_CONF_TYPE, stringType, stringType, stringType })); if (isReturnFile) { // Spark 1.5+ return type is File, hence just return the File from the stack mg.returnValue(); mg.endMethod(); } else { // Spark 1.4 return type is Option<File> // return Option.apply(<file from stack>); // where the file is actually just popped from the stack mg.invokeStatic(optionType, new Method("apply", optionType, new Type[] { Type.getType(Object.class) })); mg.checkCast(optionType); mg.returnValue(); mg.endMethod(); } return null; } }, ClassReader.EXPAND_FRAMES); byte[] byteCode = cw.toByteArray(); return defineClass(name, byteCode, 0, byteCode.length); }
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 va 2 s. co 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 ww w. j a v a 2 s . c o m 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.app.runtime.service.http.HttpHandlerGenerator.java
License:Apache License
/** * Generates a static Logger field for logging and a static initialization block to initialize the logger. *//*w w w . j a v a 2 s. co m*/ private void generateLogger(Type classType, ClassWriter classWriter) { // private static final Logger LOG = LoggerFactory.getLogger(classType); classWriter.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, "LOG", Type.getType(Logger.class).getDescriptor(), null, null); Method init = Methods.getMethod(void.class, "<clinit>"); GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_STATIC, init, null, null, classWriter); mg.visitLdcInsn(classType); mg.invokeStatic(Type.getType(LoggerFactory.class), Methods.getMethod(Logger.class, "getLogger", Class.class)); mg.putStatic(classType, "LOG", Type.getType(Logger.class)); mg.returnValue(); mg.endMethod(); }
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 ww w.j av a2 s . c o m 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:co.cask.cdap.internal.io.DatumWriterGenerator.java
License:Apache License
/** * Generates the {@link DatumWriter#encode(Object, co.cask.cdap.common.io.Encoder)} method. * @param outputType Type information of the data type for output * @param schema Schema to use for output. */// w w w . j a v a 2s . c om private void generateEncode(TypeToken<?> outputType, Schema schema) { TypeToken<?> callOutputType = getCallTypeToken(outputType, schema); Method encodeMethod = getMethod(void.class, "encode", callOutputType.getRawType(), Encoder.class); if (!Object.class.equals(callOutputType.getRawType())) { // Generate the synthetic method for the bridging Method method = getMethod(void.class, "encode", Object.class, Encoder.class); GeneratorAdapter mg = new GeneratorAdapter( Opcodes.ACC_PUBLIC + Opcodes.ACC_BRIDGE + Opcodes.ACC_SYNTHETIC, method, null, new Type[] { Type.getType(IOException.class) }, classWriter); mg.loadThis(); mg.loadArg(0); mg.checkCast(Type.getType(callOutputType.getRawType())); mg.loadArg(1); mg.invokeVirtual(classType, encodeMethod); mg.returnValue(); mg.endMethod(); } // Generate the top level public encode method String methodSignature = null; if (callOutputType.getType() instanceof ParameterizedType) { methodSignature = Signatures.getMethodSignature(encodeMethod, callOutputType, null); } GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, encodeMethod, methodSignature, new Type[] { Type.getType(IOException.class) }, classWriter); // Delegate to the actual encode method(value, encoder, schema, Sets.newIdentityHashSet()); mg.loadThis(); mg.loadArg(0); mg.loadArg(1); mg.loadThis(); mg.getField(classType, "schema", Type.getType(Schema.class)); // seenRefs Set mg.invokeStatic(Type.getType(Sets.class), getMethod(Set.class, "newIdentityHashSet")); mg.invokeVirtual(classType, getEncodeMethod(outputType, schema)); mg.returnValue(); mg.endMethod(); }
From source file:co.cask.cdap.internal.io.DatumWriterGenerator.java
License:Apache License
/** * Generates method body for encoding simple schema type by calling corresponding write method in Encoder. * @param mg Method body generator/* ww w .ja v a2 s .c o m*/ * @param type Data type to encode * @param encodeMethod Name of the encode method to invoke on the given encoder. * @param value Argument index of the value to encode. * @param encoder Method argument index of the encoder */ private void encodeSimple(GeneratorAdapter mg, TypeToken<?> type, Schema schema, String encodeMethod, int value, int encoder) { // encoder.writeXXX(value); TypeToken<?> encodeType = type; mg.loadArg(encoder); mg.loadArg(value); if (Primitives.isWrapperType(encodeType.getRawType())) { encodeType = TypeToken.of(Primitives.unwrap(encodeType.getRawType())); mg.unbox(Type.getType(encodeType.getRawType())); // A special case since INT type represents (byte, char, short and int). if (schema.getType() == Schema.Type.INT && !int.class.equals(encodeType.getRawType())) { encodeType = TypeToken.of(int.class); } } else if (schema.getType() == Schema.Type.STRING && !String.class.equals(encodeType.getRawType())) { // For non-string object that has a String schema, invoke toString(). mg.invokeVirtual(Type.getType(encodeType.getRawType()), getMethod(String.class, "toString")); encodeType = TypeToken.of(String.class); } else if (schema.getType() == Schema.Type.BYTES && UUID.class.equals(encodeType.getRawType())) { // Special case UUID, encode as byte array // ByteBuffer buf = ByteBuffer.allocate(Longs.BYTES * 2) // .putLong(uuid.getMostSignificantBits()) // .putLong(uuid.getLeastSignificantBits()); // encoder.writeBytes((ByteBuffer) buf.flip()); Type byteBufferType = Type.getType(ByteBuffer.class); Type uuidType = Type.getType(UUID.class); mg.push(Longs.BYTES * 2); mg.invokeStatic(byteBufferType, getMethod(ByteBuffer.class, "allocate", int.class)); mg.swap(); mg.invokeVirtual(uuidType, getMethod(long.class, "getMostSignificantBits")); mg.invokeVirtual(byteBufferType, getMethod(ByteBuffer.class, "putLong", long.class)); mg.loadArg(value); mg.invokeVirtual(uuidType, getMethod(long.class, "getLeastSignificantBits")); mg.invokeVirtual(byteBufferType, getMethod(ByteBuffer.class, "putLong", long.class)); mg.invokeVirtual(Type.getType(Buffer.class), getMethod(Buffer.class, "flip")); mg.checkCast(byteBufferType); encodeType = TypeToken.of(ByteBuffer.class); } mg.invokeInterface(Type.getType(Encoder.class), getMethod(Encoder.class, encodeMethod, encodeType.getRawType())); mg.pop(); }
From source file:co.cask.cdap.internal.io.FieldAccessorGenerator.java
License:Apache License
private void initializeReflectionField(GeneratorAdapter mg, Field field) { /*//from w w w .ja v a2s.com Save the reflected Field object for accessing private field. try { Field field = Fields.findField(classType, "fieldName"); field.setAccessible(true); this.field = field; } catch (Exception e) { throw Throwables.propagate(e); } */ Label beginTry = mg.newLabel(); Label endTry = mg.newLabel(); Label catchHandle = mg.newLabel(); mg.visitTryCatchBlock(beginTry, endTry, catchHandle, Type.getInternalName(Exception.class)); mg.mark(beginTry); // Field field = Fields.findField(classType, "fieldName") mg.loadArg(0); mg.push(field.getName()); mg.invokeStatic(Type.getType(Fields.class), getMethod(Field.class, "findField", java.lang.reflect.Type.class, String.class)); mg.dup(); // field.setAccessible(true); mg.push(true); mg.invokeVirtual(Type.getType(Field.class), getMethod(void.class, "setAccessible", boolean.class)); // this.field = field; // need to swap the this reference and the one in top stack (from dup() ). mg.loadThis(); mg.swap(); mg.putField(Type.getObjectType(className), "field", Type.getType(Field.class)); mg.mark(endTry); Label endCatch = mg.newLabel(); mg.goTo(endCatch); mg.mark(catchHandle); int exception = mg.newLocal(Type.getType(IllegalAccessException.class)); mg.storeLocal(exception); mg.loadLocal(exception); mg.invokeStatic(Type.getType(Throwables.class), getMethod(RuntimeException.class, "propagate", Throwable.class)); mg.throwException(); mg.mark(endCatch); }