I have an abstract class as follows:
@Component
public abstract class B<T extends C, S extends D>{
@Autowired
private JpaRepository repository;
public void add(){
repository.add(); //this add method takes an argument of a class object of T
}
public void delete(){
repository.delete();//this add method takes an argument of a class object of S
}
}
I realize I have two options to fix this. Do something similar to this where I do ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments() or I do something similar to below (not exact but you get the idea) in the sub class:
@Component
public abstract class B<T extends C, S extends D>{
@Autowired
private JpaRepository repository;
private Class<T> classT;
private Class<S> classS;
public B(Class<T> classT, Class<S> classS){
this.classT = classT;
this.classS = classS;
}
public void add(){
repository.add(); //this add method takes an argument of a class object of T
}
public void delete(){
repository.delete();//this add method takes an argument of a class object of S
}
}
public class A extends B<X, Y>{
public A(){
super(X.class, Y.class);
}
}
I wonder if there is a better way to achieve this. Specifically if I want to ensure type safety without having to subclass developer write too much code. The second approach would have been fine but every subclass having to deal with this seems a little annoying, not just to mention Spring Java @Autowired annotation has chances of getting confused with multiple constructors.