Search in sources :

Example 11 with Element

use of org.revapi.Element in project revapi by revapi.

the class DFSFilteringIterator method hasNext.

@Override
public boolean hasNext() {
    if (current != null) {
        return true;
    } else {
        while (true) {
            while (!dfsStack.isEmpty() && !dfsStack.peek().hasNext()) {
                dfsStack.pop();
            }
            if (dfsStack.isEmpty()) {
                return false;
            }
            Iterator<? extends Element> currentIterator = dfsStack.peek();
            while (currentIterator.hasNext()) {
                Element next = currentIterator.next();
                while (next == null && currentIterator.hasNext()) {
                    next = currentIterator.next();
                }
                if (next == null) {
                    break;
                }
                boolean found = false;
                if (resultClass.isAssignableFrom(next.getClass())) {
                    E cur = resultClass.cast(next);
                    if (filter == null || filter.applies(cur)) {
                        current = cur;
                        found = true;
                    }
                }
                // we're doing DFS, so once we report this element, we want to start reporting its children
                // even if we don't report the current element, we might report one of its children so we base
                // our decision on whether to descend or not regardless of whether we're reporting the current
                // element or not
                boolean descend = filter == null || filter.shouldDescendInto(next);
                if (descend) {
                    Iterator<? extends Element> childIterator = next.getChildren().iterator();
                    if (childIterator.hasNext()) {
                        dfsStack.push(childIterator);
                    }
                }
                if (found) {
                    return true;
                }
                if (descend) {
                    break;
                }
            }
        }
    }
}
Also used : Element(org.revapi.Element)

Example 12 with Element

use of org.revapi.Element in project revapi by revapi.

the class JavaApiAnalyzer method reAddSortedMethods.

private static void reAddSortedMethods(List<Element> elements, List<Element> sortedMethods) {
    int methodRank = JavaElementFactory.getModelTypeRank(MethodElement.class);
    int index = 0;
    for (; index < elements.size(); ++index) {
        Element e = elements.get(index);
        if (JavaElementFactory.getModelTypeRank(e.getClass()) >= methodRank) {
            break;
        }
    }
    // remove all the method elements
    while (index < elements.size()) {
        Element e = elements.get(index);
        if (e instanceof MethodElement) {
            elements.remove(index);
        } else {
            break;
        }
    }
    // and re-add them in the newly established order
    elements.addAll(index, sortedMethods);
}
Also used : Element(org.revapi.Element) TypeElement(org.revapi.java.model.TypeElement) MethodElement(org.revapi.java.model.MethodElement) MethodElement(org.revapi.java.model.MethodElement)

Example 13 with Element

use of org.revapi.Element in project revapi by revapi.

the class TextReporter method initialize.

@Override
public void initialize(@Nonnull AnalysisContext analysis) {
    // noinspection ConstantConditions
    if (analysis != null) {
        try {
            flushReports();
        } catch (IOException e) {
            throw new IllegalStateException("Failed to output previous analysis report.");
        }
    }
    this.analysis = analysis;
    String minLevel = analysis.getConfiguration().get("minSeverity").asString();
    String output = analysis.getConfiguration().get("output").asString();
    output = "undefined".equals(output) ? "out" : output;
    String templatePath = analysis.getConfiguration().get("template").asString();
    if ("undefined".equals(templatePath)) {
        templatePath = null;
    }
    boolean append = analysis.getConfiguration().get("append").asBoolean(false);
    this.minLevel = "undefined".equals(minLevel) ? DifferenceSeverity.POTENTIALLY_BREAKING : DifferenceSeverity.valueOf(minLevel);
    OutputStream out;
    switch(output) {
        case "out":
            out = System.out;
            break;
        case "err":
            out = System.err;
            break;
        default:
            File f = new File(output);
            if (f.exists() && !f.canWrite()) {
                LOG.warn("The configured file for text reporter, '" + f.getAbsolutePath() + "' is not a writable file." + " Defaulting the output to standard output.");
                out = System.out;
            } else {
                File parent = f.getParentFile();
                if (!parent.exists()) {
                    if (!parent.mkdirs()) {
                        LOG.warn("Failed to create directory structure to write to the configured output file '" + f.getAbsolutePath() + "'. Defaulting the output to standard output.");
                        out = System.out;
                        break;
                    }
                }
                try {
                    out = new FileOutputStream(output, append);
                } catch (FileNotFoundException e) {
                    LOG.warn("Failed to create the configured output file '" + f.getAbsolutePath() + "'." + " Defaulting the output to standard output.", e);
                    out = System.out;
                }
            }
    }
    shouldClose = out != System.out && out != System.err;
    this.output = new PrintWriter(new OutputStreamWriter(out, Charset.forName("UTF-8")));
    this.reports = new TreeSet<>((r1, r2) -> {
        Element r1El = r1.getOldElement() == null ? r1.getNewElement() : r1.getOldElement();
        Element r2El = r2.getOldElement() == null ? r2.getNewElement() : r2.getOldElement();
        // noinspection ConstantConditions
        return r1El.compareTo(r2El);
    });
    Configuration freeMarker = createFreeMarkerConfiguration();
    template = null;
    try {
        template = templatePath == null ? freeMarker.getTemplate("default-template-with-improbable-name@@#(*&$)(.ftl") : freeMarker.getTemplate(templatePath);
    } catch (IOException e) {
        throw new IllegalStateException("Failed to initialize the freemarker template.", e);
    }
}
Also used : Reporter(org.revapi.Reporter) SortedSet(java.util.SortedSet) Report(org.revapi.Report) TemplateException(freemarker.template.TemplateException) Element(org.revapi.Element) LoggerFactory(org.slf4j.LoggerFactory) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) Charset(java.nio.charset.Charset) DifferenceSeverity(org.revapi.DifferenceSeverity) OutputStreamWriter(java.io.OutputStreamWriter) Template(freemarker.template.Template) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) Logger(org.slf4j.Logger) AnalysisContext(org.revapi.AnalysisContext) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Difference(org.revapi.Difference) DefaultObjectWrapperBuilder(freemarker.template.DefaultObjectWrapperBuilder) TemplateLoader(freemarker.cache.TemplateLoader) Configuration(freemarker.template.Configuration) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) Configuration(freemarker.template.Configuration) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Element(org.revapi.Element) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 14 with Element

use of org.revapi.Element in project revapi by revapi.

the class BuildTimeReporter method getAllProblemsMessage.

public String getAllProblemsMessage() {
    StringBuilder errors = new StringBuilder("The following API problems caused the build to fail:\n");
    StringBuilder ignores = new StringBuilder();
    for (Report r : allProblems) {
        Element element = r.getNewElement();
        Archive archive;
        if (element == null) {
            element = r.getOldElement();
            assert element != null;
            archive = shouldOutputArchive(oldApi, element.getArchive()) ? element.getArchive() : null;
        } else {
            archive = shouldOutputArchive(newApi, element.getArchive()) ? element.getArchive() : null;
        }
        for (Difference d : r.getDifferences()) {
            if (isReportable(d)) {
                errors.append(d.code).append(": ").append(element.getFullHumanReadableString()).append(": ").append(d.description);
                if (archive != null) {
                    errors.append(" [").append(archive.getName()).append("]");
                }
                errors.append("\n");
                ignores.append("{\n");
                ignores.append("  \"code\": \"").append(escape(d.code)).append("\",\n");
                if (r.getOldElement() != null) {
                    ignores.append("  \"old\": \"").append(escape(r.getOldElement())).append("\",\n");
                }
                if (r.getNewElement() != null) {
                    ignores.append("  \"new\": \"").append(escape(r.getNewElement())).append("\",\n");
                }
                for (Map.Entry<String, String> e : d.attachments.entrySet()) {
                    ignores.append("  \"").append(escape(e.getKey())).append("\": \"").append(escape(e.getValue())).append("\",\n");
                }
                ignores.append("  \"justification\": <<<<< ADD YOUR EXPLANATION FOR THE NECESSITY OF THIS CHANGE" + " >>>>>\n");
                ignores.append("},\n");
            }
        }
    }
    if (errors.length() == 0) {
        return null;
    } else {
        ignores.replace(ignores.length() - 2, ignores.length(), "");
        return errors.toString() + "\nIf you're using the semver-ignore extension, update your module's version to one compatible " + "with the current changes (e.g. mvn package revapi:update-versions). If you want to " + "explicitly ignore this change and provide a justification for it, add the following JSON snippet " + "to your Revapi configuration under \"revapi.ignore\" path:\n" + ignores.toString();
    }
}
Also used : Archive(org.revapi.Archive) Report(org.revapi.Report) Element(org.revapi.Element) Difference(org.revapi.Difference) Map(java.util.Map)

Example 15 with Element

use of org.revapi.Element in project revapi by revapi.

the class SimpleElementForest method addToString.

private void addToString(StringBuilder bld, int indent, SortedSet<? extends Element> elements) {
    for (Element e : elements) {
        bld.append("\n");
        for (int i = 0; i < indent; ++i) {
            bld.append("    ");
        }
        bld.append(e.toString());
        addToString(bld, indent + 1, e.getChildren());
    }
}
Also used : Element(org.revapi.Element)

Aggregations

Element (org.revapi.Element)15 API (org.revapi.API)5 AnalysisContext (org.revapi.AnalysisContext)5 MethodElement (org.revapi.java.model.MethodElement)5 TypeElement (org.revapi.java.model.TypeElement)5 Map (java.util.Map)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Nonnull (javax.annotation.Nonnull)3 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 Charset (java.nio.charset.Charset)2 IdentityHashMap (java.util.IdentityHashMap)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Set (java.util.Set)2 TreeMap (java.util.TreeMap)2 Executors (java.util.concurrent.Executors)2 Collectors.toList (java.util.stream.Collectors.toList)2