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

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

Introduction

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

Prototype

public void ifNull(final Label label) 

Source Link

Document

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

Usage

From source file:co.cask.cdap.internal.io.DatumWriterGenerator.java

License:Apache License

/**
 * Generates method body for encoding java class. If the class given is an interface,
 * getter method will be used to access the field values, otherwise, it will assumes
 * fields are public./*  www  .  j a  va  2s .  com*/
 *
 * @param mg
 * @param schema
 * @param outputType
 * @param value
 * @param encoder
 * @param schemaLocal
 * @param seenRefs
 */
private void encodeRecord(GeneratorAdapter mg, Schema schema, TypeToken<?> outputType, int value, int encoder,
        int schemaLocal, int seenRefs) {

    try {
        Class<?> rawType = outputType.getRawType();

        // Record type might be defined by the user, hence need to preserve class loading of it
        preservedClasses.add(rawType);
        boolean isInterface = rawType.isInterface();

        /*
          Check for circular reference
          if (value != null && !seenRefs.add(value)) {
             throw new IOException(...);
          }
        */
        Label notSeen = mg.newLabel();
        mg.loadArg(value);
        mg.ifNull(notSeen);
        mg.loadArg(seenRefs);
        mg.loadArg(value);
        mg.invokeInterface(Type.getType(Set.class), getMethod(boolean.class, "add", Object.class));
        mg.ifZCmp(GeneratorAdapter.NE, notSeen);
        mg.throwException(Type.getType(IOException.class), "Circular reference not supported.");
        mg.mark(notSeen);

        // Store the list of schema fields.
        mg.loadArg(schemaLocal);
        mg.invokeVirtual(Type.getType(Schema.class), getMethod(List.class, "getFields"));
        int fieldSchemas = mg.newLocal(Type.getType(List.class));
        mg.storeLocal(fieldSchemas);

        // For each field, call the encode method for the field
        List<Schema.Field> fields = schema.getFields();
        for (int i = 0; i < fields.size(); i++) {
            Schema.Field field = fields.get(i);

            TypeToken<?> fieldType;

            // this.encodeFieldMethod(value.fieldName, encoder, fieldSchemas.get(i).getSchema());
            if (isInterface) {
                mg.loadThis();
                mg.loadArg(value);
                Method getter = getGetter(outputType, field.getName());
                fieldType = outputType.resolveType(rawType.getMethod(getter.getName()).getGenericReturnType());
                mg.invokeInterface(Type.getType(rawType), getter);
            } else {
                fieldType = outputType
                        .resolveType(Fields.findField(outputType.getType(), field.getName()).getGenericType());
                fieldAccessorRequests.put(outputType, field.getName());
                mg.loadThis();
                mg.dup();
                mg.getField(classType, getFieldAccessorName(outputType, field.getName()),
                        Type.getType(FieldAccessor.class));
                mg.loadArg(value);
                mg.invokeInterface(Type.getType(FieldAccessor.class), getAccessorMethod(fieldType));
                if (!fieldType.getRawType().isPrimitive()) {
                    doCast(mg, fieldType, field.getSchema());
                }
            }
            mg.loadArg(encoder);
            mg.loadLocal(fieldSchemas);
            mg.push(i);
            mg.invokeInterface(Type.getType(List.class), getMethod(Object.class, "get", int.class));
            mg.checkCast(Type.getType(Schema.Field.class));
            mg.invokeVirtual(Type.getType(Schema.Field.class), getMethod(Schema.class, "getSchema"));
            mg.loadArg(seenRefs);
            mg.invokeVirtual(classType, getEncodeMethod(fieldType, field.getSchema()));
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

From source file:co.cask.cdap.internal.io.DatumWriterGenerator.java

License:Apache License

/**
 * Generates method body for encoding union schema. Union schema is used for representing object references that
 * could be {@code null}./*  w  w w  .  j a v  a  2 s . c  o  m*/
 * @param mg
 * @param outputType
 * @param schema
 * @param value
 * @param encoder
 * @param schemaLocal
 * @param seenRefs
 */
private void encodeUnion(GeneratorAdapter mg, TypeToken<?> outputType, Schema schema, int value, int encoder,
        int schemaLocal, int seenRefs) {
    Label nullLabel = mg.newLabel();
    Label endLabel = mg.newLabel();
    mg.loadArg(value);

    mg.ifNull(nullLabel);
    // Not null, write out 0 and then encode the value
    encodeInt(mg, 0, encoder);

    mg.loadThis();
    mg.loadArg(value);
    doCast(mg, outputType, schema.getUnionSchema(0));
    mg.loadArg(encoder);
    mg.loadArg(schemaLocal);
    mg.push(0);
    mg.invokeVirtual(Type.getType(Schema.class), getMethod(Schema.class, "getUnionSchema", int.class));
    mg.loadArg(seenRefs);
    mg.invokeVirtual(classType, getEncodeMethod(outputType, schema.getUnionSchema(0)));

    mg.goTo(endLabel);

    mg.mark(nullLabel);
    // Null, write out 1
    encodeInt(mg, 1, encoder);
    mg.mark(endLabel);
}

From source file:co.cask.common.internal.io.DatumWriterGenerator.java

License:Apache License

/**
 * Generates method body for encoding java class. If the class given is an interface,
 * getter method will be used to access the field values, otherwise, it will assumes
 * fields are public./*from w w w. ja v a  2 s  .  c o m*/
 *
 * @param mg
 * @param schema
 * @param outputType
 * @param value
 * @param encoder
 * @param schemaLocal
 * @param seenRefs
 */
private void encodeRecord(GeneratorAdapter mg, Schema schema, TypeToken<?> outputType, int value, int encoder,
        int schemaLocal, int seenRefs) {

    try {
        Class<?> rawType = outputType.getRawType();

        // Record type might be defined by the user, hence need to preserve class loading of it
        preservedClasses.add(rawType);
        boolean isInterface = rawType.isInterface();

        /*
          Check for circular reference
          if (value != null && !seenRefs.add(value)) {
             throw new IOException(...);
          }
        */
        Label notSeen = mg.newLabel();
        mg.loadArg(value);
        mg.ifNull(notSeen);
        mg.loadArg(seenRefs);
        mg.loadArg(value);
        mg.invokeInterface(Type.getType(Set.class), getMethod(boolean.class, "add", Object.class));
        mg.ifZCmp(GeneratorAdapter.NE, notSeen);
        mg.throwException(Type.getType(IOException.class), "Circular reference not supported.");
        mg.mark(notSeen);

        // Store the list of schema fields.
        mg.loadArg(schemaLocal);
        mg.invokeVirtual(Type.getType(Schema.class), getMethod(List.class, "getFields"));
        int fieldSchemas = mg.newLocal(Type.getType(List.class));
        mg.storeLocal(fieldSchemas);

        // For each field, call the encode method for the field
        List<Schema.Field> fields = schema.getFields();
        for (int i = 0; i < fields.size(); i++) {
            Schema.Field field = fields.get(i);

            TypeToken<?> fieldType;

            // this.encodeFieldMethod(value.fieldName, encoder, fieldSchemas.get(i).getSchema());
            if (isInterface) {
                mg.loadThis();
                mg.loadArg(value);
                Method getter = getGetter(outputType, field.getName());
                fieldType = outputType.resolveType(rawType.getMethod(getter.getName()).getGenericReturnType());
                mg.invokeInterface(Type.getType(rawType), getter);
            } else {
                fieldType = outputType
                        .resolveType(Fields.findField(outputType, field.getName()).getGenericType());
                fieldAccessorRequests.put(outputType, field.getName());
                mg.loadThis();
                mg.dup();
                mg.getField(classType, getFieldAccessorName(outputType, field.getName()),
                        Type.getType(FieldAccessor.class));
                mg.loadArg(value);
                mg.invokeInterface(Type.getType(FieldAccessor.class), getAccessorMethod(fieldType));
                if (!fieldType.getRawType().isPrimitive()) {
                    doCast(mg, fieldType, field.getSchema());
                }
            }
            mg.loadArg(encoder);
            mg.loadLocal(fieldSchemas);
            mg.push(i);
            mg.invokeInterface(Type.getType(List.class), getMethod(Object.class, "get", int.class));
            mg.checkCast(Type.getType(Schema.Field.class));
            mg.invokeVirtual(Type.getType(Schema.Field.class), getMethod(Schema.class, "getSchema"));
            mg.loadArg(seenRefs);
            mg.invokeVirtual(classType, getEncodeMethod(fieldType, field.getSchema()));
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

From source file:co.cask.tigon.internal.io.DatumWriterGenerator.java

License:Apache License

/**
 * Generates method body for encoding java class. If the class given is an interface,
 * getter method will be used to access the field values, otherwise, it will assumes
 * fields are public.//from w ww  .j a  v  a  2 s  . co m
 *
 * @param mg
 * @param schema
 * @param outputType
 * @param value
 * @param encoder
 * @param schemaLocal
 * @param seenRefs
 */
private void encodeRecord(GeneratorAdapter mg, Schema schema, TypeToken<?> outputType, int value, int encoder,
        int schemaLocal, int seenRefs) {

    try {
        Class<?> rawType = outputType.getRawType();
        boolean isInterface = rawType.isInterface();

        /*
          Check for circular reference
          if (value != null && !seenRefs.add(value)) {
             throw new IOException(...);
          }
        */
        Label notSeen = mg.newLabel();
        mg.loadArg(value);
        mg.ifNull(notSeen);
        mg.loadArg(seenRefs);
        mg.loadArg(value);
        mg.invokeInterface(Type.getType(Set.class), getMethod(boolean.class, "add", Object.class));
        mg.ifZCmp(GeneratorAdapter.NE, notSeen);
        mg.throwException(Type.getType(IOException.class), "Circular reference not supported.");
        mg.mark(notSeen);

        // Store the list of schema fields.
        mg.loadArg(schemaLocal);
        mg.invokeVirtual(Type.getType(Schema.class), getMethod(List.class, "getFields"));
        int fieldSchemas = mg.newLocal(Type.getType(List.class));
        mg.storeLocal(fieldSchemas);

        // For each field, call the encode method for the field
        List<Schema.Field> fields = schema.getFields();
        for (int i = 0; i < fields.size(); i++) {
            Schema.Field field = fields.get(i);

            TypeToken<?> fieldType;

            // this.encodeFieldMethod(value.fieldName, encoder, fieldSchemas.get(i).getSchema());
            if (isInterface) {
                mg.loadThis();
                mg.loadArg(value);
                Method getter = getGetter(outputType, field.getName());
                fieldType = outputType.resolveType(rawType.getMethod(getter.getName()).getGenericReturnType());
                mg.invokeInterface(Type.getType(rawType), getter);
            } else {
                fieldType = outputType
                        .resolveType(Fields.findField(outputType, field.getName()).getGenericType());
                fieldAccessorRequests.put(outputType, field.getName());
                mg.loadThis();
                mg.dup();
                mg.getField(classType, getFieldAccessorName(outputType, field.getName()),
                        Type.getType(FieldAccessor.class));
                mg.loadArg(value);
                mg.invokeInterface(Type.getType(FieldAccessor.class), getAccessorMethod(fieldType));
                if (!fieldType.getRawType().isPrimitive()) {
                    doCast(mg, fieldType, field.getSchema());
                }
            }
            mg.loadArg(encoder);
            mg.loadLocal(fieldSchemas);
            mg.push(i);
            mg.invokeInterface(Type.getType(List.class), getMethod(Object.class, "get", int.class));
            mg.checkCast(Type.getType(Schema.Field.class));
            mg.invokeVirtual(Type.getType(Schema.Field.class), getMethod(Schema.class, "getSchema"));
            mg.loadArg(seenRefs);
            mg.invokeVirtual(classType, getEncodeMethod(fieldType, field.getSchema()));
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

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

License:Apache License

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

    Label labelNull = new Label();
    Label labelExit = new Label();

    field.load(ctx);//from ww  w.j a va 2 s  . c o  m
    g.ifNull(labelNull);
    g.push(false);
    g.goTo(labelExit);

    g.mark(labelNull);
    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 .ja v  a 2 s  . 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:io.datakernel.codegen.ExpressionHash.java

License:Apache License

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

    int resultVar = g.newLocal(INT_TYPE);

    boolean firstIteration = true;

    for (Expression argument : this.arguments) {
        if (firstIteration) {
            g.push(0);//from   w ww.j a  v a 2 s . com
            firstIteration = false;
        } else {
            g.push(31);
            g.loadLocal(resultVar);
            g.math(IMUL, INT_TYPE);
        }

        Type fieldType = argument.load(ctx);

        if (isPrimitiveType(fieldType)) {
            if (fieldType.getSort() == Type.LONG) {
                g.dup2();
                g.push(32);
                g.visitInsn(LUSHR);
                g.visitInsn(LXOR);
                g.visitInsn(L2I);
            }
            if (fieldType.getSort() == Type.FLOAT) {
                g.invokeStatic(getType(Float.class), getMethod("int floatToRawIntBits (float)"));
            }
            if (fieldType.getSort() == Type.DOUBLE) {
                g.invokeStatic(getType(Double.class), getMethod("long doubleToRawLongBits (double)"));
                g.dup2();
                g.push(32);
                g.visitInsn(LUSHR);
                g.visitInsn(LXOR);
                g.visitInsn(L2I);
            }
            g.visitInsn(IADD);
        } else {
            int tmpVar = g.newLocal(fieldType);
            g.storeLocal(tmpVar);
            g.loadLocal(tmpVar);
            Label ifNullLabel = g.newLabel();
            g.ifNull(ifNullLabel);
            g.loadLocal(tmpVar);
            g.invokeVirtual(fieldType, getMethod("int hashCode()"));
            g.visitInsn(IADD);
            g.mark(ifNullLabel);
        }

        g.storeLocal(resultVar);
    }

    if (firstIteration) {
        g.push(0);
    } else {
        g.loadLocal(resultVar);
    }

    return INT_TYPE;
}

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

License:Apache License

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

    g.newInstance(getType(StringBuilder.class));
    g.dup();//from   w ww . j av  a 2s .c o m
    g.invokeConstructor(getType(StringBuilder.class), getMethod("void <init> ()"));

    boolean first = true;

    for (Object key : arguments.keySet()) {
        String str = first ? begin : separator;
        first = false;
        if (key instanceof String) {
            str += key;
        }
        if (!str.isEmpty()) {
            g.dup();
            g.push(str);
            g.invokeVirtual(getType(StringBuilder.class), getMethod("StringBuilder append(String)"));
            g.pop();
        }

        g.dup();
        final Expression expression = arguments.get(key);
        final Type type = expression.load(ctx);
        if (isPrimitiveType(type)) {
            g.invokeStatic(wrap(type), new Method("toString", getType(String.class), new Type[] { type }));
        } else {
            final Label nullLabel = new Label();
            final Label afterToString = new Label();
            g.dup();
            g.ifNull(nullLabel);
            g.invokeVirtual(type, getMethod("String toString()"));
            g.goTo(afterToString);
            g.mark(nullLabel);
            g.pop();
            g.push(("null"));
            g.mark(afterToString);
        }
        g.invokeVirtual(getType(StringBuilder.class), getMethod("StringBuilder append(String)"));
        g.pop();
    }

    if (!end.isEmpty()) {
        g.dup();
        g.push(end);
        g.invokeVirtual(getType(StringBuilder.class), getMethod("StringBuilder append(String)"));
        g.pop();
    }

    g.invokeVirtual(getType(StringBuilder.class), getMethod("String toString()"));
    return type(ctx);
}

From source file:lucee.transformer.bytecode.statement.TryCatchFinally.java

License:Open Source License

private void _writeOutFinally(BytecodeContext bc, int lRef) throws BytecodeException {
    // ref.remove(pc);
    //Reference r=null;
    GeneratorAdapter adapter = bc.getAdapter();

    //if(fcf!=null && fcf.getAfterFinalGOTOLabel()!=null)ASMUtil.visitLabel(adapter,fcf.getFinalEntryLabel());
    ExpressionUtil.visitLine(bc, finallyLine);

    //if (reference != null)
    //    reference.removeEL(pagecontext);
    Label removeEnd = new Label();
    adapter.loadLocal(lRef);/*w w  w.  j a v  a  2 s.c om*/
    adapter.ifNull(removeEnd);
    adapter.loadLocal(lRef);
    adapter.loadArg(0);
    adapter.invokeInterface(Types.REFERENCE, REMOVE_EL);
    adapter.pop();
    adapter.visitLabel(removeEnd);

    if (finallyBody != null)
        finallyBody.writeOut(bc); // finally
    /*if(fcf!=null){
       Label l = fcf.getAfterFinalGOTOLabel();
       if(l!=null)adapter.visitJumpInsn(Opcodes.GOTO, l);
    }*/
}

From source file:org.apache.commons.weaver.privilizer.PrivilizingVisitor.java

License:Apache License

/**
 * Generates the instructions to push onto the stack whether there is a
 * security manager available.//from  w w w . j  ava  2s .  c  om
 * @param mgen to control
 */
private static void checkSecurityManager(final GeneratorAdapter mgen) {
    final Label setFalse = new Label();
    final Label done = new Label();
    mgen.invokeStatic(Type.getType(System.class),
            new Method("getSecurityManager", Type.getType(SecurityManager.class), Privilizer.EMPTY_TYPE_ARRAY));
    mgen.ifNull(setFalse);
    mgen.push(true);
    mgen.goTo(done);
    mgen.mark(setFalse);
    mgen.push(false);
    mgen.mark(done);
}