Class is a raw type. References to generic type Class<T> should be parameterized

66,763

Solution 1

The interface declares the method with a raw type. In that case, you can't override it nicely without having the warning.

The origin of your problem is that the Spring interface was declared to be Java 1.4 compliant. Note that Spring 3.0 is supposed to deliver all classes as Java 1.5 compliant, so that would fix your problem. Before you upgrade, I guess you would have to live with either the warning or the @SuppressWarning.

Solution 2

Since the interface forces you to use the raw type (i.e. doesn't allow you to specify the correct type information) you can't implement it without warnings unless you use @SupressWarnings.

The only real fix is to fix the interface (i.e. make it define boolean supports(Class<?> aClass)).

Share:
66,763
Admin
Author by

Admin

Updated on September 03, 2020

Comments

  • Admin
    Admin over 3 years

    I have the following class (from a simple Spring tutorial)

    public class CarValidator implements Validator {
    
        public boolean supports(Class aClass) {
            return Car.class.equals(aClass);
        }
    
        public void validate(Object obj, Errors errors) {
            Car car = (Car) obj;
    
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "model", "field.required", "Required field");
    
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "price", "field.required", "Required field");
    
            if( ! errors.hasFieldErrors("price")) {
                if (car.getPrice().intValue() == 0) {
                    errors.rejectValue("price", "not_zero", "Can't be free!");
                }
            }
    
        }
    }
    

    Where the Validator class is the org.springframework.validation.Validator class from Spring 2.5.

    The supports method is showing a warning (Class is a raw type. References to generic type Class should be parameterized), if I try to add parameters to this such as

    public boolean supports(Class<?> aClass) ...
    

    I get the following error:

    The method supports(Class<?>) of type CarValidator has the same erasure as supports(Class) of type Validator but does not override it

    There are lots of threads about this type of question, but I want to get a complete answer and actually understand it without 'hiding' the problem with a @SupressWarnings!

  • Admin
    Admin over 14 years
    Excellent thanks. I'll have to live with the @SuppressWarning for now.
  • Admin
    Admin over 14 years
    Yep I can live with the @SuppressWarning for now, but if I change to boolean supports(Class<?> aClass) I enter the world of type erasure and that's just nasty! Is there a different approach or do I wait for Spring 3.0?
  • CppDude
    CppDude over 14 years
    I'm glad I could help you. I liked it that you don't only want the stuff to work, but also want to understand ! If you need to find a job in the south of France ... ;-)
  • AlbeyAmakiir
    AlbeyAmakiir almost 12 years
    It's still the case that Spring LDAP is sticking with raw types, right? Even though the core part of it requires Java 1.5?