Example usage for org.objectweb.asm Opcodes GETSTATIC

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

Introduction

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

Prototype

int GETSTATIC

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

Click Source Link

Usage

From source file:org.adjective.stout.operation.GetEnumValueExpression.java

License:Apache License

public void getInstructions(ExecutionStack stack, InstructionCollector collector) {
    String enumName = Type.getInternalName(_value.getClass());
    String enumDescriptor = Type.getDescriptor(_value.getClass());
    addInstruction(collector, new FieldInstruction(Opcodes.GETSTATIC, enumName, _value.name(), enumDescriptor));
}

From source file:org.adjective.stout.operation.GetStaticFieldExpression.java

License:Apache License

public void getInstructions(ExecutionStack stack, InstructionCollector collector) {
    addInstruction(collector,/*from   w w  w .  j a v  a  2  s  .c  o m*/
            new FieldInstruction(Opcodes.GETSTATIC, _from.getInternalName(), _name, _type.getDescriptor()));
}

From source file:org.adjective.stout.tools.StackVisualiserMethodVisitor.java

License:Apache License

public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    String description = owner + "." + name;
    switch (opcode) {
    case Opcodes.GETFIELD:
        pop();/* w w w  .  j  a v a 2s .co m*/
        push(description, desc);
        break;
    case Opcodes.PUTFIELD:
        pop(2);
        break;
    case Opcodes.GETSTATIC:
        push(description, desc);
        break;
    case Opcodes.PUTSTATIC:
        pop(1);
        break;
    }
    print(opcode, description);
}

From source file:org.boretti.drools.integration.drools5.DroolsAbstractMethodVisitor.java

License:Open Source License

private void addCondition(boolean preCondition, String type, String resourceName, Type error) {
    droolsGoalExecutionLog.getLogs()/*from w  w  w.jav  a2 s.c  o m*/
            .add(new DroolsGoalExecutionLog(droolsGoalExecutionLog.getFileName(),
                    droolsGoalExecutionLog.getAction(), "Method instrumentalization for "
                            + ((preCondition) ? "pre" : "post") + "-condition " + name + "/" + desc));
    visitor.setMethodChange(true);
    Type types[] = Type.getArgumentTypes(super.methodDesc);
    //runPreCondition
    this.visitVarInsn(Opcodes.ALOAD, 0);
    this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getObjectType(parent).getInternalName(), "getClass",
            "()Ljava/lang/Class;");
    if (type == null)
        type = "COMPILED";
    this.visitFieldInsn(Opcodes.GETSTATIC, Type.getInternalName(DroolsServiceType.class), type,
            Type.getDescriptor(DroolsServiceType.class));
    this.visitLdcInsn(resourceName);
    this.visitLdcInsn(error);
    this.visitVarInsn(Opcodes.ALOAD, 0);
    this.visitIntInsn(Opcodes.BIPUSH, types.length);
    this.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
    int position = 1;
    for (int i = 0; i < types.length; i++) {
        this.visitInsn(Opcodes.DUP);
        this.visitIntInsn(Opcodes.BIPUSH, i);
        if (types[i].equals(Type.BOOLEAN_TYPE)) {
            this.visitVarInsn(Opcodes.ILOAD, position);
            this.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Boolean.class), "valueOf",
                    "(Z)Ljava/lang/Boolean;");
            position += 1;
        } else if (types[i].equals(Type.BYTE_TYPE)) {
            this.visitTypeInsn(Opcodes.NEW, "java/lang/Byte");
            this.visitInsn(Opcodes.DUP);
            this.visitVarInsn(Opcodes.ILOAD, position);
            this.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Byte.class), "<init>", "(B)V");
            position += 1;
        } else if (types[i].equals(Type.CHAR_TYPE)) {
            this.visitTypeInsn(Opcodes.NEW, "java/lang/Character");
            this.visitInsn(Opcodes.DUP);
            this.visitVarInsn(Opcodes.ILOAD, position);
            this.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Character.class), "<init>",
                    "(C)V");
            position += 1;
        } else if (types[i].equals(Type.DOUBLE_TYPE)) {
            this.visitTypeInsn(Opcodes.NEW, "java/lang/Double");
            this.visitInsn(Opcodes.DUP);
            this.visitVarInsn(Opcodes.DLOAD, position);
            this.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Double.class), "<init>", "(D)V");
            position += 2;
        } else if (types[i].equals(Type.FLOAT_TYPE)) {
            this.visitTypeInsn(Opcodes.NEW, "java/lang/Float");
            this.visitInsn(Opcodes.DUP);
            this.visitVarInsn(Opcodes.FLOAD, position);
            this.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Float.class), "<init>", "(F)V");
            position += 1;
        } else if (types[i].equals(Type.INT_TYPE)) {
            this.visitTypeInsn(Opcodes.NEW, "java/lang/Integer");
            this.visitInsn(Opcodes.DUP);
            this.visitVarInsn(Opcodes.ILOAD, position);
            this.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Integer.class), "<init>", "(I)V");
            position += 1;
        } else if (types[i].equals(Type.LONG_TYPE)) {
            this.visitTypeInsn(Opcodes.NEW, "java/lang/Long");
            this.visitInsn(Opcodes.DUP);
            this.visitVarInsn(Opcodes.LLOAD, position);
            this.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Long.class), "<init>", "(J)V");
            position += 2;
        } else if (types[i].equals(Type.SHORT_TYPE)) {
            this.visitTypeInsn(Opcodes.NEW, "java/lang/Short");
            this.visitInsn(Opcodes.DUP);
            this.visitVarInsn(Opcodes.ILOAD, position);
            this.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Short.class), "<init>", "(S)V");
            position += 1;
        } else {
            this.visitVarInsn(Opcodes.ALOAD, position);
            position += 1;
        }
        this.visitInsn(Opcodes.AASTORE);
    }
    if (preCondition)
        this.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(DroolsProvider.class),
                "runPreCondition",
                "(Ljava/lang/Class;Lorg/boretti/drools/integration/drools5/DroolsServiceType;Ljava/lang/String;Ljava/lang/Class;Ljava/lang/Object;[Ljava/lang/Object;)V");
    else
        this.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(DroolsProvider.class),
                "runPostCondition",
                "(Ljava/lang/Class;Lorg/boretti/drools/integration/drools5/DroolsServiceType;Ljava/lang/String;Ljava/lang/Class;Ljava/lang/Object;[Ljava/lang/Object;)V");
}

From source file:org.brutusin.instrumentation.utils.TreeInstructions.java

License:Apache License

public static InsnList getClassReferenceInstruction(Type type, int majorVersion) {

    InsnList list = new InsnList();
    char charType = type.getDescriptor().charAt(0);
    String wrapper;// www . j  av a2s .  c  o m
    switch (charType) {
    case 'B':
        wrapper = "java/lang/Byte";
        break;
    case 'C':
        wrapper = "java/lang/Character";
        break;
    case 'D':
        wrapper = "java/lang/Double";
        break;
    case 'F':
        wrapper = "java/lang/Float";
        break;
    case 'I':
        wrapper = "java/lang/Integer";
        break;
    case 'J':
        wrapper = "java/lang/Long";
        break;
    case 'L':
    case '[':
        return getClassConstantReference(type, majorVersion);
    case 'Z':
        wrapper = "java/lang/Boolean";
        break;
    case 'S':
        wrapper = "java/lang/Short";
        break;
    default:
        throw new ClassFormatError("Invalid method signature: " + type.getDescriptor());
    }

    list.add(new FieldInsnNode(Opcodes.GETSTATIC, wrapper, "TYPE", "Ljava/lang/Class;"));
    return list;

}

From source file:org.cacheonix.impl.transformer.CacheonixMethodGenerator.java

License:LGPL

/**
 * Generated the byte instructions to write the message to System out stream
 *
 * @param mv      Method Visitor that writes byte instructions
 * @param message message that needs to be written to the out stream
 *//*ww  w. j a  va2s  .co  m*/
public static void printingToSysout(final MethodVisitor mv, final String message) {

    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    mv.visitLdcInsn(message);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
}

From source file:org.coldswap.asm.field.PrivateStaticFieldReplacer.java

License:Open Source License

/**
 * Replaces any GETSTATIC/PUTSTATIC call of the field in the old class with the field
 * introduced in the new class./*  ww w.j  a va  2  s  .  com*/
 *
 * @param classNode containing the old class.
 * @param fieldNode containing the old field.
 */
@SuppressWarnings("unchecked")
private void replaceReferences(ClassNode classNode, FieldNode fieldNode) {
    List<MethodNode> methodNodes = classNode.methods;
    String contClass = classNode.name.substring(classNode.name.lastIndexOf("/") + 1);
    final String className = classPackage
            + TransformerNameGenerator.getPrivateStaticFieldClassName(contClass, fieldNode.name);
    for (MethodNode method : methodNodes) {
        InsnList inst = method.instructions;
        Iterator iter = inst.iterator();
        while (iter.hasNext()) {
            AbstractInsnNode absIns = (AbstractInsnNode) iter.next();
            int opcode = absIns.getOpcode();
            // check if instruction is GETSTATIC or PUTSTATIC
            if (opcode == Opcodes.GETSTATIC) {
                // get type
                if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                    final Boolean[] foundField = { false };
                    final ClassNode cNode = classNode;
                    final FieldNode fNode = fieldNode;
                    absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitFieldInsn(int i, String s, String s2, String s3) {
                            if (cNode.name.equals(s) && fNode.name.equals(s2)) {
                                foundField[0] = true;
                            }
                            super.visitFieldInsn(i, s, s2, s3);
                        }
                    });
                    if (foundField[0]) {
                        inst.set(absIns, new FieldInsnNode(Opcodes.GETSTATIC, className, fieldNode.name,
                                fieldNode.desc));
                    }
                }
            } else if (opcode == Opcodes.PUTSTATIC) {
                if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                    final Boolean[] foundField = { false };
                    final ClassNode cNode = classNode;
                    final FieldNode fNode = fieldNode;
                    absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitFieldInsn(int i, String s, String s2, String s3) {
                            if (cNode.name.equals(s) && fNode.name.equals(s2)) {
                                foundField[0] = true;
                            }
                            super.visitFieldInsn(i, s, s2, s3);
                        }
                    });
                    if (foundField[0]) {
                        inst.set(absIns, new FieldInsnNode(Opcodes.PUTSTATIC, className, fieldNode.name,
                                fieldNode.desc));
                    }
                }
            }
        }
    }
}

From source file:org.coldswap.asm.field.ProtectedStaticFieldReferenceReplacer.java

License:Open Source License

@SuppressWarnings("uncheked")
@Override/*  w ww.  j av  a2 s .  co  m*/
public int findAndReplace(ClassNode classNode) {
    int counter = 0;
    if (classNode.superName.equals(supperClass)) {
        List<MethodNode> methodNodes = classNode.methods;
        for (MethodNode method : methodNodes) {
            InsnList inst = method.instructions;
            Iterator iter = inst.iterator();
            while (iter.hasNext()) {
                AbstractInsnNode absIns = (AbstractInsnNode) iter.next();
                int opcode = absIns.getOpcode();
                // check if instruction is GETSTATIC or PUTSTATIC
                if (opcode == Opcodes.GETSTATIC) {
                    // get type
                    if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                        final Boolean[] foundField = { false };
                        absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                            @Override
                            public void visitFieldInsn(int i, String s, String s2, String s3) {
                                if (oldClass.equals(s) && fieldToReplace.name.equals(s2)) {
                                    foundField[0] = true;
                                }
                                super.visitFieldInsn(i, s, s2, s3);
                            }
                        });
                        if (foundField[0]) {
                            inst.set(absIns, new FieldInsnNode(Opcodes.GETSTATIC, newClass, fieldToReplace.name,
                                    fieldToReplace.desc));
                            counter++;
                        }
                    }
                } else if (opcode == Opcodes.PUTSTATIC) {
                    if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                        final Boolean[] foundField = { false };
                        absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                            @Override
                            public void visitFieldInsn(int i, String s, String s2, String s3) {
                                if (oldClass.equals(s) && fieldToReplace.name.equals(s2)) {
                                    foundField[0] = true;
                                }
                                super.visitFieldInsn(i, s, s2, s3);
                            }
                        });
                        if (foundField[0]) {
                            inst.set(absIns, new FieldInsnNode(Opcodes.PUTSTATIC, newClass, fieldToReplace.name,
                                    fieldToReplace.desc));
                            counter++;
                        }
                    }
                }
            }
        }
    }
    return counter;
}

From source file:org.coldswap.asm.field.ProtectedStaticFieldReplacer.java

License:Open Source License

/**
 * Replaces any GETSTATIC/PUTSTATIC call of the field in the old class with the field
 * introduced in the new class./*w ww. j  a  v  a  2 s  .  c o  m*/
 *
 * @param classNode containing the old class.
 * @param fieldNode containing the old field.
 */
private void replaceReferences(ClassNode classNode, FieldNode fieldNode) {
    List<MethodNode> methodNodes = classNode.methods;
    String contClass = classNode.name.substring(classNode.name.lastIndexOf("/") + 1);
    final String className = classPackage
            + TransformerNameGenerator.getProtectedStaticFieldClassName(contClass, fieldNode.name);
    for (MethodNode method : methodNodes) {
        InsnList inst = method.instructions;
        Iterator iter = inst.iterator();
        while (iter.hasNext()) {
            AbstractInsnNode absIns = (AbstractInsnNode) iter.next();
            int opcode = absIns.getOpcode();
            // check if instruction is GETSTATIC or PUTSTATIC
            if (opcode == Opcodes.GETSTATIC) {
                // get type
                if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                    final Boolean[] foundField = { false };
                    final ClassNode cNode = classNode;
                    final FieldNode fNode = fieldNode;
                    absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitFieldInsn(int i, String s, String s2, String s3) {
                            if (cNode.name.equals(s) && fNode.name.equals(s2)) {
                                foundField[0] = true;
                            }
                            super.visitFieldInsn(i, s, s2, s3);
                        }
                    });
                    if (foundField[0]) {
                        inst.set(absIns, new FieldInsnNode(Opcodes.GETSTATIC, className, fieldNode.name,
                                fieldNode.desc));
                    }
                }
            } else if (opcode == Opcodes.PUTSTATIC) {
                if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                    final Boolean[] foundField = { false };
                    final ClassNode cNode = classNode;
                    final FieldNode fNode = fieldNode;
                    absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitFieldInsn(int i, String s, String s2, String s3) {
                            if (cNode.name.equals(s) && fNode.name.equals(s2)) {
                                foundField[0] = true;
                            }
                            super.visitFieldInsn(i, s, s2, s3);
                        }
                    });
                    if (foundField[0]) {
                        inst.set(absIns, new FieldInsnNode(Opcodes.PUTSTATIC, className, fieldNode.name,
                                fieldNode.desc));
                    }
                }
            }
        }
    }
}

From source file:org.coldswap.asm.field.PublicStaticFieldReferenceReplacer.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from  w  w  w.  j  a  v a 2  s.  c  o  m*/
public int findAndReplace(ClassNode classNode) {
    int counter = 0;
    List<MethodNode> methodNodes = classNode.methods;
    for (MethodNode method : methodNodes) {
        InsnList inst = method.instructions;
        Iterator iter = inst.iterator();
        while (iter.hasNext()) {
            AbstractInsnNode absIns = (AbstractInsnNode) iter.next();
            int opcode = absIns.getOpcode();
            // check if instruction is GETSTATIC or PUTSTATIC
            if (opcode == Opcodes.GETSTATIC) {
                // get type
                if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                    final Boolean[] foundField = { false };
                    absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitFieldInsn(int i, String s, String s2, String s3) {
                            if (oldClass.equals(s) && fieldToReplace.name.equals(s2)) {
                                foundField[0] = true;
                            }
                            super.visitFieldInsn(i, s, s2, s3);
                        }
                    });
                    if (foundField[0]) {
                        inst.set(absIns, new FieldInsnNode(Opcodes.GETSTATIC, newClass, fieldToReplace.name,
                                fieldToReplace.desc));
                        counter++;
                    }
                }
            } else if (opcode == Opcodes.PUTSTATIC) {
                if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                    final Boolean[] foundField = { false };
                    absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitFieldInsn(int i, String s, String s2, String s3) {
                            if (oldClass.equals(s) && fieldToReplace.name.equals(s2)) {
                                foundField[0] = true;
                            }
                            super.visitFieldInsn(i, s, s2, s3);
                        }
                    });
                    if (foundField[0]) {
                        inst.set(absIns, new FieldInsnNode(Opcodes.PUTSTATIC, newClass, fieldToReplace.name,
                                fieldToReplace.desc));
                        counter++;
                    }
                }
            }
        }
    }
    return counter;
}