Example usage for org.objectweb.asm Label Label

List of usage examples for org.objectweb.asm Label Label

Introduction

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

Prototype

public Label() 

Source Link

Document

Constructs a new label.

Usage

From source file:com.github.javalbert.reflection.ClassAccessFactory.java

License:Apache License

private static Label[] newLabelArray(int size) {
    Label[] labels = new Label[size];
    for (int i = 0; i < size; i++) {
        labels[i] = new Label();
    }/*from   w  ww . j  ava  2s  . c o m*/
    return labels;
}

From source file:com.github.javalbert.reflection.ClassAccessFactory.java

License:Apache License

private void visitAccessGetter(List<? extends AssignableInfo> memberInfoList, AccessInfo accessInfo) {
    mv = cw.visitMethod(ACC_PUBLIC, accessInfo.getMethodName,
            "(" + classTypeDescriptor + "I)" + accessInfo.descriptor, null, null);
    mv.visitCode();//from  w w w . j  av a  2  s  .  c om
    Label firstLabel = new Label();
    mv.visitLabel(firstLabel);
    mv.visitVarInsn(ILOAD, 2);

    Label defaultCaseLabel = new Label();

    if (memberInfoList == null || memberInfoList.isEmpty()) {
        mv.visitInsn(POP);
        mv.visitLabel(defaultCaseLabel);
        visitAccessGetterLastPart(accessInfo.memberType, firstLabel);
        return;
    }

    boolean useTableSwitch = useTableSwitch(memberInfoList);
    Label[] labels = useTableSwitch ? getTableSwitchLabelsForAccess(defaultCaseLabel, memberInfoList)
            : newLabelArray(memberInfoList.size());

    if (useTableSwitch) {
        mv.visitTableSwitchInsn(memberInfoList.get(0).memberIndex,
                memberInfoList.get(memberInfoList.size() - 1).memberIndex, defaultCaseLabel, labels);
    } else {
        mv.visitLookupSwitchInsn(defaultCaseLabel,
                memberInfoList.stream().mapToInt(f -> f.memberIndex).toArray(), labels);
    }

    for (int i = 0; i < memberInfoList.size(); i++) {
        MemberInfo memberInfo = memberInfoList.get(i);

        mv.visitLabel(labels[i]);
        mv.visitFrame(F_SAME, 0, null, 0, null);
        mv.visitVarInsn(ALOAD, 1);

        switch (accessInfo.memberType) {
        case MEMBER_TYPE_FIELD:
            mv.visitFieldInsn(((FieldInfo) memberInfo).getFieldOpcode, internalName, memberInfo.name,
                    accessInfo.descriptor);
            break;
        case MEMBER_TYPE_PROPERTY:
            mv.visitMethodInsn(INVOKEVIRTUAL, internalName, ((PropertyInfo) memberInfo).readMethodName,
                    "()" + accessInfo.descriptor, false);
            break;
        }

        mv.visitInsn(accessInfo.returnOpcode);
    }

    mv.visitLabel(defaultCaseLabel);
    mv.visitFrame(F_SAME, 0, null, 0, null);

    visitAccessGetterLastPart(accessInfo.memberType, firstLabel);
}

From source file:com.github.javalbert.reflection.ClassAccessFactory.java

License:Apache License

private void visitAccessGetterBridge(String methodName, String returnTypeDescriptor, int returnOpcode) {
    mv = cw.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, methodName,
            "(Ljava/lang/Object;I)" + returnTypeDescriptor, null, null);
    mv.visitCode();//from w w  w  .ja  v a 2 s  . c o m
    mv.visitLabel(new Label());
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, internalName);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, classAccessInternalName, methodName,
            "(" + classTypeDescriptor + "I)" + returnTypeDescriptor, false);
    mv.visitInsn(returnOpcode);
    mv.visitMaxs(3, 3);
    mv.visitEnd();
}

From source file:com.github.javalbert.reflection.ClassAccessFactory.java

License:Apache License

private void visitAccessGetterLastPart(String memberType, Label firstLabel) {
    mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
    mv.visitInsn(DUP);/*from w w w.  jav a2s  .c o m*/
    mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
    mv.visitInsn(DUP);
    mv.visitLdcInsn("No " + memberType + " with index: ");
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;",
            false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V",
            false);
    mv.visitInsn(ATHROW);
    Label lastLabel = new Label();
    mv.visitLabel(lastLabel);
    mv.visitLocalVariable("this", classAccessTypeDescriptor, null, firstLabel, lastLabel, 0);
    mv.visitLocalVariable("obj", classTypeDescriptor, null, firstLabel, lastLabel, 1);
    mv.visitLocalVariable(memberType + "Index", "I", null, firstLabel, lastLabel, 2);
    mv.visitMaxs(5, 3);
    mv.visitEnd();
}

From source file:com.github.javalbert.reflection.ClassAccessFactory.java

License:Apache License

private void visitAccessSetter(List<? extends AssignableInfo> memberInfoList, AccessInfo accessInfo) {
    mv = cw.visitMethod(ACC_PUBLIC, accessInfo.setMethodName,
            "(" + classTypeDescriptor + "I" + accessInfo.descriptor + ")V", null, null);
    mv.visitCode();/*  ww w.  ja v a  2  s  . co m*/
    Label firstLabel = new Label();
    mv.visitLabel(firstLabel);
    mv.visitVarInsn(ILOAD, 2);

    Label defaultCaseLabel = new Label();

    if (memberInfoList == null || memberInfoList.isEmpty()) {
        mv.visitInsn(POP);
        mv.visitLabel(defaultCaseLabel);
        visitAccessSetterLastPart(accessInfo.memberType, accessInfo.descriptor, firstLabel, null);
        return;
    }

    boolean useTableSwitch = useTableSwitch(memberInfoList);
    Label[] labels = useTableSwitch ? getTableSwitchLabelsForAccess(defaultCaseLabel, memberInfoList)
            : newLabelArray(memberInfoList.size());

    if (useTableSwitch) {
        mv.visitTableSwitchInsn(memberInfoList.get(0).memberIndex,
                memberInfoList.get(memberInfoList.size() - 1).memberIndex, defaultCaseLabel, labels);
    } else {
        mv.visitLookupSwitchInsn(defaultCaseLabel,
                memberInfoList.stream().mapToInt(f -> f.memberIndex).toArray(), labels);
    }

    Label breakLabel = new Label();

    for (int i = 0; i < memberInfoList.size(); i++) {
        MemberInfo memberInfo = memberInfoList.get(i);

        mv.visitLabel(labels[i]);
        mv.visitFrame(F_SAME, 0, null, 0, null);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(accessInfo.loadOpcode, 3);

        switch (accessInfo.memberType) {
        case MEMBER_TYPE_FIELD:
            mv.visitFieldInsn(((FieldInfo) memberInfo).setFieldOpcode, internalName, memberInfo.name,
                    accessInfo.descriptor);
            break;
        case MEMBER_TYPE_PROPERTY:
            mv.visitMethodInsn(INVOKEVIRTUAL, internalName, ((PropertyInfo) memberInfo).writeMethodName,
                    "(" + accessInfo.descriptor + ")V", false);
            break;
        }

        mv.visitLabel(new Label());
        mv.visitJumpInsn(GOTO, breakLabel);
    }

    mv.visitLabel(defaultCaseLabel);
    mv.visitFrame(F_SAME, 0, null, 0, null);

    visitAccessSetterLastPart(accessInfo.memberType, accessInfo.descriptor, firstLabel, breakLabel);
}

From source file:com.github.javalbert.reflection.ClassAccessFactory.java

License:Apache License

private void visitAccessSetterBridge(String methodName, int loadOpcode, String descriptor) {
    mv = cw.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, methodName,
            "(Ljava/lang/Object;I" + descriptor + ")V", null, null);
    mv.visitCode();//w  w w .j av  a2s. c o m
    mv.visitLabel(new Label());
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, internalName);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitVarInsn(loadOpcode, 3);
    mv.visitMethodInsn(INVOKEVIRTUAL, classAccessInternalName, methodName,
            "(" + classTypeDescriptor + "I" + descriptor + ")V", false);
    mv.visitInsn(RETURN);
    if (isDescriptorDoubleOrLong(descriptor)) {
        mv.visitMaxs(5, 5);
    } else {
        mv.visitMaxs(4, 4);
    }
    mv.visitEnd();
}

From source file:com.github.javalbert.reflection.ClassAccessFactory.java

License:Apache License

private void visitAccessSetterLastPart(String memberType, String fieldDescriptor, Label firstLabel,
        Label breakLabel) {/*from w  w  w .  j  a  v  a2s . com*/
    mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
    mv.visitInsn(DUP);
    mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
    mv.visitInsn(DUP);
    mv.visitLdcInsn("No " + memberType + " with index: ");
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;",
            false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V",
            false);
    mv.visitInsn(ATHROW);

    if (breakLabel != null) {
        mv.visitLabel(breakLabel);
        mv.visitFrame(F_SAME, 0, null, 0, null);
        mv.visitInsn(RETURN);
    }

    Label lastLabel = new Label();
    mv.visitLabel(lastLabel);
    mv.visitLocalVariable("this", classAccessTypeDescriptor, null, firstLabel, lastLabel, 0);
    mv.visitLocalVariable("obj", classTypeDescriptor, null, firstLabel, lastLabel, 1);
    mv.visitLocalVariable(memberType + "Index", "I", null, firstLabel, lastLabel, 2);
    mv.visitLocalVariable("x", fieldDescriptor, null, firstLabel, lastLabel, 3);
    mv.visitMaxs(5, isDescriptorDoubleOrLong(fieldDescriptor) ? 5 : 4);
    mv.visitEnd();
}

From source file:com.github.javalbert.reflection.ClassAccessFactory.java

License:Apache License

private void visitGeneralAccessGetter(String methodName, String memberType,
        List<? extends AssignableInfo> memberInfoList) {
    mv = cw.visitMethod(ACC_PUBLIC, methodName, "(" + classTypeDescriptor + "I)Ljava/lang/Object;", null, null);
    mv.visitCode();/* w w w .ja v a  2s .c  om*/
    Label firstLabel = new Label();
    mv.visitLabel(firstLabel);
    mv.visitVarInsn(ILOAD, 2);

    Label defaultCaseLabel = new Label();

    if (memberInfoList == null || memberInfoList.isEmpty()) {
        mv.visitInsn(POP);
        mv.visitLabel(defaultCaseLabel);
        visitAccessGetterLastPart(memberType, firstLabel);
        return;
    }

    Label[] labels = getTableSwitchLabelsForAccess(defaultCaseLabel, memberInfoList);

    // Always use a table switch because there are no gaps between member indices
    mv.visitTableSwitchInsn(memberInfoList.get(0).memberIndex,
            memberInfoList.get(memberInfoList.size() - 1).memberIndex, defaultCaseLabel, labels);

    for (int i = 0; i < memberInfoList.size(); i++) {
        AssignableInfo member = memberInfoList.get(i);

        mv.visitLabel(labels[i]);
        mv.visitFrame(F_SAME, 0, null, 0, null);
        mv.visitVarInsn(ALOAD, 1);

        switch (memberType) {
        case MEMBER_TYPE_FIELD:
            mv.visitFieldInsn(((FieldInfo) member).getFieldOpcode, internalName, member.name,
                    member.descriptor);
            break;
        case MEMBER_TYPE_PROPERTY:
            mv.visitMethodInsn(INVOKEVIRTUAL, internalName, ((PropertyInfo) member).readMethodName,
                    "()" + member.descriptor, false);
            break;
        }

        if (member.type.isPrimitive()) {
            Class<?> wrapperType = ClassUtils.primitiveToWrapper(member.type);
            mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(wrapperType), "valueOf",
                    "(" + member.descriptor + ")" + Type.getDescriptor(wrapperType), false);
        }

        mv.visitInsn(ARETURN);
    }

    mv.visitLabel(defaultCaseLabel);
    mv.visitFrame(F_SAME, 0, null, 0, null);

    visitAccessGetterLastPart(memberType, firstLabel);
}

From source file:com.github.javalbert.reflection.ClassAccessFactory.java

License:Apache License

private void visitGeneralAccessSetter(String methodName, String memberType,
        List<? extends AssignableInfo> memberInfoList) {
    mv = cw.visitMethod(ACC_PUBLIC, methodName, "(" + classTypeDescriptor + "ILjava/lang/Object;)V", null,
            null);/*  www  .j a v  a 2 s .co  m*/
    mv.visitCode();
    Label firstLabel = new Label();
    mv.visitLabel(firstLabel);
    mv.visitVarInsn(ILOAD, 2);

    Label defaultCaseLabel = new Label();

    if (memberInfoList == null || memberInfoList.isEmpty()) {
        mv.visitInsn(POP);
        mv.visitLabel(defaultCaseLabel);
        visitAccessSetterLastPart(memberType, "Ljava/lang/Object;", firstLabel, null);
        return;
    }

    Label[] labels = getTableSwitchLabelsForAccess(defaultCaseLabel, memberInfoList);

    // Always use a table switch because there are no gaps between member indices
    mv.visitTableSwitchInsn(memberInfoList.get(0).memberIndex,
            memberInfoList.get(memberInfoList.size() - 1).memberIndex, defaultCaseLabel, labels);

    Label breakLabel = new Label();

    for (int i = 0; i < memberInfoList.size(); i++) {
        AssignableInfo member = memberInfoList.get(i);

        mv.visitLabel(labels[i]);
        mv.visitFrame(F_SAME, 0, null, 0, null);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 3);

        checkCast(member);

        switch (memberType) {
        case MEMBER_TYPE_FIELD:
            mv.visitFieldInsn(((FieldInfo) member).setFieldOpcode, internalName, member.name,
                    member.descriptor);
            break;
        case MEMBER_TYPE_PROPERTY:
            mv.visitMethodInsn(INVOKEVIRTUAL, internalName, ((PropertyInfo) member).writeMethodName,
                    "(" + member.descriptor + ")V", false);
            break;
        }

        mv.visitLabel(new Label());
        mv.visitJumpInsn(GOTO, breakLabel);
    }

    mv.visitLabel(defaultCaseLabel);
    mv.visitFrame(F_SAME, 0, null, 0, null);

    visitAccessSetterLastPart(memberType, "Ljava/lang/Object;", firstLabel, breakLabel);
}

From source file:com.github.javalbert.reflection.ClassAccessFactory.java

License:Apache License

private void visitIndexMethod(String categoryOfStringCase,
        List<StringCaseReturnIndex> stringCaseReturnIndices) {
    mv = cw.visitMethod(ACC_PUBLIC, categoryOfStringCase + "Index", "(Ljava/lang/String;)I", null, null);
    mv.visitCode();//from  ww w. j  a v  a2s.co  m
    final Label firstLabel = new Label();
    mv.visitLabel(firstLabel);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(DUP);
    mv.visitVarInsn(ASTORE, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false);

    final Label defaultCaseLabel = new Label();

    if (stringCaseReturnIndices.isEmpty()) {
        mv.visitInsn(POP);
        mv.visitLabel(defaultCaseLabel);
        visitIndexMethodLastPart(categoryOfStringCase, firstLabel);
        return;
    }

    Collections.sort(stringCaseReturnIndices, StringCaseReturnIndex::compareHashCode);

    int[] stringHashCodes = new int[stringCaseReturnIndices.size()];
    Label[] caseLabels = new Label[stringCaseReturnIndices.size()];
    for (int i = 0; i < stringCaseReturnIndices.size(); i++) {
        stringHashCodes[i] = stringCaseReturnIndices.get(i).hashCode;
        caseLabels[i] = new Label();
    }

    mv.visitLookupSwitchInsn(defaultCaseLabel, stringHashCodes, caseLabels);

    for (int i = 0; i < stringCaseReturnIndices.size(); i++) {
        StringCaseReturnIndex stringCaseReturnIndex = stringCaseReturnIndices.get(i);

        mv.visitLabel(caseLabels[i]);

        if (i > 0) {
            mv.visitFrame(F_SAME, 0, null, 0, null);
        } else {
            mv.visitFrame(F_APPEND, 1, new Object[] { "java/lang/String" }, 0, null);
        }

        mv.visitVarInsn(ALOAD, 2);
        mv.visitLdcInsn(stringCaseReturnIndex.name);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
        mv.visitJumpInsn(IFNE, stringCaseReturnIndex.returnIndexLabel);
        mv.visitJumpInsn(GOTO, defaultCaseLabel);
    }

    Collections.sort(stringCaseReturnIndices, StringCaseReturnIndex::compareIndex);
    for (int i = 0; i < stringCaseReturnIndices.size(); i++) {
        StringCaseReturnIndex stringCaseReturnIndex = stringCaseReturnIndices.get(i);

        mv.visitLabel(stringCaseReturnIndex.returnIndexLabel);
        mv.visitFrame(F_SAME, 0, null, 0, null);
        AsmUtils.visitZeroOperandInt(mv, stringCaseReturnIndex.index);
        mv.visitInsn(IRETURN);
    }

    mv.visitLabel(defaultCaseLabel);
    mv.visitFrame(F_SAME, 0, null, 0, null);

    visitIndexMethodLastPart(categoryOfStringCase, firstLabel);
}