Java Code Examples for jdk.internal.org.objectweb.asm.Opcodes

The following examples show how to use jdk.internal.org.objectweb.asm.Opcodes. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: nashorn   Source File: LookupSwitchInsnNode.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs a new {@link LookupSwitchInsnNode}.
 *
 * @param dflt beginning of the default handler block.
 * @param keys the values of the keys.
 * @param labels beginnings of the handler blocks. <tt>labels[i]</tt> is
 *        the beginning of the handler block for the <tt>keys[i]</tt> key.
 */
public LookupSwitchInsnNode(
    final LabelNode dflt,
    final int[] keys,
    final LabelNode[] labels)
{
    super(Opcodes.LOOKUPSWITCH);
    this.dflt = dflt;
    this.keys = new ArrayList<Integer>(keys == null ? 0 : keys.length);
    this.labels = new ArrayList<LabelNode>(labels == null ? 0 : labels.length);
    if (keys != null) {
        for (int i = 0; i < keys.length; ++i) {
            this.keys.add(new Integer(keys[i]));
        }
    }
    if (labels != null) {
        this.labels.addAll(Arrays.asList(labels));
    }
}
 
Example 2
@Override
public void visit(final int version, final int access, final String name,
        final String signature, final String superName,
        final String[] interfaces) {
    computeSVUID = (access & Opcodes.ACC_INTERFACE) == 0;

    if (computeSVUID) {
        this.name = name;
        this.access = access;
        this.interfaces = new String[interfaces.length];
        System.arraycopy(interfaces, 0, this.interfaces, 0,
                interfaces.length);
    }

    super.visit(version, access, name, signature, superName, interfaces);
}
 
Example 3
Source Project: openjdk-jdk9   Source File: DefineAnon.java    License: GNU General Public License v2.0 6 votes vote down vote up
static Class<?> getAnonClass(Class<?> hostClass, final String className) {
    final String superName = "java/lang/Object";
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
    cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER, className, null, superName, null);

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC, "test", "()V", null, null);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test0", "()V", false);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test1", "()V", false);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test2", "()V", false);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test3", "()V", false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

    final byte[] classBytes = cw.toByteArray();
    Class<?> invokerClass = UNSAFE.defineAnonymousClass(hostClass, classBytes, new Object[0]);
    UNSAFE.ensureClassInitialized(invokerClass);
    return invokerClass;
}
 
Example 4
Source Project: dragonwell8_jdk   Source File: Test.java    License: GNU General Public License v2.0 6 votes vote down vote up
public ClassVisitor make(ClassVisitor cv) {
    return new ClassVisitor(Opcodes.ASM5, cv) {

        @Override
        public void visitInnerClass(String name, String outerName,
                String innerName, int access) {
            if (whenVisitInner) {
                int access_ = (ACC_PROTECTED | access) & ~(ACC_PRIVATE | ACC_PUBLIC);
                System.out.println("visitInnerClass: name = " + name
                        + ", outerName = " + outerName
                        + ", innerName = " + innerName
                        + ", access original = 0x" + Integer.toHexString(access)
                        + ", access modified to 0x" + Integer.toHexString(access_));
                access = access_;
            }
            super.visitInnerClass(name, outerName, innerName, access);
        }
    };
}
 
Example 5
@Override
public void visitInnerClass(final String name, final String outerName,
        final String innerName, final int access) {
    checkState();
    CheckMethodAdapter.checkInternalName(name, "class name");
    if (outerName != null) {
        CheckMethodAdapter.checkInternalName(outerName, "outer class name");
    }
    if (innerName != null) {
        int start = 0;
        while (start < innerName.length()
                && Character.isDigit(innerName.charAt(start))) {
            start++;
        }
        if (start == 0 || start < innerName.length()) {
            CheckMethodAdapter.checkIdentifier(innerName, start, -1,
                    "inner class name");
        }
    }
    checkAccess(access, Opcodes.ACC_PUBLIC + Opcodes.ACC_PRIVATE
            + Opcodes.ACC_PROTECTED + Opcodes.ACC_STATIC
            + Opcodes.ACC_FINAL + Opcodes.ACC_INTERFACE
            + Opcodes.ACC_ABSTRACT + Opcodes.ACC_SYNTHETIC
            + Opcodes.ACC_ANNOTATION + Opcodes.ACC_ENUM);
    super.visitInnerClass(name, outerName, innerName, access);
}
 
Example 6
@Override
public void visitParameter(String name, int access) {
    if (name != null) {
        checkUnqualifiedName(version, name, "name");
    }
    CheckClassAdapter.checkAccess(access, Opcodes.ACC_FINAL
            + Opcodes.ACC_MANDATED + Opcodes.ACC_SYNTHETIC);
    super.visitParameter(name, access);
}
 
Example 7
Source Project: openjdk-jdk9   Source File: AnnotationsTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Copy the module-info.class for the given module, add the
 * Deprecated annotation, and write the updated module-info.class
 * to a directory.
 */
static void deprecateModule(String name,
                            boolean forRemoval,
                            String since,
                            Path output) throws IOException {
    Module module = ModuleLayer.boot().findModule(name).orElse(null);
    assertNotNull(module, name + " not found");

    InputStream in = module.getResourceAsStream("module-info.class");
    assertNotNull(in, "No module-info.class for " + name);

    try (in) {
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
                                         + ClassWriter.COMPUTE_FRAMES);

        ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, cw) { };

        ClassReader cr = new ClassReader(in);

        List<Attribute> attrs = new ArrayList<>();
        attrs.add(new ClassFileAttributes.ModuleAttribute());
        attrs.add(new ClassFileAttributes.ModulePackagesAttribute());
        attrs.add(new ClassFileAttributes.ModuleTargetAttribute());
        cr.accept(cv, attrs.toArray(new Attribute[0]), 0);

        AnnotationVisitor annotationVisitor
            = cv.visitAnnotation("Ljava/lang/Deprecated;", true);
        annotationVisitor.visit("forRemoval", forRemoval);
        annotationVisitor.visit("since", since);
        annotationVisitor.visitEnd();

        byte[] bytes = cw.toByteArray();
        Path mi = output.resolve("module-info.class");
        Files.write(mi, bytes);
    }
}
 
Example 8
Source Project: jdk8u-jdk   Source File: AnalyzerAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visitTableSwitchInsn(final int min, final int max,
        final Label dflt, final Label... labels) {
    if (mv != null) {
        mv.visitTableSwitchInsn(min, max, dflt, labels);
    }
    execute(Opcodes.TABLESWITCH, 0, null);
    this.locals = null;
    this.stack = null;
}
 
Example 9
Source Project: Bytecoder   Source File: InvokerBytecodeGenerator.java    License: Apache License 2.0 5 votes vote down vote up
/** Generates code to check that actual receiver and LambdaForm matches */
private boolean checkActualReceiver() {
    // Expects MethodHandle on the stack and actual receiver MethodHandle in slot #0
    mv.visitInsn(Opcodes.DUP);
    mv.visitVarInsn(Opcodes.ALOAD, localsMap[0]);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, MHI, "assertSame", LLV_SIG, false);
    return true;
}
 
Example 10
@Override
public void visitMethodInsn(final int opcode, final String owner,
        final String name, final String desc, final boolean itf) {
    if (api < Opcodes.ASM5) {
        super.visitMethodInsn(opcode, owner, name, desc, itf);
        return;
    }
    doVisitMethodInsn(opcode, owner, name, desc, itf);
}
 
Example 11
/**
 * Generates the instruction to create a new array.
 *
 * @param type
 *            the type of the array elements.
 */
public void newArray(final Type type) {
    int typ;
    switch (type.getSort()) {
    case Type.BOOLEAN:
        typ = Opcodes.T_BOOLEAN;
        break;
    case Type.CHAR:
        typ = Opcodes.T_CHAR;
        break;
    case Type.BYTE:
        typ = Opcodes.T_BYTE;
        break;
    case Type.SHORT:
        typ = Opcodes.T_SHORT;
        break;
    case Type.INT:
        typ = Opcodes.T_INT;
        break;
    case Type.FLOAT:
        typ = Opcodes.T_FLOAT;
        break;
    case Type.LONG:
        typ = Opcodes.T_LONG;
        break;
    case Type.DOUBLE:
        typ = Opcodes.T_DOUBLE;
        break;
    default:
        typeInsn(Opcodes.ANEWARRAY, type);
        return;
    }
    mv.visitIntInsn(Opcodes.NEWARRAY, typ);
}
 
Example 12
Source Project: jdk8u-jdk   Source File: AnalyzerAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visitIincInsn(final int var, final int increment) {
    if (mv != null) {
        mv.visitIincInsn(var, increment);
    }
    execute(Opcodes.IINC, var, null);
}
 
Example 13
Source Project: jdk8u60   Source File: ASMifier.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visitMethodInsn(final int opcode, final String owner,
        final String name, final String desc, final boolean itf) {
    if (api < Opcodes.ASM5) {
        super.visitMethodInsn(opcode, owner, name, desc, itf);
        return;
    }
    doVisitMethodInsn(opcode, owner, name, desc, itf);
}
 
Example 14
@Override
public void visitJumpInsn(final int opcode, final Label label) {
    if (mv != null) {
        mv.visitJumpInsn(opcode, label);
    }
    execute(opcode, 0, null);
    if (opcode == Opcodes.GOTO) {
        this.locals = null;
        this.stack = null;
    }
}
 
Example 15
/**
 * Generates the instruction to push the given value on the stack.
 *
 * @param value
 *            the value to be pushed on the stack. May be <tt>null</tt>.
 */
public void push(final String value) {
    if (value == null) {
        mv.visitInsn(Opcodes.ACONST_NULL);
    } else {
        mv.visitLdcInsn(value);
    }
}
 
Example 16
Source Project: jdk8u-jdk   Source File: GeneratorAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates the instruction to load 'this' on the stack.
 */
public void loadThis() {
    if ((access & Opcodes.ACC_STATIC) != 0) {
        throw new IllegalStateException(
                "no 'this' pointer within static method");
    }
    mv.visitVarInsn(Opcodes.ALOAD, 0);
}
 
Example 17
private byte arrayTypeCode(Wrapper elementType) {
    switch (elementType) {
        case BOOLEAN: return Opcodes.T_BOOLEAN;
        case BYTE:    return Opcodes.T_BYTE;
        case CHAR:    return Opcodes.T_CHAR;
        case SHORT:   return Opcodes.T_SHORT;
        case INT:     return Opcodes.T_INT;
        case LONG:    return Opcodes.T_LONG;
        case FLOAT:   return Opcodes.T_FLOAT;
        case DOUBLE:  return Opcodes.T_DOUBLE;
        case OBJECT:  return 0; // in place of Opcodes.T_OBJECT
        default:      throw new InternalError();
    }
}
 
Example 18
@Override
public void visitMethodInsn(final int opcode, final String owner,
        final String name, final String desc, final boolean itf) {
    if (api < Opcodes.ASM5) {
        super.visitMethodInsn(opcode, owner, name, desc, itf);
        return;
    }
    doVisitMethodInsn(opcode, owner, name, desc, itf);
}
 
Example 19
@Override
public MethodVisitor visitMethod(final int access, final String name,
        final String desc, final String signature, final String[] exceptions) {
    if (computeSVUID) {
        if ("<clinit>".equals(name)) {
            hasStaticInitializer = true;
        }
        /*
         * Remembers non private constructors and methods for SVUID
         * computation For constructor and method modifiers, only the
         * ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL,
         * ACC_SYNCHRONIZED, ACC_NATIVE, ACC_ABSTRACT and ACC_STRICT flags
         * are used.
         */
        int mods = access
                & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PRIVATE
                        | Opcodes.ACC_PROTECTED | Opcodes.ACC_STATIC
                        | Opcodes.ACC_FINAL | Opcodes.ACC_SYNCHRONIZED
                        | Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT | Opcodes.ACC_STRICT);

        // all non private methods
        if ((access & Opcodes.ACC_PRIVATE) == 0) {
            if ("<init>".equals(name)) {
                svuidConstructors.add(new Item(name, mods, desc));
            } else if (!"<clinit>".equals(name)) {
                svuidMethods.add(new Item(name, mods, desc));
            }
        }
    }

    return super.visitMethod(access, name, desc, signature, exceptions);
}
 
Example 20
Source Project: hottub   Source File: AnalyzerAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void doVisitMethodInsn(int opcode, final String owner,
        final String name, final String desc, final boolean itf) {
    if (mv != null) {
        mv.visitMethodInsn(opcode, owner, name, desc, itf);
    }
    if (this.locals == null) {
        labels = null;
        return;
    }
    pop(desc);
    if (opcode != Opcodes.INVOKESTATIC) {
        Object t = pop();
        if (opcode == Opcodes.INVOKESPECIAL && name.charAt(0) == '<') {
            Object u;
            if (t == Opcodes.UNINITIALIZED_THIS) {
                u = this.owner;
            } else {
                u = uninitializedTypes.get(t);
            }
            for (int i = 0; i < locals.size(); ++i) {
                if (locals.get(i) == t) {
                    locals.set(i, u);
                }
            }
            for (int i = 0; i < stack.size(); ++i) {
                if (stack.get(i) == t) {
                    stack.set(i, u);
                }
            }
        }
    }
    pushDesc(desc);
    labels = null;
}
 
Example 21
Source Project: jdk8u60   Source File: NashornTextifier.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static final boolean noFallThru(final int opcode) {
    switch (opcode) {
    case Opcodes.GOTO:
    case Opcodes.ATHROW:
    case Opcodes.ARETURN:
    case Opcodes.IRETURN:
    case Opcodes.LRETURN:
    case Opcodes.FRETURN:
    case Opcodes.DRETURN:
        return true;
    default:
        return false;
    }
}
 
Example 22
protected void addSVUID(long svuid) {
    FieldVisitor fv = super.visitField(Opcodes.ACC_FINAL
            + Opcodes.ACC_STATIC, "serialVersionUID", "J", null, svuid);
    if (fv != null) {
        fv.visitEnd();
    }
}
 
Example 23
void checkLDCConstant(final Object cst) {
    if (cst instanceof Type) {
        int s = ((Type) cst).getSort();
        if (s != Type.OBJECT && s != Type.ARRAY && s != Type.METHOD) {
            throw new IllegalArgumentException("Illegal LDC constant value");
        }
        if (s != Type.METHOD && (version & 0xFFFF) < Opcodes.V1_5) {
            throw new IllegalArgumentException(
                    "ldc of a constant class requires at least version 1.5");
        }
        if (s == Type.METHOD && (version & 0xFFFF) < Opcodes.V1_7) {
            throw new IllegalArgumentException(
                    "ldc of a method type requires at least version 1.7");
        }
    } else if (cst instanceof Handle) {
        if ((version & 0xFFFF) < Opcodes.V1_7) {
            throw new IllegalArgumentException(
                    "ldc of a handle requires at least version 1.7");
        }
        int tag = ((Handle) cst).getTag();
        if (tag < Opcodes.H_GETFIELD || tag > Opcodes.H_INVOKEINTERFACE) {
            throw new IllegalArgumentException("invalid handle tag " + tag);
        }
    } else {
        checkConstant(cst);
    }
}
 
Example 24
public void dconst(final double cst) {
    long bits = Double.doubleToLongBits(cst);
    if (bits == 0L || bits == 0x3ff0000000000000L) { // +0.0d and 1.0d
        mv.visitInsn(Opcodes.DCONST_0 + (int) cst);
    } else {
        mv.visitLdcInsn(cst);
    }
}
 
Example 25
/**
 * Checks that the given string is a valid unqualified name.
 *
 * @param version
 *            the class version.
 * @param name
 *            the string to be checked.
 * @param msg
 *            a message to be used in case of error.
 */
static void checkUnqualifiedName(int version, final String name,
        final String msg) {
    if ((version & 0xFFFF) < Opcodes.V1_5) {
        checkIdentifier(name, msg);
    } else {
        for (int i = 0; i < name.length(); ++i) {
            if (".;[/".indexOf(name.charAt(i)) != -1) {
                throw new IllegalArgumentException("Invalid " + msg
                        + " (must be a valid unqualified name): " + name);
            }
        }
    }
}
 
Example 26
@Deprecated
@Override
public void visitMethodInsn(int opcode, String owner, String name,
        String desc) {
    if (api >= Opcodes.ASM5) {
        super.visitMethodInsn(opcode, owner, name, desc);
        return;
    }
    doVisitMethodInsn(opcode, owner, name, desc,
            opcode == Opcodes.INVOKEINTERFACE);
}
 
Example 27
Source Project: openjdk-jdk9   Source File: ByteCodeVisitor.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void visitLiteral(byte value) {
    double chance = PseudoRandom.random();
    if (chance < CONSTANT_INST_CHANCE && value > -2 && value < 6) {
        currentMV.visitInsn(Opcodes.ICONST_0 + value);
    } else {
        currentMV.visitIntInsn(Opcodes.BIPUSH, value);
    }
}
 
Example 28
@Deprecated
public void invokespecial(final String owner, final String name,
        final String desc) {
    if (api >= Opcodes.ASM5) {
        invokespecial(owner, name, desc, false);
        return;
    }
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, owner, name, desc, false);
}
 
Example 29
Source Project: Bytecoder   Source File: GeneratorAdapter.java    License: Apache License 2.0 5 votes vote down vote up
/**
  * Generates the instruction to push the given value on the stack.
  *
  * @param value the value to be pushed on the stack.
  */
public void push(final Type value) {
    if (value == null) {
        mv.visitInsn(Opcodes.ACONST_NULL);
    } else {
        switch (value.getSort()) {
            case Type.BOOLEAN:
                mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Boolean", "TYPE", CLASS_DESCRIPTOR);
                break;
            case Type.CHAR:
                mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Character", "TYPE", CLASS_DESCRIPTOR);
                break;
            case Type.BYTE:
                mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Byte", "TYPE", CLASS_DESCRIPTOR);
                break;
            case Type.SHORT:
                mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Short", "TYPE", CLASS_DESCRIPTOR);
                break;
            case Type.INT:
                mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Integer", "TYPE", CLASS_DESCRIPTOR);
                break;
            case Type.FLOAT:
                mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Float", "TYPE", CLASS_DESCRIPTOR);
                break;
            case Type.LONG:
                mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Long", "TYPE", CLASS_DESCRIPTOR);
                break;
            case Type.DOUBLE:
                mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Double", "TYPE", CLASS_DESCRIPTOR);
                break;
            default:
                mv.visitLdcInsn(value);
                break;
        }
    }
}
 
Example 30
ScriptClassInstrumentor(final ClassVisitor visitor, final ScriptClassInfo sci) {
    super(Opcodes.ASM4, visitor);
    if (sci == null) {
        throw new IllegalArgumentException("Null ScriptClassInfo, is the class annotated?");
    }
    this.scriptClassInfo = sci;
    this.memberCount = scriptClassInfo.getInstancePropertyCount();
}