/*
* Copyright 2006, 2007 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.el.samples.extensions;
import java.beans.FeatureDescriptor;
import java.lang.reflect.Method;
import java.util.Iterator;
import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.ExpressionFactory;
import javax.el.PropertyNotWritableException;
import javax.el.ValueExpression;
import de.odysseus.el.ExpressionFactoryImpl;
import de.odysseus.el.tree.TreeBuilder;
import de.odysseus.el.tree.TreeStore;
import de.odysseus.el.tree.impl.Builder;
import de.odysseus.el.tree.impl.Cache;
import de.odysseus.el.util.SimpleContext;
/**
* Simple method resolver to illustrate <em>JUEL</em>'s method invocation extension.
* This resolver is responsible to resolve a single method.
*
* @author Christoph Beck
*/
public class MethodInvocations {
static class MethodResolver extends ELResolver {
private final Method method;
public MethodResolver(Method method) {
this.method = method;
}
@Override
public Method getValue(ELContext context, Object base, Object prop) {
if (method.getDeclaringClass().isInstance(base) && method.getName().equals(prop.toString())) {
context.setPropertyResolved(true);
return method;
}
return null;
}
@Override
public void setValue(ELContext context, Object base, Object property, Object value) {
throw new PropertyNotWritableException();
}
@Override
public Class<?> getCommonPropertyType(ELContext context, Object base) {
return Object.class;
}
@Override
public boolean isReadOnly(ELContext context, Object base, Object property) {
return true;
}
@Override
public Class<?> getType(ELContext context, Object base, Object property) {
return null;
}
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
return null;
}
}
/**
* Sample usage: enable method invocations of <code>String.matches(String)</code>.
*/
public static void main(String... args) throws NoSuchMethodException {
// this is the method we want to invoke
Method method = String.class.getMethod("matches", String.class);
// create our customized builder
TreeBuilder builder = new Builder(Builder.Feature.METHOD_INVOCATIONS);
// create our factory which uses our customized builder
ExpressionFactory f = new ExpressionFactoryImpl(new TreeStore(builder, new Cache(10)));
// create our resolver
ELResolver resolver = new MethodResolver(method);
// create our context
ELContext context = new SimpleContext(resolver);
// let's go...
ValueExpression e = null;
e = f.createValueExpression(context, "${'foo'.matches('foo|bar')}", boolean.class);
System.out.println(e.getValue(context)); // --> true
e = f.createValueExpression(context, "${'bar'.matches('foo|bar')}", boolean.class);
System.out.println(e.getValue(context)); // --> true
e = f.createValueExpression(context, "${'baz'.matches('foo|bar')}", boolean.class);
System.out.println(e.getValue(context)); // --> false
}
}
|