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

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

Introduction

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

Prototype

public void arrayLength() 

Source Link

Document

Generates the instruction to compute the length of an array.

Usage

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

License:Apache License

/**
 * Generates method body for encoding array value. The logic is similar to the one in
 * {@link #encodeCollection}, with collection size replaced with array length.
 *
 * @param mg/*from   w  ww.j a v  a2  s  .c om*/
 * @param componentType
 * @param componentSchema
 * @param value
 * @param encoder
 * @param schemaLocal
 * @param seenRefs
 */
private void encodeArray(GeneratorAdapter mg, TypeToken<?> componentType, Schema componentSchema, int value,
        int encoder, int schemaLocal, int seenRefs) {
    // Encode and store the array length locally
    mg.loadArg(value);
    mg.arrayLength();
    int length = mg.newLocal(Type.INT_TYPE);
    mg.storeLocal(length);

    mg.loadArg(encoder);
    mg.loadLocal(length);
    mg.invokeInterface(Type.getType(Encoder.class), getMethod(Encoder.class, "writeInt", int.class));
    mg.pop();

    // Store the component schema
    mg.loadArg(schemaLocal);
    mg.invokeVirtual(Type.getType(Schema.class), getMethod(Schema.class, "getComponentSchema"));
    int componentSchemaLocal = mg.newLocal(Type.getType(Schema.class));
    mg.storeLocal(componentSchemaLocal);

    // for (int idx = 0; idx < array.length; idx++)
    mg.push(0);
    int idx = mg.newLocal(Type.INT_TYPE);
    mg.storeLocal(idx);
    Label beginFor = mg.mark();
    Label endFor = mg.newLabel();
    mg.loadLocal(idx);
    mg.loadLocal(length);
    mg.ifICmp(GeneratorAdapter.GE, endFor);

    // Call encode method to encode array[idx]
    mg.loadThis();
    mg.loadArg(value);
    mg.loadLocal(idx);
    TypeToken<?> callTypeToken = getCallTypeToken(componentType, componentSchema);
    mg.arrayLoad(Type.getType(callTypeToken.getRawType()));
    mg.loadArg(encoder);
    mg.loadLocal(componentSchemaLocal);
    mg.loadArg(seenRefs);
    mg.invokeVirtual(classType, getEncodeMethod(componentType, componentSchema));

    mg.iinc(idx, 1);
    mg.goTo(beginFor);
    mg.mark(endFor);

    // if length > 0, write out 0 at the end of array.
    Label zeroLength = mg.newLabel();
    mg.loadLocal(length);
    mg.ifZCmp(GeneratorAdapter.LE, zeroLength);
    encodeInt(mg, 0, encoder);
    mg.mark(zeroLength);
}

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

License:Apache License

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

    if (field.type(ctx).getSort() == Type.ARRAY) {
        field.load(ctx);// w w  w .j  a v a 2 s.c  o  m
        g.arrayLength();
    } else if (field.type(ctx).getSort() == Type.OBJECT) {
        call(field, "size").load(ctx);
    }
    return Type.INT_TYPE;
}

From source file:lucee.transformer.bytecode.statement.tag.TagLoop.java

License:Open Source License

/**
 * write out file loop//  w ww .  java  2  s.  c om
 * @param adapter
 * @throws TemplateException
 */
private void writeOutTypeFile(BytecodeContext bc) throws BytecodeException {
    WhileVisitor whileVisitor = new WhileVisitor();
    loopVisitor = whileVisitor;
    GeneratorAdapter adapter = bc.getAdapter();

    // charset=@charset
    int charset = adapter.newLocal(Types.STRING);
    Attribute attrCharset = getAttribute("charset");
    if (attrCharset == null)
        adapter.visitInsn(Opcodes.ACONST_NULL);
    else
        attrCharset.getValue().writeOut(bc, Expression.MODE_REF);
    adapter.storeLocal(charset);

    // startline=@startline
    int startline = adapter.newLocal(Types.INT_VALUE);
    Attribute attrStartLine = getAttribute("startline");
    if (attrStartLine == null)
        attrStartLine = getAttribute("from"); // CF8
    if (attrStartLine == null)
        adapter.push(1);
    else {
        attrStartLine.getValue().writeOut(bc, Expression.MODE_VALUE);
        adapter.visitInsn(Opcodes.D2I);
    }
    adapter.storeLocal(startline);

    // endline=@endline
    int endline = adapter.newLocal(Types.INT_VALUE);
    Attribute attrEndLine = getAttribute("endline");
    if (attrEndLine == null)
        attrEndLine = getAttribute("to");
    if (attrEndLine == null)
        adapter.push(-1);
    else {
        attrEndLine.getValue().writeOut(bc, Expression.MODE_VALUE);
        adapter.visitInsn(Opcodes.D2I);
    }
    adapter.storeLocal(endline);

    //VariableReference index=VariableInterpreter.getVariableReference(pc,@index);
    int index = -1, item = -1;

    // item
    Attribute attrItem = getAttribute("item");
    if (attrItem != null) {
        item = adapter.newLocal(Types.VARIABLE_REFERENCE);
        adapter.loadArg(0);
        attrItem.getValue().writeOut(bc, Expression.MODE_REF);
        adapter.invokeStatic(Types.VARIABLE_INTERPRETER, GET_VARIABLE_REFERENCE);
        adapter.storeLocal(item);
    }

    // index
    Attribute attrIndex = getAttribute("index");
    if (attrIndex != null) {
        index = adapter.newLocal(Types.VARIABLE_REFERENCE);
        adapter.loadArg(0);
        attrIndex.getValue().writeOut(bc, Expression.MODE_REF);
        adapter.invokeStatic(Types.VARIABLE_INTERPRETER, GET_VARIABLE_REFERENCE);
        adapter.storeLocal(index);
    }

    //java.io.File file=FileUtil.toResourceExisting(pc,@file);
    int resource = adapter.newLocal(Types.RESOURCE);
    adapter.loadArg(0);
    getAttribute("file").getValue().writeOut(bc, Expression.MODE_REF);
    adapter.invokeStatic(RESOURCE_UTIL, TO_RESOURCE_EXISTING);
    adapter.storeLocal(resource);

    // pc.getConfig().getSecurityManager().checkFileLocation(resource);
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, GET_CONFIG);
    adapter.invokeInterface(Types.CONFIG_WEB, GET_SECURITY_MANAGER);
    adapter.loadLocal(resource);
    adapter.invokeInterface(Types.SECURITY_MANAGER, CHECK_FILE_LOCATION);

    // char[] carr=new char[characters];
    Attribute attr = getAttribute("characters");
    int carr = -1;
    if (attr != null) {
        carr = adapter.newLocal(Types.CHAR_ARRAY);
        attr.getValue().writeOut(bc, Expression.MODE_VALUE);
        adapter.cast(Types.DOUBLE_VALUE, Types.INT_VALUE);
        adapter.newArray(Types.CHAR);
        adapter.storeLocal(carr);
    }

    // BufferedReader reader = IOUtil.getBufferedReader(resource,charset);
    final int br = adapter.newLocal(Types.BUFFERED_READER);
    adapter.loadLocal(resource);
    adapter.loadLocal(charset);
    adapter.invokeStatic(IO_UTIL, GET_BUFFERED_READER);
    adapter.storeLocal(br);

    // String line;
    int line = adapter.newLocal(Types.STRING);

    // int count=0;  
    int count = adapter.newLocal(Types.INT_VALUE);
    adapter.push(0);
    adapter.storeLocal(count);

    TryFinallyVisitor tfv = new TryFinallyVisitor(new OnFinally() {
        public void _writeOut(BytecodeContext bc) {
            bc.getAdapter().loadLocal(br);
            bc.getAdapter().invokeStatic(IO_UTIL, CLOSE_EL);
        }
    }, null);
    //TryFinallyVisitor tcfv=new TryFinallyVisitor();

    // try
    tfv.visitTryBegin(bc);
    //tcfv.visitTryBegin(bc);
    // while((line=br.readLine())!=null) { 
    //WhileVisitor wv=new WhileVisitor();
    whileVisitor.visitBeforeExpression(bc);
    DecisionObjectVisitor dv = new DecisionObjectVisitor();
    dv.visitBegin();
    if (attr != null) {
        // IOUtil.read(bufferedreader,12)
        adapter.loadLocal(br);
        adapter.loadLocal(carr);
        adapter.arrayLength();
        adapter.invokeStatic(Types.IOUTIL, READ);
    } else {
        // br.readLine()
        adapter.loadLocal(br);
        adapter.invokeVirtual(Types.BUFFERED_READER, READ_LINE);
    }
    adapter.dup();
    adapter.storeLocal(line);

    dv.visitNEQ();
    adapter.visitInsn(Opcodes.ACONST_NULL);
    dv.visitEnd(bc);

    whileVisitor.visitAfterExpressionBeforeBody(bc);
    //if(++count < startLine) continue; 
    DecisionIntVisitor dv2 = new DecisionIntVisitor();
    dv2.visitBegin();
    adapter.iinc(count, 1);
    adapter.loadLocal(count);
    dv2.visitLT();
    adapter.loadLocal(startline);
    dv2.visitEnd(bc);
    Label end = new Label();
    adapter.ifZCmp(Opcodes.IFEQ, end);
    whileVisitor.visitContinue(bc);
    adapter.visitLabel(end);

    // if(endLine!=-1 && count > endLine) break; 
    DecisionIntVisitor div = new DecisionIntVisitor();
    div.visitBegin();
    adapter.loadLocal(endline);
    div.visitNEQ();
    adapter.push(-1);
    div.visitEnd(bc);
    Label end2 = new Label();
    adapter.ifZCmp(Opcodes.IFEQ, end2);

    DecisionIntVisitor div2 = new DecisionIntVisitor();
    div2.visitBegin();
    adapter.loadLocal(count);
    div2.visitGT();
    adapter.loadLocal(endline);
    div2.visitEnd(bc);
    Label end3 = new Label();
    adapter.ifZCmp(Opcodes.IFEQ, end3);
    whileVisitor.visitBreak(bc);
    adapter.visitLabel(end3);
    adapter.visitLabel(end2);

    // index and item
    if (index != -1 && item != -1) {
        // index.set(pc,line); 
        adapter.loadLocal(index);
        adapter.loadArg(0);
        adapter.loadLocal(count);
        adapter.cast(Types.INT_VALUE, Types.DOUBLE_VALUE);
        adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_DOUBLE_FROM_DOUBLE);

        adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
        adapter.pop();

        // item.set(pc,line); 
        adapter.loadLocal(item);
        adapter.loadArg(0);
        adapter.loadLocal(line);
        adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
        adapter.pop();

    }
    // only index
    else if (index != -1) {
        // index.set(pc,line); 
        adapter.loadLocal(index);
        adapter.loadArg(0);
        adapter.loadLocal(line);
        adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
        adapter.pop();

    }
    // only item
    else {
        // item.set(pc,line); 
        adapter.loadLocal(item);
        adapter.loadArg(0);
        adapter.loadLocal(line);
        adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
        adapter.pop();
    }

    getBody().writeOut(bc);

    whileVisitor.visitAfterBody(bc, getEnd());

    tfv.visitTryEnd(bc);

}

From source file:org.elasticsearch.painless.node.LArrayLength.java

License:Apache License

@Override
void load(final CompilerSettings settings, final Definition definition, final GeneratorAdapter adapter) {
    adapter.arrayLength();
}

From source file:org.evosuite.stubs.StubClassVisitor.java

License:Open Source License

private void insertReturnCast(GeneratorAdapter mg, Method m) {
    if (m.getReturnType().getSort() == Type.OBJECT) {
        mg.checkCast(m.getReturnType());
    } else if (m.getReturnType().getSort() == Type.ARRAY) {
        if (m.getReturnType().getElementType().getSort() == Type.OBJECT) {
            // TODO: String? Other arrays?
            try {
                java.lang.reflect.Method copyMethod = System.class.getMethod("arraycopy",
                        new Class<?>[] { Object.class, int.class, Object.class, int.class, int.class });

                // Object 1
                mg.dup(); // O1, O1
                mg.arrayLength(); // O1, O1.length
                int arrayLengthPos = mg.newLocal(Type.INT_TYPE);
                mg.storeLocal(arrayLengthPos);

                mg.loadLocal(arrayLengthPos);
                mg.newArray(m.getReturnType().getElementType());
                int newArrayPos = mg.newLocal(m.getReturnType());
                mg.storeLocal(newArrayPos);

                mg.push(0); // O1, 0
                mg.loadLocal(newArrayPos); // O1, 0, O2
                mg.push(0); // O1, 0, O2, 0
                mg.loadLocal(arrayLengthPos); // O1, 0, O2, 0, O1.length               
                mg.invokeStatic(Type.getType(System.class), Method.getMethod(copyMethod));
                mg.loadLocal(newArrayPos);
                mg.checkCast(m.getReturnType());
            } catch (Exception e) {
                System.out.println("Screw that");
            }/*from   w  ww.java  2 s . c  o  m*/
            // mg.checkCast(m.getReturnType());
        }
    }
}

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

License:Open Source License

private void compileScanArray(ForEachElementCompilation _forElement) throws CompilerException {
    final GeneratorAdapter mv = mv();
    final int loc = localsOffset();
    incLocalsOffset(4);/*from w w w .j ava2 s . com*/

    // store array
    mv.visitVarInsn(Opcodes.ASTORE, loc);

    // store array length
    mv.visitVarInsn(Opcodes.ALOAD, loc);
    mv.arrayLength();
    mv.visitVarInsn(Opcodes.ISTORE, 1 + loc);

    // loop index
    mv.push(0);
    mv.visitVarInsn(Opcodes.ISTORE, 2 + loc);

    // loop start
    final Label l0 = mv.mark();

    // check loop condition
    mv.visitVarInsn(Opcodes.ILOAD, 2 + loc);
    mv.visitVarInsn(Opcodes.ILOAD, 1 + loc);
    final Label l1 = new Label();
    mv.ifICmp(GeneratorAdapter.GE, l1);

    // loop body
    mv.visitVarInsn(Opcodes.ALOAD, loc);
    mv.visitVarInsn(Opcodes.ILOAD, 2 + loc);
    mv.visitInsn(Opcodes.AALOAD);
    mv.visitVarInsn(Opcodes.ASTORE, 3 + loc);
    _forElement.compile(3 + loc, 2 + loc);

    // inc loop index
    mv.visitIincInsn(2 + loc, 1);

    mv.goTo(l0);
    mv.visitLabel(l1);
}

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

License:Open Source License

@Override
protected final void compileCount(ExpressionNodeForCount _node) throws CompilerException {
    final GeneratorAdapter mv = mv();
    mv.push(_node.staticValueCount());//from   www .j  a  va  2s  .c o  m

    final SectionModel[] subs = _node.subSectionModels();
    final int[] cnts = _node.subSectionStaticValueCounts();
    for (int i = 0; i < subs.length; i++) {
        final SubSectionCompiler sub = sectionInContext().subSectionCompiler(subs[i]);
        method().compileObjectInContext();
        sectionInContext().compileCallToGetterFor(mv, sub);
        mv.arrayLength();
        if (cnts[i] != 1) {
            mv.push(cnts[i]);
            mv.visitInsn(Opcodes.IMUL);
        }
        mv.visitInsn(Opcodes.IADD);
    }

    compileConversionFromInt();
}

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

License:Open Source License

private int compileInitFromArray(final SubSectionCompiler sub, final GeneratorAdapter mv, final int l_ds) {
    // final int dl = ds.length;
    final int l_dl = mv.newLocal(Type.INT_TYPE);
    mv.loadLocal(l_ds);/* w  ww .  ja  va2s.  co m*/
    mv.arrayLength();
    mv.storeLocal(l_dl);

    // DetailImpl[] di = new DetailPrototype[ dl ];
    final int l_di = mv.newLocal(sub.arrayType());
    mv.loadLocal(l_dl);
    mv.newArray(sub.classType());
    mv.storeLocal(l_di);

    // for (int i = 0; i < dl; i++) {
    final int l_i = mv.newLocal(Type.INT_TYPE);
    final Label next = mv.newLabel();
    mv.push(0);
    mv.storeLocal(l_i);
    mv.goTo(next);
    final Label again = mv.mark();

    // ~ di[ i ] = new DetailPrototype( ds[ i ], this );
    mv.loadLocal(l_di);
    mv.loadLocal(l_i);
    mv.newInstance(sub.classType());
    mv.dup();
    mv.loadLocal(l_ds);
    mv.loadLocal(l_i);
    mv.arrayLoad(sub.inputType());
    final IndexCompiler ic = sub.isComputationListenerEnabled() ? new IndexCompiler(l_i) : null;
    compileInit(mv, sub, ic);
    mv.arrayStore(sub.classType());

    // }
    mv.iinc(l_i, 1);
    mv.mark(next);
    mv.loadLocal(l_i);
    mv.loadLocal(l_dl);
    mv.ifCmp(Type.INT_TYPE, mv.LT, again);

    return l_di;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
@Override/* w w w. j  av  a  2  s  .c  o  m*/
protected void compileBody() throws CompilerException {
    final SubSectionCompiler sub = this.sub;
    final GeneratorAdapter mv = mv();

    final CallFrame outputCall = this.callToImplement;
    final Class outputContainerClass = outputCall.getMethod().getReturnType();

    // get$Sect0()
    mv.loadThis();
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, section().classInternalName(), sub.getterName(),
            sub.getterDescriptor());

    if (outputContainerClass.isArray()) {
        mv.visitInsn(Opcodes.ARETURN);
    } else {
        // Detail[] arr = get$Sect0();
        final int l_arr = mv.newLocal(sub.arrayType());
        mv.storeLocal(l_arr);

        final int l_len = mv.newLocal(Type.INT_TYPE);
        mv.loadLocal(l_arr);
        mv.arrayLength();
        mv.storeLocal(l_len);

        // List lst = new ArrayList( arr.length );
        final int l_lst = mv.newLocal(ARRAYLIST_CLASS);
        mv.newInstance(ARRAYLIST_CLASS);
        mv.dup();
        mv.loadLocal(l_len);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, ARRAYLIST_CLASS.getInternalName(), "<init>", "(I)V");
        mv.storeLocal(l_lst);

        // for (int i = 0; i < len; i++) {
        final int l_i = mv.newLocal(Type.INT_TYPE);
        mv.push(0);
        mv.storeLocal(l_i);
        final Label test = mv.newLabel();
        mv.goTo(test);
        final Label again = mv.mark();

        // lst.add( arr[ i ] );
        mv.loadLocal(l_lst);
        mv.loadLocal(l_arr);
        mv.loadLocal(l_i);
        mv.arrayLoad(sub.classType());
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ARRAYLIST_CLASS.getInternalName(), "add",
                "(Ljava/lang/Object;)Z");
        mv.pop();

        // } // for
        mv.iinc(l_i, 1);
        mv.mark(test);
        mv.loadLocal(l_i);
        mv.loadLocal(l_len);
        mv.ifCmp(Type.INT_TYPE, mv.LT, again);

        mv.loadLocal(l_lst);
        if (outputContainerClass.isAssignableFrom(List.class)) {
            // return lst;
            mv.visitInsn(Opcodes.ARETURN);
        } else if (outputContainerClass.isAssignableFrom(Iterator.class)) {
            // return lst.iterator();
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ARRAYLIST_CLASS.getInternalName(), "iterator",
                    "()" + ITERATOR_INTF.getDescriptor());
            mv.visitInsn(Opcodes.ARETURN);
        } else {
            throw new CompilerException.UnsupportedDataType("The return type of '" + outputCall.getMethod()
                    + "' is not supported as input to a repeating section.");
        }
    }
}