View Javadoc
1   /*
2    * Copyright 2005-2025 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * The Class StubOverrideFixer.
35   */
36  public class StubOverrideFixer {
37  
38      /** The logger. */
39      private static final Logger logger = LoggerFactory.getLogger(StubOverrideFixer.class);
40  
41      /** The Constant GROOVY_METHODS. */
42      private static final Set<String> GROOVY_METHODS = Set.of("getMetaClass", "setMetaClass", "invokeMethod",
43              "getProperty", "setProperty");
44  
45      /**
46       * The main method.
47       *
48       * @param args the arguments
49       * @throws IOException Signals that an I/O exception has occurred.
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       * Process stub.
60       *
61       * @param filePath the file path
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       * Should have override.
81       *
82       * @param method the method
83       * @return true, if successful
84       */
85      private static boolean shouldHaveOverride(MethodDeclaration method) {
86          return GROOVY_METHODS.contains(method.getNameAsString());
87      }
88  
89  }