@@ -232,41 +232,41 @@ private By returnMappedBy(By byDefault, By nativeAppBy) {
232232
233233 private static class AnnotationComparator implements Comparator <Annotation > {
234234
235- @ Override
236- public int compare (Annotation o1 , Annotation o2 ) {
237- int p1 ;
238- int p2 ;
239- Method priority1 ;
240- Method priority2 ;
241-
242- Class <?> c1 = o1 .annotationType ();
243- Class <?> c2 = o2 .annotationType ();
244-
235+ private static Method getPriorityMethod (Class <? extends Annotation > clazz ) {
245236 try {
246- priority1 = c1 .getMethod (PRIORITY , ANNOTATION_ARGUMENTS );
237+ return clazz .getMethod (PRIORITY , ANNOTATION_ARGUMENTS );
247238 } catch (NoSuchMethodException e ) {
248- throw new ClassCastException (String .format ("Class %s has no '%s' method" , c1 .getName (), PRIORITY ));
239+ throw new ClassCastException (String .format ("Class %s has no '%s' method" , clazz .getName (), PRIORITY ));
249240 }
241+ }
250242
243+ private static int getPriorityValue (Method priorityMethod , Annotation annotation ,
244+ Class <? extends Annotation > clazz ) {
251245 try {
252- priority2 = c2 .getMethod (PRIORITY , ANNOTATION_ARGUMENTS );
253- } catch (NoSuchMethodException e ) {
254- throw new ClassCastException (String .format ("Class %s has no '%s' method" , c2 .getName (), PRIORITY ));
246+ return (int ) priorityMethod .invoke (annotation , ANNOTATION_PARAMETERS );
247+ } catch (IllegalAccessException | InvocationTargetException e ) {
248+ throw new IllegalArgumentException (String
249+ .format ("It is impossible to get priority. Annotation class: %s" , clazz .toString ()), e );
255250 }
251+ }
256252
257- try {
258- p1 = (int ) priority1 .invoke (o1 , ANNOTATION_PARAMETERS );
259- p2 = (int ) priority2 .invoke (o2 , ANNOTATION_PARAMETERS );
260-
261- if (p2 > p1 ) {
262- return -1 ;
263- } else if (p2 < p1 ) {
264- return 1 ;
265- } else {
266- return 0 ;
267- }
268- } catch (IllegalAccessException | InvocationTargetException e ) {
269- throw new RuntimeException (e );
253+ @ Override
254+ public int compare (Annotation o1 , Annotation o2 ) {
255+ Class <? extends Annotation > c1 = o1 .annotationType ();
256+ Class <? extends Annotation > c2 = o2 .annotationType ();
257+
258+ Method priority1 = getPriorityMethod (c1 );
259+ Method priority2 = getPriorityMethod (c2 );
260+
261+ int p1 = getPriorityValue (priority1 , o1 , c1 );
262+ int p2 = getPriorityValue (priority2 , o2 , c2 );
263+
264+ if (p2 > p1 ) {
265+ return -1 ;
266+ } else if (p2 < p1 ) {
267+ return 1 ;
268+ } else {
269+ return 0 ;
270270 }
271271 }
272272 }
0 commit comments