Example usage for org.objectweb.asm.commons GeneratorAdapter ifNonNull

List of usage examples for org.objectweb.asm.commons GeneratorAdapter ifNonNull

Introduction

In this page you can find the example usage for org.objectweb.asm.commons GeneratorAdapter ifNonNull.

Prototype

public void ifNonNull(final Label label) 

Source Link

Document

Generates the instruction to jump to the given label if the top stack value is not null.

Usage

From source file:io.datakernel.codegen.ExpressionCmpNotNull.java

License:Apache License

@Override
public Type load(Context ctx) {
    GeneratorAdapter g = ctx.getGeneratorAdapter();

    Label labelNotNull = new Label();
    Label labelExit = new Label();

    field.load(ctx);//from  w ww  .j a  v  a  2s.  co  m
    g.ifNonNull(labelNotNull);
    g.push(false);
    g.goTo(labelExit);

    g.mark(labelNotNull);
    g.push(true);

    g.mark(labelExit);

    return Type.BOOLEAN_TYPE;
}

From source file:io.datakernel.codegen.ExpressionComparatorNullable.java

License:Apache License

@Override
public Type load(Context ctx) {
    GeneratorAdapter g = ctx.getGeneratorAdapter();

    Label labelReturn = new Label();

    for (int i = 0; i < left.size(); i++) {
        Type leftFieldType = left.get(i).load(ctx); // [left]
        Type rightFieldType = right.get(i).load(ctx); // [left, right]

        Preconditions.check(leftFieldType.equals(rightFieldType));
        if (isPrimitiveType(leftFieldType)) {
            g.invokeStatic(wrap(leftFieldType),
                    new Method("compare", INT_TYPE, new Type[] { leftFieldType, leftFieldType }));
            g.dup();//from w  w w  .j a va 2s  .c om
            g.ifZCmp(NE, labelReturn);
            g.pop();
        } else {
            VarLocal varRight = newLocal(ctx, rightFieldType);
            varRight.store(ctx);

            VarLocal varLeft = newLocal(ctx, leftFieldType);
            varLeft.store(ctx);

            Label continueLabel = new Label();
            Label nonNulls = new Label();
            Label leftNonNull = new Label();

            varLeft.load(ctx);
            g.ifNonNull(leftNonNull);

            varRight.load(ctx);
            g.ifNull(continueLabel);
            g.push(-1);
            g.returnValue();

            g.mark(leftNonNull);

            varRight.load(ctx);
            g.ifNonNull(nonNulls);
            g.push(1);
            g.returnValue();

            g.mark(nonNulls);

            varLeft.load(ctx);
            varRight.load(ctx);

            g.invokeVirtual(leftFieldType,
                    new Method("compareTo", INT_TYPE, new Type[] { Type.getType(Object.class) }));
            g.dup();
            g.ifZCmp(NE, labelReturn);
            g.pop();

            g.mark(continueLabel);
        }
    }

    g.push(0);

    g.mark(labelReturn);

    return INT_TYPE;
}

From source file:org.apache.aries.proxy.impl.common.AbstractWovenProxyAdapter.java

License:Apache License

/**
 * Write the methods we need for wovenProxies on the highest supertype
 *//*from w  ww.  j  a  va  2 s. c  o m*/
private final void writeFinalWovenProxyMethods() {
    // add private fields for the Callable<Object> dispatcher
    // and InvocationListener. These aren't static because we can have
    // multiple instances of the same proxy class. These should not be
    // serialized, or used in JPA or any other thing we can think of,
    // so we annotate them as necessary

    generateField(DISPATCHER_FIELD, Type.getDescriptor(Callable.class));
    generateField(LISTENER_FIELD, Type.getDescriptor(InvocationListener.class));

    // a general methodAdapter field that we will use to with GeneratorAdapters
    // to create the methods required to implement WovenProxy
    GeneratorAdapter methodAdapter;

    // add a method for unwrapping the dispatcher
    methodAdapter = getMethodGenerator(PUBLIC_GENERATED_METHOD_ACCESS,
            new Method("org_apache_aries_proxy_weaving_WovenProxy_unwrap", DISPATCHER_TYPE, NO_ARGS));

    // /////////////////////////////////////////////////////
    // Implement the method

    // load this to get the field
    methodAdapter.loadThis();
    // get the dispatcher field and return
    methodAdapter.getField(typeBeingWoven, DISPATCHER_FIELD, DISPATCHER_TYPE);
    methodAdapter.returnValue();
    methodAdapter.endMethod();

    // /////////////////////////////////////////////////////

    // add a method for checking if the dispatcher is set
    methodAdapter = getMethodGenerator(PUBLIC_GENERATED_METHOD_ACCESS, new Method(
            "org_apache_aries_proxy_weaving_WovenProxy_isProxyInstance", Type.BOOLEAN_TYPE, NO_ARGS));

    // /////////////////////////////////////////////////////
    // Implement the method

    // load this to get the field
    methodAdapter.loadThis();
    // make a label for return true
    Label returnTrueLabel = methodAdapter.newLabel();
    // get the dispatcher field for the stack
    methodAdapter.getField(typeBeingWoven, DISPATCHER_FIELD, DISPATCHER_TYPE);
    // check if the dispatcher was non-null and goto return true if it was
    methodAdapter.ifNonNull(returnTrueLabel);
    methodAdapter.loadThis();
    // get the listener field for the stack
    methodAdapter.getField(typeBeingWoven, LISTENER_FIELD, LISTENER_TYPE);
    // check if the listener field was non-null and goto return true if it was
    methodAdapter.ifNonNull(returnTrueLabel);
    // return false if we haven't jumped anywhere
    methodAdapter.push(false);
    methodAdapter.returnValue();
    // mark the returnTrueLable
    methodAdapter.mark(returnTrueLabel);
    methodAdapter.push(true);
    methodAdapter.returnValue();
    // end the method
    methodAdapter.endMethod();

    // ///////////////////////////////////////////////////////
}

From source file:org.apache.aries.proxy.impl.common.AbstractWovenProxyAdapter.java

License:Apache License

/**
 * We write createNewProxyInstance separately because it isn't final, and is
 * overridden on each class, we also write a constructor for this method to
 * use if we don't have one.//from w  w w .j a v a 2  s .  com
 */
private final void writeCreateNewProxyInstanceAndConstructor() {
    GeneratorAdapter methodAdapter = getMethodGenerator(ACC_PUBLIC,
            new Method("org_apache_aries_proxy_weaving_WovenProxy_createNewProxyInstance",
                    WOVEN_PROXY_IFACE_TYPE, DISPATCHER_LISTENER_METHOD_ARGS));

    // /////////////////////////////////////////////////////
    // Implement the method

    // Create and instantiate a new instance, then return it
    methodAdapter.newInstance(typeBeingWoven);
    methodAdapter.dup();
    methodAdapter.loadArgs();
    methodAdapter.invokeConstructor(typeBeingWoven,
            new Method("<init>", Type.VOID_TYPE, DISPATCHER_LISTENER_METHOD_ARGS));
    methodAdapter.returnValue();
    methodAdapter.endMethod();
    //////////////////////////////////////////////////////////

    // Write a protected no-args constructor for this class
    methodAdapter = getMethodGenerator(ACC_PROTECTED | ACC_SYNTHETIC, ARGS_CONSTRUCTOR);

    // /////////////////////////////////////////////////////
    // Implement the constructor

    // For the top level supertype we need to invoke a no-args super, on object 
    //if we have to

    if (implementWovenProxy) {
        methodAdapter.loadThis();

        if (superHasNoArgsConstructor)
            methodAdapter.invokeConstructor(superType, NO_ARGS_CONSTRUCTOR);
        else {
            if (hasNoArgsConstructor)
                methodAdapter.invokeConstructor(typeBeingWoven, NO_ARGS_CONSTRUCTOR);
            else
                throw new RuntimeException(new UnableToProxyException(typeBeingWoven.getClassName(),
                        NLS.MESSAGES.getMessage("type.lacking.no.arg.constructor",
                                typeBeingWoven.getClassName(), superType.getClassName())));
        }
        methodAdapter.loadThis();
        methodAdapter.loadArg(0);
        methodAdapter.putField(typeBeingWoven, DISPATCHER_FIELD, DISPATCHER_TYPE);

        methodAdapter.loadThis();
        methodAdapter.loadArg(1);
        methodAdapter.putField(typeBeingWoven, LISTENER_FIELD, LISTENER_TYPE);
    } else {
        //We just invoke the super with args
        methodAdapter.loadThis();
        methodAdapter.loadArgs();
        methodAdapter.invokeConstructor(superType, ARGS_CONSTRUCTOR);
    }

    //Throw an NPE if the dispatcher is null, return otherwise
    methodAdapter.loadArg(0);
    Label returnValue = methodAdapter.newLabel();
    methodAdapter.ifNonNull(returnValue);
    methodAdapter.newInstance(NPE_TYPE);
    methodAdapter.dup();
    methodAdapter.push("The dispatcher must never be null!");
    methodAdapter.invokeConstructor(NPE_TYPE, NPE_CONSTRUCTOR);
    methodAdapter.throwException();

    methodAdapter.mark(returnValue);
    methodAdapter.returnValue();
    methodAdapter.endMethod();
    //////////////////////////////////////////////////////////
}

From source file:org.formulacompiler.compiler.internal.bytecode.ArrayAccessorForConstDataCompiler.java

License:Open Source License

@Override
protected void compileBody() throws CompilerException {
    final GeneratorAdapter mv = mv();
    final ArrayDescriptor dim = this.arrayNode.arrayDescriptor();
    final int n = dim.numberOfElements();
    final DataType eltDataType = this.arrayNode.getDataType();
    final TypeCompiler eltCompiler = section().engineCompiler().typeCompiler(eltDataType);
    final Type eltType = eltCompiler.type();

    // private double[] xy;
    final FieldVisitor fv = cw().visitField(Opcodes.ACC_PRIVATE, methodName(), arrayDescriptor(), null, null);
    fv.visitEnd();//from www  .ja  v  a  2s. co  m

    // if (this.xy == null) {
    final Label skipInit = mv.newLabel();
    mv.loadThis();
    mv.visitFieldInsn(Opcodes.GETFIELD, section().classInternalName(), methodName(), arrayDescriptor());
    mv.ifNonNull(skipInit);

    // ... new double[ n ]
    mv.loadThis();
    mv.push(n);
    mv.newArray(eltType);

    // ... { c1, c2, ... }
    int i = 0;
    for (ExpressionNode elt : this.arrayNode.arguments()) {
        if (elt instanceof ExpressionNodeForConstantValue) {
            mv.visitInsn(Opcodes.DUP);
            mv.push(i);
            final ExpressionNodeForConstantValue constElt = (ExpressionNodeForConstantValue) elt;
            eltCompiler.compileConst(mv, constElt.value());
            mv.arrayStore(eltType);
        }
        i++;
    }

    // this.xy *=* new double[] { ... }
    mv.visitFieldInsn(Opcodes.PUTFIELD, section().classInternalName(), methodName(), arrayDescriptor());

    // }
    // return this.xy;
    mv.mark(skipInit);
    mv.loadThis();
    mv.visitFieldInsn(Opcodes.GETFIELD, section().classInternalName(), methodName(), arrayDescriptor());
    mv.visitInsn(Opcodes.ARETURN);
}

From source file:org.formulacompiler.compiler.internal.bytecode.SubSectionLazyGetterCompiler.java

License:Open Source License

@Override
protected void compileBody() throws CompilerException {
    final SubSectionCompiler sub = this.sub;
    final GeneratorAdapter mv = mv();

    // if (this.field == null) {
    final Label alreadySet = mv.newLabel();
    mv.loadThis();/*from  ww w  .  jav a  2s. c o m*/
    mv.getField(section().classType(), sub.getterName(), sub.arrayType());
    mv.ifNonNull(alreadySet);

    // ~ final DetailInput[] ds = this.inputs.getarray();
    final CallFrame inputCall = sub.model().getCallChainToCall();
    final Class inputContainerClass = inputCall.getMethod().getReturnType();
    final Type inputContainerType = Type.getType(inputContainerClass);
    final int l_ds = mv.newLocal(inputContainerType);
    compileInputGetterCall(inputCall);
    mv.storeLocal(l_ds);

    // ~ if (ds != null) {
    final Label isNull = mv.newLabel();
    mv.loadLocal(l_ds);
    mv.ifNull(isNull);

    final int l_di;
    if (inputContainerClass.isArray()) {
        l_di = compileInitFromArray(sub, mv, l_ds);
    } else if (Collection.class.isAssignableFrom(inputContainerClass)) {
        l_di = compileInitFromCollection(sub, mv, l_ds);
    } else if (Iterable.class.isAssignableFrom(inputContainerClass)) {
        final int l_it = mv.newLocal(ITERATOR_INTF);
        mv.loadLocal(l_ds);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, ITERABLE_INTF.getInternalName(), "iterator",
                "()" + ITERATOR_INTF.getDescriptor());
        mv.storeLocal(l_it);
        l_di = compileInitFromIterator(sub, mv, l_it);
    } else if (Iterator.class.isAssignableFrom(inputContainerClass)) {
        l_di = compileInitFromIterator(sub, mv, l_ds);
    } else {
        throw new CompilerException.UnsupportedDataType("The return type of '" + inputCall.getMethod()
                + "' is not supported as input to a repeating section.");
    }

    // ~ ~ this.field = di;
    mv.loadThis();
    mv.loadLocal(l_di);
    mv.putField(section().classType(), sub.getterName(), sub.arrayType());

    // ~ } else {
    mv.goTo(alreadySet);
    mv.mark(isNull);

    // ~ ~ this.field = new DetailPrototype[ 0 ];
    mv.loadThis();
    mv.push(0);
    mv.newArray(sub.classType());
    mv.putField(section().classType(), sub.getterName(), sub.arrayType());

    // ~ }
    // }
    mv.mark(alreadySet);

    // return this.field;
    mv.loadThis();
    mv.getField(section().classType(), sub.getterName(), sub.arrayType());
    mv.visitInsn(Opcodes.ARETURN);
}

From source file:org.jruby.javasupport.proxy.JavaProxyClassFactory.java

License:LGPL

private static void generateProxyMethod(Type selfType, Type superType, ClassVisitor cw,
        GeneratorAdapter clazzInit, MethodData md) {
    if (!md.generateProxyMethod()) {
        return;/*from   ww  w  . j  a  v  a2s.c  o m*/
    }

    org.objectweb.asm.commons.Method m = md.getMethod();
    Type[] ex = toType(md.getExceptions());

    String field_name = "__mth$" + md.getName() + md.scrambledSignature();

    // create static private method field
    FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, field_name,
            PROXY_METHOD_TYPE.getDescriptor(), null, null);
    fv.visitEnd();

    clazzInit.dup();
    clazzInit.push(m.getName());
    clazzInit.push(m.getDescriptor());
    clazzInit.push(md.isImplemented());
    clazzInit.invokeStatic(PROXY_HELPER_TYPE, PROXY_HELPER_GET_METHOD);
    clazzInit.putStatic(selfType, field_name, PROXY_METHOD_TYPE);

    org.objectweb.asm.commons.Method sm = new org.objectweb.asm.commons.Method("__super$" + m.getName(),
            m.getReturnType(), m.getArgumentTypes());

    //
    // construct the proxy method
    //
    GeneratorAdapter ga = new GeneratorAdapter(Opcodes.ACC_PUBLIC, m, null, ex, cw);

    ga.loadThis();
    ga.getField(selfType, INVOCATION_HANDLER_FIELD_NAME, INVOCATION_HANDLER_TYPE);

    // if the method is extending something, then we have
    // to test if the handler is initialized...

    if (md.isImplemented()) {
        ga.dup();
        Label ok = ga.newLabel();
        ga.ifNonNull(ok);

        ga.loadThis();
        ga.loadArgs();
        ga.invokeConstructor(superType, m);
        ga.returnValue();
        ga.mark(ok);
    }

    ga.loadThis();
    ga.getStatic(selfType, field_name, PROXY_METHOD_TYPE);

    if (m.getArgumentTypes().length == 0) {
        // load static empty array
        ga.getStatic(JAVA_PROXY_TYPE, "NO_ARGS", Type.getType(Object[].class));
    } else {
        // box arguments
        ga.loadArgArray();
    }

    Label before = ga.mark();

    ga.invokeInterface(INVOCATION_HANDLER_TYPE, INVOCATION_HANDLER_INVOKE_METHOD);

    Label after = ga.mark();

    ga.unbox(m.getReturnType());
    ga.returnValue();

    // this is a simple rethrow handler
    Label rethrow = ga.mark();
    ga.visitInsn(Opcodes.ATHROW);

    for (int i = 0; i < ex.length; i++) {
        ga.visitTryCatchBlock(before, after, rethrow, ex[i].getInternalName());
    }

    ga.visitTryCatchBlock(before, after, rethrow, "java/lang/Error");
    ga.visitTryCatchBlock(before, after, rethrow, "java/lang/RuntimeException");

    Type thr = Type.getType(Throwable.class);
    Label handler = ga.mark();
    Type udt = Type.getType(UndeclaredThrowableException.class);
    int loc = ga.newLocal(thr);
    ga.storeLocal(loc, thr);
    ga.newInstance(udt);
    ga.dup();
    ga.loadLocal(loc, thr);
    ga.invokeConstructor(udt, org.objectweb.asm.commons.Method.getMethod("void <init>(java.lang.Throwable)"));
    ga.throwException();

    ga.visitTryCatchBlock(before, after, handler, "java/lang/Throwable");

    ga.endMethod();

    //
    // construct the super-proxy method
    //
    if (md.isImplemented()) {

        GeneratorAdapter ga2 = new GeneratorAdapter(Opcodes.ACC_PUBLIC, sm, null, ex, cw);

        ga2.loadThis();
        ga2.loadArgs();
        ga2.invokeConstructor(superType, m);
        ga2.returnValue();
        ga2.endMethod();
    }
}