Say we have an interface like this one:
public interface Validator<T> {
boolean isValid(T data);
}
And this is part of a core module. Multiple apps can use that same core module, with a different value for the generic T. An example implementation is (from a specific module of the application):
@Component
public class AppValidator implements Validator<String> {
@Override
public boolean isValid(String data) {
return false;
}
}
And then in the controller (which is part of the core module):
@RestController
public class ValidateController {
@Autowired
private Validator validator;
@RequestMapping("/")
public void index() {
validator.validate("");
}
}
IntelliJ is complaining that I'm using raw types; as you can see, I'm actually doing that in the controller.
My question: is there a way to inject the dependency in a bounded way (instead of injecting Validator
, injecting Validator<String>
)? But of course, the bound type could change depending on the application using the core module?
If not possible (probably due to type erasure), what's the best practice for this? Is it just to use Object
? Is there no nicer alternative that still provides type-safety?
I've seen somewhere people saying it's possible to do some magic at compile-time to change the types, but I'm not sure how, or even if I read it correctly?
I am using Spring, so I'm hoping Spring can provide something to help me here! Some magic is welcome!