Example usage for org.objectweb.asm Opcodes RETURN

List of usage examples for org.objectweb.asm Opcodes RETURN

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes RETURN.

Prototype

int RETURN

To view the source code for org.objectweb.asm Opcodes RETURN.

Click Source Link

Usage

From source file:org.graalvm.compiler.replacements.test.DeoptimizeOnExceptionTest.java

License:Open Source License

private static byte[] makeClazz() {
    // Code generated the class below using asm.
    String clazzName = DeoptimizeOnExceptionTest.class.getName().replace('.', '/');
    final ClassWriter w = new ClassWriter(0);
    w.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "t/TestJSR", null, "java/lang/Object",
            new String[] { "java/lang/Runnable" });
    MethodVisitor mv = w.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[] {});
    mv.visitCode();/*from   w w  w. j  ava2s. c o m*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(10, 10);
    mv.visitEnd();

    mv = w.visitMethod(Opcodes.ACC_PUBLIC, "run", "()V", null, null);
    mv.visitCode();
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "getM", "()Ljava/lang/Object;", false);
    Label l1 = new Label();
    mv.visitJumpInsn(Opcodes.JSR, l1);
    mv.visitInsn(Opcodes.RETURN);

    mv.visitLabel(l1);
    mv.visitVarInsn(Opcodes.ASTORE, 1);

    Label lElse = new Label();
    Label lEnd = new Label();
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false);
    mv.visitInsn(Opcodes.POP2);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "getM", "()Ljava/lang/Object;", false);
    mv.visitInsn(Opcodes.DUP);
    mv.visitJumpInsn(Opcodes.IFNULL, lElse);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "methodA", "()V", false);
    mv.visitJumpInsn(Opcodes.GOTO, lEnd);
    mv.visitLabel(lElse);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "methodB", "()V", false);
    mv.visitLabel(lEnd);

    mv.visitVarInsn(Opcodes.RET, 1);
    mv.visitMaxs(10, 10);
    mv.visitEnd();
    return w.toByteArray();
}

From source file:org.gradle.api.internal.model.DefaultObjectFactory.java

License:Apache License

private ClassGeneratingLoader loaderFor(Class<?> publicClass) {
    ///*from  w w w  .j  ava  2s . c  o m*/
    // Generate implementation class
    //

    FormattingValidationProblemCollector problemCollector = new FormattingValidationProblemCollector(
            "Named implementation class", ModelType.of(publicClass));
    visitFields(publicClass, problemCollector);
    if (problemCollector.hasProblems()) {
        throw new GradleException(problemCollector.format());
    }

    AsmClassGenerator generator = new AsmClassGenerator(publicClass, "$Impl");
    Type implementationType = generator.getGeneratedType();
    ClassWriter visitor = generator.getVisitor();
    Type publicType = Type.getType(publicClass);

    Type superClass;
    String[] interfaces;
    if (publicClass.isInterface()) {
        superClass = OBJECT;
        interfaces = new String[] { publicType.getInternalName() };
    } else {
        superClass = publicType;
        interfaces = EMPTY_STRINGS;
    }

    visitor.visit(V1_5, ACC_PUBLIC | ACC_SYNTHETIC, implementationType.getInternalName(), null,
            superClass.getInternalName(), interfaces);

    //
    // Add name field
    //

    visitor.visitField(ACC_PRIVATE, NAME_FIELD, STRING.getDescriptor(), null, null);

    //
    // Add constructor
    //

    MethodVisitor methodVisitor = visitor.visitMethod(ACC_PUBLIC, CONSTRUCTOR_NAME, RETURN_VOID_FROM_STRING,
            null, EMPTY_STRINGS);
    methodVisitor.visitCode();
    // Call this.super()
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superClass.getInternalName(), CONSTRUCTOR_NAME,
            RETURN_VOID, false);
    // Set this.name = param1
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
    methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, implementationType.getInternalName(), NAME_FIELD,
            STRING.getDescriptor());
    // Done
    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    //
    // Add `getName()`
    //

    methodVisitor = visitor.visitMethod(ACC_PUBLIC, "getName", RETURN_STRING, null, EMPTY_STRINGS);
    methodVisitor.visitCode();
    // return this.name
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitFieldInsn(Opcodes.GETFIELD, implementationType.getInternalName(), NAME_FIELD,
            STRING.getDescriptor());
    methodVisitor.visitInsn(Opcodes.ARETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    //
    // Add `toString()`
    //

    methodVisitor = visitor.visitMethod(ACC_PUBLIC, "toString", RETURN_STRING, null, EMPTY_STRINGS);
    methodVisitor.visitCode();
    // return this.name
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitFieldInsn(Opcodes.GETFIELD, implementationType.getInternalName(), NAME_FIELD,
            STRING.getDescriptor());
    methodVisitor.visitInsn(Opcodes.ARETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    visitor.visitEnd();
    generator.define();

    //
    // Generate factory class
    //

    generator = new AsmClassGenerator(publicClass, "$Factory");
    visitor = generator.getVisitor();
    visitor.visit(V1_5, ACC_PUBLIC | ACC_SYNTHETIC, generator.getGeneratedType().getInternalName(), null,
            Type.getType(ClassGeneratingLoader.class).getInternalName(), EMPTY_STRINGS);

    //
    // Add constructor
    //

    methodVisitor = visitor.visitMethod(ACC_PUBLIC, CONSTRUCTOR_NAME, RETURN_VOID, null, EMPTY_STRINGS);
    methodVisitor.visitCode();
    // Call this.super()
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL,
            Type.getType(ClassGeneratingLoader.class).getInternalName(), CONSTRUCTOR_NAME, RETURN_VOID, false);
    // Done
    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    //
    // Add factory method
    //

    methodVisitor = visitor.visitMethod(ACC_PUBLIC, "load", Type.getMethodDescriptor(OBJECT, STRING), null,
            EMPTY_STRINGS);
    methodVisitor.visitCode();
    // Call return new <implClass>(param1)
    methodVisitor.visitTypeInsn(Opcodes.NEW, implementationType.getInternalName());
    methodVisitor.visitInsn(Opcodes.DUP);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, implementationType.getInternalName(), CONSTRUCTOR_NAME,
            RETURN_VOID_FROM_STRING, false);
    methodVisitor.visitInsn(Opcodes.ARETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    visitor.visitEnd();
    Class<Object> factoryClass = generator.define();
    try {
        return (ClassGeneratingLoader) (factoryClass.newInstance());
    } catch (Exception e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
}

From source file:org.gradle.api.internal.model.NamedObjectInstantiator.java

License:Apache License

private ClassGeneratingLoader loaderFor(Class<?> publicClass) {
    ////  w  ww  . j ava2  s . c  o  m
    // Generate implementation class
    //

    FormattingValidationProblemCollector problemCollector = new FormattingValidationProblemCollector(
            "Named implementation class", ModelType.of(publicClass));
    visitFields(publicClass, problemCollector);
    if (problemCollector.hasProblems()) {
        throw new GradleException(problemCollector.format());
    }

    AsmClassGenerator generator = new AsmClassGenerator(publicClass, "$Impl");
    Type implementationType = generator.getGeneratedType();
    ClassWriter visitor = generator.getVisitor();
    Type publicType = Type.getType(publicClass);

    Type superClass;
    String[] interfaces;
    if (publicClass.isInterface()) {
        superClass = OBJECT;
        interfaces = new String[] { publicType.getInternalName(), MANAGED.getInternalName() };
    } else {
        superClass = publicType;
        interfaces = INTERFACES_FOR_ABSTRACT_CLASS;
    }

    visitor.visit(V1_5, ACC_PUBLIC | ACC_SYNTHETIC, implementationType.getInternalName(), null,
            superClass.getInternalName(), interfaces);

    //
    // Add name field
    //

    visitor.visitField(ACC_PRIVATE, NAME_FIELD, STRING.getDescriptor(), null, null);

    //
    // Add constructor
    //

    MethodVisitor methodVisitor = visitor.visitMethod(ACC_PUBLIC, CONSTRUCTOR_NAME, RETURN_VOID_FROM_STRING,
            null, EMPTY_STRINGS);
    methodVisitor.visitCode();
    // Call this.super()
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superClass.getInternalName(), CONSTRUCTOR_NAME,
            RETURN_VOID, false);
    // Set this.name = param1
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
    methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, implementationType.getInternalName(), NAME_FIELD,
            STRING.getDescriptor());
    // Done
    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    //
    // Add `getName()`
    //

    methodVisitor = visitor.visitMethod(ACC_PUBLIC, "getName", RETURN_STRING, null, EMPTY_STRINGS);
    methodVisitor.visitCode();
    // return this.name
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitFieldInsn(Opcodes.GETFIELD, implementationType.getInternalName(), NAME_FIELD,
            STRING.getDescriptor());
    methodVisitor.visitInsn(Opcodes.ARETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    //
    // Add `toString()`
    //

    methodVisitor = visitor.visitMethod(ACC_PUBLIC, "toString", RETURN_STRING, null, EMPTY_STRINGS);
    methodVisitor.visitCode();
    // return this.name
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitFieldInsn(Opcodes.GETFIELD, implementationType.getInternalName(), NAME_FIELD,
            STRING.getDescriptor());
    methodVisitor.visitInsn(Opcodes.ARETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    visitor.visitEnd();
    generator.define();

    //
    // Generate factory class
    //

    generator = new AsmClassGenerator(publicClass, "$Factory");
    visitor = generator.getVisitor();
    visitor.visit(V1_5, ACC_PUBLIC | ACC_SYNTHETIC, generator.getGeneratedType().getInternalName(), null,
            CLASS_GENERATING_LOADER.getInternalName(), EMPTY_STRINGS);

    //
    // Add constructor
    //

    methodVisitor = visitor.visitMethod(ACC_PUBLIC, CONSTRUCTOR_NAME, RETURN_VOID, null, EMPTY_STRINGS);
    methodVisitor.visitCode();
    // Call this.super()
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, CLASS_GENERATING_LOADER.getInternalName(),
            CONSTRUCTOR_NAME, RETURN_VOID, false);
    // Done
    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    //
    // Add factory method
    //

    methodVisitor = visitor.visitMethod(ACC_PUBLIC, "load", RETURN_OBJECT_FROM_STRING, null, EMPTY_STRINGS);
    methodVisitor.visitCode();
    // Call return new <implClass>(param1)
    methodVisitor.visitTypeInsn(Opcodes.NEW, implementationType.getInternalName());
    methodVisitor.visitInsn(Opcodes.DUP);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, implementationType.getInternalName(), CONSTRUCTOR_NAME,
            RETURN_VOID_FROM_STRING, false);
    methodVisitor.visitInsn(Opcodes.ARETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    visitor.visitEnd();
    Class<Object> factoryClass = generator.define();
    try {
        return (ClassGeneratingLoader) factoryClass.getConstructor().newInstance();
    } catch (Exception e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
}

From source file:org.gradle.groovy.scripts.AsmBackedEmptyScriptGenerator.java

License:Apache License

private <T extends Script> Class<? extends T> generateEmptyScriptClass(Class<T> type) {
    ClassWriter visitor = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    String typeName = type.getName() + "_Decorated";
    Type generatedType = Type.getType("L" + typeName.replaceAll("\\.", "/") + ";");
    Type superclassType = Type.getType(type);
    visitor.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, generatedType.getInternalName(), null,
            superclassType.getInternalName(), new String[0]);

    // Constructor

    String constructorDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[0]);
    MethodVisitor methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", constructorDescriptor, null,
            new String[0]);
    methodVisitor.visitCode();//  w w w  .  ja v a  2s . co m

    // super()
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassType.getInternalName(), "<init>",
            constructorDescriptor);

    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    // run() method

    String runDesciptor = Type.getMethodDescriptor(Type.getType(Object.class), new Type[0]);
    methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, "run", runDesciptor, null, new String[0]);
    methodVisitor.visitCode();

    // return null
    methodVisitor.visitInsn(Opcodes.ACONST_NULL);

    methodVisitor.visitInsn(Opcodes.ARETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    visitor.visitEnd();

    byte[] bytecode = visitor.toByteArray();
    return (Class<T>) ReflectionUtil.invoke(type.getClassLoader(), "defineClass",
            new Object[] { typeName, bytecode, 0, bytecode.length });
}

From source file:org.gradle.initialization.ExceptionDecoratingClassGenerator.java

License:Apache License

private <T> Class<? extends T> doGenerate(Class<T> type) {
    ClassWriter visitor = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    String typeName = StringUtils.substringBeforeLast(type.getName(), ".") + ".LocationAware"
            + type.getSimpleName();/*from   w  ww.j  a  va 2s  .c  o m*/
    Type generatedType = Type.getType("L" + typeName.replaceAll("\\.", "/") + ";");
    Type superclassType = Type.getType(type);

    visitor.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, generatedType.getInternalName(), null,
            superclassType.getInternalName(),
            new String[] { Type.getType(LocationAwareException.class).getInternalName() });

    Type helperType = Type.getType(ExceptionHelper.class);
    Type throwableType = Type.getType(Throwable.class);
    Type scriptSourceType = Type.getType(ScriptSource.class);
    Type integerType = Type.getType(Integer.class);

    // GENERATE private ExceptionHelper helper;
    visitor.visitField(Opcodes.ACC_PRIVATE, "helper", helperType.getDescriptor(), null, null);

    // GENERATE <init>(<type> target, ScriptSource source, Integer lineNumber)

    String methodDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE,
            new Type[] { superclassType, scriptSourceType, integerType });
    MethodVisitor methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", methodDescriptor, null,
            new String[0]);
    methodVisitor.visitCode();

    boolean noArgsConstructor;
    try {
        type.getConstructor(type);
        noArgsConstructor = false;
    } catch (NoSuchMethodException e) {
        try {
            type.getConstructor();
            noArgsConstructor = true;
        } catch (NoSuchMethodException e1) {
            throw new IllegalArgumentException(String.format(
                    "Cannot create subtype for exception '%s'. It needs a zero-args or copy constructor.",
                    type.getName()));
        }
    }

    if (noArgsConstructor) {
        // GENERATE super()
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassType.getInternalName(), "<init>",
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[0]));
        // END super()
    } else {
        // GENERATE super(target)
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassType.getInternalName(), "<init>",
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { superclassType }));
        // END super(target)
    }

    // GENERATE helper = new ExceptionHelper(this, target, source, lineNumber)
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);

    methodVisitor.visitTypeInsn(Opcodes.NEW, helperType.getInternalName());
    methodVisitor.visitInsn(Opcodes.DUP);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 2);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 3);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, helperType.getInternalName(), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE,
                    new Type[] { throwableType, throwableType, scriptSourceType, integerType }));

    methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, generatedType.getInternalName(), "helper",
            helperType.getDescriptor());

    // END helper = new ExceptionHelper(target)

    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    // END <init>(<type> target, ScriptSource source, Integer lineNumber)

    for (Method method : ExceptionHelper.class.getDeclaredMethods()) {
        // GENERATE public <type> <method>() { return helper.<method>(); }
        methodDescriptor = Type.getMethodDescriptor(Type.getType(method.getReturnType()), new Type[0]);
        methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), methodDescriptor, null,
                new String[0]);
        methodVisitor.visitCode();

        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitFieldInsn(Opcodes.GETFIELD, generatedType.getInternalName(), "helper",
                helperType.getDescriptor());
        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, helperType.getInternalName(), method.getName(),
                methodDescriptor);

        methodVisitor.visitInsn(Opcodes.ARETURN);
        methodVisitor.visitMaxs(0, 0);
        methodVisitor.visitEnd();
        // END public <type> <method>() { return helper.<method>(); }
    }

    visitor.visitEnd();

    byte[] bytecode = visitor.toByteArray();
    return (Class<T>) ReflectionUtil.invoke(type.getClassLoader(), "defineClass",
            new Object[] { typeName, bytecode, 0, bytecode.length });
}

From source file:org.gradle.model.internal.manage.schema.extract.AbstractProxyClassGenerator.java

License:Apache License

protected void finishVisitingMethod(MethodVisitor methodVisitor) {
    finishVisitingMethod(methodVisitor, Opcodes.RETURN);
}

From source file:org.greencheek.gc.memusage.agent.MonitoringAspectGenerator.java

License:Apache License

/**
 * Visits a field of the class./*from ww  w . ja  va2 s.  c o m*/
 * The method is overriden to perform the functions of:
 * <ol>
 * <li>Adding a static AtomicLong to the class</li>
 * <li>statically initialising the AtomicLong if a static initialiser isn't already present</li>
 * </ol>
 *
 * @param access the field's access flags (see {@link Opcodes}). This
 *        parameter also indicates if the field is synthetic and/or
 *        deprecated.
 * @param name the field's name.
 * @param desc the field's descriptor (see {@link Type Type}).
 * @param signature the field's signature. May be <tt>null</tt> if the
 *        field's type does not use generic types.
 * @param value the field's initial value. This parameter, which may be
 *        <tt>null</tt> if the field does not have an initial value, must
 *        be an {@link Integer}, a {@link Float}, a {@link Long}, a
 *        {@link Double} or a {@link String} (for <tt>int</tt>,
 *        <tt>float</tt>, <tt>long</tt> or <tt>String</tt> fields
 *        respectively). <i>This parameter is only used for static fields</i>.
 *        Its value is ignored for non static fields, which must be
 *        initialized through bytecode instructions in constructors or
 *        methods.
 * @return a visitor to visit field annotations and attributes, or
 *         <tt>null</tt> if this class visitor is not interested in
 *         visiting these annotations and attributes.
 */
public void visitEnd() {
    for (MethodInfo method : overrideMethodNames.values()) {
        System.out.println("Adding: public final static AtomicLong " + method.getFieldName());
        FieldVisitor fv = cv.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC,
                method.getFieldName(), "Ljava/util/concurrent/atomic/AtomicLong;", null, null);
        fv.visitEnd();
    }

    if (!hasCinit) {
        MethodVisitor mv = cv.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();
        AddStaticAtomicLongInitializer initialiser = new AddStaticAtomicLongInitializer(Opcodes.ASM4, mv,
                overrideMethodNames.values());
        initialiser.visitCode();
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

    super.visitEnd();

}

From source file:org.hua.ast.visitors.BytecodeGeneratorASTVisitor.java

@Override
public void visit(ClassDefinition node) throws ASTVisitorException {
    ClassNode classNode = new ClassNode();
    cn = classNode;// www. j a va 2 s.co m

    cn.version = Opcodes.V1_5;
    cn.superName = "java/lang/Object";
    cn.name = node.getIdentifier();
    cn.access = Opcodes.ACC_PUBLIC;

    // create constructor
    mn = new MethodNode(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mn.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    mn.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false));
    mn.instructions.add(new InsnNode(Opcodes.RETURN));

    mn.maxLocals = 1;
    mn.maxStack = 1;
    classNode.methods.add(mn);

    node.getFfDefinitions().accept(this);

    // IMPORTANT: this should be dynamically calculated
    // use COMPUTE_MAXS when computing the ClassWriter,
    // e.g. new ClassWriter(ClassWriter.COMPUTE_MAXS)
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    //         ClassVisitor ca = new CheckClassAdapter(cw);
    //         cn.accept(new CheckClassAdapter(ca));
    TraceClassVisitor cv = new TraceClassVisitor(cw, new PrintWriter(System.out));

    cn.accept(cv);
    //get code
    byte code[] = cw.toByteArray();

    Logger.getLogger(BytecodeGeneratorASTVisitor.class.getName())
            .info("Writing " + node.getIdentifier() + " class to .class file");
    // update to file
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(node.getIdentifier() + ".class");
        fos.write(code);
        fos.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(BytecodeGeneratorASTVisitor.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(BytecodeGeneratorASTVisitor.class.getName()).log(Level.SEVERE, null, ex);
    }

    cn = null;
    System.out.println("##### finished writing to file");

}

From source file:org.hua.ast.visitors.BytecodeGeneratorASTVisitor.java

@Override
public void visit(FunctionDefinition node) throws ASTVisitorException {
    fd = node;//from   ww w.  ja  va2  s . c  o m
    //fix the signature       
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~ " + node.getIdentifier()
            + " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    if (node.getStorageSpecifier() == null) { //if the function is static
        LocalIndexPool safeLocalIndexPool = ASTUtils.getSafeLocalIndexPool(node);
        int localIndex = safeLocalIndexPool.getLocalIndex();
    }
    LocalIndexPool safeLocalIndexPool = ASTUtils.getSafeLocalIndexPool(node);
    String methodType = Type.getMethodDescriptor(node.getType().getTypeSpecifier(),
            node.getParameters().getParameterTypes());
    int accessor = Opcodes.ACC_PUBLIC;
    if (ASTUtils.getIsStatic(node)) {
        accessor = accessor + Opcodes.ACC_STATIC;
    }
    MethodNode methodNode;
    if (node.getIdentifier().equals("main")) {
        methodNode = new MethodNode(accessor, node.getIdentifier(), "([Ljava/lang/String;)V", null, null);
    } else {
        methodNode = new MethodNode(accessor, node.getIdentifier(), methodType, null, null);
    }
    mn = methodNode;

    node.getParameters().accept(this);
    node.getCompoundStatement().accept(this);

    mn.instructions.add(new InsnNode(Opcodes.RETURN));
    //            mn.maxLocals = 30;
    //        mn.maxStack = 30;

    cn.methods.add(mn);

    fd = null;
    mn = null;
}

From source file:org.hua.ast.visitors.BytecodeGeneratorASTVisitor.java

@Override
public void visit(ReturnStatement node) throws ASTVisitorException {
    if (node.getExpression() != null) {
        node.getExpression().accept(this);
        SymTable<SymTableEntry> sTable = ASTUtils.getSafeEnv(node);
        mn.instructions.add(new VarInsnNode(Opcodes.RETURN, 0));
    } else {/*from   w  ww.j a  v  a  2 s  .  c  o  m*/
        mn.instructions.add(new InsnNode(Opcodes.RETURN));
    }
}