PubMethod.java revision 2958:27da0c3ac83a
1/*
2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package com.sun.tools.sjavac.pubapi;
26
27import java.io.Serializable;
28import java.util.List;
29import java.util.Set;
30import java.util.stream.Collectors;
31
32import javax.lang.model.element.Modifier;
33
34public class PubMethod implements Serializable {
35
36    private static final long serialVersionUID = -7813050194553446243L;
37
38    Set<Modifier> modifiers;
39    List<PubApiTypeParam> typeParams;
40    TypeDesc returnType;
41    String identifier;
42    List<TypeDesc> paramTypes;
43    List<TypeDesc> throwDecls;
44
45    public PubMethod(Set<Modifier> modifiers,
46                     List<PubApiTypeParam> typeParams,
47                     TypeDesc returnType,
48                     String identifier,
49                     List<TypeDesc> paramTypes,
50                     List<TypeDesc> throwDecls) {
51        this.modifiers = modifiers;
52        this.typeParams = typeParams;
53        this.returnType = returnType;
54        this.identifier = identifier;
55        this.paramTypes = paramTypes;
56        this.throwDecls = throwDecls;
57    }
58
59    // We need to include return type and type parameters to be sure to have
60    // different values for different methods. (A method can be overloaded with
61    // the only difference being the upper bound of the return type.)
62    public String asSignatureString() {
63        StringBuilder sb = new StringBuilder();
64
65        // <A extends String, Serializable, B extends List>
66        if (typeParams.size() > 0) {
67            sb.append(typeParams.stream()
68                                .map(PubApiTypeParam::asString)
69                                .collect(Collectors.joining(",", "<", "> ")));
70        }
71        sb.append(TypeDesc.encodeAsString(returnType));
72        sb.append(" ");
73        sb.append(identifier);
74        sb.append("(");
75        sb.append(paramTypes.stream()
76                            .map(TypeDesc::encodeAsString)
77                            .collect(Collectors.joining(",")));
78        sb.append(")");
79        return sb.toString();
80    }
81
82    @Override
83    public boolean equals(Object obj) {
84        if (getClass() != obj.getClass())
85            return false;
86        PubMethod other = (PubMethod) obj;
87        return modifiers.equals(other.modifiers)
88            && typeParams.equals(other.typeParams)
89            && returnType.equals(other.returnType)
90            && identifier.equals(other.identifier)
91            && paramTypes.equals(other.paramTypes)
92            && throwDecls.equals(other.throwDecls);
93    }
94
95    @Override
96    public int hashCode() {
97        return modifiers.hashCode()
98             ^ typeParams.hashCode()
99             ^ returnType.hashCode()
100             ^ identifier.hashCode()
101             ^ paramTypes.hashCode()
102             ^ throwDecls.hashCode();
103    }
104
105    public String toString() {
106        return String.format("%s[modifiers: %s, typeParams: %s, retType: %s, identifier: %s, params: %s, throws: %s]",
107                             getClass().getSimpleName(),
108                             modifiers,
109                             typeParams,
110                             returnType,
111                             identifier,
112                             paramTypes,
113                             throwDecls);
114    }
115}
116