001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package test.javax.management.compliance.signature.support;
010:
011: import java.lang.reflect.Modifier;
012: import java.util.ArrayList;
013:
014: /**
015: * @version $Revision: 1.4 $
016: */
017: public class MethodWrapper extends MemberWrapper {
018: private ArrayList signature;
019: private ArrayList exceptions;
020: private ObjectMethod method;
021:
022: public MethodWrapper(ObjectMethod method) {
023: // Clear synchronized modifier, not relevant
024: int mods = method.getModifiers();
025: if (Modifier.isSynchronized(mods))
026: mods -= Modifier.SYNCHRONIZED;
027: modifiers = mods;
028: type = method.getReturnType().getName();
029: name = method.getName();
030: signature = convert(method.getParameterTypes(), false);
031: exceptions = convert(method.getExceptionTypes(), true);
032: this .method = method;
033: }
034:
035: public boolean isSameMethod(MethodWrapper other) {
036: int mask = Modifier.PUBLIC | Modifier.PROTECTED
037: | Modifier.STATIC | Modifier.ABSTRACT;
038: if ((modifiers & mask) != (other.modifiers & mask))
039: return false;
040: return name.equals(other.name) && type.equals(other.type)
041: && signature.equals(other.signature);
042: }
043:
044: public boolean sameSignatureModifiers(MethodWrapper other) {
045: return modifiers == other.modifiers;
046: }
047:
048: public boolean throwsClauseDiffer(MethodWrapper other) {
049: ArrayList this Exceptions = convert(method.getExceptionTypes(),
050: true);
051: ArrayList otherExceptions = convert(other.method
052: .getExceptionTypes(), true);
053: if (this Exceptions.equals(otherExceptions))
054: return false;
055: return true;
056: }
057:
058: public boolean throwsClauseDifferForRuntimeExceptionsOnly(
059: MethodWrapper other) {
060: Class[] this Types = method.getExceptionTypes();
061: ArrayList this Exceptions = convert(this Types, true);
062: Class[] otherTypes = other.method.getExceptionTypes();
063: ArrayList otherExceptions = convert(otherTypes, true);
064:
065: ArrayList this Copy = (ArrayList) this Exceptions.clone();
066:
067: this Exceptions.removeAll(otherExceptions);
068: if (!this Exceptions.isEmpty()) {
069: if (containsCheckedException(this Exceptions, this Types))
070: return false;
071: }
072:
073: otherExceptions.removeAll(this Copy);
074: if (!otherExceptions.isEmpty()) {
075: if (containsCheckedException(otherExceptions, otherTypes))
076: return false;
077: }
078:
079: return true;
080: }
081:
082: private boolean containsCheckedException(ArrayList exceptions,
083: Class[] types) {
084: for (int i = 0; i < exceptions.size(); ++i) {
085: String name = (String) exceptions.get(i);
086: boolean found = false;
087: for (int j = 0; j < types.length; ++j) {
088: Class type = types[j];
089: if (name.equals(type.getName())) {
090: found = true;
091: if (!RuntimeException.class.isAssignableFrom(type))
092: return true;
093: }
094: }
095: if (!found)
096: throw new IllegalStateException();
097: }
098: return false;
099: }
100:
101: public String toString() {
102: if (toString == null) {
103: StringBuffer buffer = new StringBuffer(super .toString());
104: buffer.append("(");
105: for (int i = 0; i < signature.size(); ++i) {
106: if (i > 0)
107: buffer.append(",");
108: buffer.append(signature.get(i));
109: }
110: buffer.append(")");
111: if (exceptions.size() > 0) {
112: buffer.append(" throws ");
113: for (int i = 0; i < exceptions.size(); ++i) {
114: if (i > 0)
115: buffer.append(",");
116: buffer.append(exceptions.get(i));
117: }
118: }
119: toString = buffer.toString();
120: }
121: return toString;
122: }
123: }
|