1

我正在尝试使用 EL 表达式创建一个谓词,以使用提供的字符串来描述过滤器来过滤任意对象。它适用于原始类型,但由于某种原因,它会尝试访问我提供的 POJO,就好像它们是他们试图接收的类型一样。

例如,过滤器:"item.num1 > item.num2"应用于 POJO:

 private class TestClass {
        private String str;
        private int num1, num2;

        public String getStr() {
            return str;
        }

        public void setStr(String str) {
            this.str = str;
        }

        // getters/setters continue below here
 }

产生此异常:

javax.el.PropertyNotFoundException: Property 'num1' not readable on type int
    at javax.el.BeanELResolver$BeanProperty.read(BeanELResolver.java:267)
    at javax.el.BeanELResolver$BeanProperty.access$000(BeanELResolver.java:216)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:62)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:55)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
    at org.apache.el.parser.AstGreaterThan.getValue(AstGreaterThan.java:38)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
    at com.<redacted>.filters.ELPredicate.apply(ELPredicate.java:78)
    at com.google.common.collect.Iterators$7.computeNext(Unknown Source)
    at com.google.common.collect.AbstractIterator.tryToComputeNext(Unknown Source)
    at com.google.common.collect.AbstractIterator.hasNext(Unknown Source)
    at com.google.common.collect.Lists.newArrayList(Unknown Source)
    at com.google.common.collect.Lists.newArrayList(Unknown Source)
    at com.<redacted>.common.el.ELPredicateTest.pojoTest(ELPredicateTest.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我放在一起的谓词是这样的:

public class ELPredicate<T extends Object> implements Predicate<T> {
    private String expression;
    private String radix;

    /**
     * @param expression
     *            expression
     * @param radix
     *            string to be replaced with value in expression
     */
    public ELPredicate(String expression, String radix) {
        this.expression = String.format("${%s}", expression);
        this.radix = radix;
    }

    @Override
    public boolean apply(final T input) {
        final Map<String, T> map = ImmutableMap.of(radix, input);
        ELContext context = new ELContext() {

            @Override
            public VariableMapper getVariableMapper() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public FunctionMapper getFunctionMapper() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public ELResolver getELResolver() {
                return new CompositeELResolver() {
                    {
                        this.add(new AttributeELResolver((Map<String, Object>) map));
                        this.add(new BeanELResolver(true));
                        this.add(new MapELResolver(true));
                        this.add(new ListELResolver(true));
                        this.add(new ArrayELResolver(true));

                    }
                };
            }
        };

        return (boolean) ExpressionFactory.newInstance().createValueExpression(context, expression, boolean.class).getValue(context);
    }

}

AttributeELResolver 是我无法在此处粘贴的内部代码,但它只是将表达式中的字符串解析为映射中的值,它让我可以定义“项目”是什么。

4

1 回答 1

4

找到了解决方案!

事实证明,这是一个访问问题,而不是我最初认为的类型兼容性问题。将私有内部类 (TestClass) 更改为 public 使一切按预期工作。

于 2013-07-03T16:22:02.067 回答