Search in sources :

Example 56 with Method

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();
}
Also used : Method(org.objectweb.asm.commons.Method) TypeInfo(com.google.template.soy.jbcsrc.restricted.TypeInfo)

Example 57 with Method

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();
}
Also used : Method(org.objectweb.asm.commons.Method) TypeInfo(com.google.template.soy.jbcsrc.restricted.TypeInfo)

Example 58 with Method

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)) }));
}
Also used : ProtoUtils.hasJsType(com.google.template.soy.internal.proto.ProtoUtils.hasJsType) Type(org.objectweb.asm.Type) JSType(com.google.protobuf.DescriptorProtos.FieldOptions.JSType) SanitizedType(com.google.template.soy.types.SanitizedType) SoyProtoType(com.google.template.soy.types.SoyProtoType) SoyRuntimeType(com.google.template.soy.jbcsrc.restricted.SoyRuntimeType) ProtoUtils.getJsType(com.google.template.soy.internal.proto.ProtoUtils.getJsType) MapType(com.google.template.soy.types.MapType) SoyType(com.google.template.soy.types.SoyType) JavaType(com.google.protobuf.Descriptors.FieldDescriptor.JavaType) ListType(com.google.template.soy.types.ListType) Method(org.objectweb.asm.commons.Method) TypeInfo(com.google.template.soy.jbcsrc.restricted.TypeInfo) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 59 with Method

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());
}
Also used : PrintStream(java.io.PrintStream) Type(org.objectweb.asm.Type) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) JarEntry(java.util.jar.JarEntry) ClassWriter(org.objectweb.asm.ClassWriter)

Example 60 with Method

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();
}
Also used : GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) LoggerFactory(org.slf4j.LoggerFactory) Logger(org.slf4j.Logger)

Aggregations

Method (org.objectweb.asm.commons.Method)78 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)58 Type (org.objectweb.asm.Type)38 ClassWriter (org.objectweb.asm.ClassWriter)14 Label (org.objectweb.asm.Label)14 MethodVisitor (org.objectweb.asm.MethodVisitor)9 IOException (java.io.IOException)7 ClassReader (org.objectweb.asm.ClassReader)7 TypeInfo (com.google.template.soy.jbcsrc.restricted.TypeInfo)6 MethodNode (org.objectweb.asm.tree.MethodNode)6 ExprString (lucee.transformer.expression.ExprString)5 LitString (lucee.transformer.expression.literal.LitString)5 ClassVisitor (org.objectweb.asm.ClassVisitor)5 Schema (co.cask.cdap.api.data.schema.Schema)4 ArrayList (java.util.ArrayList)4 File (java.io.File)3 Set (java.util.Set)3 Nullable (javax.annotation.Nullable)3 ExprDouble (lucee.transformer.expression.ExprDouble)3 MetricsContext (co.cask.cdap.api.metrics.MetricsContext)2