How return the type implemented by the interface

The idea is make ExporParts function in Export Class works with any WebData derivative. To do that I need to know the T type class and it name.

The solution proposed works, but I have to write it manually. I was wondering if possible know the class name only with the current information about the type.

In the other hand, reflection is not an option. Too expensive.

Thanks.

Example

// Data gathered at runtime, may be other derivative class
WebData data = new BikeModel();
Webdata data2 = new FooModel();

BikeParser parser = new BikeParser();
FooParser parser2 = new FooParser();

// Should be BikeModel, FooModel
Class<?> returnedtype = parser.GetreturnType();
Class<?> returnedtype2 = parser2.GetreturnType();

// Exporter algorithms wrapper
Exporter exporter = new Exporter();
exporter.SetExporter("BikeExporter",returnedtype);

// Finally export data
exporter.ExportData(data);

// Works too
exporter.SetExporter("FooExporter",returnedtype2);
exporter.ExportData(data2);

Implementation:

public abstract class WebData { ... }

// Data models
public class BikeModel extends WebData { ... }
public class FooModel extends WebData { ... }

public interface IParser <T extends WebData>
{    
T ParseData();    
Class<T> GetReturnType();
}

// Concrete class
public class BikeParser implements IParser<BikeModel>
{
@Override
public BikeModel ParseData() { ... }

@Override
public Class<BikeModel> GetReturnType()
{
    return BikeModel.class;
}

// interface to export diferent types of data
// BikeModel, FooModel, etc.
public interface IExporter<T extends WebData>
{
    void ExporParts(T data);
}

// Concrete Exporters
public class BikeExporter implements IExporter<BikeModel> { ... }
public class FooExporter implements IExporter<FooModel> { ... }

public class Exporter
{

   private IExporter exporter;

   public void SetExporter(String name, Class<T extends WebData> type)
   {
    exporter = ExporterFactory.GetExporter(name,type)
   }

   public <T extends WebData> void ExporParts(T data)
   {           
      Class<T> c = (Class<T>) data.getClass();        
      exporter.ExporParts(c.cast(data));
   }
}
Jon Skeet
people
quotationmark

I think you're just looking for a class literal:

@Override
public Class<BikeModel> GetReturnType()
{
    return BikeModel.class;
}

people

See more on this question at Stackoverflow