Source code: com/puppycrawl/tools/checkstyle/checks/usage/transmogrify/MethodSignature.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 import java.util.Vector;
32
33
34
35 /**
36 * <code>MethodSignature</code> is used to resolve various methods
37 * in the same scope of the same name based on formal parameter lists
38 *
39 * @see MethodDef
40 */
41
42 public class MethodSignature implements ISignature{
43
44 private IClass[] _argTypes = null;
45
46 public MethodSignature(IClass[] argTypes) {
47 _argTypes = argTypes;
48 }
49
50 public MethodSignature(Vector argTypes) {
51 _argTypes = new IClass[argTypes.size()];
52 argTypes.toArray(_argTypes);
53 }
54
55 /**
56 * returns an array of the types of the arguments in the signature
57 *
58 * @return ClassDef[]
59 */
60 public IClass[] getParameters() {
61 return _argTypes;
62 }
63
64 /**
65 * Whether this method signature is compatible with the signature of the
66 * argument. That is to say, each type for this signature are subclasses,
67 * subinterfaces, or implement the interface for each corresponding type
68 * in the argument signature.
69 *
70 * @param signature the signature of the method definition being compared
71 * @return whether the signatures are compatible
72 */
73 public boolean isCompatibleWith(ISignature signature) {
74 boolean result = true;
75
76 IClass[] comparedArgTypes = signature.getParameters();
77 if (_argTypes.length != comparedArgTypes.length) {
78 result = false;
79 }
80 else {
81 for (int i = 0; i < _argTypes.length; i++) {
82 // TODO: Checkstyle modification. Why can _argTypes[i] be null?
83 // if (!_argTypes[i].isCompatibleWith(comparedArgTypes[i])) {
84 if ((_argTypes[i] != null)
85 && !_argTypes[i].isCompatibleWith(comparedArgTypes[i]))
86 {
87 result = false;
88 break;
89 }
90 }
91 }
92
93 return result;
94 }
95
96 public boolean isSame(ISignature signature) {
97 return equals(signature);
98 }
99
100 /**
101 * compares two objects for equality. If the compared object is a
102 * <code>MethodSignature</code> and the argTypes match, they are the
103 * same
104 *
105 * @return boolean
106 */
107 public boolean equals(Object o) {
108 boolean result = false;
109
110 if (o instanceof MethodSignature) {
111 MethodSignature signature = (MethodSignature)o;
112 result = java.util.Arrays.equals(getParameters(), signature.getParameters());
113 }
114
115 return result;
116 }
117
118 /**
119 * returns a String representation of this object. Includes information
120 * about the types of the arguments in the signature
121 *
122 * @return String
123 */
124 public String toString() {
125 StringBuffer result = new StringBuffer( "(" );
126
127 for ( int i = 0; i < _argTypes.length; i++ ) {
128 result.append( _argTypes[i] != null ? _argTypes[i].getName() : "[null]" );
129 if ( i < (_argTypes.length - 1) ) {
130 result.append( ", " );
131 }
132 }
133 result.append( ")" );
134
135 return result.toString();
136 }
137
138 }