/*
 * Copyright 2003-2012 the original author or authors.
 *
 * 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.codehaus.groovy.runtime.callsite;

import java.util.Map;
import java.util.Map.Entry;

import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;

import groovy.lang.Closure;

/**
 * Helper class for internal use only. This allows to call a {@link Closure} and 
 * convert the result to a boolean. It will do this by caching the possible "doCall"
 * as well as the "asBoolean" in CallSiteArray fashion. "asBoolean" will not be 
 * called if the result is null or a Boolean. In case of null we return false and
 * in case of a Boolean we simply unbox. This logic is designed after the one present 
 * in {@link DefaultTypeTransformation#castToBoolean(Object)}. The purpose of 
 * this class is to avoid the slow "asBoolean" call in that method.
 * @author <a href="mailto:blackdrag@gmx.org">Jochen "blackdrag" Theodorou</a>
 *
 */
public class BooleanClosureWrapper {
    private final CallSiteArray csa = new CallSiteArray(BooleanClosureWrapper.class, new String[]{"call", "asBoolean"});
    private final Closure wrapped;
    private final int numberOfArguments;
    
    public BooleanClosureWrapper(Closure wrapped) {
        this.wrapped = wrapped;
        numberOfArguments = wrapped.getMaximumNumberOfParameters();
    }

    /**
     * normal closure call
     */
    public boolean call(Object... args) {
        try {
            // make cached call to doCall:
            Object ret = csa.array[0].call(wrapped, args);
            // handle conversion to boolean
            if (ret == null) return false;
            if (ret instanceof Boolean) {
                return ((Boolean) ret).booleanValue();
            }
            // it was not null and not boolean, so call asBoolean
            ret = csa.array[1].call(ret, CallSiteArray.NOPARAM);
            return ((Boolean) ret).booleanValue();
        } catch (Throwable t) {
            sun.misc.Unsafe.getUnsafe().throwException(t);
            return false;
        }
    }
    
    /**
     * Bridge for a call based on a map entry. If the call is done on a {@link Closure}
     * taking one argument, then we give in the {@link Entry}, otherwise we will
     * give in the key and value.
     */
    public <K,V> boolean callForMap(Map.Entry<K, V> entry) {
        if (numberOfArguments==2) {
            return call(entry.getKey(), entry.getValue());
        } else {
            return call(entry);
        }
    }
}

@@ -711,16 +711,67 @@
 Entry;%0A%0A
+import org.codehaus.groovy.runtime.ExceptionUtils;%0A
 import o
@@ -2548,50 +2548,154 @@
     
-sun.misc.Unsafe.getUnsafe().throwException
+// ExceptionUtils is a bytecode generated helper class %0A            // to allow throwing checked exceptions%0A            ExceptionUtils.sneakyThrow
 (t);

add sneaky exception thrower usage