10: Fix interrupting executing on parse error
This commit is contained in:
@@ -3,12 +3,13 @@ package com.bartek.esa.core.archetype;
|
||||
import com.bartek.esa.core.model.enumeration.Severity;
|
||||
import com.bartek.esa.core.model.object.Issue;
|
||||
import com.bartek.esa.core.xml.XmlHelper;
|
||||
import com.bartek.esa.error.EsaException;
|
||||
import com.bartek.esa.file.matcher.GlobMatcher;
|
||||
import com.github.javaparser.ParseProblemException;
|
||||
import com.github.javaparser.Problem;
|
||||
import com.github.javaparser.StaticJavaParser;
|
||||
import com.github.javaparser.TokenRange;
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.expr.Expression;
|
||||
import io.vavr.control.Try;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
@@ -16,6 +17,8 @@ import javax.xml.xpath.XPathConstants;
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
public abstract class JavaPlugin extends BasePlugin {
|
||||
private final GlobMatcher globMatcher;
|
||||
private final XmlHelper xmlHelper;
|
||||
@@ -36,8 +39,27 @@ public abstract class JavaPlugin extends BasePlugin {
|
||||
return;
|
||||
}
|
||||
|
||||
CompilationUnit compilationUnit = Try.of(() -> StaticJavaParser.parse(file)).getOrElseThrow(EsaException::new);
|
||||
run(compilationUnit);
|
||||
try {
|
||||
CompilationUnit compilationUnit = StaticJavaParser.parse(file);
|
||||
run(compilationUnit);
|
||||
} catch (ParseProblemException e) {
|
||||
printParsingErrorToStderr(e, file);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void printParsingErrorToStderr(ParseProblemException e, File file) {
|
||||
e.getProblems().stream()
|
||||
.map(p -> format("%s%s:\n%s\nIgnoring file...\n", file.getAbsolutePath(), getRange(p), p.getMessage()))
|
||||
.forEach(System.err::println);
|
||||
}
|
||||
|
||||
private String getRange(Problem problem) {
|
||||
return problem.getLocation()
|
||||
.flatMap(TokenRange::toRange)
|
||||
.map(range -> format(" (line %d, col %d)", range.begin.line, range.begin.column))
|
||||
.orElse("");
|
||||
}
|
||||
|
||||
private boolean isApplicationPackageFile(File file) {
|
||||
@@ -59,7 +81,7 @@ public abstract class JavaPlugin extends BasePlugin {
|
||||
}
|
||||
|
||||
String path = packageValue.getNodeValue().replaceAll("\\.", "/");
|
||||
return globMatcher.fileMatchesGlobPattern(file, String.format("**/%s/**", path));
|
||||
return globMatcher.fileMatchesGlobPattern(file, format("**/%s/**", path));
|
||||
}
|
||||
|
||||
protected Integer getLineNumberFromExpression(Expression expression) {
|
||||
|
||||
@@ -21,7 +21,7 @@ public class PluginExecutor {
|
||||
|
||||
public Set<Issue> executeForFiles(File manifest, Set<File> files, Set<Plugin> plugins, boolean debug) {
|
||||
return files.stream()
|
||||
.peek(file -> { if(debug) System.out.printf("File: %s", file.getAbsolutePath()); })
|
||||
.peek(file -> { if(debug) System.out.printf("File: %s\n", file.getAbsolutePath()); })
|
||||
.map(file -> executeForFile(manifest, file, plugins, debug))
|
||||
.flatMap(Set::stream)
|
||||
.collect(toSet());
|
||||
@@ -29,8 +29,8 @@ public class PluginExecutor {
|
||||
|
||||
private Set<Issue> executeForFile(File manifest, File file, Set<Plugin> plugins, boolean debug) {
|
||||
Document xmlManifest = xmlHelper.parseXml(manifest);
|
||||
return plugins.parallelStream()
|
||||
.peek(plugin -> { if(debug) System.out.printf(" Plugin: %s", plugin.getClass().getCanonicalName()); })
|
||||
return plugins.stream()
|
||||
.peek(plugin -> { if(debug) System.out.printf(" Plugin: %s\n", plugin.getClass().getCanonicalName()); })
|
||||
.peek(plugin -> plugin.update(file, xmlManifest))
|
||||
.filter(plugin -> plugin.supports(file))
|
||||
.map(Plugin::runForIssues)
|
||||
|
||||
@@ -6,6 +6,7 @@ import lombok.Data;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@@ -27,6 +28,6 @@ public class Issue implements Comparable {
|
||||
return compByFile;
|
||||
}
|
||||
|
||||
return lineNumber - another.lineNumber;
|
||||
return Optional.ofNullable(lineNumber).orElse(0) - Optional.ofNullable(another.lineNumber).orElse(0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user