/*
 * Copyright 2015 Michael Rozumyanskiy
 *
 * 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 io.michaelrocks.lightsaber.processor.signature;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.objectweb.asm.Type;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MethodSignature {
    private final TypeSignature returnType;
    private final List<TypeSignature> argumentTypes;

    public MethodSignature(final Type methodType) {
        this.returnType = getReturnTypeSignature(methodType);
        this.argumentTypes = Collections.unmodifiableList(getArgumentTypesSignatures(methodType));
    }

    public MethodSignature(final TypeSignature returnType, final List<TypeSignature> argumentTypes) {
        this.returnType = returnType;
        this.argumentTypes = Collections.unmodifiableList(new ArrayList<>(argumentTypes));
    }

    private static TypeSignature getReturnTypeSignature(final Type methodType) {
        return new TypeSignature(methodType.getReturnType());
    }

    private static List<TypeSignature> getArgumentTypesSignatures(final Type methodType) {
        final ArrayList<TypeSignature> argumentTypes = new ArrayList<>();
        for (final Type argumentType : methodType.getArgumentTypes()) {
            argumentTypes.add(new TypeSignature(argumentType));
        }
        return argumentTypes;
    }

    @Override
    public boolean equals(final Object object) {
        if (this == object) {
            return true;
        }

        if (object == null || getClass() != object.getClass()) {
            return false;
        }

        final MethodSignature that = (MethodSignature) object;
        return new EqualsBuilder()
                .append(returnType, that.returnType)
                .append(argumentTypes, that.argumentTypes)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(returnType)
                .append(argumentTypes)
                .toHashCode();
    }

    @Override
    public String toString() {
        return "(" + StringUtils.join(argumentTypes, ", ") + "): " + returnType;
    }
}

@@ -957,16 +957,51 @@
 ature %7B%0A
+    private final Type methodType;%0A
     priv
@@ -1134,32 +1134,70 @@
 e methodType) %7B%0A
+        this.methodType = methodType;%0A
         this.ret
@@ -1206,19 +1206,22 @@
 nType = 
-get
+create
 ReturnTy
@@ -1303,19 +1303,22 @@
 bleList(
-get
+create
 Argument
@@ -1452,24 +1452,92 @@
 entTypes) %7B%0A
+        this.methodType = createRawType(returnType, argumentTypes);%0A
         this
@@ -1652,32 +1652,266 @@
 Types));%0A    %7D%0A%0A
+    public Type getMethodType() %7B%0A        return methodType;%0A    %7D%0A%0A    public TypeSignature getReturnType() %7B%0A        return returnType;%0A    %7D%0A%0A    public List%3CTypeSignature%3E getArgumentTypes() %7B%0A        return argumentTypes;%0A    %7D%0A%0A
     private stat
@@ -1919,35 +1919,38 @@
 c TypeSignature 
-get
+create
 ReturnTypeSignat
@@ -2078,35 +2078,38 @@
 %3CTypeSignature%3E 
-get
+create
 ArgumentTypesSig
@@ -2390,32 +2390,499 @@
 ntTypes;%0A    %7D%0A%0A
+    private static Type createRawType(final TypeSignature returnTypeSignature,%0A            final List%3CTypeSignature%3E argumentTypesSignatures) %7B%0A        final Type%5B%5D argumentTypes = new Type%5BargumentTypesSignatures.size()%5D;%0A        for (int i = 0; i %3C argumentTypesSignatures.size(); ++i) %7B%0A            argumentTypes%5Bi%5D = argumentTypesSignatures.get(i).getRawType();%0A        %7D%0A        return Type.getMethodType(returnTypeSignature.getRawType(), argumentTypes);%0A    %7D%0A%0A
     @Override%0A  

Add more stuff to MethodSignature