Type may be weakened
Reports variable and method return types that can be changed to a more abstract (weaker) type. This allows making the code more abstract, hence more reusable.
Example:
Enable the Use righthand type checkbox below to prevent weakening the left side of assignments when the right side is not a type cast or a new expression. When storing the result of a method call in a variable, it is useful to retain the type of the method call result instead of unnecessarily weakening it.
Enable the Use parameterized type checkbox below to use the parameterized type of the collection as the weakest type when the object evaluated is used as an argument to a collection method with a parameter type of java.lang.Object
. Use this option to prevent weakening to Object
when passing an object to the following collection methods: get()
, remove()
, contains()
, indexOf()
, lastIndexOf()
, containsKey()
and containsValue()
.
Enable the Do not weaken to Object checkbox below to specify whether a type should be weakened to java.lang.Object
. Weakening to java.lang.Object
is rarely very useful.
Enable the Only weaken to an interface checkbox below to only report a problem when the type can be weakened to an interface type.
Enable the Do not weaken return type checkbox below to prevent reporting a problem when the return type may be weakened. Only variables will be analyzed.
Enable the Do not suggest weakening variable declared as 'var' checkbox below to prevent reporting on local variables declared using the 'var' keyword (Java 10+)
Stop classes are intended to prevent weakening to classes lower than stop classes, even if it is possible. In some cases, this may improve readability.
Inspection options
Option | Type | Default |
---|---|---|
Use righthand type as weakest type in assignments | Checkbox | true |
Use parameterized type of collection for method call arguments | Checkbox | true |
Do not weaken to 'java.lang.Object' | Checkbox | true |
Only weaken to an interface | Checkbox | true |
Do not weaken return type | Checkbox | true |
Do not suggest weakening variable declared as 'var' | Checkbox | false |
Stop classes | StringList | [] |
Inspection Details | |
---|---|
Available in: | IntelliJ IDEA 2023.3, Qodana for JVM 2023.3 |
Plugin: | Java, 233.SNAPSHOT |