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?
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...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.