-1

How to get Subclass object using implemented interface, if interface is used as Type Parameter for DynamoDBTypeConverter.(e.g. DynamoDBTypeConverter ).

public enum state implements EnumInterface{
    CREATED("0");
}

public enum color implements EnumInterface{
    GREEN("0");
}

public interface EnumInterface{
    void getStatus();
}

public class DynamoDbEnumConverter implements DynamoDBTypeConvereter<String,EnumInterface>{
    public EnumInterface unconvert(String value){
        // find Object run time, EnumInterface represent color or stat
    }
}

Get whether Enum interface represents color or state in unconvert method.

Dev
  • 1
  • Does `String value` represent an enum type (like color or state), or an enum constant (like `CREATED` or `GREEN`)? – VGR Apr 01 '19 at 02:23
  • @VGR Represent the value, for example constant GREEN, I am storing value "0". – Dev Apr 01 '19 at 05:29
  • Is there a reason you want to discover your enum classes dynamically? Why not keep a `Collection>` inside your DynamoDbEnumConverter? – VGR Apr 01 '19 at 12:17

1 Answers1

0

Check this page out: What are Reified Generics? How do they solve Type Erasure problems and why can't they be added without major changes?

Generics are erased in Java.

The only way you're going to get your code to work without hacking around is by providing one instance of the DynamoDbEnumConverter for each EnumInterface:

class DynamoDbEnumConverter<T extends Enum<T> & EnumInterface> implements DynamoDBTypeConvereter<String, T> {
    private Class<T> enumType;

    public DynamoDbEnumConverter(Class<T> enumType) {
        this.enumType = enumType;
    }

    public EnumInterface unconvert(String value) {
        return Enum.valueOf(enumType, value);
    }
}

And then:

DynamoDbEnumConverter<Color> colorConverter = new DynamoDbEnumConverter<>(Color.class);
Not a JD
  • 1,864
  • 6
  • 14
  • Dev wants DynamoDbEnumConverter to look through *all* types which implement EnumInterface, not just one of them. – VGR Apr 01 '19 at 12:19
  • Ah - I took "Get whether Enum interface represents color or state in unconvert method" too literally :( – Not a JD Apr 01 '19 at 13:20