How Can I Find a Java Class Implementing a Generic Interface Given an Instance of the Generic Type?

203 views Asked by At

I’m working on a project that has a series of “Rule” classes (CustomerRule, VolumeRule, TimeRule, etc.). These classes are auto-generated and shouldn’t be modified from their generated form.

To process these rules, I have a series of “RuleProcessor” classes (CustomerRuleProcessor, VolumeRuleProcessor, etc.) that implement the following interface:

public interface RuleProcessor<T extends Rule> {
   T apply(T rule, EvaluationInput input);
}

With an implementation looking like this:

public class CustomerRuleProcessor implements RuleProcessor<CustomerRule> {...}

Finally, the rule classes themselves are grouped together and stored as JSON in the DB. So there could be multiple rules of different types stored together to be processed as one group. When retrieved, I’ll have a List<? extends Rule> that will be streamed and processed.

My question is this:

  • Given a concrete class (for example, CustomerRule), is there a way to find the processing class that implements the RuleProcessor<CustomerRule> interface?

I know I could just use the naming convention and use class.getName() + "Processor". But I’m wondering if there’s a better, more elegant solution.

0

There are 0 answers