Example usage for org.objectweb.asm.commons InstructionAdapter putstatic

List of usage examples for org.objectweb.asm.commons InstructionAdapter putstatic

Introduction

In this page you can find the example usage for org.objectweb.asm.commons InstructionAdapter putstatic.

Prototype

public void putstatic(final String owner, final String name, final String descriptor) 

Source Link

Usage

From source file:com.gargoylesoftware.js.nashorn.internal.runtime.linker.JavaAdapterBytecodeGenerator.java

License:Open Source License

private void generateClassInit() {
    final InstructionAdapter mv = new InstructionAdapter(
            cw.visitMethod(ACC_STATIC, CLASS_INIT, Type.getMethodDescriptor(Type.VOID_TYPE), null, null));

    mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "getClassOverrides", GET_CLASS_INITIALIZER_DESCRIPTOR, false);
    final Label initGlobal;
    if (samName != null) {
        // If the class is a SAM, allow having a ScriptFunction passed as class overrides
        final Label notAFunction = new Label();
        mv.dup();//from   w  ww  . j ava 2  s  .c o  m
        mv.instanceOf(SCRIPT_FUNCTION_TYPE);
        mv.ifeq(notAFunction);
        mv.checkcast(SCRIPT_FUNCTION_TYPE);

        // Assign MethodHandle fields through invoking getHandle() for a ScriptFunction, only assigning the SAM
        // method(s).
        for (final MethodInfo mi : methodInfos) {
            if (mi.getName().equals(samName)) {
                mv.dup();
                loadMethodTypeAndGetHandle(mv, mi, GET_HANDLE_FUNCTION_DESCRIPTOR);
            } else {
                mv.visitInsn(ACONST_NULL);
            }
            mv.putstatic(generatedClassName, mi.methodHandleFieldName, METHOD_HANDLE_TYPE_DESCRIPTOR);
        }
        initGlobal = new Label();
        mv.goTo(initGlobal);
        mv.visitLabel(notAFunction);
    } else {
        initGlobal = null;
    }
    // Assign MethodHandle fields through invoking getHandle() for a ScriptObject
    for (final MethodInfo mi : methodInfos) {
        mv.dup();
        mv.aconst(mi.getName());
        loadMethodTypeAndGetHandle(mv, mi, GET_HANDLE_OBJECT_DESCRIPTOR);
        mv.putstatic(generatedClassName, mi.methodHandleFieldName, METHOD_HANDLE_TYPE_DESCRIPTOR);
    }

    if (initGlobal != null) {
        mv.visitLabel(initGlobal);
    }
    // Assign "global = Context.getGlobal()"
    invokeGetGlobalWithNullCheck(mv);
    mv.putstatic(generatedClassName, GLOBAL_FIELD_NAME, GLOBAL_TYPE_DESCRIPTOR);

    generateConverterInit(mv, false);
    endInitMethod(mv);
}

From source file:com.gargoylesoftware.js.nashorn.internal.runtime.linker.JavaAdapterBytecodeGenerator.java

License:Open Source License

private void generateConverterInit(final InstructionAdapter mv, final boolean samOnly) {
    assert !samOnly || !classOverride;
    for (final Map.Entry<Class<?>, String> converterField : converterFields.entrySet()) {
        final Class<?> returnType = converterField.getKey();
        if (!classOverride) {
            mv.visitVarInsn(ALOAD, 0);/*from   w ww .j a  v  a 2 s .  com*/
        }

        if (samOnly && !samReturnTypes.contains(returnType)) {
            mv.visitInsn(ACONST_NULL);
        } else {
            mv.aconst(Type.getType(converterField.getKey()));
            mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "getObjectConverter", GET_CONVERTER_METHOD_DESCRIPTOR,
                    false);
        }

        if (classOverride) {
            mv.putstatic(generatedClassName, converterField.getValue(), METHOD_HANDLE_TYPE_DESCRIPTOR);
        } else {
            mv.putfield(generatedClassName, converterField.getValue(), METHOD_HANDLE_TYPE_DESCRIPTOR);
        }
    }
}

From source file:com.github.jasmo.obfuscate.ScrambleStrings.java

License:Open Source License

private void createStaticConstructor(ClassNode owner) throws UnsupportedEncodingException {
    MethodNode original = BytecodeHelper.getMethod(owner, "<clinit>", "()V");
    MethodVisitor mv = owner.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
    // generate instructions
    InstructionAdapter builder = new InstructionAdapter(mv);
    builder.iconst(stringList.size());// ww w. j a  v  a  2 s.c o m
    builder.newarray(Type.getType(String.class));
    for (int i = 0; i < stringList.size(); i++) {
        builder.dup();
        builder.iconst(i);
        builder.aconst(Base64.getEncoder().encodeToString(stringList.get(i).getBytes("UTF-8")));
        builder.astore(InstructionAdapter.OBJECT_TYPE);
    }
    builder.putstatic(unscrambleClass.name, FIELD_NAME, "[Ljava/lang/String;");
    // merge with original if it exists
    if (original != null) {
        // original should already end with RETURN
        owner.methods.remove(original);
        original.instructions.accept(builder);
    } else {
        builder.areturn(Type.VOID_TYPE);
    }
}

From source file:com.google.devtools.build.android.resources.IntArrayFieldInitializer.java

License:Open Source License

@Override
public int writeCLInit(InstructionAdapter insts, String className) {
    insts.iconst(values.size());/*from   w  ww .  j  a v a 2  s. co  m*/
    insts.newarray(Type.INT_TYPE);
    int curIndex = 0;
    for (Integer value : values) {
        insts.dup();
        insts.iconst(curIndex);
        insts.iconst(value);
        insts.astore(Type.INT_TYPE);
        ++curIndex;
    }
    insts.putstatic(className, fieldName, DESC);
    // Needs up to 4 stack slots for: the array ref for the putstatic, the dup of the array ref
    // for the store, the index, and the value to store.
    return 4;
}

From source file:com.google.devtools.build.android.resources.IntFieldInitializer.java

License:Open Source License

@Override
public int writeCLInit(InstructionAdapter insts, String className) {
    insts.iconst(value);// w  w w  .j a  va  2s  .  c  o  m
    insts.putstatic(className, fieldName, DESC);
    // Just needs one stack slot for the iconst.
    return 1;
}

From source file:edu.ubc.mirrors.holograms.HologramMethodGenerator.java

License:Open Source License

public static void initializeStaticFields(Type owner, InstructionAdapter mv) {
    // Initialize the static field that holds the ClassMirror for this holographic Class
    mv.aconst(owner);/* w  ww  . jav a2s. c o m*/
    new MethodHandle() {
        protected void methodCall() throws Throwable {
            ObjectHologram.getClassMirrorForHolographicClass(null);
        }
    }.invoke(mv);
    mv.dup();
    mv.putstatic(owner.getInternalName(), "classMirror", classMirrorType.getDescriptor());
}