1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.github.spotbugs;
17
18 import com.github.javaparser.ParseProblemException;
19 import com.github.javaparser.StaticJavaParser;
20 import com.github.javaparser.ast.CompilationUnit;
21 import com.github.javaparser.ast.body.MethodDeclaration;
22
23 import java.io.IOException;
24 import java.nio.charset.StandardCharsets;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.util.Set;
28 import java.util.stream.Stream;
29
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35
36 public class StubOverrideFixer {
37
38
39 private static final Logger logger = LoggerFactory.getLogger(StubOverrideFixer.class);
40
41
42 private static final Set<String> GROOVY_METHODS = Set.of("getMetaClass", "setMetaClass", "invokeMethod",
43 "getProperty", "setProperty");
44
45
46
47
48
49
50
51 public static void main(String[] args) throws IOException {
52 Path stubsDir = Path.of(args[0]);
53 try (Stream<Path> stream = Files.walk(stubsDir)) {
54 stream.filter(p -> p.toString().endsWith(".java")).forEach(StubOverrideFixer::processStub);
55 }
56 }
57
58
59
60
61
62
63 private static void processStub(Path filePath) {
64 try {
65 CompilationUnit cu = StaticJavaParser.parse(filePath);
66
67 cu.findAll(MethodDeclaration.class).forEach(method -> {
68 if (shouldHaveOverride(method)) {
69 method.addAnnotation("java.lang.Override");
70 }
71 });
72
73 Files.write(filePath, cu.toString().getBytes(StandardCharsets.UTF_8));
74 } catch (IOException | ParseProblemException e) {
75 logger.error("Error processing: {} - {}", filePath, e.getMessage());
76 }
77 }
78
79
80
81
82
83
84
85 private static boolean shouldHaveOverride(MethodDeclaration method) {
86 return GROOVY_METHODS.contains(method.getNameAsString());
87 }
88
89 }