15

Given a class containing an enum:

public class MyClass {
    public enum NestedEnum {        
        value1(1),
        value2(2);

        private int code;

        private NestedEnum(int code) {
            this.code = code;
        }

        public int getCode() {
            return code;
        }
    }
}

how do I reference NestedEnum? This:

#{T(MyClass.NestedEnum).value1.getCode()}

results in the exception:

org.springframework.expression.spel.SpelEvaluationException: EL1005E:(pos 0): Type cannot be found 'namespace.MyClass.NestedEnum'

This:

#{T(T(MyClass).NestedEnum).value1.getCode()}

results in the exception:

org.springframework.expression.spel.SpelParseException: EL1043E:(pos 3): Unexpected token.  Expected 'rparen())' but was 'lparen(()'

I cannot think of any other good options to try.

1 Answer 1

25

You have to separate the enum using a $ sign:

#{T(MyClass$NestedEnum).value1.getCode()}
Sign up to request clarification or add additional context in comments.

2 Comments

Would it be better to specify the Type->NestedEnum change in your answer, instead? Oftentimes 'wrong' code in questions should be left alone, so that the fix in an answer can be more obvious.
This would have been a solution too, but I assume it would have impair the understandability of the answer. I also assume, it was a copy&paste error of the questioner.Neverteheless, thanks for your hint.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.