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  */
25 
26 package com.sun.tools.sjavac.pubapi;
27 
28 import java.io.Serializable;
29 import java.util.List;
30 import java.util.Set;
31 import java.util.stream.Collectors;
32 
33 import javax.lang.model.element.Modifier;
34 
35 public class PubMethod implements Serializable {
36 
37     private static final long serialVersionUID = -7813050194553446243L;
38 
39     Set<Modifier> modifiers;
40     List<PubApiTypeParam> typeParams;
41     TypeDesc returnType;
42     String identifier;
43     List<TypeDesc> paramTypes;
44     List<TypeDesc> throwDecls;
45 
PubMethod(Set<Modifier> modifiers, List<PubApiTypeParam> typeParams, TypeDesc returnType, String identifier, List<TypeDesc> paramTypes, List<TypeDesc> throwDecls)46     public PubMethod(Set<Modifier> modifiers,
47                      List<PubApiTypeParam> typeParams,
48                      TypeDesc returnType,
49                      String identifier,
50                      List<TypeDesc> paramTypes,
51                      List<TypeDesc> throwDecls) {
52         this.modifiers = modifiers;
53         this.typeParams = typeParams;
54         this.returnType = returnType;
55         this.identifier = identifier;
56         this.paramTypes = paramTypes;
57         this.throwDecls = throwDecls;
58     }
59 
60     // We need to include return type and type parameters to be sure to have
61     // different values for different methods. (A method can be overloaded with
62     // the only difference being the upper bound of the return type.)
asSignatureString()63     public String asSignatureString() {
64         StringBuilder sb = new StringBuilder();
65 
66         // <A extends String, Serializable, B extends List>
67         if (typeParams.size() > 0) {
68             sb.append(typeParams.stream()
69                                 .map(PubApiTypeParam::asString)
70                                 .collect(Collectors.joining(",", "<", "> ")));
71         }
72         sb.append(TypeDesc.encodeAsString(returnType));
73         sb.append(" ");
74         sb.append(identifier);
75         sb.append("(");
76         sb.append(paramTypes.stream()
77                             .map(TypeDesc::encodeAsString)
78                             .collect(Collectors.joining(",")));
79         sb.append(")");
80         return sb.toString();
81     }
82 
83     @Override
equals(Object obj)84     public boolean equals(Object obj) {
85         if (getClass() != obj.getClass())
86             return false;
87         PubMethod other = (PubMethod) obj;
88         return modifiers.equals(other.modifiers)
89             && typeParams.equals(other.typeParams)
90             && returnType.equals(other.returnType)
91             && identifier.equals(other.identifier)
92             && paramTypes.equals(other.paramTypes)
93             && throwDecls.equals(other.throwDecls);
94     }
95 
96     @Override
hashCode()97     public int hashCode() {
98         return modifiers.hashCode()
99              ^ typeParams.hashCode()
100              ^ returnType.hashCode()
101              ^ identifier.hashCode()
102              ^ paramTypes.hashCode()
103              ^ throwDecls.hashCode();
104     }
105 
toString()106     public String toString() {
107         return String.format("%s[modifiers: %s, typeParams: %s, retType: %s, identifier: %s, params: %s, throws: %s]",
108                              getClass().getSimpleName(),
109                              modifiers,
110                              typeParams,
111                              returnType,
112                              identifier,
113                              paramTypes,
114                              throwDecls);
115     }
116 }
117