View Javadoc

1   /*
2    * Copyright 2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.callbackparams.support;
18  
19  import org.apache.bcel.classfile.Method;
20  import org.apache.bcel.generic.ClassGen;
21  import org.apache.bcel.generic.Type;
22  
23  /**
24   * Immutable hash-key implementation that can be used as method-id, regardless
25   * of whether the method is originally references through an instance of
26   * "java.lang.reflect.Method" or "org.apache.bcel.classfile.Method".
27   * The method is identified by class name, method name, return-type name and the
28   * names of the argument types. (The return-type is part of the method id so
29   * that the impact of generics will be handled properly.)
30   *
31   * @author Henrik Kaipe
32   */
33  public class MethodHashKey {
34  
35      private final String className;
36      final String methodName; // Not private as it will be accessed within package
37      private final String signature;
38  
39      public MethodHashKey(String className, String methodName, String signature){
40          this.className = className;
41          this.methodName = methodName;
42          this.signature = signature;
43      }
44  
45      public static MethodHashKey getHashKey(java.lang.reflect.Method m) {
46          return new MethodHashKey(m.getDeclaringClass().getName(),
47                  m.getName(), Type.getSignature(m));
48      }
49  
50      public static MethodHashKey getHashKey(ClassGen cg, Method m) {
51          return new MethodHashKey(cg.getClassName(),
52                  m.getName(), m.getSignature());
53      }
54  
55      public boolean equals(Object obj) {
56          if (obj == null) {
57              return false;
58          }
59          if (getClass() != obj.getClass()) {
60              return false;
61          }
62          final MethodHashKey other = (MethodHashKey) obj;
63          if ((this.className == null) ? (other.className != null) : !this.className.equals(other.className)) {
64              return false;
65          }
66          if ((this.methodName == null) ? (other.methodName != null) : !this.methodName.equals(other.methodName)) {
67              return false;
68          }
69          if ((this.signature == null) ? (other.signature != null) : !this.signature.equals(other.signature)) {
70              return false;
71          }
72          return true;
73      }
74  
75      public int hashCode() {
76          int hash = 3;
77          hash = 53 * hash + (this.className != null ? this.className.hashCode() : 0);
78          hash = 53 * hash + (this.methodName != null ? this.methodName.hashCode() : 0);
79          hash = 53 * hash + (this.signature != null ? this.signature.hashCode() : 0);
80          return hash;
81      }
82  }