/**
 * MVEL (The MVFLEX Expression Language)
 *
 * Copyright (C) 2007 Christopher Brock, MVFLEX/Valhalla Project and the Codehaus
 *
 * 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 org.mvel2.optimizers.impl.refl.nodes;

import org.mvel2.CompileException;
import static org.mvel2.util.ParseTools.getBestCandidate;
import static org.mvel2.MVEL.getProperty;
import org.mvel2.compiler.AccessorNode;
import org.mvel2.integration.VariableResolverFactory;

import java.lang.reflect.Method;

public class GetterAccessor implements AccessorNode {
    private AccessorNode nextNode;
    private final Method method;

    public static final Object[] EMPTY = new Object[0];

    public Object getValue(Object ctx, Object elCtx, VariableResolverFactory vars) {
        try {
            if (nextNode != null) {
                return nextNode.getValue(method.invoke(ctx, EMPTY), elCtx, vars);
            }
            else {
                return method.invoke(ctx, EMPTY);
            }
        }
        catch (IllegalArgumentException e) {
            if (ctx != null && method.getDeclaringClass() != ctx.getClass()) {
                Method o = getBestCandidate(EMPTY, method.getName(), ctx.getClass(), ctx.getClass().getMethods(), true);
                if (o != null) {
                    return executeOverrideTarget(o, ctx, elCtx, vars);
                }
            }

            /**
             * HACK: Try to access this another way.
             */
            if (nextNode != null) {
                return nextNode.getValue(getProperty(method.getName() + "()", ctx), elCtx, vars);
            }
            else {
                return getProperty(method.getName() + "()", ctx);
            }
        }
        catch (Exception e) {
            throw new CompileException("cannot invoke getter: " + method.getName()
                    + " [declr.class: " + method.getDeclaringClass().getName() + "; act.class: "
                    + (ctx != null ? ctx.getClass().getName() : "null") + "]", e);
        }
    }

    public GetterAccessor(Method method) {
        this.method = method;
    }

    public Method getMethod() {
        return method;
    }

    public AccessorNode setNextNode(AccessorNode nextNode) {
        return this.nextNode = nextNode;
    }

    public AccessorNode getNextNode() {
        return nextNode;
    }

    public String toString() {
        return method.getDeclaringClass().getName() + "." + method.getName();
    }

    public Object setValue(Object ctx, Object elCtx, VariableResolverFactory vars, Object value) {
        try {
            if (nextNode != null) {
                return nextNode.setValue(method.invoke(ctx, EMPTY), elCtx, vars, value);
            }
            else {
                throw new CompileException("bad payload");
            }
        }
        catch (IllegalArgumentException e) {
            /**
             * HACK: Try to access this another way.
             */

            if (nextNode != null) {
                return nextNode.setValue(getProperty(method.getName() + "()", ctx), elCtx, vars, value);
            }
            else {
                return getProperty(method.getName() + "()", ctx);
            }
        }
        catch (CompileException e) {
            throw e;
        }
        catch (Exception e) {
            throw new CompileException("error " + method.getName() + ": " + e.getClass().getName() + ":" + e.getMessage(), e);
        }
    }

    public Class getKnownEgressType() {
        return method.getReturnType();
    }

    private Object executeOverrideTarget(Method o, Object ctx, Object elCtx, VariableResolverFactory vars) {
        try {
            if (nextNode != null) {
                return nextNode.getValue(o.invoke(ctx, EMPTY), elCtx, vars);
            }
            else {
                return o.invoke(ctx, EMPTY);
            }
        }
        catch (Exception e2) {
            throw new CompileException("unable to invoke method", e2);
        }
    }
}

@@ -775,66 +775,8 @@
 on;%0A
-import static org.mvel2.util.ParseTools.getBestCandidate;%0A
 impo
@@ -906,16 +906,74 @@
 Factory;
+%0Aimport static org.mvel2.util.ParseTools.getBestCandidate;
 %0A%0Aimport
@@ -2220,32 +2220,470 @@
     %7D%0A        %7D%0A
+        catch (NullPointerException e) %7B%0A            if (ctx == null) %7B%0A                throw new CompileException(%22unable to invoke method: %22 + method.getDeclaringClass().getName() + %22.%22 + method.getName() + %22: %22 +%0A                        %22target of method is null%22, e);%0A            %7D%0A            else %7B%0A                throw new CompileException(%22cannot invoke getter: %22 + method.getName() + %22 (see trace)%22, e);%0A            %7D%0A        %7D%0A
         catch (E
@@ -2952,16 +2952,28 @@
 l%22) + %22%5D
+ (see trace)
 %22, e);%0A 

update error message