diff options
author | Danny van Bruggen <hexagonaal@gmail.com> | 2018-02-03 22:09:35 +0100 |
---|---|---|
committer | Danny van Bruggen <hexagonaal@gmail.com> | 2018-02-03 22:09:35 +0100 |
commit | 6036e9961e7f8ea8f94c7d9b3ab9160b517d156b (patch) | |
tree | 595d8bd2f58a198df716ea39d2a8060e44781b3a /javaparser-core | |
parent | 9f20df294175a8dcc2f0c1331c818589c5de9cb4 (diff) | |
download | javaparser-6036e9961e7f8ea8f94c7d9b3ab9160b517d156b.tar.gz |
Add postprocessing chain
Diffstat (limited to 'javaparser-core')
4 files changed, 62 insertions, 56 deletions
diff --git a/javaparser-core/src/main/java/com/github/javaparser/JavaParser.java b/javaparser-core/src/main/java/com/github/javaparser/JavaParser.java index 287e88e6b..7b8ee87a5 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/JavaParser.java +++ b/javaparser-core/src/main/java/com/github/javaparser/JavaParser.java @@ -27,16 +27,13 @@ import com.github.javaparser.ast.Node; import com.github.javaparser.ast.PackageDeclaration; import com.github.javaparser.ast.body.BodyDeclaration; import com.github.javaparser.ast.body.Parameter; -import com.github.javaparser.ast.comments.CommentsCollection; import com.github.javaparser.ast.expr.*; import com.github.javaparser.ast.stmt.BlockStmt; import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt; import com.github.javaparser.ast.stmt.Statement; import com.github.javaparser.ast.type.ClassOrInterfaceType; import com.github.javaparser.ast.type.Type; -import com.github.javaparser.ast.validator.ProblemReporter; import com.github.javaparser.javadoc.Javadoc; -import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter; import java.io.*; import java.nio.charset.Charset; @@ -53,7 +50,6 @@ import static com.github.javaparser.utils.Utils.assertNotNull; * @author JĂșlio Vilmar Gesser */ public final class JavaParser { - private final CommentsInserter commentsInserter; private final ParserConfiguration configuration; private GeneratedJavaParser astParser = null; @@ -74,7 +70,6 @@ public final class JavaParser { */ public JavaParser(ParserConfiguration configuration) { this.configuration = configuration; - commentsInserter = new CommentsInserter(configuration); } /** @@ -129,20 +124,13 @@ public final class JavaParser { final GeneratedJavaParser parser = getParserForProvider(provider); try { N resultNode = start.parse(parser); - if (configuration.isAttributeComments()) { - final CommentsCollection comments = parser.getCommentsCollection(); - commentsInserter.insertComments(resultNode, comments.copy().getComments()); - } - if (configuration.isLexicalPreservationEnabled()) { - LexicalPreservingPrinter.setup(resultNode); - } - - configuration.getValidator().accept(resultNode, new ProblemReporter(parser.problems)); - parser.problems.sort(PROBLEM_BY_BEGIN_POSITION); - ParseResult<N> result = new ParseResult<>(resultNode, parser.problems, parser.getTokens(), parser.getCommentsCollection()); - considerInjectingSymbolResolver(result, configuration); + + configuration.getPostProcessors().forEach(postProcessor -> + postProcessor.process(result, configuration)); + + result.getProblems().sort(PROBLEM_BY_BEGIN_POSITION); return result; } catch (Exception e) { @@ -531,14 +519,4 @@ public final class JavaParser { public static PackageDeclaration parsePackageDeclaration(String packageDeclaration) { return simplifiedParse(PACKAGE_DECLARATION, provider(packageDeclaration)); } - - private void considerInjectingSymbolResolver(ParseResult<?> parseResult, ParserConfiguration parserConfiguration) { - parserConfiguration.getSymbolResolver().ifPresent(symbolResolver -> - parseResult.getResult().ifPresent(result -> { - if (result instanceof CompilationUnit) { - ((CompilationUnit) result).setData(Node.SYMBOL_RESOLVER_KEY, symbolResolver); - } - }) - ); - } } diff --git a/javaparser-core/src/main/java/com/github/javaparser/ParseResult.java b/javaparser-core/src/main/java/com/github/javaparser/ParseResult.java index 903d401ef..77180be62 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ParseResult.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ParseResult.java @@ -21,6 +21,7 @@ package com.github.javaparser; +import com.github.javaparser.ast.Node; import com.github.javaparser.ast.comments.CommentsCollection; import java.util.List; @@ -28,7 +29,6 @@ import java.util.Optional; import java.util.function.Consumer; import static com.github.javaparser.utils.Utils.EOL; -import static java.util.Collections.singletonList; /** * The results given when parsing with an instance of JavaParser. @@ -54,22 +54,6 @@ public class ParseResult<T> { } /** - * Used when parsing failed completely with an exception. - */ - ParseResult(Throwable throwable) { - this(null, singletonList( - new Problem(createMessage(throwable), null, throwable)), null, null); - } - - private static String createMessage(Throwable throwable) { - String message = throwable.getMessage(); - if (message == null) { - return throwable.getClass().getSimpleName(); - } - return message; - } - - /** * @return if parsing was successful, meaning no errors of any kind were encountered. */ public boolean isSuccessful() { @@ -102,7 +86,7 @@ public class ParseResult<T> { /** * @return the complete list of tokens that were parsed, or empty if parsing failed completely. * @deprecated lists of tokens are now kept in every node. - * Calling this method is comparable to calling getResult().get().getTokenRange().get() + * Calling this method is comparable to calling getResult().get().getTokenRange().get() */ @Deprecated public Optional<List<JavaToken>> getTokens() { @@ -134,4 +118,12 @@ public class ParseResult<T> { } return message.toString(); } + + /** + * A post processor that can be added to ParserConfiguration to add some processing right after parsing. + */ + public interface PostProcessor { + void process(ParseResult<? extends Node> result, ParserConfiguration configuration); + } + } diff --git a/javaparser-core/src/main/java/com/github/javaparser/ParserConfiguration.java b/javaparser-core/src/main/java/com/github/javaparser/ParserConfiguration.java index f722eca7a..05afde2fb 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ParserConfiguration.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ParserConfiguration.java @@ -21,14 +21,18 @@ package com.github.javaparser; +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.Node; import com.github.javaparser.ast.validator.Java8Validator; +import com.github.javaparser.ast.validator.ProblemReporter; import com.github.javaparser.ast.validator.Validator; +import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter; import com.github.javaparser.resolution.SymbolResolver; +import java.util.ArrayList; +import java.util.List; import java.util.Optional; -import static com.github.javaparser.utils.Utils.assertNotNull; - /** * The configuration that is used by the parser. * Note that this can be changed even when reusing the same JavaParser instance. @@ -43,6 +47,34 @@ public class ParserConfiguration { private SymbolResolver symbolResolver = null; private int tabSize = 1; private Validator validator = new Java8Validator(); + private final List<ParseResult.PostProcessor> postProcessors = new ArrayList<>(); + + public ParserConfiguration() { + postProcessors.add((result, configuration) -> { + if (configuration.isLexicalPreservationEnabled()) { + if (configuration.isLexicalPreservationEnabled()) { + result.ifSuccessful(LexicalPreservingPrinter::setup); + } + } + }); + postProcessors.add((result, configuration) -> { + if (configuration.isAttributeComments()) { + result.ifSuccessful(resultNode -> result + .getCommentsCollection().ifPresent(comments -> + new CommentsInserter(configuration).insertComments(resultNode, comments.copy().getComments()))); + } + }); + postProcessors.add((result, configuration) -> + getValidator().ifPresent(validator -> + validator.accept(result.getResult().get(), new ProblemReporter(newProblem -> result.getProblems().add(newProblem))))); + postProcessors.add((result, configuration) -> configuration.getSymbolResolver().ifPresent(symbolResolver -> + result.ifSuccessful(resultNode -> { + if (resultNode instanceof CompilationUnit) { + resultNode.setData(Node.SYMBOL_RESOLVER_KEY, symbolResolver); + } + }) + )); + } public boolean isAttributeComments() { return attributeComments; @@ -100,16 +132,16 @@ public class ParserConfiguration { return this; } - public Validator getValidator() { - return validator; + public Optional<Validator> getValidator() { + return Optional.of(validator); } /** - * The validator to run directly after parsing. + * The language level validator to run directly after parsing. * By default it is {@link Java8Validator} + * If it is null, all validation is turned off and only hard parse errors will be reported. */ public ParserConfiguration setValidator(Validator validator) { - assertNotNull(validator); this.validator = validator; return this; } @@ -142,4 +174,8 @@ public class ParserConfiguration { this.symbolResolver = symbolResolver; return this; } + + public List<ParseResult.PostProcessor> getPostProcessors() { + return postProcessors; + } } diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/validator/ProblemReporter.java b/javaparser-core/src/main/java/com/github/javaparser/ast/validator/ProblemReporter.java index f5dc663b2..eb54810c1 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/validator/ProblemReporter.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/validator/ProblemReporter.java @@ -4,7 +4,7 @@ import com.github.javaparser.Problem; import com.github.javaparser.TokenRange; import com.github.javaparser.ast.nodeTypes.NodeWithTokenRange; -import java.util.List; +import java.util.function.Consumer; import static com.github.javaparser.utils.CodeGenerationUtils.f; @@ -12,10 +12,10 @@ import static com.github.javaparser.utils.CodeGenerationUtils.f; * A simple interface where validators can report found problems. */ public class ProblemReporter { - private final List<Problem> problems; + private final Consumer<Problem> problemConsumer; - public ProblemReporter(List<Problem> problems) { - this.problems = problems; + public ProblemReporter(Consumer<Problem> problemConsumer) { + this.problemConsumer = problemConsumer; } /** @@ -29,6 +29,6 @@ public class ProblemReporter { } public void report(TokenRange range, String message, Object... args) { - problems.add(new Problem(f(message, args), range, null)); + problemConsumer.accept(new Problem(f(message, args), range, null)); } } |