Example usage for org.objectweb.asm Opcodes PUTSTATIC

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

Introduction

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

Prototype

int PUTSTATIC

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

Click Source Link

Usage

From source file:com.google.devtools.build.android.desugar.LambdaClassFixer.java

License:Open Source License

@Override
public void visitEnd() {
    checkState(!hasState || hasFactory, "Expected factory method for capturing lambda %s", getInternalName());
    if (!hasState) {
        checkState(signature == null, "Didn't expect generic constructor signature %s %s", getInternalName(),
                signature);/*from w w w.  j av a 2s  . c om*/
        checkState(lambdaInfo.factoryMethodDesc().startsWith("()"),
                "Expected 0-arg factory method for %s but found %s", getInternalName(),
                lambdaInfo.factoryMethodDesc());
        // Since this is a stateless class we populate and use a static singleton field "$instance".
        // Field is package-private so we can read it from the class that had the invokedynamic.
        String singletonFieldDesc = lambdaInfo.factoryMethodDesc().substring("()".length());
        super.visitField(Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, SINGLETON_FIELD_NAME, singletonFieldDesc,
                (String) null, (Object) null).visitEnd();

        MethodVisitor codeBuilder = super.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", (String) null,
                new String[0]);
        codeBuilder.visitTypeInsn(Opcodes.NEW, getInternalName());
        codeBuilder.visitInsn(Opcodes.DUP);
        codeBuilder.visitMethodInsn(Opcodes.INVOKESPECIAL, getInternalName(), "<init>",
                checkNotNull(desc, "didn't see a constructor for %s", getInternalName()), /*itf*/ false);
        codeBuilder.visitFieldInsn(Opcodes.PUTSTATIC, getInternalName(), SINGLETON_FIELD_NAME,
                singletonFieldDesc);
        codeBuilder.visitInsn(Opcodes.RETURN);
        codeBuilder.visitMaxs(2, 0); // two values are pushed onto the stack
        codeBuilder.visitEnd();
    }

    copyRewrittenLambdaMethods();
    if (!allowDefaultMethods) {
        copyBridgeMethods(interfaces);
    }
    super.visitEnd();
}

From source file:com.google.devtools.build.wireless.testing.java.injector.WhiteBoxMethodAdapter.java

License:Apache License

/**
 * Tells if the given instruction is corresponding to 
 * {@link Opcodes#PUTSTATIC}.// www  .  j a  va 2s  . co  m
 * 
 * @param opcode The given instruction;
 * @return <code>true</code> if the given instruction is 
 *    {@link Opcodes#PUTSTATIC}, <code>false</code> otherwise.
 */
private boolean isStaticField(int opcode) {
    return opcode == Opcodes.PUTSTATIC;
}

From source file:com.google.singletondetector.visitors.SingletonUsageMethodVisitor.java

License:Apache License

@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    if ((opcode == Opcodes.GETSTATIC || opcode == Opcodes.PUTSTATIC) && desc.startsWith("L")) {
        sd.fieldInstruction(owner);/*from   w  w  w. ja va  2s . c  o m*/
    }
}

From source file:com.google.test.metric.asm.MethodVisitorBuilder.java

License:Apache License

public void visitFieldInsn(final int opcode, String owner, final String name, final String desc) {
    owner = namer.nameClass(owner);/* w ww . j av a  2 s . co  m*/
    switch (opcode) {
    case Opcodes.PUTSTATIC:
        recorder.add(new PutFieldRunnable(repository, owner, name, desc, true));
        break;
    case Opcodes.PUTFIELD:
        recorder.add(new PutFieldRunnable(repository, owner, name, desc, false));
        break;
    case Opcodes.GETSTATIC:
        recorder.add(new GetFieldRunnable(repository, owner, name, desc, true));
        break;
    case Opcodes.GETFIELD:
        recorder.add(new GetFieldRunnable(repository, owner, name, desc, false));
        break;
    }
}

From source file:com.googlecode.ddom.weaver.ext.ModelExtensionFactoryImplementation.java

License:Apache License

public void accept(ClassVisitor classVisitor) {
    // Note: the name chosen here must match what is expected in ExtensionFactoryLocator
    String name = Util.classNameToInternalName(implementationInfo.getFactoryInterface().getName() + "$$Impl");
    String factoryInterfaceName = Util
            .classNameToInternalName(implementationInfo.getFactoryInterface().getName());
    classVisitor.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, name, null, "java/lang/Object",
            new String[] { factoryInterfaceName });
    {/*from  www  .  j a  v  a2 s .c  o  m*/
        FieldVisitor fw = classVisitor.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC,
                "INSTANCE", "L" + factoryInterfaceName + ";", null, null);
        if (fw != null) {
            fw.visitEnd();
        }
    }
    {
        FieldVisitor fw = classVisitor.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "delegates",
                "Ljava/util/Map;", null, null);
        if (fw != null) {
            fw.visitEnd();
        }
    }
    {
        MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PRIVATE, "<init>", "()V", null, null);
        if (mv != null) {
            mv.visitCode();
            Label l0 = new Label();
            mv.visitLabel(l0);
            // Call constructor from superclass
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
            // Create delegates map
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap");
            mv.visitInsn(Opcodes.DUP);
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V");
            mv.visitFieldInsn(Opcodes.PUTFIELD, name, "delegates", "Ljava/util/Map;");
            // Populate delegates map
            for (ModelExtensionInfo modelExtensionInfo : implementationInfo.getModelExtensions()) {
                for (ModelExtensionInterfaceInfo extensionInterface : modelExtensionInfo
                        .getExtensionInterfaces()) {
                    if (!extensionInterface.isAbstract()) {
                        // TODO: this is stupid; we should not recreate the info object here
                        ModelExtensionClassInfo modelExtensionClassInfo = new ModelExtensionClassInfo(
                                implementationInfo.getImplementation(), modelExtensionInfo.getRootInterface(),
                                extensionInterface);
                        String factoryDelegateImplName = Util.classNameToInternalName(
                                modelExtensionClassInfo.getFactoryDelegateImplementationClassName());
                        mv.visitVarInsn(Opcodes.ALOAD, 0);
                        mv.visitFieldInsn(Opcodes.GETFIELD, name, "delegates", "Ljava/util/Map;");
                        mv.visitLdcInsn(Type.getObjectType(Util.classNameToInternalName(
                                modelExtensionClassInfo.getExtensionInterface().getClassInfo().getName())));
                        mv.visitTypeInsn(Opcodes.NEW, factoryDelegateImplName);
                        mv.visitInsn(Opcodes.DUP);
                        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, factoryDelegateImplName, "<init>", "()V");
                        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put",
                                "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
                        mv.visitInsn(Opcodes.POP);
                    }
                }
            }
            mv.visitInsn(Opcodes.RETURN);
            Label l1 = new Label();
            mv.visitLabel(l1);
            mv.visitLocalVariable("this", "L" + name + ";", null, l0, l1, 0);
            mv.visitMaxs(4, 1);
            mv.visitEnd();
        }
    }
    String factoryDelegateInterfaceName = Util
            .classNameToInternalName(implementationInfo.getFactoryDelegateInterfaceName());
    String getDelegateDesc = "(Ljava/lang/Class;)L" + factoryDelegateInterfaceName + ";";
    {
        MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PRIVATE, "getDelegate", getDelegateDesc, null,
                null);
        if (mv != null) {
            mv.visitCode();
            Label l0 = new Label();
            mv.visitLabel(l0);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(Opcodes.GETFIELD, name, "delegates", "Ljava/util/Map;");
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "get",
                    "(Ljava/lang/Object;)Ljava/lang/Object;");
            mv.visitTypeInsn(Opcodes.CHECKCAST, factoryDelegateInterfaceName);
            mv.visitInsn(Opcodes.ARETURN);
            Label l1 = new Label();
            mv.visitLabel(l1);
            mv.visitLocalVariable("this", "L" + name + ";", null, l0, l1, 0);
            mv.visitLocalVariable("extensionInterface", "Ljava/lang/Class;", null, l0, l1, 1);
            mv.visitMaxs(2, 2);
            mv.visitEnd();
        }
    }
    String implementationName = Util.classNameToInternalName(implementationInfo.getImplementation().getName());
    for (ConstructorInfo constructor : implementationInfo.getConstructors()) {
        Type[] constructorArgumentTypes = constructor.getArgumentTypes();
        Type[] argumentTypes = new Type[constructorArgumentTypes.length + 1];
        argumentTypes[0] = Type.getObjectType("java/lang/Class");
        System.arraycopy(constructorArgumentTypes, 0, argumentTypes, 1, constructorArgumentTypes.length);
        MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PUBLIC, "create",
                Type.getMethodDescriptor(Type.getObjectType(implementationName), argumentTypes), null, null);
        if (mv != null) {
            mv.visitCode();
            Label l0 = new Label();
            mv.visitLabel(l0);
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            Label l1 = new Label();
            mv.visitJumpInsn(Opcodes.IFNONNULL, l1);
            mv.visitTypeInsn(Opcodes.NEW, implementationName);
            mv.visitInsn(Opcodes.DUP);
            for (int i = 0; i < constructorArgumentTypes.length; i++) {
                mv.visitVarInsn(constructorArgumentTypes[i].getOpcode(Opcodes.ILOAD), i + 2);
            }
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, implementationName, "<init>",
                    constructor.getDescriptor());
            mv.visitInsn(Opcodes.ARETURN);
            mv.visitLabel(l1);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, name, "getDelegate", getDelegateDesc);
            for (int i = 0; i < constructorArgumentTypes.length; i++) {
                mv.visitVarInsn(constructorArgumentTypes[i].getOpcode(Opcodes.ILOAD), i + 2);
            }
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, factoryDelegateInterfaceName, "create",
                    constructor.getFactoryDelegateMethodDescriptor());
            mv.visitInsn(Opcodes.ARETURN);
            Label l3 = new Label();
            mv.visitLabel(l3);
            mv.visitLocalVariable("this", "L" + name + ";", null, l0, l3, 0);
            mv.visitLocalVariable("extensionInterface", "Ljava/lang/Class;", null, l0, l3, 1);
            for (int i = 0; i < constructorArgumentTypes.length; i++) {
                mv.visitLocalVariable("arg" + i, constructorArgumentTypes[i].getDescriptor(), null, l0, l3,
                        i + 2);
            }
            mv.visitMaxs(argumentTypes.length + 1, argumentTypes.length + 1);
            mv.visitEnd();
        }
    }
    {
        MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
        if (mv != null) {
            mv.visitCode();
            mv.visitTypeInsn(Opcodes.NEW, name);
            mv.visitInsn(Opcodes.DUP);
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, name, "<init>", "()V");
            mv.visitFieldInsn(Opcodes.PUTSTATIC, name, "INSTANCE", "L" + factoryInterfaceName + ";");
            mv.visitInsn(Opcodes.RETURN);
            mv.visitMaxs(2, 0);
            mv.visitEnd();
        }
    }
    classVisitor.visitEnd();
}

From source file:com.googlecode.ddom.weaver.inject.InjectionAdapter.java

License:Apache License

@Override
public void visitEnd() {
    for (InjectableFieldInfo fieldInfo : injectionInfo.getInjectableFields()) {
        MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
                fieldInfo.getFactoryMethodName(), fieldInfo.getFactoryMethodDesc(), null, new String[0]);
        if (mv != null) {
            fieldInfo.getInjector().generateFactoryMethodCode(mv);
        }/*  w  w w.  j  av a2  s .  c om*/
    }
    if (injectionInfo.hasInjectableInstanceFields()) {
        MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PRIVATE, "inject$$instance", "()V", null, new String[0]);
        if (mv != null) {
            mv.visitCode();
            Label l0 = new Label();
            mv.visitLabel(l0);
            for (InjectableFieldInfo fieldInfo : injectionInfo.getInjectableFields()) {
                if (!fieldInfo.isStatic()) {
                    mv.visitVarInsn(Opcodes.ALOAD, 0);
                    mv.visitMethodInsn(Opcodes.INVOKESTATIC, className, fieldInfo.getFactoryMethodName(),
                            fieldInfo.getFactoryMethodDesc());
                    mv.visitFieldInsn(Opcodes.PUTFIELD, className, fieldInfo.getFieldName(),
                            fieldInfo.getFieldDesc());
                }
            }
            mv.visitInsn(Opcodes.RETURN);
            Label l1 = new Label();
            mv.visitLabel(l1);
            mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0);
            mv.visitMaxs(2, 1);
            mv.visitEnd();
        }
    }
    if (injectionInfo.hasInjectableClassFields()) {
        MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
                hasClassInitializer ? "inject$$class" : "<clinit>", "()V", null, new String[0]);
        if (mv != null) {
            mv.visitCode();
            for (InjectableFieldInfo fieldInfo : injectionInfo.getInjectableFields()) {
                if (fieldInfo.isStatic()) {
                    mv.visitMethodInsn(Opcodes.INVOKESTATIC, className, fieldInfo.getFactoryMethodName(),
                            fieldInfo.getFactoryMethodDesc());
                    mv.visitFieldInsn(Opcodes.PUTSTATIC, className, fieldInfo.getFieldName(),
                            fieldInfo.getFieldDesc());
                }
            }
            mv.visitInsn(Opcodes.RETURN);
            mv.visitMaxs(1, 0);
            mv.visitEnd();
        }
    }
    super.visitEnd();
}

From source file:com.liferay.portal.upgrade.test.BaseBuildAutoUpgradeTestCase.java

License:Open Source License

private void _initTableColumns(MethodVisitor methodVisitor, Object[][] tableColumns) {

    methodVisitor.visitCode();/*from   www  .  j av  a 2 s. c  om*/

    methodVisitor.visitInsn(Opcodes.ICONST_0 + tableColumns.length);
    methodVisitor.visitTypeInsn(Opcodes.ANEWARRAY, Type.getDescriptor(Object[].class));
    methodVisitor.visitInsn(Opcodes.DUP);

    for (int i = 0; i < tableColumns.length; i++) {
        Object[] tableColumn = tableColumns[i];

        methodVisitor.visitInsn(Opcodes.ICONST_0 + i);
        methodVisitor.visitInsn(Opcodes.ICONST_2);
        methodVisitor.visitTypeInsn(Opcodes.ANEWARRAY, Type.getInternalName(Object.class));
        methodVisitor.visitInsn(Opcodes.DUP);
        methodVisitor.visitInsn(Opcodes.ICONST_0);
        methodVisitor.visitLdcInsn(tableColumn[0]);
        methodVisitor.visitInsn(Opcodes.AASTORE);
        methodVisitor.visitInsn(Opcodes.DUP);
        methodVisitor.visitInsn(Opcodes.ICONST_1);
        methodVisitor.visitIntInsn(Opcodes.BIPUSH, (Integer) tableColumn[1]);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Integer.class), "valueOf",
                Type.getMethodDescriptor(Type.getType(Integer.class), Type.INT_TYPE), false);

        methodVisitor.visitInsn(Opcodes.AASTORE);
        methodVisitor.visitInsn(Opcodes.AASTORE);

        if (i < (tableColumns.length - 1)) {
            methodVisitor.visitInsn(Opcodes.DUP);
        }
    }

    methodVisitor.visitFieldInsn(Opcodes.PUTSTATIC,
            Type.getInternalName(BuildAutoUpgradeTestEntityModelImpl.class), "TABLE_COLUMNS",
            Type.getDescriptor(Object[][].class));
    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();
}

From source file:com.mebigfatguy.junitflood.jvm.OperandStack.java

License:Apache License

public void performFieldInsn(int opcode, String owner, String name, String desc) {
    switch (opcode) {
    case Opcodes.GETSTATIC:
    case Opcodes.GETFIELD: {
        Operand op = fields.get(owner + ":" + name);
        if (op == null) {
            op = new Operand(owner + ":" + name, desc);
        }/*from  www . j  a  v a  2 s  .c o  m*/
        push(op);
    }
        break;

    case Opcodes.PUTSTATIC:
    case Opcodes.PUTFIELD:
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            fields.put(owner + ":" + name, op);
        }
        break;
    }
}

From source file:com.sun.fortress.runtimeSystem.MethodInstantiater.java

License:Open Source License

public void visitFieldInsn(int opcode, String owner, String name, String orig_desc) {
    owner = xlation.getTypeName(owner);/*from  ww  w  .j a  va2 s.  co  m*/
    name = xlation.getTypeName(name);
    String desc = xlation.getFieldDesc(orig_desc);
    if (owner.endsWith(FACTORY_SUFFIX) && name.equals(Naming.RTTI_SINGLETON)) {
        rttiReference(owner);
    } else if (opcode == Opcodes.PUTSTATIC && name.equals(Naming.CLOSURE_FIELD_NAME)) {
        String unwrapped_desc = xlation.getFieldDesc(orig_desc, true);
        String wrapped_desc = xlation.getFieldDesc(orig_desc, false);
        if (!wrapped_desc.equals(unwrapped_desc)) {
            /* This is a hack; when a closure looks different depending on
             * how its descriptor translates (wrapped vs not), that means that
             * there will be a second closure field, and it needs to be initialized.
             */
            mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, unwrapped_desc);
            mv.visitFieldInsn(Opcodes.GETSTATIC, owner, name, unwrapped_desc);
            mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, wrapped_desc);

        } else {
            mv.visitFieldInsn(opcode, owner, name, desc);
        }
    } else {
        mv.visitFieldInsn(opcode, owner, name, desc);
    }
}

From source file:com.trigersoft.jaque.expression.ExpressionMethodVisitor.java

License:Apache License

@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    Expression e;//from   w  w w  . j  ava  2  s. co  m
    switch (opcode) {
    case Opcodes.GETFIELD:
        try {
            e = Expression.get(_exprStack.pop(), name);
        } catch (NoSuchFieldException nsfe) {
            throw new RuntimeException(nsfe);
        }
        break;
    case Opcodes.GETSTATIC:
        try {
            e = Expression.get(_classVisitor.getClass(Type.getObjectType(owner)), name);
        } catch (NoSuchFieldException nsfe) {
            throw new RuntimeException(nsfe);
        }
        break;
    case Opcodes.PUTFIELD:
    case Opcodes.PUTSTATIC:
    default:
        throw notLambda(opcode);
    }

    _exprStack.push(e);
}