001: // Transmogrify License
002: //
003: // Copyright (c) 2001, ThoughtWorks, Inc.
004: // All rights reserved.
005: // Redistribution and use in source and binary forms, with or without
006: // modification, are permitted provided that the following conditions
007: // are met:
008: // - Redistributions of source code must retain the above copyright notice,
009: // this list of conditions and the following disclaimer.
010: // - Redistributions in binary form must reproduce the above copyright
011: // notice, this list of conditions and the following disclaimer in the
012: // documentation and/or other materials provided with the distribution.
013: // Neither the name of the ThoughtWorks, Inc. nor the names of its
014: // contributors may be used to endorse or promote products derived from this
015: // software without specific prior written permission.
016: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
017: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
018: // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
019: // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
020: // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
021: // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
022: // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
023: // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
024: // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
025: // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
026: // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027:
028: package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify;
029:
030: import java.lang.reflect.Method;
031:
032: /**
033: * <code>MethodDef</code> contains all the pertinent information for
034: * a method, including return type, formal parameters, and exceptions
035: * thrown
036: *
037: * @see ClassDef
038: * @see MethodSignature
039: */
040: public class ExternalMethod extends ExternalDefinition implements
041: IMethod {
042: private Method _javaMethod;
043: private ISignature _signature;
044:
045: public ExternalMethod(Method javaMethod) {
046: _javaMethod = javaMethod;
047: _signature = new ExternalSignature(_javaMethod
048: .getParameterTypes());
049: }
050:
051: public String getName() {
052: return _javaMethod.getName();
053: }
054:
055: /**
056: * Returns the <code>ClassDef</code> for the return type of this method.
057: *
058: * @return the <code>ClassDef</code> for the return type of this method
059: */
060: public IClass getType() {
061: IClass result = null;
062: if (_javaMethod.getReturnType().isArray()) {
063: result = new ArrayDef(new ExternalClass(_javaMethod
064: .getReturnType().getComponentType()));
065: } else {
066: result = new ExternalClass(_javaMethod.getReturnType());
067: }
068:
069: return result;
070: }
071:
072: /**
073: * Returns the signature of this method.
074: *
075: * @return the signature of this method
076: */
077: public ISignature getSignature() {
078: return _signature;
079: }
080:
081: public boolean hasSameSignature(ISignature signature) {
082: return _signature.isSame(signature);
083: }
084:
085: public boolean hasCompatibleSignature(ISignature signature) {
086: return signature.isCompatibleWith(getSignature());
087: }
088:
089: public String getQualifiedName() {
090: return getName() + getSignature();
091: }
092:
093: public Method getJavaMethod() {
094: return _javaMethod;
095: }
096:
097: public IClass[] getExceptions() {
098: Class[] javaExceptions = getJavaMethod().getExceptionTypes();
099: IClass[] result = new IClass[javaExceptions.length];
100:
101: for (int i = 0; i < result.length; i++) {
102: result[i] = new ExternalClass(javaExceptions[i]);
103: }
104:
105: return result;
106: }
107:
108: public String toString() {
109: return getQualifiedName();
110: }
111:
112: public boolean equals(Object o) {
113: boolean result = false;
114:
115: if (o instanceof ExternalMethod) {
116: ExternalMethod compared = (ExternalMethod) o;
117: result = getJavaMethod().equals(compared.getJavaMethod());
118: }
119:
120: return result;
121: }
122:
123: public int hashCode() {
124: return getJavaMethod().hashCode();
125: }
126: }
|