Search in sources :

Example 1 with Check

use of org.revapi.java.spi.Check in project revapi by revapi.

the class JavaApiAnalyzer method getJSONSchema.

@Nullable
@Override
public Reader getJSONSchema() {
    Map<String, Reader> checkSchemas = new HashMap<>(4);
    for (Check c : checks) {
        if (c.getExtensionId() != null) {
            checkSchemas.put(c.getExtensionId(), c.getJSONSchema());
        }
    }
    Reader rdr = new InputStreamReader(getClass().getResourceAsStream("/META-INF/config-schema.json"), Charset.forName("UTF-8"));
    if (checkSchemas.isEmpty()) {
        return rdr;
    } else {
        try {
            ModelNode baseSchema = ModelNode.fromJSONString(consume(rdr));
            ModelNode checksNode = baseSchema.get("properties", "checks");
            checksNode.get("type").set("object");
            for (Map.Entry<String, Reader> entry : checkSchemas.entrySet()) {
                String checkId = entry.getKey();
                Reader checkSchemaReader = entry.getValue();
                ModelNode checkSchema = ModelNode.fromJSONString(consume(checkSchemaReader));
                checksNode.get("properties").get(checkId).set(checkSchema);
            }
            return new StringReader(baseSchema.toJSONString(false));
        } catch (IOException e) {
            throw new IllegalStateException("Could not read the schema for the revapi extension...", e);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) Check(org.revapi.java.spi.Check) StringReader(java.io.StringReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) IOException(java.io.IOException) ModelNode(org.jboss.dmr.ModelNode) HashMap(java.util.HashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) TreeMap(java.util.TreeMap) Nullable(javax.annotation.Nullable)

Example 2 with Check

use of org.revapi.java.spi.Check in project revapi by revapi.

the class JavaApiAnalyzer method initialize.

@Override
public void initialize(@Nonnull AnalysisContext analysisContext) {
    this.analysisContext = analysisContext;
    this.configuration = AnalysisConfiguration.fromModel(analysisContext.getConfiguration());
    for (Check c : checks) {
        if (c.getExtensionId() != null) {
            ModelNode checkConfig = analysisContext.getConfiguration().get("checks", c.getExtensionId());
            AnalysisContext checkCtx = analysisContext.copyWithConfiguration(checkConfig);
            c.initialize(checkCtx);
        }
    }
}
Also used : Check(org.revapi.java.spi.Check) AnalysisContext(org.revapi.AnalysisContext) ModelNode(org.jboss.dmr.ModelNode)

Example 3 with Check

use of org.revapi.java.spi.Check in project revapi by revapi.

the class JavaElementDifferenceAnalyzer method doRestrictedCheck.

private <T extends JavaModelElement> void doRestrictedCheck(T oldElement, T newElement, CheckType interest, Collection<Check> possibleChecks) {
    lastAnnotationResults = null;
    if (!(isCheckedElsewhere(oldElement, oldEnvironment) && isCheckedElsewhere(newElement, newEnvironment))) {
        checkTypeStack.push(interest);
        checksStack.push(possibleChecks);
        for (Check c : possibleChecks) {
            Stats.of(c.getClass().getName()).start();
            switch(interest) {
                case FIELD:
                    c.visitField((FieldElement) oldElement, (FieldElement) newElement);
                    break;
                case METHOD:
                    c.visitMethod((MethodElement) oldElement, (MethodElement) newElement);
                    break;
                case METHOD_PARAMETER:
                    c.visitMethodParameter((MethodParameterElement) oldElement, (MethodParameterElement) newElement);
                    break;
            }
            Stats.of(c.getClass().getName()).end(oldElement, newElement);
        }
    } else {
        // "ignore what's on the stack because no checks actually happened".
        checkTypeStack.push(CheckType.NONE);
        checksStack.push(emptyList());
    }
}
Also used : Check(org.revapi.java.spi.Check)

Example 4 with Check

use of org.revapi.java.spi.Check in project revapi by revapi.

the class JavaElementDifferenceAnalyzer method endAnalysis.

@Override
public Report endAnalysis(@Nullable Element oldElement, @Nullable Element newElement) {
    if (oldElement == nonExistenceOldRoot && newElement == nonExistenceNewRoot) {
        nonExistenceMode = false;
        nonExistenceOldRoot = null;
        nonExistenceNewRoot = null;
    }
    if (conforms(oldElement, newElement, AnnotationElement.class)) {
        // the annotations are always reported at the parent element
        return new Report(Collections.emptyList(), oldElement, newElement);
    }
    List<Difference> differences = new ArrayList<>();
    CheckType lastInterest = checkTypeStack.pop();
    if (lastInterest.isConcrete()) {
        for (Check c : checksStack.pop()) {
            List<Difference> p = c.visitEnd();
            if (p != null) {
                differences.addAll(p);
            }
        }
    }
    if (lastAnnotationResults != null && !lastAnnotationResults.isEmpty()) {
        differences.addAll(lastAnnotationResults);
        lastAnnotationResults.clear();
    }
    if (!differences.isEmpty()) {
        LOG.trace("Detected following problems: {}", differences);
    }
    Timing.LOG.trace("Ended analysis of {} and {}.", oldElement, newElement);
    ListIterator<Difference> it = differences.listIterator();
    while (it.hasNext()) {
        Difference d = it.next();
        if (analysisConfiguration.reportUseForAllDifferences() || analysisConfiguration.getUseReportingCodes().contains(d.code)) {
            StringBuilder oldUseChain = null;
            StringBuilder newUseChain = null;
            if (oldElement != null) {
                oldUseChain = new StringBuilder();
                appendUses(oldEnvironment, oldElement, oldUseChain);
            }
            if (newElement != null) {
                newUseChain = new StringBuilder();
                appendUses(newEnvironment, newElement, newUseChain);
            }
            Map<String, String> atts = new HashMap<>(d.attachments);
            if (oldUseChain != null) {
                atts.put("exampleUseChainInOldApi", oldUseChain.toString());
            }
            if (newUseChain != null) {
                atts.put("exampleUseChainInNewApi", newUseChain.toString());
            }
            d = Difference.builder().addAttachments(atts).addClassifications(d.classification).withCode(d.code).withName(d.name).withDescription(d.description).build();
        }
        it.set(d);
    }
    return new Report(differences, oldElement, newElement);
}
Also used : Report(org.revapi.Report) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Check(org.revapi.java.spi.Check) Difference(org.revapi.Difference)

Example 5 with Check

use of org.revapi.java.spi.Check 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)

Aggregations

Check (org.revapi.java.spi.Check)5 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ModelNode (org.jboss.dmr.ModelNode)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Collections.emptyList (java.util.Collections.emptyList)1 IdentityHashMap (java.util.IdentityHashMap)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Nullable (javax.annotation.Nullable)1 AnalysisContext (org.revapi.AnalysisContext)1 Difference (org.revapi.Difference)1 Report (org.revapi.Report)1 AnnotationElement (org.revapi.java.model.AnnotationElement)1 MethodElement (org.revapi.java.model.MethodElement)1 TypeElement (org.revapi.java.model.TypeElement)1