mercredi 7 octobre 2020

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

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.





Aucun commentaire:

Enregistrer un commentaire