Hi I am trying to create a generic method that takes in a parameter which type is an abstract parameterised class. Inside the method I want to create a instances of the passed parameter given it is a subclass of the abstract type since we cannot instantiate abstract classes.
However I am having some issues. And the error message says the RuleType cannot be resolved to a type which is not surprising anyway but I don’t know how can I accomplish this.
My current code is below.
private <F extends AbstractRule<T>, T> void applyRyle(F cRule,ArrayList<T> aFeatures)
{
Class RuleType = cRule.getClass();
// if the parameter passed is a subclass of AbstractRule create some objects
if(!RuleType.getSuperclass().getName().equals("AbstractRule"))
{
ArrayList<RuleType<T>> aRules = new ArrayList<RuleType<T>>();
for(int i = 0; i < 10; i++)
{
RuleType<T> aRule= new RuleType<T(pattern.getMatrices().size(), 2,aFeatures);
aRule.initialise(aRule.getMaxLocalLevel());
aRules.add(aRule);
}
}
Some of this can be done by using reflection:
though it’s probably not the best way to accomplish what you really want.