[Java] Generic type parameter declared with a final type bound

java

(Werner Thumann) #1

Description
Generic type parameters with final type bounds are not generic at all since there is just one possibility for the type parameter. Such generics should be refactored to work with the specific final type instead.

Type
Code Smell

Snippet

public class GenericTypeParameterDeclaredWithAFinalTypeBound {

	public static <T extends String> T getStringGeneric() { // Noncompliant
		return null;
	}
	
	public static String getString() { // Compliant
		return null;
	}
	
}

Note
Also covered by Eclipse JDT.


(Alexandre Gigleux) #3

Hello,

Thanks for the suggestion.

Here is the specification https://jira.sonarsource.com/browse/RSPEC-4968 and the linked implementation ticket https://jira.sonarsource.com/browse/SONARJAVA-2940

Regards