0

What is the correct way to use DependencyInjection / ContextInjectionFactory without generating a type safety warning?

 public class MyClass<T> {...}

 // Without Dependency Injection
 MyClass<MyType> x = new MyClass<MyType>();

 // With Dependency Injection and TypeSafety warning
 MyClass<MyType> x = ContextInjectionFactory.make(MyClass.class, context);
ekjcfn3902039
  • 1,673
  • 3
  • 29
  • 54

2 Answers2

0

You can use wildcard to avoid generating a type safety warning:-

MyClass<?> x = ContextInjectionFactory.make(MyClass.class, context);
Amit Bhati
  • 5,569
  • 1
  • 24
  • 45
0

Your going to have to use the wild card generic <?>.

MyClass<?> x = ContextInjectionFactory.make(MyClass.class, context);

This will remove the warning but then all generic methods will only work with Object. One way around this is to cast x to MyType.

MyClass<MyType> myClass = (MyClass<MyType>) x;
Luke Melaia
  • 1,470
  • 14
  • 22