KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > compliance > signature > support > MethodWrapper


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.javax.management.compliance.signature.support;
10
11 import java.lang.reflect.Modifier JavaDoc;
12 import java.util.ArrayList JavaDoc;
13
14 /**
15  * @version $Revision: 1.4 $
16  */

17 public class MethodWrapper extends MemberWrapper
18 {
19    private ArrayList JavaDoc signature;
20    private ArrayList JavaDoc exceptions;
21    private ObjectMethod method;
22
23    public MethodWrapper(ObjectMethod method)
24    {
25       // Clear synchronized modifier, not relevant
26
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 JavaDoc thisExceptions = convert(method.getExceptionTypes(), true);
51       ArrayList JavaDoc 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 JavaDoc[] thisTypes = method.getExceptionTypes();
59       ArrayList JavaDoc thisExceptions = convert(thisTypes, true);
60       Class JavaDoc[] otherTypes = other.method.getExceptionTypes();
61       ArrayList JavaDoc otherExceptions = convert(otherTypes, true);
62
63       ArrayList JavaDoc thisCopy = (ArrayList JavaDoc)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 JavaDoc exceptions, Class JavaDoc[] types)
81    {
82       for (int i = 0; i < exceptions.size(); ++i)
83       {
84          String JavaDoc name = (String JavaDoc)exceptions.get(i);
85          boolean found = false;
86          for (int j = 0; j < types.length; ++j)
87          {
88             Class JavaDoc type = types[j];
89             if (name.equals(type.getName()))
90             {
91                found = true;
92                if (!RuntimeException JavaDoc.class.isAssignableFrom(type)) return true;
93             }
94          }
95          if (!found) throw new IllegalStateException JavaDoc();
96       }
97       return false;
98    }
99
100    public String JavaDoc toString()
101    {
102       if (toString == null)
103       {
104          StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(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