-3

I need get name from Enum with reflection of java.

I know I can that for class: (but I could not find it about Enums)

public class EnumWrapper<T> {

    @SuppressWarnings("unchecked")
    public String getType() {
        Type type = getClass().getGenericSuperclass();
        ParameterizedType pt = (ParameterizedType) type;
        Class<T> entity = (Class<T>) pt.getActualTypeArguments()[0];
        String name = entity.getName();

        return name;
    }
}

I maybe set any of the Enums :

public enum Gender {
    WOMAN, MAN, OTHER
}

public enum Language {
    Norwegian, ENGLISH, PERSIAN
}

I want to get "Gender" or "Language" only from " getType() " method.

EnumWrapper<Gender> ew = new EnumWrapper<>();
String nameEnum = ew.getType();  // Gender
mehnet ali
  • 73
  • 3
  • 12
  • There will always be only one instance of an enum so you should not be able to even create one. Just get the value by name or so. Have a look here: https://stackoverflow.com/questions/604424/how-to-get-an-enum-value-from-a-string-value-in-java – Thomas Mar 15 '21 at 15:08
  • dear Thomas. I used lot of Enum, and maybe I set every Enum in EnumWrapper. but I need understand that. – mehnet ali Mar 15 '21 at 15:30
  • Well, generic types can only provide type information anyway so it won't be individual values. But since enums are classes with special properties `Class` should be sufficient. You don't even have to create an instance of a class to get the class' name, just call `entity.getName()` (although `entity` is misleading, I'd use `entityClass` or `entityType` instead). – Thomas Mar 15 '21 at 15:37
  • I wrote "make Object" *or* "get Name". anyway I can't do it. if you answer my question is better. thankyou – mehnet ali Mar 15 '21 at 15:41
  • 1
    Well, your code doesn't return any object you've created so that "make object" seems unnecessary. You might want to elaborate on how you want to use that code as it is very unclear of what you actually want to achieve. And btw the code you've posted wouldn't even compile and would also not work if you'd directly create instances of `EnumWrapper` (I assume you're using concrete subclasses such as `TypeWrapper extends EnumWrapper`). – Thomas Mar 15 '21 at 15:48
  • And btw: "if you answer my question is better." - you're asking for help here and even though struggling with a problem can be frustrating it would be good to stay polite and try to work through the problem. In some cases the question is "I need X" but you actually want to achieve Y so the answer should not (and sometimes can't) be "here's how you solve X" but better "here's how you achieve Y in a better way than you thought (or even in a way that works)" - so please bear with the comments and additional questions. – Thomas Mar 15 '21 at 15:51
  • I Edited my Question for clear. But I dont understand your answers ! And I think about How can I rate your answer? Because it is not possible to rate the comment – mehnet ali Mar 15 '21 at 15:57
  • 1
    It's not much clearer since you only added enums. How are you trying to use that code? Can you provide examples? I assume you have something like `GenderWrapper extends EnumWrapper` - is that correct? Would you then need to call `getType()` and get `"Gender"`? – Thomas Mar 15 '21 at 16:00
  • `Gender.OTHER.name()`? – Johannes Kuhn Mar 15 '21 at 16:05
  • I want to get "Gender" or "Language" only from " getType() " – mehnet ali Mar 15 '21 at 16:06
  • In short: Ask the field for the Type. – Johannes Kuhn Mar 15 '21 at 16:33
  • I think the Question is so clear , but I don't understand your comments – mehnet ali Mar 15 '21 at 16:36
  • Ok, now it's clear. And the answer is: You can't (yet). – Johannes Kuhn Mar 15 '21 at 19:24

1 Answers1

1

I'm not sure if this is what you want, but it follows the call style of call you indicated above:

EnumWrapper<Gender> ew = new EnumWrapper<>();
String nameEnum = ew.getType();

This Wrapper class returns the full package name of any parameter type (class or enum). It works using the variable args list, you don't need to supply any values to the constructor it as works off the empty array supplied by the VM:

public class Wrapper<T> {
    private String type;
    public Wrapper(T...args) {
        type = args.getClass().getComponentType().getTypeName();
    }

    String getType() {
        return type;
    }

    public static void main(String[] args) {
        System.out.println(new Wrapper<String>().getType());
        System.out.println(new Wrapper<Gender>().getType());
        System.out.println(new Wrapper<Language>().getType());
    }
}
... prints
java.lang.String
Gender
Language
DuncG
  • 12,137
  • 2
  • 21
  • 33
  • thanks, but I need without make object can do it. thanks – mehnet ali Mar 15 '21 at 19:34
  • Please add more detail to your question, with examples of how you expect the class to work as you say don't want a new object yet your code declares a class and suggests it has getType call to be called as `result = new EnumWrapper<...>().getType()`. – DuncG Mar 16 '21 at 09:11