Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Java reflection when the enum type is a Class?

Tags:

I was using an enum in which the constant was a Class. I needed to invoke a method on the constant but could not introduce a compile time dependency and the enum was not always available at runtime (part of optional install). Therefore, I wanted to use reflection.

This is easy, but I hadn't used reflection with enums before.

The enum looked something like this:

public enum PropertyEnum {    SYSTEM_PROPERTY_ONE("property.one.name", "property.one.value"),    SYSTEM_PROPERTY_TWO("property.two.name", "property.two.value");    private String name;      private String defaultValue;    PropertyEnum(String name) {     this.name = name;   }    PropertyEnum(String name, String value) {     this.name = name;     this.defaultValue = value;   }     public String getName() {     return name;   }    public String getValue() {     return System.getProperty(name);   }    public String getDefaultValue() {     return defaultValue;   }    } 

What is an example of invoking a method of the constant using reflection?

like image 279
David G Avatar asked Sep 26 '08 16:09

David G


People also ask

Can enum be a class Java?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).

How do you view enums outside class?

You have to declare enum as public ... (depend on what you want) and then use class name as prefix when outside package. @RongNK - It doesn't have to be public ; if its in the same package, it can have default access. You need to use the class name as a prefix even within the same package unless you import the enum.

Can you have an enum in a class?

Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.

What code can you write to get an instance of this enum?

Enum valueOf() An enum class automatically gets a static valueOf() method in the class when compiled. The valueOf() method can be used to obtain an instance of the enum class for a given String value. Here is an example: Level level = Level.


1 Answers

import java.lang.reflect.Method;  class EnumReflection {    public static void main(String[] args)     throws Exception   {     Class<?> clz = Class.forName("test.PropertyEnum");     /* Use method added in Java 1.5. */     Object[] consts = clz.getEnumConstants();     /* Enum constants are in order of declaration. */     Class<?> sub = consts[0].getClass();     Method mth = sub.getDeclaredMethod("getDefaultValue");     String val = (String) mth.invoke(consts[0]);     /* Prove it worked. */     System.out.println("getDefaultValue " +        val.equals(PropertyEnum.SYSTEM_PROPERTY_ONE.getDefaultValue()));   }  } 
like image 190
David G Avatar answered Sep 29 '22 12:09

David G