Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Ignore warnings on directory/package level

Some of our code is auto-generated (by Apache Axis) and it reports a ton of warnings. An example would be:

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

Here, the warning would be

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

Of course it makes no sense to actually address these warnings, as we have to trust Apache Axis anyway and it would simply re-create the code as-is again.

Because our team uses different IDEs, from VSCode to IntelliJ IDEA to Eclipse, I'm looking for an IDE-agnostic way to exclude warnings coming from the auto-generated package/directory. It would be best if this option could be set for the workspace by being under version control.

In TypeScript, I'd set an exclude array in tsconfig.json

Does Java, for example, have an equivalent?

like image 838
knallfrosch Avatar asked Jul 26 '19 07:07

knallfrosch


People also ask

How do I ignore a Java warning?

If we don't want to fix the warning, then we can suppress it with the @SuppressWarnings annotation. This annotation allows us to say which kinds of warnings to ignore. While warning types can vary by compiler vendor, the two most common are deprecation and unchecked.

How do you suppress warnings?

Use of @SuppressWarnings is to suppress or ignore warnings coming from the compiler, i.e., the compiler will ignore warnings if any for that piece of code. 1. @SuppressWarnings("unchecked") public class Calculator { } - Here, it will ignore all unchecked warnings coming from that class.

How do I ignore warning Orcode?

To ignore an error or a warning From the pop-up menu, select Configure "Name of the error / warning" | Ignore This Error / Warning.

What is @SuppressWarnings unchecked in Java?

@SuppressWarnings("unchecked") is used when Java generics just don't let you do what you want to, and thus, you need to explicitly specify to the compiler that whatever you are doing is legal and can be executed at the time of execution.


2 Answers

i only find this way to solve the issue now:

add @SuppressWarnings("rawtypes") to your method or variable like:

@SuppressWarnings("rawtypes")
private java.util.HashMap faultExceptionNameMap = new java.util.HashMap();
like image 188
Leo Zhu - MSFT Avatar answered Oct 23 '22 03:10

Leo Zhu - MSFT


I would recommend adding the @SuppressWarnings to the auto-generated code after its generated:

<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>

The configuration above would essentially do this through maven. If you have other build tools you would do something similar.

like image 44
Rohit Shetty Avatar answered Oct 23 '22 04:10

Rohit Shetty