auto-matter icon indicating copy to clipboard operation
auto-matter copied to clipboard

Don't emit `@SafeVarargs` for reifiable types

Open bndbsh opened this issue 5 years ago • 7 comments

Attempt to detect if a type is reifiable, and omit the annotation in those cases.

https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html

However one problem as noted in the comments is we can't get at a ParameterizedTypeName's enclosing type. So for instance, this code will treat Foo<X>.Bar as reifiable, even though it isn't.

One idea to get around it would be to toString() the type and check if it contains any type parameters, although that seems kinda messy.

bndbsh avatar Apr 19 '19 21:04 bndbsh

Hi! Giving a look at how java.util.List.of() is implemented, could we just do the same that they do and generate always the following?

@SafeVarargs
@SuppressWarnings("varargs")

lepistone avatar May 16 '19 15:05 lepistone

~~@lepistone That indeed seems like a simpler approach. 👍~~

danielnorberg avatar May 20 '19 01:05 danielnorberg

Would using @SuppressWarnings("varargs") help in this case though? Adding it to a method with a reifiable vararg parameter does not seem to make IntelliJ happier about the method.

Javac with -Xlint:all does not emit any warning about @SafeVarargs either way. Is there some static analysis tool that does complain (other than IntelliJ)?

danielnorberg avatar May 20 '19 02:05 danielnorberg

@danielnorberg in my case, with Java 11, Javac with -Xlint:all does emit warnings, and auto-matter is the only reason why I have to pass -XLint:-varargs to maven-compiler-plugin. So this is not about intelliJ. I wonder why I get this warning and you don't.

lepistone avatar May 20 '19 09:05 lepistone

@lepistone Indeed. I had simply failed to enable showWarnings.

https://github.com/danielnorberg/auto-matter/pull/72

danielnorberg avatar May 20 '19 13:05 danielnorberg

Although the @SuppressWarnings("varargs") has been released in 0.15.3, I guess it could still be neat to avoid both @SafeVarargs and @SuppressWarnings in code generated for reifiable types.

danielnorberg avatar May 21 '19 07:05 danielnorberg

@bndbsh I attempted to handle nested types here, please take a look: https://github.com/mninja/auto-matter/pull/1

danielnorberg avatar May 21 '19 14:05 danielnorberg