/*
 * Copyright 2006-2009 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.tree.impl.ast;

import javax.el.ELContext;
import javax.el.ELException;
import javax.el.MethodInfo;
import javax.el.MethodNotFoundException;
import javax.el.PropertyNotFoundException;
import javax.el.ValueReference;

import de.odysseus.el.misc.LocalMessages;
import de.odysseus.el.tree.Bindings;
import de.odysseus.el.tree.Node;

public class AstMethod extends AstNode {
	private final AstProperty property;
	private final AstParameters params;
	
	public AstMethod(AstProperty property, AstParameters params) {
		this.property = property;
		this.params = params;
	}

	public boolean isLiteralText() {
		return false;
	}

	public Class<?> getType(Bindings bindings, ELContext context) {
		return null;
	}

	public boolean isReadOnly(Bindings bindings, ELContext context) {
		return true;
	}

	public void setValue(Bindings bindings, ELContext context, Object value) {
		throw new ELException(LocalMessages.get("error.value.set.rvalue", getStructuralId(bindings)));
	}

	public MethodInfo getMethodInfo(Bindings bindings, ELContext context, Class<?> returnType, Class<?>[] paramTypes) {
		return null;
	}

	public boolean isLeftValue() {
		return false;
	}
	
	public boolean isMethodInvocation() {
		return true;
	}
	
	public final ValueReference getValueReference(Bindings bindings, ELContext context) {
		return null;
	}

	@Override
	public void appendStructure(StringBuilder builder, Bindings bindings) {
		property.appendStructure(builder, bindings);
		params.appendStructure(builder, bindings);
	}

	@Override
	public Object eval(Bindings bindings, ELContext context) {
		Object base = property.getPrefix().eval(bindings, context);
		if (base == null) {
			throw new PropertyNotFoundException(LocalMessages.get("error.property.base.null", property.getPrefix()));
		}
		Object method = property.getProperty(bindings, context);
		if (method == null) {
			throw new PropertyNotFoundException(LocalMessages.get("error.property.method.notfound", "null", base));
		}
		String name = bindings.convert(method, String.class);

		context.setPropertyResolved(false);
		Object result = context.getELResolver().invoke(context, base, name, null, params.eval(bindings, context));
		if (!context.isPropertyResolved()) {
			throw new MethodNotFoundException(LocalMessages.get("error.property.method.notfound", name, base.getClass()));
		}
		return result;
	}

	public Object invoke(Bindings bindings, ELContext context, Class<?> returnType, Class<?>[] paramTypes, Object[] paramValues) {
		/*
		 * ignore returnType, paramTypes and paramValues
		 */
		return eval(bindings, context);
	}

	public int getCardinality() {
		return 2;
	}

	public Node getChild(int i) {
		return i == 0 ? property : i == 1 ? params : null;
	}

	@Override
	public String toString() {
		return "<method>";
	}
}

@@ -2205,34 +2205,25 @@
 %0A%09%7D%0D%0A%0D%0A%09
-@Override%0D%0A%09public
+protected
  Object 
@@ -2259,24 +2259,56 @@
 text context
+, boolean answerNullIfBaseIsNull
 ) %7B%0D%0A%09%09Objec
@@ -2378,32 +2378,90 @@
 ase == null) %7B%0D%0A
+%09%09%09if (answerNullIfBaseIsNull) %7B%0D%0A%09%09%09%09return null;%0D%0A%09%09%09%7D%0D%0A
 %09%09%09throw new Pro
@@ -3141,32 +3141,152 @@
  result;%0D%0A%09%7D%0D%0A%0D%0A
+%09@Override%0D%0A%09public Object eval(Bindings bindings, ELContext context) %7B%0D%0A%09%09return eval(bindings, context, true);%0D%0A%09%7D%0D%0A%0D%0A
 %09public Object i
@@ -3402,73 +3402,8 @@
  %7B%0D%0A
-%09%09/*%0D%0A%09%09 * ignore returnType, paramTypes and paramValues%0D%0A%09%09 */%0D%0A
 %09%09re
@@ -3425,24 +3425,31 @@
 ngs, context
+, false
 );%0D%0A%09%7D%0D%0A%0D%0A%09p

fix bug #3154206: accept if base == null when evaluated via getValue()