This question is not the same as Eclipse warning - Class is a raw type. References to generic type Class<T> should be parameterized. Only the generated warning is the same.
The code below results in the warning:
X.PropertyType is a raw type. References to generic type X.PropertyType should be parameterized.
at the lines indicated when built using Eclipse. One quick fix offered by Eclipse is to add @SuppressWarnings("unchecked")
but I don't want to do that without fully understanding the implications, which at the moment I don't.
How can I eliminate the warning without suppressing it? I've seen other questions on SO related to this warning but I think I need an answer specific to the code below to understand the issue.
import java.util.HashMap;
import java.util.Map;
public class X
{
// *** WARNING ***
private final Map<String, PropertyType> m_properties = new HashMap<String, PropertyType>();
public class PropertyType<T>
{
private final String m_name;
private final T m_value;
private final Class<T> m_type;
public PropertyType(String name, T value, Class<T> type)
{
m_name = name;
m_value = value;
m_type = type;
}
public String getName() { return m_name; }
public T getValue() { return m_value; }
public Class<T> getType() { return m_type; }
}
public <U> void setProperty(String name, U value)
{
// *** WARNING ***
m_properties.put(name, new PropertyType(name, value, String.class));
}
}
Also, in the method setProperty
, I would like to pass the type of value
when creating an instance of PropertyType
. I'm just passing a String
type at the moment. What is the best way to do that?
I could modify setProperty
as follows:
public <U> void setProperty(String name, U value, Class<U> type)
but I've been looking for a better way.