-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
idea: check proper use of declaration-site type variance #216
Comments
Great idea. In terms of "how to implement": likely Error Prone is more suitable for such a check (with the added benefit that it can automatically emit appropriate fixes). NB: A low-false positive check would have to both consider method overrides, as well as constraints imposed by the APIs (if any) to which the |
I incline OpenRewrite might be slightly better at suggesting the fixes.
Technically speaking, the set of checks for Error Prone and forbidden-apis overlap, however, I find that forbidden-apis is lightweight (it does not require extra compiler, etc), so I consider forbidden-apis as a reasonable location for the check even though I understand there will be no "automatic code fix". However, I agree variance check might be a stretch for forbidden-apis goals.
You are right. It should probably avoid cases like |
Perhaps; as a long-time user of Error Prone and one of the primary devs behind Error Prone Support I'm likely biased. ;) I do agree that OpenRewrite is better at handling the transitive (cross-compilation unit) rewrites that may be required for an automated fix. |
Ah, nice. I tend adding Error Prone to all Java projects I'm working with (I'm quite happy with performance and output messages), so thanks for keeping it afloat. It is sad error-prone does not work for Kotlin though :-/ |
Hi, I am not sure if this can be implemented in forbiddenapis at all, as this is not related to "forbidden method invocations". |
I agree with that. Focus on forbiddenapis is to quickly parse bytecode and detect invalid patterns (method invocations and class usage from there). Because it works on bytecode/class files, it is good at parsing output of any compiler (so kotlin and scala is known to work perfectly with forbiddenapis, Groovy also kinda works), but the limitation to bytecode checks makes it not a good match for such type-based checks that require source code or more detailed type information that is erased from byte code. I am not fully sure, but for method declarations in class files bytecode saves the so-called "signature" including generics as an attribute, but at moment this one is not checked. So there is possiblility to check the usage of those classes in code, but its another code path than general forbidden-apis checks as implemented at moment. |
Generic signatures are very well available on the bytecode, see https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.9 |
That is exactly why I believe adding check in forbidden-apis would be great for both Java and Kotlin users. Suppose someone adds a function in Kotlin, and they receive In other words, the check would be useful for Java, Kotlin, and Scala, users, and it would be non-trivial to achieve in Error Prone, and OpenRewrite.
What I suggest here is to parse signatures of methods only, so it would be way faster than the current "parse method body". |
See:
Currently, Java requires use-site type variance, so if someone has
Function<IN, OUT>
method parameter, it should rather beFunction<? super IN, ? extends OUT>
.Unfortunately, it is not easy to notice that
? super
and? extends
is missing, so it would be nice if there was a tool that could detect missing variance and suggest adding it.The list of well-known classes could be hard-coded within forbidden-apis:
Function
,Predicate
,BiFunction
,Consumer
,Supplier
, and so on.Here a recent case:
WDYT?
See also:
The text was updated successfully, but these errors were encountered: