Source code: com/puppycrawl/tools/checkstyle/checks/usage/transmogrify/ExternalMethod.java
1
2 // Transmogrify License
3 //
4 // Copyright (c) 2001, ThoughtWorks, Inc.
5 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
8 // are met:
9 // - Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // - Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 // Neither the name of the ThoughtWorks, Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from this
16 // software without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify;
30
31
32
33
34 import java.lang.reflect.Method;
35
36 /**
37 * <code>MethodDef</code> contains all the pertinent information for
38 * a method, including return type, formal parameters, and exceptions
39 * thrown
40 *
41 * @see ClassDef
42 * @see MethodSignature
43 */
44 public class ExternalMethod extends ExternalDefinition implements IMethod {
45 private Method _javaMethod;
46 private ISignature _signature;
47
48 public ExternalMethod(Method javaMethod) {
49 _javaMethod = javaMethod;
50 _signature = new ExternalSignature(_javaMethod.getParameterTypes());
51 }
52
53 public String getName() {
54 return _javaMethod.getName();
55 }
56
57 /**
58 * Returns the <code>ClassDef</code> for the return type of this method.
59 *
60 * @return the <code>ClassDef</code> for the return type of this method
61 */
62 public IClass getType() {
63 IClass result = null;
64 if (_javaMethod.getReturnType().isArray()) {
65 result = new ArrayDef(new ExternalClass(_javaMethod.getReturnType().getComponentType()));
66 }
67 else {
68 result = new ExternalClass(_javaMethod.getReturnType());
69 }
70
71 return result;
72 }
73
74 /**
75 * Returns the signature of this method.
76 *
77 * @return the signature of this method
78 */
79 public ISignature getSignature() {
80 return _signature;
81 }
82
83 public boolean hasSameSignature(ISignature signature) {
84 return _signature.isSame(signature);
85 }
86
87 public boolean hasCompatibleSignature(ISignature signature) {
88 return signature.isCompatibleWith(getSignature());
89 }
90
91 public String getQualifiedName() {
92 return getName() + getSignature();
93 }
94
95 public Method getJavaMethod() {
96 return _javaMethod;
97 }
98
99 public IClass[] getExceptions() {
100 Class[] javaExceptions = getJavaMethod().getExceptionTypes();
101 IClass[] result = new IClass[javaExceptions.length];
102
103 for (int i = 0; i < result.length; i++) {
104 result[i] = new ExternalClass(javaExceptions[i]);
105 }
106
107 return result;
108 }
109
110 public String toString() {
111 return getQualifiedName();
112 }
113
114 public boolean equals(Object o) {
115 boolean result = false;
116
117 if (o instanceof ExternalMethod) {
118 ExternalMethod compared = (ExternalMethod)o;
119 result = getJavaMethod().equals(compared.getJavaMethod());
120 }
121
122 return result;
123 }
124
125 public int hashCode() {
126 return getJavaMethod().hashCode();
127 }
128 }