Java - 忽略 directory/package 级别的警告

Java - Ignore warnings on directory/package level

我们的一些代码是(由 Apache Axis)自动生成的,它会报告大量警告。一个例子是:

private java.util.HashMap faultExceptionNameMap = new java.util.HashMap();

在这里,警告是

HashMap is a raw type. References to generic type HashMap should be parameterized.

当然,实际上解决这些警告是没有意义的,因为无论如何我们都必须信任 Apache Axis,它只会按原样重新创建代码。

因为我们的团队使用不同的 IDEs,从 VSCode 到 IntelliJ IDEA 再到 Eclipse,我正在寻找一种 IDE 不可知的方法来排除来自自动生成的 package/directory 的警告。最好能在版本控制下为工作区设置这个选项。

在 TypeScript 中,我会在 tsconfig.json

中设置一个 exclude 数组

例如,Java是否有等价物?

我现在只找到这种方法来解决问题:

@SuppressWarnings("rawtypes") 添加到您的方法或变量中,例如:

@SuppressWarnings("rawtypes")
private java.util.HashMap faultExceptionNameMap = new java.util.HashMap();

我建议在生成代码后将@SuppressWarnings 添加到自动生成的代码中:

<plugin>
   <groupId>com.google.code.maven-replacer-plugin</groupId>
   <artifactId>maven-replacer-plugin</artifactId>
    <version>1.3.2</version>
   <executions>
    <execution>
     <phase>prepare-package</phase>
     <goals>
     <goal>replace</goal>
     </goals>
     </execution>
    </executions>
    <configuration>
    <includes>
      <include>target/generated-sources/apache/blahblah//*.java</include>
    </includes>

   <regex>true</regex>
   <regexFlags>
    <regexFlag>MULTILINE</regexFlag>
   </regexFlags>

  <replacements>
   <replacement>
     <token>^(@SuppressWarnings\(.*?\)\s+)?public class</token>
   <value>@SuppressWarnings("all") public class</value>
    </replacement>
   </replacements>
  </configuration>
</plugin>

上面的配置基本上是通过 maven 来完成的。如果你有其他构建工具,你会做类似的事情。