I have spent most of a day trying to understand why Java cannot compile a simple generic method with a generic type argument, but only when the enclosing class is imported. Basically, I am exposing a Spring context operation as a static method in a class called SpringApplicationContext:
public static <T> List<T> listBeansOfType(Class<T> type)
{
return new ArrayList<T>(context.getBeansOfType(type).values());
}
When I call it from a unit test in the same package, it works. Here’s the test call:
List<DomainRepo> repos =
SpringApplicationContext.listBeansOfType(DomainRepo.class);
However, the exact same code fails when I call it from code in another package. The compiler says it cannot find the symbol listBeansOfType(java.lang.Class). I have imported the class, but I have not done a static import on the method.
Even stranger is the fact that this code worked a week ago when I wrote it. Since then, I had to put it all aside while working through a release process. The same error occurs when I compile with maven or Eclipse, using java 1.6. Any hints will be more than welcome.
Make sure you have all needed maven dependencies. If your Junit Test works, but mvn compile fails the dependency could be included with scope Test.
Check the type of “context” and where the method getBeansOfType() is defined. They might be missing as mvn dependency.