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

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

Introduction

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

Prototype

int NE

To view the source code for org.objectweb.asm.commons GeneratorAdapter NE.

Click Source Link

Document

Constant for the #ifCmp method.

Usage

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

License:Apache License

/**
 * Generates the constructor. The constructor generated has signature {@code (Schema, FieldAccessorFactory)}.
 */// w  w w .  j  ava  2 s .c o m
private void generateConstructor() {
    Method constructor = getMethod(void.class, "<init>", Schema.class, FieldAccessorFactory.class);

    // Constructor(Schema schema, FieldAccessorFactory accessorFactory)
    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, constructor, null, null, classWriter);

    // super(); // Calling Object constructor
    mg.loadThis();
    mg.invokeConstructor(Type.getType(Object.class), getMethod(void.class, "<init>"));

    // if (!SCHEMA_HASH.equals(schema.getSchemaHash().toString())) { throw IllegalArgumentException }
    mg.getStatic(classType, "SCHEMA_HASH", Type.getType(String.class));
    mg.loadArg(0);
    mg.invokeVirtual(Type.getType(Schema.class), getMethod(SchemaHash.class, "getSchemaHash"));
    mg.invokeVirtual(Type.getType(SchemaHash.class), getMethod(String.class, "toString"));
    mg.invokeVirtual(Type.getType(String.class), getMethod(boolean.class, "equals", Object.class));
    Label hashEquals = mg.newLabel();
    mg.ifZCmp(GeneratorAdapter.NE, hashEquals);
    mg.throwException(Type.getType(IllegalArgumentException.class), "Schema not match.");
    mg.mark(hashEquals);

    // this.schema = schema;
    mg.loadThis();
    mg.loadArg(0);
    mg.putField(classType, "schema", Type.getType(Schema.class));

    // For each record field that needs an accessor, get the accessor and store it in field.
    for (Map.Entry<TypeToken<?>, String> entry : fieldAccessorRequests.entries()) {
        String fieldAccessorName = getFieldAccessorName(entry.getKey(), entry.getValue());

        classWriter.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, fieldAccessorName,
                Type.getDescriptor(FieldAccessor.class), null, null);
        // this.fieldAccessorName
        //  = accessorFactory.getFieldAccessor(TypeToken.of(Class.forName("className")), "fieldName");
        mg.loadThis();
        mg.loadArg(1);
        mg.push(entry.getKey().getRawType().getName());
        mg.invokeStatic(Type.getType(Class.class), getMethod(Class.class, "forName", String.class));
        mg.invokeStatic(Type.getType(TypeToken.class), getMethod(TypeToken.class, "of", Class.class));
        mg.push(entry.getValue());
        mg.invokeInterface(Type.getType(FieldAccessorFactory.class),
                getMethod(FieldAccessor.class, "getFieldAccessor", TypeToken.class, String.class));
        mg.putField(classType, fieldAccessorName, Type.getType(FieldAccessor.class));
    }

    mg.returnValue();
    mg.endMethod();
}

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./*from  ww  w.j a v  a  2 s. 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.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.//w  w w  .  jav 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();

        // 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./*ww w  . j  a  v a 2s .c om*/
 *
 * @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:com.xruby.compiler.codegen.RubyCompilerImpl.java

License:BSD License

public void visitWhileConditionEnd(boolean always_true, boolean is_until) {
    MethodGenerator mg = cg_.getMethodGenerator();
    if (always_true) {
        mg.push(true);//w w  w . j ava2s  .co m
    } else {
        mg.RubyValue_isTrue();
    }

    if (is_until) {
        mg.ifZCmp(GeneratorAdapter.NE, mg.getLabelManager().getCurrentX());
    } else {
        mg.ifZCmp(GeneratorAdapter.EQ, mg.getLabelManager().getCurrentX());
    }

    mg.mark(mg.getLabelManager().getCurrentRedo());
}

From source file:com.xruby.compiler.codegen.RubyCompilerImpl.java

License:BSD License

public Object visitAfterUnlessCondition() {
    MethodGenerator mg = cg_.getMethodGenerator();
    mg.RubyValue_isTrue();/*from   w w w. java2  s . c  o m*/
    Label label = new Label();
    mg.ifZCmp(GeneratorAdapter.NE, label);
    return label;
}

From source file:com.xruby.compiler.codegen.RubyCompilerImpl.java

License:BSD License

public void visitPotentialProcCall() {
    MethodGenerator mg = cg_.getMethodGenerator();
    mg.dup();/* w  w  w. j a v  a2s  .com*/
    mg.instanceOf(Types.RUBY_PROC_TYPE);

    Label label1 = new Label();
    mg.ifZCmp(GeneratorAdapter.EQ, label1);

    mg.dup();
    mg.checkCast(Types.RUBY_PROC_TYPE);

    //check if in the right context
    //TODO have not considered all the situations
    mg.dup();
    mg.RubyProc_isDefinedInAnotherBlock();
    Label label2 = new Label();
    mg.ifZCmp(GeneratorAdapter.NE, label2);

    cg_.addVariableToBinding();//TODO should we use updateBinding()?
    mg.mark(label2);
    mg.pop();

    mg.mark(label1);
}

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

License:Apache License

@Override
public Type load(Context ctx) {
    VarLocal varReadedSubClass = newLocal(ctx, nom.type(ctx));
    nom.load(ctx);// w  w w  . ja va 2  s.c  o m
    varReadedSubClass.store(ctx);

    Label labelExit = new Label();
    GeneratorAdapter g = ctx.getGeneratorAdapter();

    for (int i = 0; i < list.size(); i++) {
        Label labelNext = new Label();

        g.push(i);
        varReadedSubClass.load(ctx);
        g.ifCmp(INT_TYPE, GeneratorAdapter.NE, labelNext);

        list.get(i).load(ctx);
        g.goTo(labelExit);

        g.mark(labelNext);
    }

    if (defaultExp != null) {
        defaultExp.load(ctx);
    } else {
        final Variable sb = let(constructor(StringBuilder.class));
        call(sb, "append", value("Key '")).load(ctx);
        call(sb, "append", cast(varReadedSubClass, getType(int.class))).load(ctx);
        call(sb, "append", value(String.format("' not in range [0-%d)", list.size()))).load(ctx);
        constructor(IllegalArgumentException.class, call(sb, "toString")).load(ctx);
        g.throwException();
    }
    g.mark(labelExit);

    return type(ctx);
}

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

License:Apache License

@Override
public Type load(Context ctx) {
    VarLocal varKey = newLocal(ctx, key.type(ctx));
    key.load(ctx);//www.j a v a2s  . c  o  m
    varKey.store(ctx);

    Label labelExit = new Label();
    GeneratorAdapter g = ctx.getGeneratorAdapter();

    final boolean keyPrimitiveType = Utils.isPrimitiveType(key.type(ctx));
    for (int i = 0; i < listKey.size(); i++) {
        Label labelNext = new Label();
        if (keyPrimitiveType) {
            listKey.get(i).load(ctx);
            varKey.load(ctx);
            g.ifCmp(key.type(ctx), GeneratorAdapter.NE, labelNext);
        } else {
            call(listKey.get(i), "equals", varKey).load(ctx);
            g.push(true);
            g.ifCmp(Type.BOOLEAN_TYPE, GeneratorAdapter.NE, labelNext);
        }

        listValue.get(i).load(ctx);
        g.goTo(labelExit);

        g.mark(labelNext);
    }

    if (defaultExp != null) {
        defaultExp.load(ctx);
    } else {
        final Variable sb = let(constructor(StringBuilder.class));
        call(sb, "append", value("Key '")).load(ctx);
        call(sb, "append", keyPrimitiveType ? varKey : call(key, "toString")).load(ctx);
        call(sb, "append", value("' not in keyList: [")).load(ctx);
        final Iterator<Expression> iterator = listKey.iterator();
        while (iterator.hasNext()) {
            final Expression expression = iterator.next();
            final boolean primitiveType = Utils.isPrimitiveType(expression.type(ctx));
            call(sb, "append", primitiveType ? expression : call(expression, "toString")).load(ctx);
            if (iterator.hasNext()) {
                call(sb, "append", value(", ")).load(ctx);
            }
        }
        call(sb, "append", value("]")).load(ctx);

        constructor(IllegalArgumentException.class, call(sb, "toString")).load(ctx);
        g.throwException();
    }
    g.mark(labelExit);

    return type(ctx);
}

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

License:Apache License

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

    VarLocal iterator = newLocal(ctx, getType(Iterator.class));
    call(value, "iterator").load(ctx);
    iterator.store(ctx);/* www .  java2  s. c o  m*/

    g.mark(labelLoop);

    call(iterator, "hasNext").load(ctx);
    g.push(true);
    g.ifCmp(BOOLEAN_TYPE, GeneratorAdapter.NE, labelExit);

    VarLocal item = newLocal(ctx, getType(iteratorType));

    cast(call(iterator, "next"), iteratorType).load(ctx);
    item.store(ctx);

    forKey.forVar(getter(item, "key")).load(ctx);
    forValue.forVar(getter(item, "value")).load(ctx);

    g.goTo(labelLoop);

    g.mark(labelExit);

    return Type.VOID_TYPE;
}