KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > reflect > MethodComparator


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.reflect;
5
6 import com.tc.aspectwerkz.exception.WrappedRuntimeException;
7 import com.tc.aspectwerkz.transform.TransformationConstants;
8 import com.tc.aspectwerkz.util.Strings;
9
10 import java.lang.reflect.Method JavaDoc;
11 import java.util.Comparator JavaDoc;
12
13 /**
14  * Compares Methods. To be used when sorting methods.
15  *
16  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
17  */

18 public final class MethodComparator implements Comparator JavaDoc {
19   /**
20    * Compares normal method names.
21    */

22   public static final int NORMAL_METHOD = 0;
23
24   /**
25    * Compares prefixed method names.
26    */

27   public static final int PREFIXED_METHOD = 1;
28
29   /**
30    * Compares method infos.
31    */

32   public static final int METHOD_INFO = 2;
33
34   /**
35    * Defines the type of comparator.
36    */

37   private final int m_type;
38
39   /**
40    * Sets the type.
41    *
42    * @param type the type
43    */

44   private MethodComparator(final int type) {
45     m_type = type;
46   }
47
48   /**
49    * Returns the comparator instance.
50    *
51    * @param type the type of the method comparison
52    * @return the instance
53    */

54   public static Comparator JavaDoc getInstance(final int type) {
55     return new MethodComparator(type);
56   }
57
58   /**
59    * Compares two objects.
60    *
61    * @param o1
62    * @param o2
63    * @return int
64    */

65   public int compare(final Object JavaDoc o1, final Object JavaDoc o2) {
66     switch (m_type) {
67       case NORMAL_METHOD:
68         return compareNormal((Method JavaDoc) o1, (Method JavaDoc) o2);
69       case PREFIXED_METHOD:
70         return comparePrefixed((Method JavaDoc) o1, (Method JavaDoc) o2);
71       case METHOD_INFO:
72         return compareMethodInfo((MethodInfo) o1, (MethodInfo) o2);
73       default:
74         throw new RuntimeException JavaDoc("invalid method comparison type");
75     }
76   }
77
78   /**
79    * Compares two methods.
80    *
81    * @param m1
82    * @param m2
83    * @return int
84    */

85   private int compareNormal(final Method JavaDoc m1, final Method JavaDoc m2) {
86     try {
87       if (m1.equals(m2)) {
88         return 0;
89       }
90       final String JavaDoc m1Name = m1.getName();
91       final String JavaDoc m2Name = m2.getName();
92       if (!m1Name.equals(m2Name)) {
93         return m1Name.compareTo(m2Name);
94       }
95       final Class JavaDoc[] args1 = m1.getParameterTypes();
96       final Class JavaDoc[] args2 = m2.getParameterTypes();
97       if (args1.length < args2.length) {
98         return -1;
99       }
100       if (args1.length > args2.length) {
101         return 1;
102       }
103       if (args1.length == 0) {
104         return 0;
105       }
106       for (int i = 0; i < args1.length; i++) {
107         //handles array types - AW-104
108
int result = TypeConverter.convertTypeToJava(args1[i]).compareTo(
109                 TypeConverter.convertTypeToJava(args2[i])
110         );
111         if (result != 0) {
112           return result;
113         }
114       }
115     } catch (Throwable JavaDoc e) {
116       throw new WrappedRuntimeException(e);
117     }
118     System.err.println(m1.getName());
119     System.err.println(m2.getName());
120     throw new Error JavaDoc("should be unreachable");
121   }
122
123   /**
124    * Compares two prefixed methods. Assumes the the prefixed methods looks like this: "somePrefix SEP methodName SEP"
125    *
126    * @param m1
127    * @param m2
128    * @return int
129    */

130   private int comparePrefixed(final Method JavaDoc m1, final Method JavaDoc m2) {
131     try {
132       if (m1.equals(m2)) {
133         return 0;
134       }
135
136       // compare only the original method names, i.e. remove the prefix and suffix
137
final String JavaDoc[] m1Tokens = Strings.splitString(m1.getName(), TransformationConstants.DELIMITER);
138       final String JavaDoc[] m2Tokens = Strings.splitString(m2.getName(), TransformationConstants.DELIMITER);
139       final String JavaDoc m1Name = m1Tokens[1];
140       final String JavaDoc m2Name = m2Tokens[1];
141       if (!m1Name.equals(m2Name)) {
142         return m1Name.compareTo(m2Name);
143       }
144       final Class JavaDoc[] args1 = m1.getParameterTypes();
145       final Class JavaDoc[] args2 = m2.getParameterTypes();
146       if (args1.length < args2.length) {
147         return -1;
148       }
149       if (args1.length > args2.length) {
150         return 1;
151       }
152       if (args1.length == 0) {
153         return 0;
154       }
155       for (int i = 0; i < args1.length; i++) {
156         //handles array types - AW-104
157
int result = TypeConverter.convertTypeToJava(args1[i]).compareTo(
158                 TypeConverter.convertTypeToJava(args2[i])
159         );
160         if (result != 0) {
161           return result;
162         }
163       }
164     } catch (Throwable JavaDoc e) {
165       throw new WrappedRuntimeException(e);
166     }
167     System.err.println(m1.getName());
168     System.err.println(m2.getName());
169     throw new Error JavaDoc("should be unreachable");
170   }
171
172   /**
173    * Compares two methods meta-data.
174    *
175    * @param m1
176    * @param m2
177    * @return int
178    */

179   private int compareMethodInfo(final MethodInfo m1, final MethodInfo m2) {
180     try {
181       if (m1.equals(m2)) {
182         return 0;
183       }
184       final String JavaDoc m1Name = m1.getName();
185       final String JavaDoc m2Name = m2.getName();
186       if (!m1Name.equals(m2Name)) {
187         return m1Name.compareTo(m2Name);
188       }
189       final ClassInfo[] args1 = m1.getParameterTypes();
190       final ClassInfo[] args2 = m2.getParameterTypes();
191       if (args1.length < args2.length) {
192         return -1;
193       }
194       if (args1.length > args2.length) {
195         return 1;
196       }
197       if (args1.length == 0) {
198         return 0;
199       }
200       for (int i = 0; i < args1.length; i++) {
201         int result;
202         if (args1[i].getName().equals(args2[i].getName())) {
203           result = 0;
204         } else {
205           result = args1[i].getName().compareTo(args2[i].getName());
206         }
207         if (result != 0) {
208           return result;
209         }
210       }
211     } catch (Throwable JavaDoc e) {
212       throw new WrappedRuntimeException(e);
213     }
214     System.err.println(m1.getName());
215     System.err.println(m2.getName());
216     throw new Error JavaDoc("should be unreachable");
217   }
218 }
Popular Tags