/*
 * Copyright 2018, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.code.js;

import java.util.Objects;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * A reference to a method of a Protobuf type.
 *
 * <p>The reference allows to identify a method in code
 * since it includes a type name and a method name.
 */
public final class MethodReference {

    /** The type declaring the method. */
    private final TypeName typeName;
    /** The name of the method. */
    private final String methodName;
    /** Whether the method is defined on the prototype or not. */
    private final boolean onPrototype;

    private MethodReference(TypeName typeName, String methodName, boolean onPrototype) {
        checkNotNull(methodName);
        this.typeName = typeName;
        this.methodName = methodName;
        this.onPrototype = onPrototype;
    }

    /**
     * Obtains the reference to the instance method of the specified type.
     */
    public static MethodReference onType(TypeName typeName, String methodName) {
        return new MethodReference(typeName, methodName, false);
    }

    /**
     * Obtains the reference to the static method of the specified type.
     */
    public static MethodReference onPrototype(TypeName typeName, String methodName) {
        return new MethodReference(typeName, methodName, true);
    }

    /**
     * Obtains the reference to the constructor of the specified type.
     */
    public static MethodReference constructor(TypeName name) {
        return new MethodReference(name, "", false);
    }

    /**
     * Obtains the value of the reference.
     */
    public String value() {
        if (methodName.isEmpty()) {
            return typeName.toString();
        }
        String delimiter = onPrototype
                           ? ".prototype."
                           : ".";
        return typeName + delimiter + methodName;
    }

    @Override
    public String toString() {
        return value();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof MethodReference)) {
            return false;
        }
        MethodReference reference = (MethodReference) o;
        return onPrototype == reference.onPrototype &&
                typeName.equals(reference.typeName) &&
                methodName.equals(reference.methodName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(typeName, methodName, onPrototype);
    }
}

@@ -1044,91 +1044,78 @@
 ort 
-java.util.Objects;%0A%0Aimport static com.google.common.base.Preconditions.checkNotNull
+io.spine.value.StringTypeValue;%0A%0Aimport static java.lang.String.format
 ;%0A%0A/
@@ -1318,501 +1318,153 @@
 nce 
-%7B%0A%0A    /** The type declaring the method. */%0A    private final TypeName typeName;%0A    /** The name of the method. */%0A    private final String methodName;%0A    /** Whether the method is defined on the prototype or not. */%0A    private final boolean onPrototype;%0A%0A    private MethodReference(TypeName typeName, String methodName, boolean onPrototype) %7B%0A        checkNotNull(methodName);%0A        this.typeName = typeName;%0A        this.methodName = methodName;%0A        this.onPrototype = onPrototype
+extends StringTypeValue %7B%0A%0A    private static final long serialVersionUID = 0L;%0A%0A    private MethodReference(String value) %7B%0A        super(value)
 ;%0A  
@@ -1652,61 +1652,101 @@
     
-return new MethodReference(typeName, methodName, fals
+String value = format(%22%25s.%25s%22, typeName, methodName);%0A        return new MethodReference(valu
 e);%0A
@@ -1939,220 +1939,71 @@
     
-return new MethodReference(typeName, methodName, true);%0A    %7D%0A%0A    /**%0A     * Obtains the reference to the constructor of the specified type.%0A     */%0A    public static MethodReference constructor(TypeName name) %7B
+String value = format(%22%25s.prototype.%25s%22, typeName, methodName);
 %0A   
@@ -2038,22 +2038,12 @@
 nce(
-name, %22%22, fals
+valu
 e);%0A
@@ -2080,752 +2080,123 @@
 the 
-value of the reference.%0A     */%0A    public String value() %7B%0A        if (methodName.isEmpty()) %7B%0A            return typeName.toString();%0A        %7D%0A        String delimiter = onPrototype%0A                           ? %22.prototype.%22%0A                           : %22.%22;%0A        return typeName + delimiter + methodName;%0A    %7D%0A%0A    @Override%0A    public String toString() %7B%0A        return value();%0A    %7D%0A%0A    @Override%0A    public boolean equals(Object o) %7B%0A        if (this == o) %7B%0A            return true;%0A        %7D%0A        if (!(o instanceof MethodReference)) %7B%0A            return false;%0A        %7D%0A        MethodReference reference = (MethodReference) o;%0A        return onPrototype == reference.onPrototype &&%0A                typeName.equals(reference.
+reference to the constructor of the specified type.%0A     */%0A    public static MethodReference constructor(TypeName 
 type
@@ -2205,10 +2205,9 @@
 me) 
-&&
+%7B
 %0A   
@@ -2215,167 +2215,51 @@
     
-        methodName.equals(reference.methodName);%0A    %7D%0A%0A    @Override%0A    public int hashCode() %7B%0A        return Objects.hash(typeName, methodName, onPrototype
+return new MethodReference(typeName.value()
 );%0A 

Make `MethodReference` a `StringTypeValue`