Search in sources :

Example 1 with SpecializationThrowsData

use of com.oracle.truffle.dsl.processor.model.SpecializationThrowsData in project graal by oracle.

the class SpecializationMethodParser method parseSpecialization.

private SpecializationData parseSpecialization(TemplateMethod method) {
    List<SpecializationThrowsData> exceptionData = new ArrayList<>();
    boolean unexpectedResultRewrite = false;
    if (method.getMethod() != null) {
        AnnotationValue rewriteValue = ElementUtils.getAnnotationValue(method.getMarkerAnnotation(), "rewriteOn");
        List<TypeMirror> exceptionTypes = ElementUtils.getAnnotationValueList(TypeMirror.class, method.getMarkerAnnotation(), "rewriteOn");
        List<TypeMirror> rewriteOnTypes = new ArrayList<>();
        for (TypeMirror exceptionType : exceptionTypes) {
            SpecializationThrowsData throwsData = new SpecializationThrowsData(method.getMarkerAnnotation(), rewriteValue, exceptionType);
            if (!ElementUtils.canThrowType(method.getMethod().getThrownTypes(), exceptionType)) {
                method.addError("A rewriteOn checked exception was specified but not thrown in the method's throws clause. The @%s method must specify a throws clause with the exception type '%s'.", Specialization.class.getSimpleName(), ElementUtils.getQualifiedName(exceptionType));
            }
            if (ElementUtils.typeEquals(exceptionType, getContext().getType(UnexpectedResultException.class))) {
                if (ElementUtils.typeEquals(method.getMethod().getReturnType(), getContext().getType(Object.class))) {
                    method.addError("A specialization with return type 'Object' cannot throw UnexpectedResultException.");
                }
                unexpectedResultRewrite = true;
            }
            rewriteOnTypes.add(throwsData.getJavaClass());
            exceptionData.add(throwsData);
        }
        for (TypeMirror typeMirror : method.getMethod().getThrownTypes()) {
            if (!ElementUtils.canThrowType(rewriteOnTypes, typeMirror)) {
                method.addError(rewriteValue, "A checked exception '%s' is thrown but is not specified using the rewriteOn property. " + "Checked exceptions that are not used for rewriting are not handled by the DSL. Use RuntimeExceptions for this purpose instead.", ElementUtils.getQualifiedName(typeMirror));
            }
        }
        Collections.sort(exceptionData, new Comparator<SpecializationThrowsData>() {

            @Override
            public int compare(SpecializationThrowsData o1, SpecializationThrowsData o2) {
                return ElementUtils.compareByTypeHierarchy(o1.getJavaClass(), o2.getJavaClass());
            }
        });
    }
    SpecializationData specialization = new SpecializationData(getNode(), method, SpecializationKind.SPECIALIZED, exceptionData, unexpectedResultRewrite);
    if (method.getMethod() != null) {
        String insertBeforeName = ElementUtils.getAnnotationValue(String.class, method.getMarkerAnnotation(), "insertBefore");
        if (!insertBeforeName.equals("")) {
            specialization.setInsertBeforeName(insertBeforeName);
        }
        List<String> replacesDefs = new ArrayList<>();
        replacesDefs.addAll(ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "replaces"));
        // TODO remove if deprecated contains api is removed.
        replacesDefs.addAll(ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "contains"));
        Set<String> containsNames = specialization.getReplacesNames();
        containsNames.clear();
        if (replacesDefs != null) {
            for (String include : replacesDefs) {
                if (!containsNames.contains(include)) {
                    specialization.getReplacesNames().add(include);
                } else {
                    AnnotationValue value = ElementUtils.getAnnotationValue(specialization.getMarkerAnnotation(), "replaces");
                    if (value == null) {
                        // TODO remove if deprecated api was removed.
                        value = ElementUtils.getAnnotationValue(specialization.getMarkerAnnotation(), "contains");
                    }
                    specialization.addError(value, "Duplicate replace declaration '%s'.", include);
                }
            }
        }
    }
    return specialization;
}
Also used : ArrayList(java.util.ArrayList) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) Specialization(com.oracle.truffle.api.dsl.Specialization) UnexpectedResultException(com.oracle.truffle.api.nodes.UnexpectedResultException) TypeMirror(javax.lang.model.type.TypeMirror) SpecializationThrowsData(com.oracle.truffle.dsl.processor.model.SpecializationThrowsData) AnnotationValue(javax.lang.model.element.AnnotationValue)

Aggregations

Specialization (com.oracle.truffle.api.dsl.Specialization)1 UnexpectedResultException (com.oracle.truffle.api.nodes.UnexpectedResultException)1 SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)1 SpecializationThrowsData (com.oracle.truffle.dsl.processor.model.SpecializationThrowsData)1 ArrayList (java.util.ArrayList)1 AnnotationValue (javax.lang.model.element.AnnotationValue)1 TypeMirror (javax.lang.model.type.TypeMirror)1