/*
 * Copyright 2003-2009 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.classgen.asm.sc;

import org.codehaus.groovy.ast.ConstructorNode;
import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
import org.codehaus.groovy.ast.expr.TupleExpression;
import org.codehaus.groovy.classgen.asm.InvocationWriter;
import org.codehaus.groovy.classgen.asm.WriterController;
import org.codehaus.groovy.transform.StaticTypesTransformation;

public class StaticInvocationWriter extends InvocationWriter {
    public StaticInvocationWriter(WriterController wc) {
        super(wc);
    }

    @Override
    public void writeInvokeConstructor(final ConstructorCallExpression call) {
        ConstructorNode cn = (ConstructorNode) call.getNodeMetaData(StaticTypesTransformation.StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
        if (cn==null) {
            super.writeInvokeConstructor(call);
            return;
        }

        String ownerDescriptor = prepareConstructorCall(cn);
        TupleExpression args = makeArgumentList(call.getArguments());
        loadArguments(args.getExpressions(), cn.getParameters());
        finnishConstructorCall(cn, ownerDescriptor, args.getExpressions().size());

    }
}

@@ -707,16 +707,59 @@
 orNode;%0A
+import org.codehaus.groovy.ast.MethodNode;%0A
 import o
@@ -1298,41 +1298,31 @@
     
-Constructor
+Method
 Node 
-c
+m
 n = (
-Constructor
+Method
 Node
@@ -1428,17 +1428,17 @@
     if (
-c
+m
 n==null)
@@ -1513,24 +1513,332 @@
 n;%0A        %7D
+%0A        ConstructorNode cn;%0A        if (mn instanceof ConstructorNode) %7B%0A            cn = (ConstructorNode) mn;%0A        %7D else %7B%0A            cn = new ConstructorNode(mn.getModifiers(), mn.getParameters(), mn.getExceptions(), mn.getCode());%0A            cn.setDeclaringClass(mn.getDeclaringClass());%0A        %7D
 %0A%0A        St

Handle case where method node in metadata is not a constructor node