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;
}
}
}
}
}
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);
}
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);
}
}
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();
}
}
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());
}
}
Aggregations