/*
   Copyright 2009-2012 Attila Szegedi

   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.dynalang.dynalink.beans;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

import org.dynalang.dynalink.CallSiteDescriptor;
import org.dynalang.dynalink.linker.GuardedInvocation;
import org.dynalang.dynalink.linker.LinkRequest;
import org.dynalang.dynalink.linker.LinkerServices;
import org.dynalang.dynalink.linker.TypeBasedGuardingDynamicLinker;
import org.dynalang.dynalink.support.Guards;

/**
 * Simple linker that implements the "dyn:call" operation for {@link DynamicMethod} objects - the objects returned by
 * "dyn:getMethod" from {@link AbstractJavaLinker}.
 */
class DynamicMethodLinker implements TypeBasedGuardingDynamicLinker {
    @Override
    public boolean canLinkType(Class<?> type) {
        return DynamicMethod.class.isAssignableFrom(type);
    };

    @Override
    public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, LinkerServices linkerServices) {
        final Object receiver = linkRequest.getReceiver();
        if(!(receiver instanceof DynamicMethod)) {
            return null;
        }
        final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
        if(desc.getNameTokenCount() != 2 && desc.getNameToken(CallSiteDescriptor.SCHEME) != "dyn")  {
            return null;
        }
        final String operator = desc.getNameToken(CallSiteDescriptor.OPERATOR);
        if(operator == "call") {
            final MethodType type = desc.getMethodType();
            return new GuardedInvocation(MethodHandles.dropArguments(((DynamicMethod)receiver).getInvocation(
                    type.dropParameterTypes(0, 1), linkerServices), 0, type.parameterType(0)), Guards.getIdentityGuard(
                            receiver));
        }
        return null;
    }
}

@@ -629,16 +629,54 @@
 beans;%0A%0A
+import java.lang.invoke.MethodHandle;%0A
 import j
@@ -2107,65 +2107,40 @@
     
-return new GuardedInvocation(MethodHandles.dropArguments(
+final MethodHandle invocation = 
 ((Dy
@@ -2175,37 +2175,16 @@
 ocation(
-%0A                    
 type.dro
@@ -2205,32 +2205,213 @@
 s(0, 1),
- linkerServices)
+%0A                    linkerServices);%0A            if(invocation == null) %7B%0A                return null;%0A            %7D%0A            return new GuardedInvocation(MethodHandles.dropArguments(invocation
 , 0, typ
@@ -2430,16 +2430,36 @@
 ype(0)),
+%0A                   
  Guards.
@@ -2479,37 +2479,8 @@
 ard(
-%0A                            
 rece

Fix for getting a NPE when trying to invoke a dynamic method on incorrect number of arguments