Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raw use of parameterized class

Tags:

java

generics

I wrote a helper method for getting values of static fields of specified type via reflection. The code is working fine, but I am getting "raw use of parameterized class" warning on line:

final List<Collection> fields = getStaticFieldValues(Container.class, Collection.class); 

The issue is that type parameter T can be generic type. Is there way to rewrite method getStaticFieldValues to work around this issue?

Code listing:

import static java.util.Arrays.asList; import static org.junit.Assert.assertEquals;  import java.lang.reflect.*; import java.util.*;  import org.junit.Test;  public class GenericsTest {      @Test     public void test() {         // Warning "raw use of parameterized class 'Collection'"         final List<Collection> fields = getStaticFieldValues(Container.class, Collection.class);         assertEquals(asList("A", "B", "C"), fields.get(0));     }      private static <T> List<T> getStaticFieldValues(Class<?> fieldSource, Class<T> fieldType) {         List<T> values = new ArrayList<>();         Field[] declaredFields = fieldSource.getDeclaredFields();         for (Field field : declaredFields) {             if (Modifier.isStatic(field.getModifiers()) && fieldType.isAssignableFrom(field.getType())) {                 try {                     final T fieldValue = (T) field.get(null);                     values.add(fieldValue);                 } catch (IllegalAccessException e) {                     throw new RuntimeException("Error getting static field values");                 }             }         }         return values;     }      public static class Container<T> {         public static Collection<String> elements = asList("A", "B", "C");     } } 
like image 341
dsavickas Avatar asked Jul 10 '14 09:07

dsavickas


People also ask

What does raw use of parameterized class mean?

Raw types refer to using a generic type without specifying a type parameter. For example, List is a raw type, while List<String> is a parameterized type. When generics were introduced in JDK 1.5, raw types were retained only to maintain backwards compatibility with older versions of Java.

What is parameterized class in Java?

The type parameter section of a generic class can have one or more type parameters separated by commas. These classes are known as parameterized classes or parameterized types because they accept one or more parameters.

What are raw types in Java?

A raw type is a name for a generic interface or class without its type argument: List list = new ArrayList(); // raw type. Instead of: List<Integer> listIntgrs = new ArrayList<>(); // parameterized type. List<Integer> is a parameterized type of interface List<E> while List is a raw type of interface List<E>.

What does raw Use mean Java?

A raw type is the name of a generic class or interface without any type arguments. For example, given the generic Box class: public class Box<T> { public void set(T t) { /* ... */ } // ... }


1 Answers

in the definition of method getStaticFieldValues() change:

getStaticFieldValues(Class<?> fieldSource, Class<T> fieldType) 

to

getStaticFieldValues(Class<?> fieldSource, Class<?> fieldType) 
like image 181
EduSanCon Avatar answered Sep 20 '22 19:09

EduSanCon