Repository navigation
Expand file tree
/
Copy pathEcjDiagnosticProvider.java
More file actions
67 lines (60 loc) · 2.42 KB
/
Copy pathEcjDiagnosticProvider.java
File metadata and controls
67 lines (60 loc) · 2.42 KB
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
/*
* SonarQube Java
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java.model;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.CompilationUnit;
final class EcjDiagnosticProvider {
private static final Map<JProblem.Type, Integer> IDS = Map.of(
JProblem.Type.UNDEFINED_TYPE, IProblem.UndefinedType,
JProblem.Type.PREVIEW_FEATURE_USED, IProblem.PreviewFeatureUsed,
JProblem.Type.UNUSED_IMPORT, IProblem.UnusedImport,
JProblem.Type.REDUNDANT_CAST, IProblem.UnnecessaryCast,
JProblem.Type.ASSIGNMENT_HAS_NO_EFFECT, IProblem.AssignmentHasNoEffect,
JProblem.Type.MASKED_CATCH, IProblem.MaskedCatch);
private EcjDiagnosticProvider() {
}
static Set<String> compilerOptions() {
return Set.of(
JavaCore.COMPILER_PB_UNUSED_IMPORT,
JavaCore.COMPILER_PB_UNNECESSARY_TYPE_CHECK,
JavaCore.COMPILER_PB_NO_EFFECT_ASSIGNMENT,
JavaCore.COMPILER_PB_HIDDEN_CATCH_BLOCK
);
}
static List<JWarning> warnings(CompilationUnit ast, LineColumnConverter positions) {
return Stream.of(ast.getProblems())
.map(problem -> convert(problem, positions))
.filter(Objects::nonNull)
.toList();
}
private static JWarning convert(IProblem problem, LineColumnConverter positions) {
for (JProblem.Type type : JProblem.Type.values()) {
if (problem.getID() == IDS.get(type)) {
LineColumnConverter.Pos start = positions.toPos(problem.getSourceStart());
LineColumnConverter.Pos end = positions.toPos(problem.getSourceStart());
return new JWarning(problem.getMessage(), type, start.line(), start.columnOffset(), end.line(), end.columnOffset());
}
}
return null;
}
}