forked from jenkinsci/analysis-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AjcParser.java
110 lines (93 loc) · 3.53 KB
/
AjcParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package edu.hm.hafner.analysis.parser;
import java.io.UncheckedIOException;
import java.util.Iterator;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import edu.hm.hafner.analysis.Categories;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.IssueParser;
import edu.hm.hafner.analysis.ParsingException;
import edu.hm.hafner.analysis.ReaderFactory;
import edu.hm.hafner.analysis.Report;
/**
* A parser for AspectJ (ajc) compiler warnings.
*
* @author Tom Diamond
*/
public class AjcParser extends IssueParser {
private static final long serialVersionUID = -9123765511497052454L;
private static final Pattern ESCAPE_CHARACTERS = Pattern.compile((char) 27 + "\\[.*" + (char) 27 + "\\[0m");
private static final String WARNING_TAG = "[WARNING] ";
static final String ADVICE = "Advice";
@Override
public Report parse(final ReaderFactory reader) throws ParsingException {
try (Stream<String> lines = reader.readStream()) {
return parse(lines);
}
catch (UncheckedIOException e) {
throw new ParsingException(e);
}
}
private Report parse(final Stream<String> lines) {
Report warnings = new Report();
States state = States.START;
IssueBuilder builder = new IssueBuilder();
Iterator<String> lineIterator = lines.iterator();
while (lineIterator.hasNext()) {
String line = lineIterator.next();
// clean up any ESC characters (e.g. terminal colors)
line = ESCAPE_CHARACTERS.matcher(line).replaceAll("");
switch (state) {
case START:
if (line.startsWith("[INFO] Showing AJC message detail for messages of types")) {
state = States.PARSING;
}
break;
case PARSING:
if (line.startsWith(WARNING_TAG)) {
line = line.substring(WARNING_TAG.length());
state = States.WAITING_FOR_END;
fillMessageAndCategory(builder, line);
}
break;
case WAITING_FOR_END:
if (line.startsWith("\t")) {
fillFileName(builder, line);
}
else if ("".equals(line)) {
state = States.PARSING;
warnings.add(builder.build());
}
break;
}
}
return warnings;
}
private void fillFileName(final IssueBuilder builder, final String line) {
int indexOfColon = line.lastIndexOf(':');
if (indexOfColon != -1) {
builder.setFileName(line.substring(0, indexOfColon));
if (line.length() > indexOfColon + 1) {
builder.setLineStart(line.substring(indexOfColon + 1));
}
}
}
private void fillMessageAndCategory(final IssueBuilder builder, final String line) {
String category;
if (line.contains("is deprecated") || line.contains("overrides a deprecated")) {
category = Categories.DEPRECATION;
}
else if (line.contains("adviceDidNotMatch")) {
category = AjcParser.ADVICE;
}
else {
category = "";
}
builder.setMessage(line);
builder.setCategory(category);
}
/** Available states for the parser. */
private enum States {
START, PARSING, WAITING_FOR_END
}
}