|
12 | 12 | import com.github.javaparser.symbolsolver.utils.SymbolSolverCollectionStrategy; |
13 | 13 | import com.github.javaparser.utils.CollectionStrategy; |
14 | 14 | import com.github.javaparser.utils.SourceRoot; |
| 15 | +import dev.dendrodocs.tool.descriptions.Description; |
15 | 16 | import java.io.IOException; |
16 | 17 | import java.util.ArrayList; |
17 | 18 | import java.util.List; |
18 | | -import dev.dendrodocs.tool.descriptions.Description; |
19 | 19 | import net.jimblackler.jsonschemafriend.Schema; |
20 | 20 | import net.jimblackler.jsonschemafriend.SchemaException; |
21 | 21 | import net.jimblackler.jsonschemafriend.SchemaStore; |
|
27 | 27 | * output that follows LivingDocumentation conventions. |
28 | 28 | */ |
29 | 29 | public class Analyzer { |
30 | | - private final ObjectMapper objectMapper; |
31 | | - |
32 | | - /** Constructs an Analyzer with the given Jackson ObjectMapper. */ |
33 | | - public Analyzer(ObjectMapper objectMapper) { |
34 | | - this.objectMapper = objectMapper; |
35 | | - } |
| 30 | + private final ObjectMapper objectMapper; |
36 | 31 |
|
37 | | - /** |
38 | | - * Returns a type solver that can resolve from neighboring source code, the classpath provided in |
39 | | - * the {@link AnalysisJob}, and by reflection (which is required to resolve types like Object). |
40 | | - */ |
41 | | - private TypeSolver typeSolverFor(AnalysisJob job) throws IOException { |
42 | | - var typeSolverBuilder = new TypeSolverBuilder(); |
| 32 | + /** Constructs an Analyzer with the given Jackson ObjectMapper. */ |
| 33 | + public Analyzer(ObjectMapper objectMapper) { |
| 34 | + this.objectMapper = objectMapper; |
| 35 | + } |
43 | 36 |
|
44 | | - for (String cp : job.classpath()) { |
45 | | - if (cp.toLowerCase().endsWith(".jar")) { |
46 | | - typeSolverBuilder = typeSolverBuilder.withJAR(cp); |
47 | | - } |
48 | | - } |
| 37 | + /** |
| 38 | + * Returns a type solver that can resolve from neighboring source code, the classpath provided in |
| 39 | + * the {@link AnalysisJob}, and by reflection (which is required to resolve types like Object). |
| 40 | + */ |
| 41 | + private TypeSolver typeSolverFor(AnalysisJob job) throws IOException { |
| 42 | + var typeSolverBuilder = new TypeSolverBuilder(); |
49 | 43 |
|
50 | | - return typeSolverBuilder.withCurrentClassloader().withSourceCode(job.project()).build(); |
| 44 | + for (String cp : job.classpath()) { |
| 45 | + if (cp.toLowerCase().endsWith(".jar")) { |
| 46 | + typeSolverBuilder = typeSolverBuilder.withJAR(cp); |
| 47 | + } |
51 | 48 | } |
52 | 49 |
|
53 | | - private Boolean validateJson(List<Description> descriptions) { |
54 | | - try { |
55 | | - Schema schema = new SchemaStore().loadSchema( |
56 | | - this.getClass().getResource("/schema.json")); |
57 | | - new Validator().validate(schema, new ObjectMapper().convertValue(descriptions, Object.class)); |
58 | | - } catch (SchemaException e) { |
59 | | - e.printStackTrace(); |
60 | | - return false; |
61 | | - } |
62 | | - return true; |
| 50 | + return typeSolverBuilder.withCurrentClassloader().withSourceCode(job.project()).build(); |
| 51 | + } |
| 52 | + |
| 53 | + private Boolean validateJson(List<Description> descriptions) { |
| 54 | + try { |
| 55 | + Schema schema = new SchemaStore().loadSchema(this.getClass().getResource("/schema.json")); |
| 56 | + new Validator().validate(schema, new ObjectMapper().convertValue(descriptions, Object.class)); |
| 57 | + } catch (SchemaException e) { |
| 58 | + e.printStackTrace(); |
| 59 | + return false; |
63 | 60 | } |
| 61 | + return true; |
| 62 | + } |
64 | 63 |
|
65 | | - /** Execute the given job. An exception is thrown if a file can not be read or parsed. */ |
66 | | - public void analyze(AnalysisJob job) throws IOException { |
67 | | - ParserConfiguration parserConfiguration = new ParserConfiguration() |
68 | | - .setSymbolResolver(new JavaSymbolSolver(typeSolverFor(job))); |
69 | | - CollectionStrategy strategy = new SymbolSolverCollectionStrategy(parserConfiguration); |
70 | | - List<Description> descriptions = new ArrayList<>(); |
| 64 | + /** Execute the given job. An exception is thrown if a file can not be read or parsed. */ |
| 65 | + public void analyze(AnalysisJob job) throws IOException { |
| 66 | + ParserConfiguration parserConfiguration = |
| 67 | + new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolverFor(job))); |
| 68 | + CollectionStrategy strategy = new SymbolSolverCollectionStrategy(parserConfiguration); |
| 69 | + List<Description> descriptions = new ArrayList<>(); |
71 | 70 |
|
72 | | - for (SourceRoot sourceRoot : strategy.collect(job.project()).getSourceRoots()) { |
73 | | - List<ParseResult<CompilationUnit>> results = sourceRoot.tryToParse(); |
74 | | - SymbolResolver solver = sourceRoot.getParserConfiguration().getSymbolResolver().orElseThrow(); |
| 71 | + for (SourceRoot sourceRoot : strategy.collect(job.project()).getSourceRoots()) { |
| 72 | + List<ParseResult<CompilationUnit>> results = sourceRoot.tryToParse(); |
| 73 | + SymbolResolver solver = sourceRoot.getParserConfiguration().getSymbolResolver().orElseThrow(); |
75 | 74 |
|
76 | | - for (ParseResult<CompilationUnit> result : results) { |
77 | | - CompilationUnit compilationUnit = result.getResult().orElseThrow(); |
78 | | - AnalysisVisitor visitor = new AnalysisVisitor(solver); |
| 75 | + for (ParseResult<CompilationUnit> result : results) { |
| 76 | + CompilationUnit compilationUnit = result.getResult().orElseThrow(); |
| 77 | + AnalysisVisitor visitor = new AnalysisVisitor(solver); |
79 | 78 |
|
80 | | - List<Description> visited = compilationUnit.accept(visitor, this); |
81 | | - descriptions.addAll(visited); |
82 | | - } |
83 | | - } |
| 79 | + List<Description> visited = compilationUnit.accept(visitor, this); |
| 80 | + descriptions.addAll(visited); |
| 81 | + } |
| 82 | + } |
84 | 83 |
|
85 | | - ObjectWriter writer = job.pretty() |
86 | | - ? objectMapper.writerWithDefaultPrettyPrinter() |
87 | | - : objectMapper.writer(); |
88 | | - if (validateJson(descriptions)) { |
89 | | - writer.writeValue(job.output().toFile(), descriptions); |
90 | | - } |
| 84 | + ObjectWriter writer = |
| 85 | + job.pretty() ? objectMapper.writerWithDefaultPrettyPrinter() : objectMapper.writer(); |
| 86 | + if (validateJson(descriptions)) { |
| 87 | + writer.writeValue(job.output().toFile(), descriptions); |
91 | 88 | } |
| 89 | + } |
92 | 90 | } |
0 commit comments