Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reference a nested type in SpEL?

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.

like image 512
jyoungdev Avatar asked Mar 07 '12 04:03

jyoungdev


People also ask

What are Spel expressions?

The Spring Expression Language (SpEL for short) is a powerful expression language that supports querying and manipulating an object graph at runtime. The language syntax is similar to Unified EL but offers additional features, most notably method invocation and basic string templating functionality.

Which of the following kinds of expression Cannot be compiled by the Spring framework?

The following kinds of expression cannot be compiled at the moment: Expressions involving assignment. Expressions relying on the conversion service. Expressions using custom resolvers or accessors.


1 Answers

You have to separate the enum using a $ sign:

#{T(MyClass$NestedEnum).value1.getCode()}
like image 76
micfra Avatar answered Sep 30 '22 18:09

micfra