1 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 ; 11 import java.util.Comparator ; 12 13 18 public final class MethodComparator implements Comparator { 19 22 public static final int NORMAL_METHOD = 0; 23 24 27 public static final int PREFIXED_METHOD = 1; 28 29 32 public static final int METHOD_INFO = 2; 33 34 37 private final int m_type; 38 39 44 private MethodComparator(final int type) { 45 m_type = type; 46 } 47 48 54 public static Comparator getInstance(final int type) { 55 return new MethodComparator(type); 56 } 57 58 65 public int compare(final Object o1, final Object o2) { 66 switch (m_type) { 67 case NORMAL_METHOD: 68 return compareNormal((Method ) o1, (Method ) o2); 69 case PREFIXED_METHOD: 70 return comparePrefixed((Method ) o1, (Method ) o2); 71 case METHOD_INFO: 72 return compareMethodInfo((MethodInfo) o1, (MethodInfo) o2); 73 default: 74 throw new RuntimeException ("invalid method comparison type"); 75 } 76 } 77 78 85 private int compareNormal(final Method m1, final Method m2) { 86 try { 87 if (m1.equals(m2)) { 88 return 0; 89 } 90 final String m1Name = m1.getName(); 91 final String m2Name = m2.getName(); 92 if (!m1Name.equals(m2Name)) { 93 return m1Name.compareTo(m2Name); 94 } 95 final Class [] args1 = m1.getParameterTypes(); 96 final Class [] 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 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 e) { 116 throw new WrappedRuntimeException(e); 117 } 118 System.err.println(m1.getName()); 119 System.err.println(m2.getName()); 120 throw new Error ("should be unreachable"); 121 } 122 123 130 private int comparePrefixed(final Method m1, final Method m2) { 131 try { 132 if (m1.equals(m2)) { 133 return 0; 134 } 135 136 final String [] m1Tokens = Strings.splitString(m1.getName(), TransformationConstants.DELIMITER); 138 final String [] m2Tokens = Strings.splitString(m2.getName(), TransformationConstants.DELIMITER); 139 final String m1Name = m1Tokens[1]; 140 final String m2Name = m2Tokens[1]; 141 if (!m1Name.equals(m2Name)) { 142 return m1Name.compareTo(m2Name); 143 } 144 final Class [] args1 = m1.getParameterTypes(); 145 final Class [] 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 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 e) { 165 throw new WrappedRuntimeException(e); 166 } 167 System.err.println(m1.getName()); 168 System.err.println(m2.getName()); 169 throw new Error ("should be unreachable"); 170 } 171 172 179 private int compareMethodInfo(final MethodInfo m1, final MethodInfo m2) { 180 try { 181 if (m1.equals(m2)) { 182 return 0; 183 } 184 final String m1Name = m1.getName(); 185 final String 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 e) { 212 throw new WrappedRuntimeException(e); 213 } 214 System.err.println(m1.getName()); 215 System.err.println(m2.getName()); 216 throw new Error ("should be unreachable"); 217 } 218 } | Popular Tags |