Enums and Annotations - java

I want to use an Annotation in compile-safe form.
To pass the value() to the Annotation i want to use the String representation of an enum.
Is there a way to use #A with a value from enum E ?
public class T {
public enum E {
a,b;
}
// C1: i want this, but it won't compile
#A(E.a)
void bar() {
// C2: no chance, it won't compile
#A(E.a.toString())
void bar2() {
}
// C3: this is ok
#A("a"+"b")
void bar3() {
}
// C4: is constant like C3, is'nt it ?
#A(""+E.a)
void bar4() {
}
}
#interface A {
String value();
}
Update
I need the String type in #A.
The point is i can do this
#A("" + 1)
void foo() {
}
But here the compiler claims "attribute value must be constant". Is'nt E.a constant ?
#A("" + E.a)
void foo() {
}

The problem is that you're smarter than the compiler :-)
E.a is a constant, but E.a.toString() is not. It looks like it should be, but the compiler can't figure that out.
The reason why "a"+"b" and "" + 1 work is that the compiler is smart enough to generate the constants at compile time.
When it sees "" + E.a, it uses E.a.toString(). The call to toString() is enough to throw it off.
Does E have to be an enum? You could try:
public final class E {
public static final String a = "a";
public static final String b = "b";
};

Make the value in the annotation of type E:
#interface A {
E value();
}
Then you can use
#A(E.a)

Related

How to get value by Java enum class name and field name

How to get value by Java enum class name and field name?
The sample code is as follows, but I don't know how to pass the enum class as a parameter.
public enum ErrorCodes1{
OK(0),
NOT_EXIST_USER(1),
FAIL_TO_SEND_MAIL(2),
...
}
public enum ErrorCodes2{
OK(0),
NOT_EXIST_USER(1),
FAIL_TO_SEND_MESSA(2),
...
}
public void foo1()
{
foo2(ErrorCodes1.class, "NOT_EXIST_USER");
foo2(ErrorCodes2.class, "NOT_EXIST_USER");
}
public void foo2(Enum EnumClass, String EnumText)
{
int code = xxxx; //I want to get code(1) via EnumText and EnumClass, but I don't know how to do it.
}
You can use the type Class as a param of your function foo2
public static void foo2(Class<?> enumClass, String enumText) {
int code = -1; // I want to get code(1) via EnumText and EnumClass, but I don't know how to do
// it.
switch (enumClass.getCanonicalName()) {
case "ErrorCodes2": {
ErrorCodes1 errorCode = ErrorCodes1.valueOf(enumText);
code = errorCode.ordinal();
}
case "ErrorCodes1": {
ErrorCodes1 errorCode = ErrorCodes1.valueOf(enumText);
code = errorCode.ordinal();
}
}
System.out.println(code);
}
After that, you can use valueOf to instantiate your enum from a string that contains the enum value. I don't think this is the best solution but its works.
PS: The param of a function begins with a lowercase letter.
Option A, simple: check for the type explicitly.
int code;
if (enumClass instanceof ErrorCodes1) {
code = ((ErrorCodes1) enumClass).valueOf(enumText).ordinal();
} else if (enumClass instanceof ErrorCodes2)
// repeat
This implies you can pass an error object itself, not necessarily its class. Unfortunately, switch does not work with Class type.
Option B, probably overkill: use Reflection.
public void foo2(Class<?> enumClass, String enumText) {
try{
Object resultingEnum = enumClass.getMethod("valueOf", String.class).invoke(null, enumText);
int code = (Integer) resultingEnum.getClass().getMethod("ordinal").invoke(resultingEnum);
// ...
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e ) {
// ignore: enumClass is not an expected class
}
}
This will suit you in case you have an unlimited number of ErrorCode enums, though it is a barely plausible situation.

Java switch statement get to default on enum

I have a Java (for Android) code similar to this one:
enum MyEnum {
A, B, C
}
String f(MyEnum e) {
if (e == null) {
return null;
}
switch(e) {
case A: return "AA";
case B: return "BB";
case C: return "CC";
default: throw new IllegalStateException("invalid enum");
}
}
and I got the exception in the default clause thrown once! Can somebody explain if this is theoretically possible and how?
For example in C++ you can have an enum variable which value is non of the declared enum values, but I guess in Java you cannot do that, correct me if I am wrong.
I dont see how this could fail, but i would propose refactoring your enum to this:
enum MyEnum {
A("AA"),
B("BB"),
C("CC");
private final String value;
public MyEnum(String value){
this.value = value;
}
public String f(){
return value;
}
}
now you can still do the same operations, but it 100% safe to add new enums
public void foo(MyEnum enum){
System.out.println(enum.f());
}

Is it possible to extend enum in Java 8?

Just playing and came up with a sweet way to add functionality to enums in Java Enum toString() method with this.
Some further tinkering allowed me to nearly also add a tidy (i.e. not throwing an exception) reverse look-up but there's a problem. It's reporting:
error: valueOf(String) in X cannot implement valueOf(String) in HasValue
public enum X implements PoliteEnum, ReverseLookup {
overriding method is static
Is there a way?
The aim here is to silently add (via an interface implementation with a default method like I added politeName in the linked answer) a lookup method that does the valueOf function without throwing an exception. Is it possible? It is clearly now possible to extend enum - one of my major problems with Java until now.
Here's my failed attempt:
public interface HasName {
public String name();
}
public interface PoliteEnum extends HasName {
default String politeName() {
return name().replace("_", " ");
}
}
public interface Lookup<P, Q> {
public Q lookup(P p);
}
public interface HasValue {
HasValue valueOf(String name);
}
public interface ReverseLookup extends HasValue, Lookup<String, HasValue> {
#Override
default HasValue lookup(String from) {
try {
return valueOf(from);
} catch (IllegalArgumentException e) {
return null;
}
}
}
public enum X implements PoliteEnum/* NOT ALLOWED :( , ReverseLookup*/ {
A_For_Ism, B_For_Mutton, C_Forth_Highlanders;
}
public void test() {
// Test the politeName
for (X x : X.values()) {
System.out.println(x.politeName());
}
// ToDo: Test lookup
}
You are over-complicating your design. If you are willing to accept that you can invoke a default method on an instance only, there entire code may look like this:
interface ReverseLookupSupport<E extends Enum<E>> {
Class<E> getDeclaringClass();
default E lookup(String name) {
try {
return Enum.valueOf(getDeclaringClass(), name);
} catch(IllegalArgumentException ex) { return null; }
}
}
enum Test implements ReverseLookupSupport<Test> {
FOO, BAR
}
You can test it with:
Test foo=Test.FOO;
Test bar=foo.lookup("BAR"), baz=foo.lookup("BAZ");
System.out.println(bar+" "+baz);
An non-throwing/catching alternative would be:
interface ReverseLookupSupport<E extends Enum<E>> {
Class<E> getDeclaringClass();
default Optional<E> lookup(String name) {
return Stream.of(getDeclaringClass().getEnumConstants())
.filter(e->e.name().equals(name)).findFirst();
}
to use like:
Test foo=Test.FOO;
Test bar=foo.lookup("BAR").orElse(null), baz=foo.lookup("BAZ").orElse(null);
System.out.println(bar+" "+baz);
Here, there's basically two points. Specifically the reason it doesn't compile is 8.4.8.1:
It is a compile-time error if an instance method overrides a static method.
In other words, an enum can't implement HasValue because of the name clash.
Then there's the more general issue we have which is that static methods just cannot be 'overridden'. Since valueOf is a static method inserted by the compiler on the Enum-derived class itself, there's no way to change it. We also can't use interfaces to solve it since they do not have static methods.
In this specific case it's a place where composition can make this kind of thing less repetetive, for example:
public class ValueOfHelper<E extends Enum<E>> {
private final Map<String, E> map = new HashMap<String, E>();
public ValueOfHelper(Class<E> cls) {
for(E e : EnumSet.allOf(cls))
map.put(e.name(), e);
}
public E valueOfOrNull(String name) {
return map.get(name);
}
}
public enum Composed {
A, B, C;
private static final ValueOfHelper<Composed> HELPER = (
new ValueOfHelper<Composed>(Composed.class)
);
public static Composed valueOfOrNull(String name) {
return HELPER.valueOfOrNull(name);
}
}
(Plus, I'd recommend that over catching the exception anyway.)
I realize "you can't do it" is not really a desirable answer but I don't see a way around it due to the static aspect.
The case is the same as you can not create default toString() in interface. The enum already contains signature for static valueOf(String) method therefore you can not override it.
The enum are compile time constant and because of that it really doubtful that they will be extensible someday.
If you want to get the constant via name you can use this:
public static <E extends Enum<E>> Optional<E> valueFor(Class<E> type, String name) {
return Arrays.stream(type.getEnumConstants()).filter( x -> x.name().equals(name)).findFirst();
}
I think I have an answer - it's hacky and uses reflection but seems to fit the brief - i.e. reverse lookup without methods in the enum and without throwing exception.
public interface HasName {
public String name();
}
public interface PoliteEnum extends HasName {
default String politeName() {
return name().replace("_", " ");
}
}
public interface Lookup<P, Q> {
public Q lookup(P p);
}
public interface ReverseLookup<T extends Enum<T>> extends Lookup<String, T> {
#Override
default T lookup(String s) {
return (T) useMap(this, s);
}
}
// Probably do somethiong better than this in the final version.
static final Map<String, Enum> theMap = new HashMap<>();
static Enum useMap(Object o, String s) {
if (theMap.isEmpty()) {
try {
// Yukk!!
Enum it = (Enum)o;
Class c = it.getDeclaringClass();
// Reflect to call the static method.
Method method = c.getMethod("values");
// Yukk!!
Enum[] enums = (Enum[])method.invoke(null);
// Walk the enums.
for ( Enum e : enums) {
theMap.put(e.name(), e);
}
} catch (Exception ex) {
// Ewwww
}
}
return theMap.get(s);
}
public enum X implements PoliteEnum, ReverseLookup<X> {
A_For_Ism,
B_For_Mutton,
C_Forth_Highlanders;
}
public void test() {
for (X x : X.values()) {
System.out.println(x.politeName());
}
for (X x : X.values()) {
System.out.println(x.lookup(x.name()));
}
}
prints
A For Ism
B For Mutton
C Forth Highlanders
A_For_Ism
B_For_Mutton
C_Forth_Highlanders
Added
Inspired by #Holger - this is what I feel is most like what I was looking for:
public interface ReverseLookup<E extends Enum<E>> extends Lookup<String, E> {
// Map of all classes that have lookups.
Map<Class, Map<String, Enum>> lookups = new ConcurrentHashMap<>();
// What I need from the Enum.
Class<E> getDeclaringClass();
#Override
default E lookup(String name) throws InterruptedException, ExecutionException {
// What class.
Class<E> c = getDeclaringClass();
// Get the map.
final Map<String, Enum> lookup = lookups.computeIfAbsent(c,
k -> Stream.of(c.getEnumConstants())
// Roll each enum into the lookup.
.collect(Collectors.toMap(Enum::name, Function.identity())));
// Look it up.
return c.cast(lookup.get(name));
}
}
// Use the above interfaces to add to the enum.
public enum X implements PoliteName, ReverseLookup<X> {
A_For_Ism,
B_For_Mutton,
C_Forth_Highlanders;
}

java enum receive values from string name

I have enum like:
public enum Enum2
{
ONE,TWO,THREE;
}
I can list all values like:
public static void main(String... args)
{
for (Enum2 e : Enum2.values())
{
System.out.println(e);
}
}
Is it possible list values if I have only string name of Enum?
String enum_name="Enum2";
E.g. if in some logic like:
if (a>b)
{
enum_name="EnumA";
}
else
{
enum_name="EnumB";
}
And after I receive string name of enum - I can list all values.
Class<?> enumClazz = Class.forName("com.mycompany.Enum2");
for (Enum<?> e : ((Class<? extends Enum<?>>)enumClazz).getEnumConstants()) {
System.out.println(e.name()); // The variable "e" would be Enum2.ONE, etc
}
Thank you #Harry for helping me get this right.
Your question is not much clear to be but this is what you may want to do
Class<?> cls = Class.forName("EnumName");
if (cls.isEnum()) {
Field[] flds = cls.getDeclaredFields();
//-- your logic for fields.
}
You can use: Class.getEnumConstants(). For more see this.
yes, with
Enum2.EnumA.toString();

enum inheritance, or something similar

I have a string (which is a message) that I get as input and I need to do one of 4 possible things depending on the string
I know that there is eunm.valueOf() option, but I have 4 different enums, each with few possible messages.
looks something like:
public enum first{ONE,TWO,THREE};
public enum second{FOUR,FIVE,SIX};
public enum third{SEVEN,EIGHT,NINE};
public void work(String message){
//Here I want to compare message string to one of the 3 enums
}
is it possible to do this in one method of the enum?
or should I just try to create one, and if I get an exception try the other and so on?
As others have commented, it may be better to think through whether you really need 4 distinct enums.
But if you do, you could have them implement a common interface. Then you can map the input strings to the appropriate enum member, and call its method to accomplish what you want. Something like
public interface SomeInterface {
void doSomething();
};
public enum First implements SomeInterface {
ONE,TWO,THREE;
#Override
public void doSomething() { ... }
};
...
Map<String, SomeInterface> myMap = new HashMap<String, SomeInterface>();
for (First item : First.values()) {
myMap.put(item.toString(), item);
}
...
public void work(String message){
SomeInterface obj = myMap.get(message);
if (obj != null) {
obj.doSomething();
}
}
This assumes that the 4 possible things you want to do correspond to the 4 enums. If not, you can override the method separately for each and any enum member too, e.g.
public enum First implements SomeInterface {
ONE,
TWO {
#Override
public void doSomething() { // do something specific to TWO }
},
THREE;
#Override
public void doSomething() { // general solution for all values of First }
};
Enumerations in Java are full blown classes. Individual values can even override the behavior to meet their needs. It's pretty cool. You can use this to your advantage:
public enum Value implements Worker
{
ONE,
TWO,
THREE
{
#Override
public void doWork(String message)
{
// overrides behavior of base enum
}
},
FOUR,
/* ... */,
NINE;
private final String message;
Value() { this(""); }
Value(String message) { this.message = message; }
public void doWork(String message)
{
if (this.message.equals(message))
{
/* ... */
}
}
}
public interface Worker
{
void doWork(String message);
}
You can create a Map of them all
static final Map<String, Enum> enumMap = new LinkedHashMap<String, Enum>(){{
for(First e: First.values()) put(e.name(), e);
for(Second e: Second.values()) put(e.name(), e);
for(Third e: Third.values()) put(e.name(), e);
}};
Enum e = enumMap.get(name);
What you're really looking for is a aggregation of the other enums. The easiest way to get that is to make a new enum that puts all of those choices in a new enum. Something to this effect:
public enum Combination {
NEWONE(first.ONE), NEWTWO(first.TWO), NEWTHREE(first.THREE),
NEWFOUR(second.FOUR), NEWFIVE(second.FIVE), NEWSIX(second.SIX),
NEWSEVEN(third.SEVEN), NEWEIGHT(third.EIGHT), NEWNINE(third.NINE);
private String contents;
public Combination(first f) {
contents = f.toString();
}
public Combination(second s) {
contents = s.toString();
}
public Combination(third t) {
contents = t.toString();
}
public String toString() {
return contents;
}
}
This will more correctly aggregate the previous enums into a single data structure.
Even given your odd/even example in the comments, I don't feel multiple enums are the way to go here. I would use something like (warning, untested):
public enum Numbers {
ONE("first"), TWO("first"), THREE("first"), FOUR("second"), FIVE("second"), SIX("second"), SEVEN("third"), EIGHT("third"), NINE("third")
private String type;
Numbers(String t) { this.type = t; }
String getType { return this.type; }
}
Then you can use valueOf() to look up the enum element, and getType() to find out which of your three categories it belongs to.
It isn't entirely clear what you are asking, but perhaps you want to define a mapping between strings and constants, like this:
enum Type { FIRST, SECOND, THIRD };
Map<String, Type> mapping = new HashSet<String, Type>(){{
put("ONE", Type.FIRST);
put("TWO", Type.FIRST);
//...
put("NINE", Type.THIRD);
}};
public Type getTypeFromString(String s) {
return mapping.get(s);
}

Categories

Resources