use of org.objectweb.asm.commons.Method in project closure-templates by google.
the class ProtoUtils method getGetterMethod.
/**
* Returns the {@link MethodRef} for the generated getter method.
*/
private static MethodRef getGetterMethod(FieldDescriptor descriptor) {
Preconditions.checkArgument(!descriptor.isExtension(), "extensions do not have getter methods. %s", descriptor);
TypeInfo message = messageRuntimeType(descriptor.getContainingType());
boolean isProto3Enum = isProto3EnumField(descriptor);
return MethodRef.createInstanceMethod(message, new Method("get" + getFieldName(descriptor, true) + // For proto3 enums we access the Value field
(isProto3Enum ? "Value" : ""), isProto3Enum ? Type.INT_TYPE : getRuntimeType(descriptor), NO_METHOD_ARGS)).asNonNullable().asCheap();
}
use of org.objectweb.asm.commons.Method in project closure-templates by google.
the class ProtoUtils method getBuildMethod.
/**
* Returns the {@link MethodRef} for the generated build method.
*/
private static MethodRef getBuildMethod(Descriptor descriptor) {
TypeInfo message = messageRuntimeType(descriptor);
TypeInfo builder = builderRuntimeType(descriptor);
return MethodRef.createInstanceMethod(builder, new Method("build", message.type(), NO_METHOD_ARGS)).asNonNullable();
}
use of org.objectweb.asm.commons.Method in project closure-templates by google.
the class ProtoUtils method getPutMethod.
/**
* Returns the {@link MethodRef} for the generated put method for proto map.
*/
private static MethodRef getPutMethod(FieldDescriptor descriptor) {
Preconditions.checkState(descriptor.isMapField());
List<FieldDescriptor> mapFields = descriptor.getMessageType().getFields();
TypeInfo builder = builderRuntimeType(descriptor.getContainingType());
return MethodRef.createInstanceMethod(builder, new Method("put" + getFieldName(descriptor, true), builder.type(), new Type[] { getRuntimeType(mapFields.get(0)), getRuntimeType(mapFields.get(1)) }));
}
use of org.objectweb.asm.commons.Method in project cdap by caskdata.
the class ContainerLauncherGenerator method generateMainClass.
/**
* 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
*/
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) throws Exception {
// 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());
}
use of org.objectweb.asm.commons.Method in project cdap by caskdata.
the class HttpHandlerGenerator method generateLogger.
/**
* Generates a static Logger field for logging and a static initialization block to initialize the logger.
*/
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();
}
Aggregations