Search in sources :

Example 6 with MethodElement

use of org.revapi.java.model.MethodElement in project revapi by revapi.

the class JavaElementDifferenceAnalyzer method beginAnalysis.

@Override
public void beginAnalysis(@Nullable Element oldElement, @Nullable Element newElement) {
    Timing.LOG.trace("Beginning analysis of {} and {}.", oldElement, newElement);
    Check.Type elementsType = getCheckType(oldElement, newElement);
    Collection<Check> possibleChecks = nonExistenceMode ? descendingChecksByTypes.getOrDefault(elementsType, emptySet()) : checksByInterest.get(elementsType);
    if (conforms(oldElement, newElement, TypeElement.class)) {
        checkTypeStack.push(CheckType.CLASS);
        checksStack.push(possibleChecks);
        lastAnnotationResults = null;
        for (Check c : possibleChecks) {
            Stats.of(c.getClass().getName()).start();
            c.visitClass(oldElement == null ? null : (TypeElement) oldElement, newElement == null ? null : (TypeElement) newElement);
            Stats.of(c.getClass().getName()).end(oldElement, newElement);
        }
    } else if (conforms(oldElement, newElement, AnnotationElement.class)) {
        // treat them a bit differently
        if (lastAnnotationResults == null) {
            lastAnnotationResults = new ArrayList<>(4);
        }
        // Annotations are handled differently and this would lead to the stack corruption and missed problems!!!
        for (Check c : possibleChecks) {
            Stats.of(c.getClass().getName()).start();
            List<Difference> cps = c.visitAnnotation(oldElement == null ? null : (AnnotationElement) oldElement, newElement == null ? null : (AnnotationElement) newElement);
            if (cps != null) {
                lastAnnotationResults.addAll(cps);
            }
            Stats.of(c.getClass().getName()).end(oldElement, newElement);
        }
    } else if (conforms(oldElement, newElement, FieldElement.class)) {
        doRestrictedCheck((FieldElement) oldElement, (FieldElement) newElement, CheckType.FIELD, possibleChecks);
    } else if (conforms(oldElement, newElement, MethodElement.class)) {
        doRestrictedCheck((MethodElement) oldElement, (MethodElement) newElement, CheckType.METHOD, possibleChecks);
    } else if (conforms(oldElement, newElement, MethodParameterElement.class)) {
        doRestrictedCheck((MethodParameterElement) oldElement, (MethodParameterElement) newElement, CheckType.METHOD_PARAMETER, possibleChecks);
    }
    if (!nonExistenceMode && (oldElement == null || newElement == null)) {
        nonExistenceMode = true;
        nonExistenceOldRoot = oldElement;
        nonExistenceNewRoot = newElement;
    }
}
Also used : AnnotationElement(org.revapi.java.model.AnnotationElement) JavaTypeElement(org.revapi.java.spi.JavaTypeElement) TypeElement(org.revapi.java.model.TypeElement) Check(org.revapi.java.spi.Check) ArrayList(java.util.ArrayList) MethodElement(org.revapi.java.model.MethodElement) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List)

Example 7 with MethodElement

use of org.revapi.java.model.MethodElement in project revapi by revapi.

the class AnnotatedElementFilterTest method testChangesReportedOnAnnotationElements.

@Test
public void testChangesReportedOnAnnotationElements() throws Exception {
    CollectingReporter reporter = runAnalysis(CollectingReporter.class, "{\"revapi\": {\"java\": {\"filter\": {\"annotated\": {\"regex\": true," + " \"include\":[\"@Attributes.Anno.*\"]}}}}}", "v1/annotations/Attributes.java", "v2/annotations/Attributes.java");
    List<Report> reports = reporter.getReports();
    Assert.assertEquals(2, reports.size());
    Report parameterChange = reports.stream().filter(r -> r.getNewElement() instanceof MethodParameterElement).findFirst().orElse(null);
    Report annotationChanges = reports.stream().filter(r -> r.getNewElement() instanceof MethodElement).findFirst().orElse(null);
    Assert.assertEquals(1, parameterChange.getDifferences().size());
    Assert.assertEquals("java.method.parameterTypeChanged", parameterChange.getDifferences().get(0).code);
    Assert.assertEquals(3, annotationChanges.getDifferences().size());
    Assert.assertEquals(new HashSet<>(Arrays.asList("java.annotation.attributeValueChanged", "java.annotation.attributeAdded", "java.element.nowDeprecated")), annotationChanges.getDifferences().stream().map(d -> d.code).collect(Collectors.toSet()));
}
Also used : Report(org.revapi.Report) MethodParameterElement(org.revapi.java.model.MethodParameterElement) MethodElement(org.revapi.java.model.MethodElement) Test(org.junit.Test)

Aggregations

MethodElement (org.revapi.java.model.MethodElement)7 TypeElement (org.revapi.java.model.TypeElement)5 Element (org.revapi.Element)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 HashMap (java.util.HashMap)2 IdentityHashMap (java.util.IdentityHashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 Collectors.toList (java.util.stream.Collectors.toList)2 CoIterator (org.revapi.CoIterator)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Charset (java.nio.charset.Charset)1 Collection (java.util.Collection)1 Collections.emptyList (java.util.Collections.emptyList)1 Comparator (java.util.Comparator)1 Iterator (java.util.Iterator)1