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

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

Introduction

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

Prototype

public void newInstance(final Type type) 

Source Link

Document

Generates the instruction to create a new object.

Usage

From source file:com.android.build.gradle.internal.incremental.StringSwitch.java

License:Apache License

/**
 * Generates a standard error exception with message similar to:
 *
 *    String switch could not find 'equals.(Ljava/lang/Object;)Z' with hashcode 0
 *    in com/example/basic/GrandChild//  w  ww  . j  a v  a  2  s  .com
 *
 * @param mv The generator adaptor used to emit the lookup switch code.
 * @param visitedClassName The abstract string trie structure.
 */
void writeMissingMessageWithHash(GeneratorAdapter mv, String visitedClassName) {
    mv.newInstance(INSTANT_RELOAD_EXCEPTION_TYPE);
    mv.dup();
    mv.push("String switch could not find '%s' with hashcode %s in %s");
    mv.push(3);
    mv.newArray(OBJECT_TYPE);
    mv.dup();
    mv.push(0);
    visitString();
    mv.arrayStore(OBJECT_TYPE);
    mv.dup();
    mv.push(1);
    visitString();
    visitHashMethod(mv);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
    mv.arrayStore(OBJECT_TYPE);
    mv.dup();
    mv.push(2);
    mv.push(visitedClassName);
    mv.arrayStore(OBJECT_TYPE);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "format",
            "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;", false);
    mv.invokeConstructor(INSTANT_RELOAD_EXCEPTION_TYPE, Method.getMethod("void <init> (String)"));
    mv.throwException();
}

From source file:com.mogujie.instantrun.IntSwitch.java

License:Apache License

void writeMissingMessageWithHash(GeneratorAdapter mv, String visitedClassName) {
    mv.newInstance(INSTANT_RELOAD_EXCEPTION_TYPE);
    mv.dup();/*from ww w.j  a  va 2s .co  m*/
    mv.push("int switch could not find %d in %s");
    mv.push(3);
    mv.newArray(OBJECT_TYPE);
    mv.dup();
    mv.push(0);
    visitInt();
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
    mv.arrayStore(OBJECT_TYPE);
    mv.dup();
    mv.push(2);
    mv.push(visitedClassName);
    mv.arrayStore(OBJECT_TYPE);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "format",
            "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;", false);
    mv.invokeConstructor(INSTANT_RELOAD_EXCEPTION_TYPE, Method.getMethod("void <init> (String)"));
    mv.throwException();
}

From source file:com.quartercode.jtimber.rh.agent.asm.InsertChildAccessorsClassAdapter.java

License:Open Source License

private void generateGetChildrenMethod() {

    GeneratorAdapter mg = new GeneratorAdapter(ACC_PUBLIC, GET_CHILDREN_METHOD, null, null, cv);

    // Create the list; if the superclass is a node, call the getChildren() method on the superclass and use the result as the list
    // The leave the list at the bottom of the stack
    if (hasNodeAsSuperclass) {
        mg.loadThis();// w ww  . ja  v a2 s. c  o m
        mg.invokeConstructor(superclassType, GET_CHILDREN_METHOD);
    } else {
        mg.newInstance(ARRAY_LIST_CLASS);
        mg.dup();
        mg.invokeConstructor(ARRAY_LIST_CLASS, DEFAULT_CONSTRUCTOR);
    }

    // ----- Stack: [list]

    // Add all field values to the list
    for (Pair<String, Type> field : fields) {
        Type fieldType = field.getRight();

        // Duplicate the list; the duplication is necessary because the following invocation of the add() method on the list
        // will "consume" this reference
        mg.dup();

        // ----- Stack: [list, list]

        // Push the current field value
        ASMUtils.generateGetField(mg, classType, field.getLeft(), fieldType);

        // ----- Stack: [list, list, fieldValue]

        // Add the field object to the list; the called static method executes some checks and handles wrappers
        mg.invokeStatic(FUNCS_CLASS, FUNC_ADD_ACTUAL_CHILDREN_TO_LIST);

        // ----- Stack: [list]
    }

    // Return the list (which is at the bottom of the stack)
    mg.returnValue();

    mg.endMethod();
}

From source file:com.quartercode.jtimber.rh.agent.asm.InsertJAXBTweaksClassAdapter.java

License:Open Source License

private void generateAfterUnmarshalMethod(Method method) {

    GeneratorAdapter mg = new GeneratorAdapter(ACC_PUBLIC, method, null, null, cv);

    /*/*from   w  ww . j a  v  a 2 s.c  o  m*/
     * Iterate through all fields annotated with "SubstituteWithWrapper" and replace their current value with their current value wrapped inside a wrapper.
     * This section first reads the current field value, then creates a new wrapper which wraps around that field value, and finally sets the field to the wrapper.
     */
    for (Triple<String, Type, Type> field : fieldsForWrapperSubstitution) {
        String fieldName = field.getLeft();
        Type fieldType = fields.get(fieldName);
        Type wrapperType = field.getMiddle();
        // Null means "default"; in that case, the field type is used as the type of the first argument of the wrapper constructor
        Type wrapperConstructorArgType = field.getRight() == null ? fieldType : field.getRight();

        // Note that this reference will be used for the PUTFIELD instruction later on
        mg.loadThis();

        // ----- Stack: [this]

        // Creates the wrapper using the current field value
        {
            // Create a new instance of the wrapper type and duplicate it for the constructor call later on
            mg.newInstance(wrapperType);
            mg.dup();

            // ----- Stack: [this, wrapper, wrapper]

            // Retrieve the current field value
            ASMUtils.generateGetField(mg, classType, fieldName, fieldType);

            // ----- Stack: [this, wrapper, wrapper, fieldValue]

            // Call the constructor of the new wrapper using the current field value as the first argument
            mg.invokeConstructor(wrapperType,
                    Method.getMethod("void <init> (" + wrapperConstructorArgType.getClassName() + ")"));

            // ----- Stack: [this, wrapper]
        }

        // Store the new wrapper in the field the value has been retrieved from before
        // The substitution is complete
        mg.putField(classType, fieldName, fieldType);

        // ----- Stack: []
    }

    /*
     * Iterate through all fields.
     * For each field, call the addParent() method with "this" as parent if the current field value is parent-aware
     */
    for (Entry<String, Type> field : fields.entrySet()) {
        String fieldName = field.getKey();
        Type fieldType = field.getValue();

        ASMUtils.generateGetField(mg, classType, fieldName, fieldType);

        // ----- Stack: [fieldValue]

        ASMUtils.generateAddOrRemoveThisAsParent(mg, "addParent");
    }

    // End the method
    mg.returnValue();
    mg.endMethod();
}

From source file:com.xruby.runtime.lang.util.RubyTypeFactory.java

License:BSD License

private void defineRubyMethodWithClass(GeneratorAdapter mg, int rubyTypeIdx, int factoryIdx, Class innerClass,
        CgMethodItem item) {/*  w  w w  .  j a va  2s.  co  m*/
    loadRubyType(mg, rubyTypeIdx, item);
    mg.push(item.name);
    Type type = Type.getType(innerClass);
    mg.newInstance(type);
    mg.dup();
    mg.invokeConstructor(type, CgUtil.CONSTRUCTOR);
    defineMethod(mg, item);
    defineAlias(mg, factoryIdx, rubyTypeIdx, item);
}

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

License:Apache License

@Override
public Type load(Context ctx) {
    GeneratorAdapter g = ctx.getGeneratorAdapter();
    Class<?>[] fieldTypes = new Class<?>[this.fields.size()];
    Expression[] fieldVars = new Expression[this.fields.size()];
    for (int i = 0; i < this.fields.size(); i++) {
        Expression field = this.fields.get(i);
        Type fieldType = field.type(ctx);
        fieldTypes[i] = getJavaType(ctx.getClassLoader(), fieldType);
        fieldVars[i] = field;//  w  w  w. j  a v a2  s  .  com
    }
    try {
        Constructor<?> constructor = type.getConstructor(fieldTypes);
        g.newInstance(getType(type));
        g.dup();
        for (Expression fieldVar : fieldVars) {
            fieldVar.load(ctx);
        }
        g.invokeConstructor(getType(type), getMethod(constructor));
        return getType(type);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(format("No constructor %s.<init>(%s). %s", type.getName(),
                (fieldTypes.length != 0 ? argsToString(fieldTypes) : ""), exceptionInGeneratedClass(ctx)));
    }
}

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();/*  w ww .j ava 2s  . com*/
    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.Page.java

License:Open Source License

private void writeOutNewComponent(BytecodeContext statConstr, BytecodeContext constr, List<LitString> keys,
        ClassWriter cw, Tag component) throws BytecodeException {

    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL,
            NEW_COMPONENT_IMPL_INSTANCE, null, new Type[] { Types.PAGE_EXCEPTION }, cw);
    BytecodeContext bc = new BytecodeContext(null, statConstr, constr, this, keys, cw, name, adapter,
            NEW_COMPONENT_IMPL_INSTANCE, writeLog(), suppressWSbeforeArg, output);
    Label methodBegin = new Label();
    Label methodEnd = new Label();

    adapter.visitLocalVariable("this", "L" + name + ";", null, methodBegin, methodEnd, 0);
    ExpressionUtil.visitLine(bc, component.getStart());
    adapter.visitLabel(methodBegin);/*from w  w w  . ja v a 2 s . c om*/

    int comp = adapter.newLocal(Types.COMPONENT_IMPL);
    adapter.newInstance(Types.COMPONENT_IMPL);
    adapter.dup();

    Attribute attr;
    // ComponentPage
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.checkCast(Types.COMPONENT_PAGE);

    // !!! also check CFMLScriptTransformer.addMetaData if you do any change here !!!

    // Output
    attr = component.removeAttribute("output");
    if (attr != null) {
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_REF);
    } else
        ASMConstants.NULL(adapter);

    // synchronized 
    attr = component.removeAttribute("synchronized");
    if (attr != null)
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_VALUE);
    else
        adapter.push(false);

    // extends
    attr = component.removeAttribute("extends");
    if (attr != null)
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_REF);
    else
        adapter.push("");

    // implements
    attr = component.removeAttribute("implements");
    if (attr != null)
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_REF);
    else
        adapter.push("");

    // hint
    attr = component.removeAttribute("hint");
    if (attr != null) {
        Expression value = attr.getValue();
        if (!(value instanceof Literal)) {
            value = LitString.toExprString("[runtime expression]");
        }
        ExpressionUtil.writeOutSilent(value, bc, Expression.MODE_REF);
    } else
        adapter.push("");

    // dspName
    attr = component.removeAttribute("displayname");
    if (attr == null)
        attr = component.getAttribute("display");
    if (attr != null)
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_REF);
    else
        adapter.push("");

    // callpath
    adapter.visitVarInsn(Opcodes.ALOAD, 2);
    // relpath
    adapter.visitVarInsn(Opcodes.ILOAD, 3);

    // style
    attr = component.removeAttribute("style");
    if (attr != null)
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_REF);
    else
        adapter.push("");

    // persistent
    attr = component.removeAttribute("persistent");
    boolean persistent = false;
    if (attr != null) {
        persistent = ASMUtil.toBoolean(attr, component.getStart()).booleanValue();
    }

    // persistent
    attr = component.removeAttribute("accessors");
    boolean accessors = false;
    if (attr != null) {
        accessors = ASMUtil.toBoolean(attr, component.getStart()).booleanValue();
    }

    adapter.push(persistent);
    adapter.push(accessors);
    //ExpressionUtil.writeOutSilent(attr.getValue(),bc, Expression.MODE_VALUE);

    //adapter.visitVarInsn(Opcodes.ALOAD, 4);
    createMetaDataStruct(bc, component.getAttributes(), component.getMetaData());

    adapter.invokeConstructor(Types.COMPONENT_IMPL, CONSTR_COMPONENT_IMPL);

    adapter.storeLocal(comp);

    //Component Impl(ComponentPage componentPage,boolean output, String extend, String hint, String dspName)

    // initComponent(pc,c);
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.loadArg(0);
    adapter.loadLocal(comp);
    adapter.invokeVirtual(Types.COMPONENT_PAGE, INIT_COMPONENT);

    adapter.visitLabel(methodEnd);

    // return component;
    adapter.loadLocal(comp);

    adapter.returnValue();
    //ExpressionUtil.visitLine(adapter, component.getEndLine());
    adapter.endMethod();

}

From source file:lucee.transformer.bytecode.Page.java

License:Open Source License

private void writeOutNewInterface(BytecodeContext statConstr, BytecodeContext constr, List<LitString> keys,
        ClassWriter cw, Tag interf) throws BytecodeException {
    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL,
            NEW_INTERFACE_IMPL_INSTANCE, null, new Type[] { Types.PAGE_EXCEPTION }, cw);
    BytecodeContext bc = new BytecodeContext(null, statConstr, constr, this, keys, cw, name, adapter,
            NEW_INTERFACE_IMPL_INSTANCE, writeLog(), suppressWSbeforeArg, output);
    Label methodBegin = new Label();
    Label methodEnd = new Label();

    adapter.visitLocalVariable("this", "L" + name + ";", null, methodBegin, methodEnd, 0);
    ExpressionUtil.visitLine(bc, interf.getStart());
    adapter.visitLabel(methodBegin);/*from   www .  j av  a2  s . com*/

    //ExpressionUtil.visitLine(adapter, interf.getStartLine());

    int comp = adapter.newLocal(Types.INTERFACE_IMPL);

    adapter.newInstance(Types.INTERFACE_IMPL);
    adapter.dup();

    Attribute attr;
    // Interface Page
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.checkCast(Types.INTERFACE_PAGE);

    // extened
    attr = interf.removeAttribute("extends");
    if (attr != null)
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_REF);
    else
        adapter.push("");

    // hint
    attr = interf.removeAttribute("hint");
    if (attr != null)
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_REF);
    else
        adapter.push("");

    // dspName
    attr = interf.removeAttribute("displayname");
    if (attr == null)
        attr = interf.getAttribute("display");
    if (attr != null)
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_REF);
    else
        adapter.push("");

    // callpath
    adapter.visitVarInsn(Opcodes.ALOAD, 1);
    // relpath
    adapter.visitVarInsn(Opcodes.ILOAD, 2);

    // interface udfs
    adapter.visitVarInsn(Opcodes.ALOAD, 3);

    createMetaDataStruct(bc, interf.getAttributes(), interf.getMetaData());

    adapter.invokeConstructor(Types.INTERFACE_IMPL, CONSTR_INTERFACE_IMPL);

    adapter.storeLocal(comp);

    // initInterface(pc,c);
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    //adapter.loadArg(0);
    adapter.loadLocal(comp);
    adapter.invokeVirtual(Types.INTERFACE_PAGE, INIT_INTERFACE);

    adapter.visitLabel(methodEnd);

    // return interface;
    adapter.loadLocal(comp);

    adapter.returnValue();
    //ExpressionUtil.visitLine(adapter, interf.getEndLine());
    adapter.endMethod();

}

From source file:lucee.transformer.bytecode.Page.java

License:Open Source License

public static void createMetaDataStruct(BytecodeContext bc, Map attrs, Map meta) throws BytecodeException {

    GeneratorAdapter adapter = bc.getAdapter();
    if ((attrs == null || attrs.size() == 0) && (meta == null || meta.size() == 0)) {
        ASMConstants.NULL(bc.getAdapter());
        bc.getAdapter().cast(Types.OBJECT, STRUCT_IMPL);
        return;//from w ww.  j  av  a 2 s .  co m
    }

    int sct = adapter.newLocal(STRUCT_IMPL);
    adapter.newInstance(STRUCT_IMPL);
    adapter.dup();
    adapter.invokeConstructor(STRUCT_IMPL, INIT_STRUCT_IMPL);
    adapter.storeLocal(sct);
    if (meta != null) {
        _createMetaDataStruct(bc, adapter, sct, meta);
    }
    if (attrs != null) {
        _createMetaDataStruct(bc, adapter, sct, attrs);
    }

    adapter.loadLocal(sct);
}