Search in sources :

Example 1 with EqualsMethod

use of org.checkerframework.checker.interning.qual.EqualsMethod in project checker-framework by typetools.

the class AnnotationUtils method sameElementValues.

// **********************************************************************
// Annotation values: other methods (e.g., testing and transforming)
// **********************************************************************
/**
 * Returns true if the two annotations have the same elements (fields). The arguments {@code am1}
 * and {@code am2} must be the same type of annotation.
 *
 * @param am1 the first AnnotationMirror to compare
 * @param am2 the second AnnotationMirror to compare
 * @return true if if the two annotations have the same elements (fields)
 */
@EqualsMethod
public static boolean sameElementValues(AnnotationMirror am1, AnnotationMirror am2) {
    if (am1 == am2) {
        return true;
    }
    Map<? extends ExecutableElement, ? extends AnnotationValue> vals1 = am1.getElementValues();
    Map<? extends ExecutableElement, ? extends AnnotationValue> vals2 = am2.getElementValues();
    for (ExecutableElement meth : ElementFilter.methodsIn(am1.getAnnotationType().asElement().getEnclosedElements())) {
        AnnotationValue aval1 = vals1.get(meth);
        AnnotationValue aval2 = vals2.get(meth);
        // optimization via equality test
        @SuppressWarnings("interning:not.interned") boolean identical = aval1 == aval2;
        if (identical) {
            // Handles when both aval1 and aval2 are null, and maybe other cases too.
            continue;
        }
        if (aval1 == null) {
            aval1 = meth.getDefaultValue();
        }
        if (aval2 == null) {
            aval2 = meth.getDefaultValue();
        }
        if (!sameAnnotationValue(aval1, aval2)) {
            return false;
        }
    }
    return true;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationValue(javax.lang.model.element.AnnotationValue) EqualsMethod(org.checkerframework.checker.interning.qual.EqualsMethod)

Example 2 with EqualsMethod

use of org.checkerframework.checker.interning.qual.EqualsMethod in project checker-framework by typetools.

the class InterningVisitor method visitMethod.

// Ensure that method annotations are not written on methods they don't apply to.
@Override
public Void visitMethod(MethodTree node, Void p) {
    ExecutableElement methElt = TreeUtils.elementFromDeclaration(node);
    boolean hasCompareToMethodAnno = atypeFactory.getDeclAnnotation(methElt, CompareToMethod.class) != null;
    boolean hasEqualsMethodAnno = atypeFactory.getDeclAnnotation(methElt, EqualsMethod.class) != null;
    boolean hasInternMethodAnno = atypeFactory.getDeclAnnotation(methElt, InternMethod.class) != null;
    int params = methElt.getParameters().size();
    if (hasCompareToMethodAnno && !(params == 1 || params == 2)) {
        checker.reportError(node, "invalid.method.annotation", "@CompareToMethod", "1 or 2", methElt, params);
    } else if (hasEqualsMethodAnno && !(params == 1 || params == 2)) {
        checker.reportError(node, "invalid.method.annotation", "@EqualsMethod", "1 or 2", methElt, params);
    } else if (hasInternMethodAnno && !(params == 0)) {
        checker.reportError(node, "invalid.method.annotation", "@InternMethod", "0", methElt, params);
    }
    return super.visitMethod(node, p);
}
Also used : InternMethod(org.checkerframework.checker.interning.qual.InternMethod) ExecutableElement(javax.lang.model.element.ExecutableElement) CompareToMethod(org.checkerframework.checker.interning.qual.CompareToMethod) EqualsMethod(org.checkerframework.checker.interning.qual.EqualsMethod)

Aggregations

ExecutableElement (javax.lang.model.element.ExecutableElement)2 EqualsMethod (org.checkerframework.checker.interning.qual.EqualsMethod)2 AnnotationValue (javax.lang.model.element.AnnotationValue)1 CompareToMethod (org.checkerframework.checker.interning.qual.CompareToMethod)1 InternMethod (org.checkerframework.checker.interning.qual.InternMethod)1