1 8 9 package test.javax.management.compliance.signature.support; 10 11 import java.lang.reflect.Modifier ; 12 import java.util.ArrayList ; 13 14 17 public class MethodWrapper extends MemberWrapper 18 { 19 private ArrayList signature; 20 private ArrayList exceptions; 21 private ObjectMethod method; 22 23 public MethodWrapper(ObjectMethod method) 24 { 25 int mods = method.getModifiers(); 27 if (Modifier.isSynchronized(mods)) mods -= Modifier.SYNCHRONIZED; 28 modifiers = mods; 29 type = method.getReturnType().getName(); 30 name = method.getName(); 31 signature = convert(method.getParameterTypes(), false); 32 exceptions = convert(method.getExceptionTypes(), true); 33 this.method = method; 34 } 35 36 public boolean isSameMethod(MethodWrapper other) 37 { 38 int mask = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.STATIC | Modifier.ABSTRACT; 39 if ((modifiers & mask) != (other.modifiers & mask)) return false; 40 return name.equals(other.name) && type.equals(other.type) && signature.equals(other.signature); 41 } 42 43 public boolean sameSignatureModifiers(MethodWrapper other) 44 { 45 return modifiers == other.modifiers; 46 } 47 48 public boolean throwsClauseDiffer(MethodWrapper other) 49 { 50 ArrayList thisExceptions = convert(method.getExceptionTypes(), true); 51 ArrayList otherExceptions = convert(other.method.getExceptionTypes(), true); 52 if (thisExceptions.equals(otherExceptions)) return false; 53 return true; 54 } 55 56 public boolean throwsClauseDifferForRuntimeExceptionsOnly(MethodWrapper other) 57 { 58 Class [] thisTypes = method.getExceptionTypes(); 59 ArrayList thisExceptions = convert(thisTypes, true); 60 Class [] otherTypes = other.method.getExceptionTypes(); 61 ArrayList otherExceptions = convert(otherTypes, true); 62 63 ArrayList thisCopy = (ArrayList )thisExceptions.clone(); 64 65 thisExceptions.removeAll(otherExceptions); 66 if (!thisExceptions.isEmpty()) 67 { 68 if (containsCheckedException(thisExceptions, thisTypes)) return false; 69 } 70 71 otherExceptions.removeAll(thisCopy); 72 if (!otherExceptions.isEmpty()) 73 { 74 if (containsCheckedException(otherExceptions, otherTypes)) return false; 75 } 76 77 return true; 78 } 79 80 private boolean containsCheckedException(ArrayList exceptions, Class [] types) 81 { 82 for (int i = 0; i < exceptions.size(); ++i) 83 { 84 String name = (String )exceptions.get(i); 85 boolean found = false; 86 for (int j = 0; j < types.length; ++j) 87 { 88 Class type = types[j]; 89 if (name.equals(type.getName())) 90 { 91 found = true; 92 if (!RuntimeException .class.isAssignableFrom(type)) return true; 93 } 94 } 95 if (!found) throw new IllegalStateException (); 96 } 97 return false; 98 } 99 100 public String toString() 101 { 102 if (toString == null) 103 { 104 StringBuffer buffer = new StringBuffer (super.toString()); 105 buffer.append("("); 106 for (int i = 0; i < signature.size(); ++i) 107 { 108 if (i > 0) buffer.append(","); 109 buffer.append(signature.get(i)); 110 } 111 buffer.append(")"); 112 if (exceptions.size() > 0) 113 { 114 buffer.append(" throws "); 115 for (int i = 0; i < exceptions.size(); ++i) 116 { 117 if (i > 0) buffer.append(","); 118 buffer.append(exceptions.get(i)); 119 } 120 } 121 toString = buffer.toString(); 122 } 123 return toString; 124 } 125 } 126 | Popular Tags |