0

I'm trying to do an abstract class which has two methods, aggregate and writeHeader. Both methods calls a static method that have a Class<?> parameter. It looks like this :

public abstract class AbstractExtractLineMapper<T extends BaseObject> implements LineAggregator<T>, FlatFileHeaderCallback {
  @Override
  public void writeHeader(Writer writer) throws IOException {
    TreeSet<CSVdata> csvElements = CSVDataGenerator.loadCSVstructureFromAnnotations(CLASS<?> NEEDED);
    [...]
    writer.write(sb.toString());
  }

  @Override
  public String aggregate(T object) {
    TreeSet<CSVdata> csvElements = CSVDataGenerator.loadCSVstructureFromAnnotations(object.getClass());
    [...]
    return sb.toString();
  }
}

For aggregate, I have an instance of T, on which I can call getClass(). But I cannot find a compiling code to replace Class<?> NEEDED of my writeHeader method. Please note that writeHeader is called before aggregate as it writes the header of the CSV file. In some cases, aggregate will not be called at all (if CSV file to write has no lines, only header).

As some of you may have noticed, these interfaces are from the Spring framework (batch module), but I do not think this information is needed to answer.

Wis
  • 705
  • 1
  • 11
  • 34
  • 1
    `protected abstract Class getElementClass();` – JimmyB Nov 17 '16 at 13:11
  • I've struggled with generics for a while, trying to build something this similar, and the only way I found out was to add the `Class>` as a parameter so you can "have" the class type in your method. But here, as it is Spring batch methods, it might be diffcult to solve that kind of problem, unless you extend these methods with your own in which you will add the `Class>` parameter – DamCx Nov 17 '16 at 13:12
  • The proposal of JimmyB will work, I'll need to implement the abstract `getElementClass()` method for all classes extending this one. Not a big deal. As you pointed out DamCx, this is Spring Batch methods and I do not think I can change their signatures. And overloading them would be of no use, the framework will always call this specific signature. – Wis Nov 17 '16 at 13:17
  • maybe you could achieve that by applying reflection http://stackoverflow.com/questions/557663/retrieving-type-parameters-from-an-instance-of-a-generic-base-interface – Merve Sahin Nov 17 '16 at 14:48

0 Answers0