Example usage for org.objectweb.asm Opcodes INVOKESPECIAL

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

Introduction

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

Prototype

int INVOKESPECIAL

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

Click Source Link

Usage

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

License:Open Source License

private void buildComputationFactoryMethod() {
    final GeneratorAdapter mv = newMethod("newComputation",
            "(Ljava/lang/Object;)" + ByteCodeEngineCompiler.COMPUTATION_INTF.getDescriptor());
    mv.newInstance(ByteCodeEngineCompiler.GEN_ROOT_CLASS);
    mv.dup();/*from   w w w .  ja  v  a  2 s  . co  m*/
    mv.loadArg(0);
    compileClassRef(this.userInputClass, this.userInputType);
    mv.checkCast(this.userInputType);
    mv.loadThis();
    mv.getField(classType(), ByteCodeEngineCompiler.ENV_MEMBER_NAME, ByteCodeEngineCompiler.ENV_CLASS);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, ByteCodeEngineCompiler.GEN_ROOT_CLASS.getInternalName(), "<init>",
            "(" + this.userInputType.getDescriptor() + ByteCodeEngineCompiler.ENV_DESC + ")V");
    mv.visitInsn(Opcodes.ARETURN);
    endMethod(mv);
}

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

License:Open Source License

private void buildUserFactoryMethod() {
    final GeneratorAdapter mv = newMethod(this.userFactoryMethod.getName(),
            Type.getMethodDescriptor(this.userFactoryMethod));
    mv.newInstance(ByteCodeEngineCompiler.GEN_ROOT_CLASS);
    mv.dup();// ww  w  .  ja  v a2s.  c om
    mv.loadArg(0);
    mv.loadThis();
    mv.getField(classType(), ByteCodeEngineCompiler.ENV_MEMBER_NAME, ByteCodeEngineCompiler.ENV_CLASS);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, ByteCodeEngineCompiler.GEN_ROOT_CLASS.getInternalName(), "<init>",
            "(" + this.userInputType.getDescriptor() + ByteCodeEngineCompiler.ENV_DESC + ")V");
    mv.visitInsn(Opcodes.ARETURN);
    endMethod(mv);
}

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

License:Open Source License

private void tryToCallSuper() {
    final Class superClass = this.method.getDeclaringClass();
    if (!Modifier.isInterface(superClass.getModifiers()) && !Modifier.isAbstract(this.method.getModifiers())) {

        mv().loadThis();//from   ww w  .j  a va2 s  .  c om
        for (int i = 0; i < this.paramTypes.length; i++) {
            mv().loadArg(i);
        }

        final String superTypeName = Type.getType(superClass).getInternalName();
        final String superMethodDesc = this.methodType.getDescriptor();
        mv().visitMethodInsn(Opcodes.INVOKESPECIAL, superTypeName, this.method.getName(), superMethodDesc);

        mv().returnValue();
    } else {
        failWhenNoMatch();
    }
}

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

License:Open Source License

protected void callInheritedConstructor(GeneratorAdapter _mv, int _inputsVar) throws CompilerException {
    try {/* w w w .  j a  va2s  .  c om*/
        if (outputClass() == null || outputClass().isInterface()) {
            _mv.loadThis();
            _mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
        } else if (!callConstructorWithInputs(_mv, _inputsVar)) {
            outputClass().getConstructor(); // ensure it is here and accessible
            _mv.loadThis();
            _mv.visitMethodInsn(Opcodes.INVOKESPECIAL, outputType().getInternalName(), "<init>", "()V");
        }
    } catch (NoSuchMethodException e) {
        throw new CompilerException.ConstructorMissing(
                "There is no default constructor and none with the input type as sole parameter.", e);
    }
}

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

License:Open Source License

protected boolean callConstructorWithInputs(GeneratorAdapter _mv, int _inputsVar) {
    try {//  w w  w  .j av  a 2  s . c  o  m
        outputClass().getConstructor(inputClass()); // ensure it is here and accessible
    } catch (NoSuchMethodException e) {
        return false;
    }

    _mv.loadThis();
    if (0 <= _inputsVar) {
        _mv.visitVarInsn(Opcodes.ALOAD, _inputsVar);
    } else {
        _mv.visitInsn(Opcodes.ACONST_NULL);
    }
    _mv.visitMethodInsn(Opcodes.INVOKESPECIAL, outputType().getInternalName(), "<init>",
            "(" + inputType().getDescriptor() + ")V");

    return true;
}

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

License:Open Source License

@Override
protected boolean callConstructorWithInputs(GeneratorAdapter _mv, int _inputsVar) {
    final int P_PARENT = 2;

    // try super( _inputs, _parent );
    try {/*  ww  w .j  a  va 2  s  .  c  om*/
        // ensure it is here and accessible
        outputClass().getConstructor(inputClass(), parentSectionCompiler().model().getOutputClass());
    } catch (NoSuchMethodException e) {
        return super.callConstructorWithInputs(_mv, _inputsVar);
    }

    _mv.loadThis();
    if (0 <= _inputsVar) {
        _mv.visitVarInsn(Opcodes.ALOAD, _inputsVar);
    } else {
        _mv.visitInsn(Opcodes.ACONST_NULL);
    }
    _mv.visitVarInsn(Opcodes.ALOAD, P_PARENT);
    _mv.visitMethodInsn(Opcodes.INVOKESPECIAL, outputType().getInternalName(), "<init>",
            "(" + inputType().getDescriptor() + parentSectionCompiler().outputType().getDescriptor() + ")V");

    return true;
}

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

License:Open Source License

private int compileInitFromIterator(final SubSectionCompiler sub, final GeneratorAdapter mv, final int l_ds) {
    final String n_iter = ITERATOR_INTF.getInternalName();
    final String n_coll = COLLECTION_INTF.getInternalName();
    final String n_arraylist = ARRAYLIST_CLASS.getInternalName();

    // final Collection<DetailPrototype> coll = new ArrayList<DetailPrototype>();
    final int l_coll = mv.newLocal(ARRAYLIST_CLASS);
    mv.newInstance(ARRAYLIST_CLASS);/*w  w w  .  j av a 2s . c  om*/
    mv.dup();
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, n_arraylist, "<init>", "()V");
    mv.storeLocal(l_coll);

    final IndexCompiler ic;
    if (sub.isComputationListenerEnabled()) {
        final int l_i = mv.newLocal(Type.INT_TYPE);
        mv.push(0);
        mv.storeLocal(l_i);
        ic = new IndexCompiler(l_i);
    } else
        ic = null;

    // while (ds.hasNext()) {
    final Label next = mv.newLabel();
    mv.goTo(next);
    final Label again = mv.mark();

    // ~ coll.add( new DetailPrototype( ds.next(), this ) );
    mv.loadLocal(l_coll);
    mv.newInstance(sub.classType());
    mv.dup();
    mv.loadLocal(l_ds);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, n_iter, "next", "()Ljava/lang/Object;");
    mv.checkCast(sub.inputType());
    compileInit(mv, sub, ic);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, n_coll, "add", "(Ljava/lang/Object;)Z");
    mv.pop();

    // index++;
    if (ic != null)
        ic.compileInc(mv);

    // }
    mv.mark(next);
    mv.loadLocal(l_ds);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, n_iter, "hasNext", "()Z");
    mv.ifZCmp(mv.NE, again);

    // final DetailPrototype[] di = coll.toArray( new DetailPrototype[ coll.size() ] );
    final int l_di = mv.newLocal(sub.arrayType());
    mv.loadLocal(l_coll);
    mv.loadLocal(l_coll);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, n_coll, "size", "()I");
    mv.newArray(sub.classType());
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, n_coll, "toArray", "([Ljava/lang/Object;)[Ljava/lang/Object;");
    mv.checkCast(sub.arrayType());
    mv.storeLocal(l_di);

    return l_di;
}

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

License:Open Source License

private void compileInit(final GeneratorAdapter _mv, final SubSectionCompiler _sub, IndexCompiler _ic) {
    final StringBuilder descriptor = new StringBuilder("(");
    descriptor.append(_sub.inputType().getDescriptor());
    descriptor.append(section().classDescriptor());
    if (_ic != null)
        descriptor.append(ByteCodeEngineCompiler.INDEX_TYPE.getDescriptor());
    descriptor.append(")V");

    _mv.loadThis();/*from  w ww .  j a  v  a2s.com*/
    // Section index as 3rd parameter if needed
    if (_ic != null) {
        _ic.compileLoad(_mv);
    }

    _mv.visitMethodInsn(Opcodes.INVOKESPECIAL, _sub.classInternalName(), "<init>", descriptor.toString());
}

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

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from www  .  j a  v  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.");
        }
    }
}

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

License:Open Source License

final void buildStaticContext() {
    if (this.staticContextBuilt)
        return;//from w  w  w. ja  va  2  s  . c o  m
    this.staticContextBuilt = true;

    final SectionCompiler root = engineCompiler().rootCompiler();
    final ClassWriter cw = root.cw();
    final FieldVisitor fv = cw.visitField(Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, RUNTIME_CONTEXT_NAME,
            RUNTIME_CONTEXT_TYPE.getDescriptor(), null, null);
    fv.visitEnd();

    final GeneratorAdapter mv = root.initializer();
    final MathContext mc = numericType().mathContext();
    if (mc == MathContext.DECIMAL32) {
        compilePredefinedMathContext(mv, "DECIMAL32");
    } else if (mc == MathContext.DECIMAL64) {
        compilePredefinedMathContext(mv, "DECIMAL64");
    } else if (mc == MathContext.DECIMAL128) {
        compilePredefinedMathContext(mv, "DECIMAL128");
    } else if (mc == MathContext.UNLIMITED) {
        compilePredefinedMathContext(mv, "UNLIMITED");
    } else {
        mv.visitTypeInsn(Opcodes.NEW, RUNTIME_CONTEXT_TYPE.getInternalName());
        mv.visitInsn(Opcodes.DUP);
        mv.push(mc.getPrecision());
        final Type modeType = Type.getType(RoundingMode.class);
        mv.getStatic(modeType, mc.getRoundingMode().name(), modeType);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, RUNTIME_CONTEXT_TYPE.getInternalName(), "<init>",
                "(ILjava/math/RoundingMode;)V");
    }
    mv.visitFieldInsn(Opcodes.PUTSTATIC, root.classInternalName(), RUNTIME_CONTEXT_NAME,
            RUNTIME_CONTEXT_DESCRIPTOR);
}