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

The following examples show how to use jdk.internal.org.objectweb.asm.Opcodes. 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 File: CheckClassAdapter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@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 #2
Source File: LookupSwitchInsnNode.java    From nashorn with 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 #3
Source File: DefineAnon.java    From openjdk-jdk9 with 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 File: Test.java    From dragonwell8_jdk with 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
Source File: SerialVersionUIDAdder.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@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 #6
Source File: CheckMethodAdapter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@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 #7
Source File: SerialVersionUIDAdder.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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 #8
Source File: GeneratorAdapter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #9
Source File: CheckMethodAdapter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #10
Source File: CheckMethodAdapter.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@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 #11
Source File: AnnotationsTest.java    From openjdk-jdk9 with 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 #12
Source File: AnalyzerAdapter.java    From jdk8u-jdk with 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 #13
Source File: Textifier.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visitFrame(final int type, final int nLocal,
        final Object[] local, final int nStack, final Object[] stack) {
    buf.setLength(0);
    buf.append(ltab);
    buf.append("FRAME ");
    switch (type) {
    case Opcodes.F_NEW:
    case Opcodes.F_FULL:
        buf.append("FULL [");
        appendFrameTypes(nLocal, local);
        buf.append("] [");
        appendFrameTypes(nStack, stack);
        buf.append(']');
        break;
    case Opcodes.F_APPEND:
        buf.append("APPEND [");
        appendFrameTypes(nLocal, local);
        buf.append(']');
        break;
    case Opcodes.F_CHOP:
        buf.append("CHOP ").append(nLocal);
        break;
    case Opcodes.F_SAME:
        buf.append("SAME");
        break;
    case Opcodes.F_SAME1:
        buf.append("SAME1 ");
        appendFrameTypes(1, stack);
        break;
    }
    buf.append('\n');
    text.add(buf.toString());
}
 
Example #14
Source File: InvokerBytecodeGenerator.java    From Bytecoder with 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 #15
Source File: MethodNode.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Deprecated
@Override
public void visitMethodInsn(int opcode, String owner, String name,
        String desc) {
    if (api >= Opcodes.ASM5) {
        super.visitMethodInsn(opcode, owner, name, desc);
        return;
    }
    instructions.add(new MethodInsnNode(opcode, owner, name, desc));
}
 
Example #16
Source File: AnalyzerAdapter.java    From TencentKona-8 with 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 #17
Source File: ASMifier.java    From jdk8u60 with 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 #18
Source File: AnalyzerAdapter.java    From jdk8u-jdk with 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 #19
Source File: ByteCodeVisitor.java    From openjdk-jdk9 with 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 #20
Source File: GeneratorAdapter.java    From jdk8u-jdk with 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 #21
Source File: GeneratorAdapter.java    From TencentKona-8 with GNU General Public License v2.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. May be <tt>null</tt>.
 */
public void push(final String value) {
    if (value == null) {
        mv.visitInsn(Opcodes.ACONST_NULL);
    } else {
        mv.visitLdcInsn(value);
    }
}
 
Example #22
Source File: NashornTextifier.java    From jdk8u60 with 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 #23
Source File: Printer.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method instruction. See
 * {@link jdk.internal.org.objectweb.asm.MethodVisitor#visitMethodInsn}.
 */
@Deprecated
public void visitMethodInsn(final int opcode, final String owner,
        final String name, final String desc) {
    if (api >= Opcodes.ASM5) {
        boolean itf = opcode == Opcodes.INVOKEINTERFACE;
        visitMethodInsn(opcode, owner, name, desc, itf);
        return;
    }
    throw new RuntimeException("Must be overriden");
}
 
Example #24
Source File: InvokerBytecodeGenerator.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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 #25
Source File: AdviceAdapter.java    From openjdk-8-source with 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 #26
Source File: ScriptClassInstrumentor.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example #27
Source File: InstructionAdapter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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 #28
Source File: AnalyzerAdapter.java    From hottub with 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 #29
Source File: SerialVersionUIDAdder.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@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 #30
Source File: GeneratorAdapter.java    From Bytecoder with 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;
        }
    }
}