001/*
002 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package jdk.internal.jvmci.runtime.test;
024
025import java.lang.reflect.*;
026import java.util.*;
027
028import jdk.internal.jvmci.meta.*;
029import jdk.internal.jvmci.runtime.*;
030
031class NameAndSignature {
032
033    public static final MetaAccessProvider metaAccess = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess();
034
035    final String name;
036    final Class<?> returnType;
037    final Class<?>[] parameterTypes;
038
039    public NameAndSignature(Method m) {
040        this.name = m.getName();
041        this.returnType = m.getReturnType();
042        this.parameterTypes = m.getParameterTypes();
043    }
044
045    @Override
046    public boolean equals(Object obj) {
047        if (obj instanceof NameAndSignature) {
048            NameAndSignature s = (NameAndSignature) obj;
049            return s.returnType == returnType && name.equals(s.name) && Arrays.equals(s.parameterTypes, parameterTypes);
050        }
051        return false;
052    }
053
054    @Override
055    public int hashCode() {
056        return name.hashCode();
057    }
058
059    @Override
060    public String toString() {
061        StringBuilder sb = new StringBuilder(name + "(");
062        String sep = "";
063        for (Class<?> p : parameterTypes) {
064            sb.append(sep);
065            sep = ", ";
066            sb.append(p.getName());
067        }
068        return sb.append(')').append(returnType.getName()).toString();
069    }
070
071    public boolean signatureEquals(ResolvedJavaMethod m) {
072        Signature s = m.getSignature();
073        ResolvedJavaType declaringClass = m.getDeclaringClass();
074        if (!s.getReturnType(declaringClass).resolve(declaringClass).equals(metaAccess.lookupJavaType(returnType))) {
075            return false;
076        }
077        if (s.getParameterCount(false) != parameterTypes.length) {
078            return false;
079        }
080        for (int i = 0; i < parameterTypes.length; i++) {
081            if (!s.getParameterType(i, declaringClass).resolve(declaringClass).equals(metaAccess.lookupJavaType(parameterTypes[i]))) {
082                return false;
083            }
084        }
085        return true;
086    }
087}