Question

I have a class that has a raw type member variable called Argument<T>. The class is meant to simply wrap around this type, and assign all values via the constructor in a generic way, because the type of Argument and of the parameters passed to the constructor is not known at compile time.

public class ArgumentWrapper {

    private Argument argument;

    public ArgumentImplementation(Class<?> type, String name, 
            Object defaultValue, Set<?> allowedValues, 
            Comparable<?> lowerBound, Comparable<?> upperBound) {
        argument = new Argument<>(type);
        argument.setName(name);
        argument.setDefaultValue(defaultValue);
        argument.setAllowedValues(allowedValues);
        argument.setLowerBound(lowerBound);
        argument.setUpperBound(upperBound);
    }

    // some getters...

}

Now in my code I get a lot of warnings saying

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

at the member and

Type safety: The method setDefaultValue(Object) belongs to the raw type Argument.
References to generic type Argument<T> should be parameterized

where the constructor parameters are assigned to it.

I know I cannot change the member to private Argument<?> argument; because then I get errors when the generic parameters are assigned to it in the constructor.

What is your recommendation to handle this? How do I avoid these warnings without introducing errors? How can I handle the generic type member variable different / better?

Was it helpful?

Solution

With help of a comment and some trying back and forth I got the answer myself.

The ArgumentWrapper class should be generic, and the generic type T should be used everywhere.

public class ArgumentWrapper<T> {

    private Argument<T> argument;

    public ArgumentImplementation(Class<T> type, String name, 
            T defaultValue, Set<T> allowedValues, 
            Comparable<T> lowerBound, Comparable<T> upperBound) {
        argument = new Argument<T>(type);
        argument.setName(name);
        argument.setDefaultValue(defaultValue);
        argument.setAllowedValues(allowedValues);
        argument.setLowerBound(lowerBound);
        argument.setUpperBound(upperBound);
    }

    // some getters...

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top