Example usage for org.objectweb.asm Opcodes ACC_PUBLIC

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

Introduction

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

Prototype

int ACC_PUBLIC

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

Click Source Link

Usage

From source file:me.themallard.bitmmo.api.transformer.Transformer.java

License:Open Source License

protected final void createGetter(ClassNode cn, FieldNode fn, String name) {
    MethodVisitor mv = cn.visitMethod(Opcodes.ACC_PUBLIC, name, "()" + fn.desc, null, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);/* w w w . j a va2 s.c o m*/
    mv.visitFieldInsn(Opcodes.GETFIELD, cn.name, fn.name, fn.desc);
    mv.visitInsn(Type.getType(fn.desc).getOpcode(Opcodes.IRETURN));
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:me.themallard.bitmmo.api.transformer.Transformer.java

License:Open Source License

protected final void createSetter(ClassNode cn, FieldNode fn, String name) {
    MethodVisitor mv = cn.visitMethod(Opcodes.ACC_PUBLIC, name, "(" + fn.desc + ")V", null, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);//from w  w w  . j  a v a  2 s  .  com
    mv.visitVarInsn(Type.getType(fn.desc).getOpcode(Opcodes.ILOAD), 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, cn.name, fn.name, fn.desc);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:me.themallard.bitmmo.impl.plugin.playerhook.PlayerPlugin.java

License:Open Source License

@Override
public void run(ClassNode cn) {
    MethodVisitor mv = cn.visitMethod(Opcodes.ACC_PUBLIC, "getPosition",
            "()" + "Lme/themallard/bitmmo/impl/plugin/position/IPosition;", null, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);//  w w w.j a  v a  2 s . c o m
    mv.visitFieldInsn(Opcodes.GETFIELD, "Entity", "position", "LPosition;");
    mv.visitInsn(
            Type.getType("Lme/themallard/bitmmo/impl/plugin/position/IPosition;").getOpcode(Opcodes.IRETURN));
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:me.tunsi.test.dynamic.JiteClassTest.java

License:Apache License

@Test
public void testFields() throws Exception {
    JiteClass jiteClass = new JiteClass("testFields", p(Object.class), new String[0]) {
        {/*from   w w w .j  a  va  2  s . c o m*/
            defineField("foo", ACC_PUBLIC | ACC_STATIC, ci(String.class), "bar");
        }
    };

    Class<?> clazz = new DynamicClassLoader().define(jiteClass);
    Field foo = clazz.getDeclaredField("foo");

    assertEquals("foo field was not a string", String.class, foo.getType());
    assertEquals("foo field was not set to 'bar'", "bar", foo.get(null));

    jiteClass.defineField("foo1", Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, ci(String.class), "Bardd");
    clazz = new DynamicClassLoader().define(jiteClass);
    Field foo2 = clazz.getDeclaredField("foo1");
    System.out.println(foo2.get(null));
}

From source file:net.cazzar.corelib.asm.MethodTransformer.java

License:Open Source License

protected final void replaceMethod(ClassNode node, String name, String desc, InsnList instructions,
        TryCatchBlockNode... tryCatchBlocks) {
    replaceMethod(node, Opcodes.ACC_PUBLIC, name, desc, instructions, tryCatchBlocks);
}

From source file:net.cazzar.corelib.asm.MethodTransformer.java

License:Open Source License

protected final void appendToMethod(ClassNode node, String name, String desc, InsnList insns,
        TryCatchBlockNode... tryCatchBlockNodes) {
    appendToMethod(node, Opcodes.ACC_PUBLIC, name, desc, insns, tryCatchBlockNodes);
}

From source file:net.enilink.composition.asm.ExtendedClassNode.java

License:Open Source License

/**
 * Creates an {@link ExtendedClassNode}.
 * //from  ww w .j a v  a  2  s  .c  o m
 * @param type
 *            Java type that gets defined by this class node.
 * @param parentClass
 *            The direct super class or the primary interface for this class
 *            node.
 * @param parentClassInfo
 *            Optional meta information for <code>parentClass</code>.
 */
public ExtendedClassNode(Type type, Class<?> parentClass, ClassInfo parentClassInfo) {
    super(Opcodes.ASM5);
    String[] interfaces = new String[parentClass.getInterfaces().length];
    int i = 0;
    for (Class<?> face : parentClass.getInterfaces()) {
        interfaces[i++] = Type.getInternalName(face);
    }

    Class<?> superClass = parentClass.isInterface() ? Object.class : parentClass;
    visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, type.getInternalName(), null, Type.getInternalName(superClass),
            interfaces);

    if (parentClassInfo != null) {
        parentClassInfo.copyAnnotations(this);
    }

    this.type = type;
    this.parentClass = parentClass;
    parentType = Type.getType(parentClass);
}

From source file:net.fabricmc.base.transformer.AccessTransformer.java

License:Apache License

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (!name.startsWith("net.minecraft")) {
        return bytes;
    }//from   w  w  w  .  j a  v  a2s .co  m
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);
    boolean isClassProtected = classNode.access == Opcodes.ACC_PROTECTED;
    boolean isClassPrivate = classNode.access == Opcodes.ACC_PRIVATE;
    if (isClassProtected || isClassPrivate) {
        classNode.access = Opcodes.ACC_PUBLIC;
    }
    for (MethodNode method : classNode.methods) {
        boolean isProtected = method.access == Opcodes.ACC_PROTECTED;
        boolean isPrivate = method.access == Opcodes.ACC_PRIVATE;
        if (isProtected || isPrivate) {
            method.access = Opcodes.ACC_PUBLIC;
        }
    }
    for (FieldNode field : classNode.fields) {
        boolean isProtected = field.access == Opcodes.ACC_PROTECTED;
        boolean isPrivate = field.access == Opcodes.ACC_PRIVATE;
        if (isProtected || isPrivate) {
            field.access = Opcodes.ACC_PUBLIC;
        }
    }
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    return writer.toByteArray();
}

From source file:net.minecraftforge.fml.common.asm.transformers.SoundEngineFixTransformer.java

License:Open Source License

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    if (transformedName.equals("paulscode.sound.Source")) {
        ClassNode classNode = new ClassNode();
        ClassReader classReader = new ClassReader(basicClass);
        classReader.accept(classNode, 0);

        classNode.fields.add(new FieldNode(Opcodes.ACC_PUBLIC, "removed", "Z", null, null)); // adding field 'public boolean removed;'

        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classNode.accept(writer);//from  ww w.java 2 s  . c  om
        return writer.toByteArray();
    } else if (transformedName.equals("paulscode.sound.Library")) {
        ClassNode classNode = new ClassNode();
        ClassReader classReader = new ClassReader(basicClass);
        classReader.accept(classNode, 0);

        MethodNode method = null;
        for (MethodNode m : classNode.methods) {
            if (m.name.equals("removeSource") && m.desc.equals("(Ljava/lang/String;)V")) // trying to find paulscode.sound.Library.removeSource(String)
            {
                method = m;
                break;
            }
        }
        if (method == null)
            throw new RuntimeException(
                    "Error processing " + transformedName + " - no removeSource method found");

        AbstractInsnNode referenceNode = null;

        for (Iterator<AbstractInsnNode> iterator = method.instructions.iterator(); iterator.hasNext();) {
            AbstractInsnNode insn = iterator.next();
            if (insn instanceof MethodInsnNode && ((MethodInsnNode) insn).owner.equals("paulscode/sound/Source") // searching for mySource.cleanup() node (line 1086)
                    && ((MethodInsnNode) insn).name.equals("cleanup")) {
                referenceNode = insn;
                break;
            }
        }

        if (referenceNode != null) {
            LabelNode after = (LabelNode) referenceNode.getNext();

            AbstractInsnNode beginning = referenceNode.getPrevious();

            int varIndex = ((VarInsnNode) beginning).var;

            method.instructions.insertBefore(beginning, new VarInsnNode(Opcodes.ALOAD, varIndex)); // adding extra if (mySource.toStream)
            method.instructions.insertBefore(beginning,
                    new FieldInsnNode(Opcodes.GETFIELD, "paulscode/sound/Source", "toStream", "Z"));
            LabelNode elseNode = new LabelNode();
            method.instructions.insertBefore(beginning, new JumpInsnNode(Opcodes.IFEQ, elseNode)); // if fails (else) -> go to mySource.cleanup();

            method.instructions.insertBefore(beginning, new VarInsnNode(Opcodes.ALOAD, varIndex)); // if (mySource.toStream) { mySource.removed = true; }
            method.instructions.insertBefore(beginning, new InsnNode(Opcodes.ICONST_1));
            method.instructions.insertBefore(beginning,
                    new FieldInsnNode(Opcodes.PUTFIELD, "paulscode/sound/Source", "removed", "Z"));

            method.instructions.insertBefore(beginning, new JumpInsnNode(Opcodes.GOTO, after)); // still inside if -> jump to sourceMap.remove( sourcename );

            method.instructions.insertBefore(beginning, elseNode);
        }

        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classNode.accept(writer);
        return writer.toByteArray();
    } else if (transformedName.equals("paulscode.sound.StreamThread")) {
        ClassNode classNode = new ClassNode();
        ClassReader classReader = new ClassReader(basicClass);
        classReader.accept(classNode, 0);

        MethodNode method = null;
        for (MethodNode m : classNode.methods) {
            if (m.name.equals("run") && m.desc.equals("()V")) // trying to find paulscode.sound.StreamThread.run();
            {
                method = m;
                break;
            }
        }
        if (method == null)
            throw new RuntimeException("Error processing " + transformedName + " - no run method found");

        AbstractInsnNode referenceNode = null;

        for (Iterator<AbstractInsnNode> iterator = method.instructions.iterator(); iterator.hasNext();) {
            AbstractInsnNode insn = iterator.next();
            if (insn instanceof MethodInsnNode && ((MethodInsnNode) insn).owner.equals("java/util/ListIterator") // searching for 'src = iter.next();' node (line 110)
                    && ((MethodInsnNode) insn).name.equals("next")) {
                referenceNode = insn.getNext().getNext();
                break;
            }
        }

        if (referenceNode != null) {
            int varIndex = ((VarInsnNode) referenceNode).var;

            LabelNode after = (LabelNode) referenceNode.getNext();
            method.instructions.insertBefore(after, new VarInsnNode(Opcodes.ALOAD, varIndex)); // add if(removed)
            method.instructions.insertBefore(after,
                    new FieldInsnNode(Opcodes.GETFIELD, "paulscode/sound/Source", "removed", "Z"));
            method.instructions.insertBefore(after, new JumpInsnNode(Opcodes.IFEQ, after));

            // if the source has been marked as removed, clean it up and set the variable to null so it will be removed from the list
            method.instructions.insertBefore(after, new VarInsnNode(Opcodes.ALOAD, varIndex)); // src.cleanup();
            method.instructions.insertBefore(after, new MethodInsnNode(Opcodes.INVOKEVIRTUAL,
                    "paulscode/sound/Source", "cleanup", "()V", false));
            method.instructions.insertBefore(after, new InsnNode(Opcodes.ACONST_NULL)); // src = null;
            method.instructions.insertBefore(after, new VarInsnNode(Opcodes.ASTORE, varIndex));
        }

        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classNode.accept(writer);
        return writer.toByteArray();
    }

    return basicClass;
}

From source file:net.petercashel.jmsDd.util.ASMTransformer.java

License:Apache License

public static byte[] transform(String name, byte[] bytes) {
    if (debug)//from  w w  w . ja  v  a 2s.  c o m
        System.out.println(bytes.length);
    ClassNode classNode = new ClassNode();
    String classNameASM = name.replace('.', '/');
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);
    boolean DoModInit = false;
    boolean HasInit = false;
    String initDesc = "()V";
    boolean lockDesc = false;

    try {
        try {
            for (int i = 0; i < classNode.visibleAnnotations.size(); i++) {
                AnnotationNode ann = (AnnotationNode) classNode.visibleAnnotations.get(i);
                if (ann.desc.equalsIgnoreCase("Lnet/petercashel/jmsDd/module/Module;")) {
                    try {
                        if (debug)
                            System.out.println("ANNOTE!");
                        DoModInit = true;
                        Map<String, Object> values = asmList2Map(ann.values);
                        ModuleSystem.modulesToLoad.put(
                                values.get("ModuleName").toString().replace("[", "").replace("]", ""),
                                classNode.name.replace("/", "."));
                    } catch (Exception e) {
                        e.printStackTrace();

                    }
                }
            }
        } catch (Exception e) {
        }

        try {
            if (DoModInit) {

                for (int i = 0; i < classNode.methods.size(); i++) {
                    MethodNode m = (MethodNode) classNode.methods.get(i);
                    if (m.name.contentEquals("<init>")) {
                        initDesc = m.desc;
                        if (m.desc.contentEquals("()V")) {
                            HasInit = true;
                            if (debug)
                                System.out.println("Found <init>");
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (debug)
        System.out.println("Still alive?");
    try {

        //          L0
        //             LINENUMBER 43 L0
        //             ALOAD 0
        //             INVOKESPECIAL // CLASSNAME FOR ASM  // ()V    ////// This is effectically a super() call but to the discovered constructor
        //            L1
        //             LINENUMBER 44 L1
        //             INVOKESTATIC net/petercashel/jmsDd/API/API$Impl.getAPI ()Lnet/petercashel/jmsDd/API/API;
        //             ALOAD 0
        //             INVOKEINTERFACE net/petercashel/jmsDd/API/API.registerEventBus (Ljava/lang/Object;)V
        //            L2
        //             LINENUMBER 46 L2
        //             RETURN
        //            L3
        //             LOCALVARIABLE this // "L" + CLASSNAME FOR ASM + ";" // L0 L3 0
        //             LOCALVARIABLE e Lnet/petercashel/jmsDd/event/module/DummyEvent; L0 L3 1
        //             MAXSTACK = 2
        //             MAXLOCALS = 2
        if (DoModInit) {
            if (HasInit) {
                if (debug)
                    System.out.println("Adding Extra Constructor to " + name);
                MethodNode constructor = new MethodNode(Opcodes.ACC_PUBLIC, "<init>",
                        "(Lnet/petercashel/jmsDd/event/module/DummyEvent;)V", null, null);

                Label L0 = new Label();
                constructor.visitLabel(L0);
                constructor.visitVarInsn(Opcodes.ALOAD, 0);
                constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, classNameASM, "<init>", initDesc);

                Label L1 = new Label();
                constructor.visitLabel(L1);
                constructor.visitMethodInsn(Opcodes.INVOKESTATIC, "net/petercashel/jmsDd/API/API$Impl",
                        "getAPI", "()Lnet/petercashel/jmsDd/API/API;");
                constructor.visitVarInsn(Opcodes.ALOAD, 0);
                constructor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "net/petercashel/jmsDd/API/API",
                        "registerEventBus", "(Ljava/lang/Object;)V");

                Label L2 = new Label();
                constructor.visitLabel(L2);
                constructor.visitInsn(Opcodes.RETURN);

                Label L3 = new Label();
                constructor.visitLabel(L3);
                constructor.visitLocalVariable("this", "L" + classNameASM + ";", null, L0, L3, 0);
                constructor.visitLocalVariable("e", "Lnet/petercashel/jmsDd/event/module/DummyEvent;", null, L0,
                        L3, 1);
                constructor.visitMaxs(2, 2);
                constructor.visitEnd();
                classNode.methods.add(constructor);

            } else {
                System.err.println("WARNING! " + name
                        + " Doesn't have a default no-args constructor.  Module loader cannot chain load the constructor. \n If you are recieving this error and your module has no constructors defined, or a no-args constructor defined, \n please report the bug to the author of JMSDd.");
                MethodNode constructor = new MethodNode(Opcodes.ACC_PUBLIC, "<init>",
                        "(Lnet/petercashel/jmsDd/event/module/DummyEvent;)V", null, null);

                Label L0 = new Label();
                constructor.visitLabel(L0);
                constructor.visitVarInsn(Opcodes.ALOAD, 0);
                //INVOKESPECIAL java/lang/Object.<init> ()V ////// There is no other constructor, call super() to Object.
                constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");

                Label L1 = new Label();
                constructor.visitLabel(L1);
                constructor.visitMethodInsn(Opcodes.INVOKESTATIC, "net/petercashel/jmsDd/API/API$Impl",
                        "getAPI", "()Lnet/petercashel/jmsDd/API/API;");
                constructor.visitVarInsn(Opcodes.ALOAD, 0);
                constructor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "net/petercashel/jmsDd/API/API",
                        "registerEventBus", "(Ljava/lang/Object;)V");

                Label L2 = new Label();
                constructor.visitLabel(L2);
                constructor.visitInsn(Opcodes.RETURN);

                Label L3 = new Label();
                constructor.visitLabel(L3);
                constructor.visitLocalVariable("this", "L" + classNameASM + ";", null, L0, L3, 0);
                constructor.visitLocalVariable("e", "Lnet/petercashel/jmsDd/event/module/DummyEvent;", null, L0,
                        L3, 1);
                constructor.visitMaxs(2, 2);
                constructor.visitEnd();
                classNode.methods.add(constructor);
            }
            classNode.visitEnd();
            ClassWriter wr = new ClassWriter(0);
            classNode.accept(wr);

            bytes = wr.toByteArray();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (debug)
        System.out.println("Still alive.");
    if (debug)
        System.out.println(bytes.length);
    if (plugins.size() > 0) {
        for (ASMPlugin p : plugins) {
            try {
                bytes = p.transform(name, bytes);
            } catch (Exception e) {

            }
        }
    }

    return bytes;
}