1

Inside my IF all 3 lines having warnings. How I can remove it without supress? Is there a better solution?

public void template() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
        if(templateResult == null){
            Class templateClass = getGenericTypeArgument(this.getClass(), 0);
            Constructor<? extends Template> constructor = templateClass.getConstructor(new Class[]{List.class, List.class});
            templateResult = (T) constructor.newInstance(listData, listaDataRes);       
        }
    }
Helio Bentzen
  • 645
  • 2
  • 12
  • 24
  • can you provide code for this class? – MaxZoom May 28 '15 at 00:30
  • Usually you should not see any warning. If you do it means that you have some non-standard things in your code – MaxZoom May 28 '15 at 00:31
  • this test was in a isolated class.. I have 3 warnings: Class templateClass = getGenericTypeArgument(this.getClass(), 0); Multiple markers at this line - Class is a raw type. References to generic type Class should be parameterized - Class is a raw type. References to generic type Class should be parameterized – Helio Bentzen May 28 '15 at 00:51

1 Answers1

2

You can remove some of them by not using raw types:

Class<?> templateClass = getGenericTypeArgument(this.getClass(), 0);
//    ^
Constructor<? extends Template> constructor =
    templateClass.getConstructor(new Class<?>[]{List.class, List.class});
//                                         ^

It doesn't look to me like you can avoid the unchecked cast to T.

Is there a better solution?

It looks to me like you might be using the getGenericSuperclass().getActualTypeArguments() thing and you can remove all warnings by passing the Class<T> as a parameter to the object (shown here) instead.

Community
  • 1
  • 1
Radiodef
  • 37,180
  • 14
  • 90
  • 125