Generic type issues

Discussion in 'Plugin Development' started by RcExtract, Aug 3, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    RcExtract

    I have a class:
    Code:Java
    1.  
    2. /**
    3. * A tool used to convert objects between two types.
    4. * @param <I> The original type, or the deserialized type.
    5. * @param <O> The serialized type.
    6. */
    7. public abstract class Converter<I, O> implements Function<I, O> {
    8.  
    9. private final Class<I> i;
    10. private final Class<O> o;
    11.  
    12. /**
    13.   * Constructs a new Converter.
    14.   * @param i The original type, or the deserialized type.
    15.   * @param o The serialized type.
    16.   */
    17. public Converter(Class<I> i, Class<O> o) {
    18. this.i = i;
    19. this.o = o;
    20. }
    21.  
    22. /**
    23.   * Gets the original type, or the deserialized type.
    24.   * @return The original type, or the deserialized type.
    25.   */
    26. public Class<I> getInputType() {
    27. return i;
    28. }
    29.  
    30. /**
    31.   * Gets the serialized type.
    32.   * @return The serialized type.
    33.   */
    34. public Class<O> getOutputType() {
    35. return o;
    36. }
    37.  
    38. /**
    39.   * Reverts an object from a serialized to a deserialized type.
    40.   * @param o The target object.
    41.   * @return The deserialized form of the target object.
    42.   */
    43. public abstract I revert(O o);
    44.  
    45. @Override
    46. public boolean equals(Object object) {
    47. if (!(object instanceof Converter)) return false;
    48. if (object == this) return true;
    49. Converter<?, ?> converter = (Converter<?, ?>) object;
    50. return i == converter.getInputType() && o == converter.getOutputType();
    51. }
    52.  
    53. }
    54.  


    When i try to inherit it:
    Code:Java
    1. public class IdentifiableConverter extends Converter<Identifiable, Properties<Identifiable>> {
    2.  
    3. public IdentifiableConverter() {
    4. super(Identifiable.class, Properties.class);
    5. //Compiler Error: The constructor Converter<Identifiable,Properties<Identifiable>>(Class<Identifiable>, Class<Properties>) is undefined
    6. }
    7.  
    8. @Override
    9. public Properties<Identifiable> apply(Identifiable t) {
    10. // TODO Auto-generated method stub
    11. return null;
    12. }
    13.  
    14. @Override
    15. public Identifiable revert(Properties<Identifiable> o) {
    16. // TODO Auto-generated method stub
    17. return null;
    18. }
    19.  
    20. }

    Please help. Thanks.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @RcExtract You don't need a constructor like that.
    You can access I and O instead.
     
  3. Offline

    RcExtract

    Isnt there generic type erasure? I need to access the generic type.
     
  4. Offline

    timtower Administrator Administrator Moderator

    Use I and O for that.
     
  5. Offline

    RcExtract

    I dont understand what you are talking about. Can u show an example please?

    I think the problem now is Properties.class return Class<Properties> instead of Class<Properties<Identifiable>>.
     
  6. Offline

    timtower Administrator Administrator Moderator

    @RcExtract You can get the subclass using the Class interface I believe for the Identifiable.
    But making specific converters seems more logical to me in this case.
    Depending on what you want to do anyways.
     
  7. Offline

    RcExtract

    What i am trying to do is Converter inheritance. But the same thing happens for the implementations of Identifiable interface. There is still a compiler error. Still i dont understand what u are talking about. Sorry.

    The compiler keeps calling me to change constructor parameters from Class<I> and Class<O> to Class<I> and Class<Properties>. Can someone explain please? How can i obtain Class<Properties<Identifiable>> instead of Class<Properties>? Thanks.

    EDIT: Cannot cast from Class<Properties> to Class<Properties<Identifiable>>. But I can first cast Class<Properties> to Class<?> then cast Class<?> to Class<Properties<Identifiable>>. Um... It looks ugly.
     
    Last edited: Aug 3, 2018
Thread Status:
Not open for further replies.

Share This Page