Example usage for org.objectweb.asm TypeReference METHOD_RETURN

List of usage examples for org.objectweb.asm TypeReference METHOD_RETURN

Introduction

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

Prototype

int METHOD_RETURN

To view the source code for org.objectweb.asm TypeReference METHOD_RETURN.

Click Source Link

Document

The sort of type references that target the return type of a method.

Usage

From source file:org.decojer.cavaj.readers.asm.ReadMethodVisitor.java

License:Open Source License

@Override
public AnnotationVisitor visitTypeAnnotation(final int typeRef, @Nullable final TypePath typePath,
        final String desc, final boolean visible) {
    final A a = this.annotationVisitor.init(desc, visible ? RetentionPolicy.RUNTIME : RetentionPolicy.CLASS);
    if (a == null) {
        log.warn(getM() + ": Cannot read annotation for descriptor '" + desc + "'!");
        return null;
    }/*from   www  . j  a  v  a  2  s .  co  m*/
    final TypeReference typeReference = new TypeReference(typeRef);
    switch (typeReference.getSort()) {
    case TypeReference.METHOD_FORMAL_PARAMETER: {
        final int formalParameterIndex = typeReference.getFormalParameterIndex();
        final T[] paramTs = getM().getParamTs();
        if (paramTs.length <= formalParameterIndex) {
            log.warn(getM() + ": Cannot apply type annotation '" + a
                    + "' to missing method parameter at index '" + formalParameterIndex + "'!");
            break;
        }
        final T paramT = paramTs[formalParameterIndex];
        assert paramT != null;
        paramTs[formalParameterIndex] = annotateT(paramT, a, typePath);
        break;
    }
    case TypeReference.METHOD_RECEIVER: {
        // for type annotations like: void test(@Annots This this, ...) for none-static methods
        // TODO receiver needs full signature, test-method DU#getQualifiedT(T) does't work,
        // because we would have to read outer classes first
        T receiverT = getM().getReceiverT();
        if (receiverT == null) {
            receiverT = getM().getT();
        }
        if (receiverT == null) {
            assert getM().isDynamic();
            log.warn(getM() + ": Cannot apply type annotation '" + a + "' to missing method receiver!");
            break;
        }
        getM().setReceiverT(annotateT(receiverT, a, typePath));
        break;
    }
    case TypeReference.METHOD_RETURN:
        getM().setReturnT(annotateT(getM().getReturnT(), a, typePath));
        break;
    case TypeReference.METHOD_TYPE_PARAMETER: {
        final int typeParameterIndex = typeReference.getTypeParameterIndex();
        final T[] typeParams = getM().getTypeParams();
        if (typeParams.length <= typeParameterIndex) {
            log.warn(getM() + ": Cannot apply type annotation '" + a
                    + "' to missing method type parameter at index '" + typeParameterIndex + "'!");
            break;
        }
        final T typeParam = typeParams[typeParameterIndex];
        assert typeParam != null;
        typeParams[typeParameterIndex] = annotateT(typeParam, a, typePath);
        break;
    }
    case TypeReference.METHOD_TYPE_PARAMETER_BOUND: {
        final int typeParameterIndex = typeReference.getTypeParameterIndex();
        final int typeParameterBoundIndex = typeReference.getTypeParameterBoundIndex();
        final T t = getM().getTypeParams()[typeParameterIndex];
        if (typeParameterBoundIndex == 0) {
            // 0: annotation targets extends type
            final T superT = t.getSuperT();
            if (superT == null) {
                log.warn(getM() + ": Cannot apply type annotation '" + a + "' to missing super type!");
                break;
            }
            t.setSuperT(annotateT(superT, a, typePath));
            break;
        }
        // 1-based interface index
        final T[] interfaceTs = t.getInterfaceTs();
        if (interfaceTs.length < typeParameterBoundIndex) {
            log.warn(getM() + ": Cannot apply type annotation '" + a + "' to missing interface type!");
            break;
        }
        final T interfaceT = interfaceTs[typeParameterBoundIndex - 1];
        assert interfaceT != null;
        interfaceTs[typeParameterBoundIndex - 1] = annotateT(interfaceT, a, typePath);
        break;
    }
    case TypeReference.THROWS: {
        final int exceptionIndex = typeReference.getExceptionIndex();
        final T[] throwsTs = getM().getThrowsTs();
        if (throwsTs.length < exceptionIndex) {
            log.warn(getM() + ": Cannot apply type annotation '" + a + "' to missing throws type!");
            break;
        }
        final T throwsT = throwsTs[exceptionIndex];
        assert throwsT != null;
        throwsTs[exceptionIndex] = annotateT(throwsT, a, typePath);
        break;
    }
    default:
        log.warn(
                getM() + ": Unknown type annotation ref sort '0x" + Integer.toHexString(typeReference.getSort())
                        + "' : " + typeRef + " : " + typePath + " : " + desc + " : " + visible);
    }
    return this.annotationVisitor;
}

From source file:org.eclipse.ocl.examples.codegen.asm5.ASM5JavaAnnotationReader.java

License:Open Source License

public @Nullable Boolean getIsNonNull(@NonNull Method method) {
    final String className = method.getDeclaringClass().getName();
    final String requiredDesc = getMethodKey(className, method.getName(), Type.getMethodDescriptor(method));
    Map<@NonNull Integer, @Nullable Boolean> typeref2state = desc2typerefValue2state.get(requiredDesc);
    Integer returnTypeReference = TypeReference.newTypeReference(TypeReference.METHOD_RETURN).getValue();
    if (typeref2state != null) {
        return typeref2state.get(returnTypeReference);
    }/* ww w  .j  a  v a2  s .com*/
    if (!readClasses.add(className)) {
        return null;
    }
    //      System.out.println("getIsNonNull: " + requiredDesc + " " + Integer.toHexString(returnTypeReference));
    InputStream classStream = null;
    try {
        final int flags = ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE;
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        String classFileName = className.replace('.', '/') + ".class";
        classStream = contextClassLoader.getResourceAsStream(classFileName);
        final ClassReader cr = new ClassReader(classStream) {

            @Override
            public void accept(ClassVisitor classVisitor, int flags) {
                super.accept(classVisitor, flags);
            }

            @Override
            public void accept(ClassVisitor classVisitor, Attribute[] attrs, int flags) {
                super.accept(classVisitor, attrs, flags);
            }

        };
        ClassVisitor cv = new ClassVisitor(Opcodes.ASM5) {
            @Override
            public void visit(int version, int access, String name, String signature, String superName,
                    String[] interfaces) {
            }

            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                return null;
            }

            @Override
            public void visitAttribute(Attribute attr) {
            }

            @Override
            public void visitEnd() {
            }

            @Override
            public FieldVisitor visitField(int access, String name, String desc, String signature,
                    Object value) {
                return null;
            }

            @Override
            public void visitInnerClass(String name, String outerName, String innerName, int access) {
            }

            @Override
            public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                    String[] exceptions) {
                final String methodDesc = getMethodKey(className, name, desc);// + " " + signature;
                //               System.out.println("  ClassVisitor.visitMethod: " + methodDesc);
                final HashMap<@NonNull Integer, @Nullable Boolean> typerefValue2state = new HashMap<@NonNull Integer, @Nullable Boolean>();
                desc2typerefValue2state.put(methodDesc, typerefValue2state);
                return new MethodVisitor(Opcodes.ASM5) {
                    @Override
                    public AnnotationVisitor visitAnnotation(String annotationDesc, boolean visible) {
                        return null;
                    }

                    @Override
                    public AnnotationVisitor visitAnnotationDefault() {
                        return null;
                    }

                    @Override
                    public void visitAttribute(Attribute attr) {
                    }

                    @Override
                    public void visitCode() {
                    }

                    @Override
                    public void visitEnd() {
                    }

                    @Override
                    public void visitFieldInsn(int opcode, String owner, String name, String desc) {
                    }

                    @Override
                    public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) {
                    }

                    @Override
                    public void visitIincInsn(int var, int increment) {
                    }

                    @Override
                    public void visitInsn(int opcode) {
                    }

                    @Override
                    public AnnotationVisitor visitInsnAnnotation(int typeRef, TypePath typePath, String desc,
                            boolean visible) {
                        return null;
                    }

                    @Override
                    public void visitIntInsn(int opcode, int operand) {
                    }

                    @Override
                    public void visitInvokeDynamicInsn(String name, String desc, Handle bsm,
                            Object... bsmArgs) {
                    }

                    @Override
                    public void visitJumpInsn(int opcode, Label label) {
                    }

                    @Override
                    public void visitLabel(Label label) {
                    }

                    @Override
                    public void visitLdcInsn(Object cst) {
                    }

                    @Override
                    public void visitLineNumber(int line, Label start) {
                    }

                    @Override
                    public void visitLocalVariable(String name, String desc, String signature, Label start,
                            Label end, int index) {
                    }

                    @Override
                    public AnnotationVisitor visitLocalVariableAnnotation(int typeRef, TypePath typePath,
                            Label[] start, Label[] end, int[] index, String desc, boolean visible) {
                        return null;
                    }

                    @Override
                    public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
                    }

                    @Override
                    public void visitMaxs(int maxStack, int maxLocals) {
                    }

                    @Override
                    public void visitMethodInsn(int opcode, String owner, String name, String desc) {
                    }

                    @Override
                    public void visitMethodInsn(int opcode, String owner, String name, String desc,
                            boolean itf) {
                    }

                    @Override
                    public void visitMultiANewArrayInsn(String desc, int dims) {
                    }

                    @Override
                    public void visitParameter(String name, int access) {
                    }

                    @Override
                    public AnnotationVisitor visitParameterAnnotation(int parameter, String desc,
                            boolean visible) {
                        return null;
                    }

                    @Override
                    public void visitTableSwitchInsn(int min, int max, Label dflt, Label... labels) {
                    }

                    @Override
                    public AnnotationVisitor visitTryCatchAnnotation(int typeRef, TypePath typePath,
                            String desc, boolean visible) {
                        return null;
                    }

                    @Override
                    public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
                    }

                    @Override
                    public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc,
                            boolean visible) {
                        //                     System.out.println("    MethodVisitor-TypeAnnotation:" + Integer.toHexString(typeRef) + " " + typePath + " " + desc + " " + visible);
                        //                     TypeReference typeReference = new TypeReference(typeRef);
                        //                     System.out.println("    : " + Integer.toHexString(typeReference.getValue()) + ":" + typeReference.getSort() + ":" + typeReference.getTypeParameterIndex());
                        if (desc.equals(nonNullDesc)) {
                            //                        System.out.println("    MethodVisitor-TypeAnnotation:" + Integer.toHexString(typeRef) + " " + typePath + " " + desc);
                            typerefValue2state.put(typeRef, true);
                        } else if (desc.equals(nullableDesc)) {
                            //                        System.out.println("    MethodVisitor-TypeAnnotation:" + Integer.toHexString(typeRef) + " " + typePath + " " + desc);
                            typerefValue2state.put(typeRef, false);
                        }
                        return null;
                    }

                    @Override
                    public void visitTypeInsn(int opcode, String type) {
                    }

                    @Override
                    public void visitVarInsn(int opcode, int var) {
                    }
                };
            }

            @Override
            public void visitOuterClass(String owner, String name, String desc) {
            }

            @Override
            public void visitSource(String source, String debug) {
            }

            @Override
            public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc,
                    boolean visible) {
                //               System.out.println("  ClassVisitor-TypeAnnotation:" + typeRef + " " + typePath + " " + desc + " " + visible);
                return null;
            }
        };
        cr.accept(cv, flags);
    } catch (IOException e) {
        logger.error("Failed to read '" + className + "'", e);
    } finally {
        if (classStream != null) {
            try {
                classStream.close();
            } catch (IOException e) {
            }
        }
    }
    typeref2state = desc2typerefValue2state.get(requiredDesc);
    if (typeref2state == null) {
        return null;
    }
    Boolean state = typeref2state.get(returnTypeReference);
    //      System.out.println("  => " + state);
    return state;
}