Skip to content

Commit

Permalink
Merge branch 'codeborne-fix-method-list-caching' into 1.3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
pepite committed Oct 27, 2014
2 parents 576a3d5 + 72213cd commit 1d2c220
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
32 changes: 0 additions & 32 deletions framework/src/play/mvc/ActionInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,6 @@ public static void invoke(Http.Request request, Http.Response response) {
// @Catch
Object[] args = new Object[]{ex.getTargetException()};
List<Method> catches = Java.findAllAnnotatedMethods(Controller.getControllerClass(), Catch.class);
Collections.sort(catches, new Comparator<Method>() {

public int compare(Method m1, Method m2) {
Catch catch1 = m1.getAnnotation(Catch.class);
Catch catch2 = m2.getAnnotation(Catch.class);
return catch1.priority() - catch2.priority();
}
});
ControllerInstrumentation.stopActionCall();
for (Method mCatch : catches) {
Class[] exceptions = mCatch.getAnnotation(Catch.class).value();
Expand Down Expand Up @@ -289,14 +281,6 @@ private static boolean isActionMethod(Method method) {

private static void handleBefores(Http.Request request) throws Exception {
List<Method> befores = Java.findAllAnnotatedMethods(Controller.getControllerClass(), Before.class);
Collections.sort(befores, new Comparator<Method>() {

public int compare(Method m1, Method m2) {
Before before1 = m1.getAnnotation(Before.class);
Before before2 = m2.getAnnotation(Before.class);
return before1.priority() - before2.priority();
}
});
ControllerInstrumentation.stopActionCall();
for (Method before : befores) {
String[] unless = before.getAnnotation(Before.class).unless();
Expand Down Expand Up @@ -331,14 +315,6 @@ public int compare(Method m1, Method m2) {

private static void handleAfters(Http.Request request) throws Exception {
List<Method> afters = Java.findAllAnnotatedMethods(Controller.getControllerClass(), After.class);
Collections.sort(afters, new Comparator<Method>() {

public int compare(Method m1, Method m2) {
After after1 = m1.getAnnotation(After.class);
After after2 = m2.getAnnotation(After.class);
return after1.priority() - after2.priority();
}
});
ControllerInstrumentation.stopActionCall();
for (Method after : afters) {
String[] unless = after.getAnnotation(After.class).unless();
Expand Down Expand Up @@ -387,14 +363,6 @@ static void handleFinallies(Http.Request request, Throwable caughtException) thr

try {
List<Method> allFinally = Java.findAllAnnotatedMethods(Controller.getControllerClass(), Finally.class);
Collections.sort(allFinally, new Comparator<Method>() {

public int compare(Method m1, Method m2) {
Finally finally1 = m1.getAnnotation(Finally.class);
Finally finally2 = m2.getAnnotation(Finally.class);
return finally1.priority() - finally2.priority();
}
});
ControllerInstrumentation.stopActionCall();
for (Method aFinally : allFinally) {
String[] unless = aFinally.getAnnotation(Finally.class).unless();
Expand Down
49 changes: 46 additions & 3 deletions framework/src/play/utils/Java.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package play.utils;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.FutureTask;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.bytecode.SourceFileAttribute;
Expand All @@ -25,6 +43,7 @@

import static java.util.Collections.addAll;
import static org.apache.commons.io.IOUtils.closeQuietly;
import static java.util.Collections.sort;

/**
* Java utils
Expand Down Expand Up @@ -186,7 +205,7 @@ public static Object invokeChildOrStatic(Class<?> clazz, String method, Object..
{
invokedClass = assignableClasses.get(0);
}

return Java.invokeStaticOrParent(invokedClass, method, args);
}

Expand Down Expand Up @@ -279,7 +298,6 @@ public static String rawJavaType(Class clazz) {
* @return A list of method object
*/
public static List<Method> findAllAnnotatedMethods(Class<?> clazz, Class<? extends Annotation> annotationType) {

return getJavaWithCaching().findAllAnnotatedMethods(clazz, annotationType);
}

Expand Down Expand Up @@ -495,7 +513,7 @@ public int hashCode() {
* @param annotationType The annotation class
* @return A list of method object
*/
public List<Method> findAllAnnotatedMethods(Class<?> clazz, Class<? extends Annotation> annotationType) {
public List<Method> findAllAnnotatedMethods(Class<?> clazz, final Class<? extends Annotation> annotationType) {

if( clazz == null ) {
return new ArrayList<Method>(0);
Expand All @@ -514,20 +532,45 @@ public List<Method> findAllAnnotatedMethods(Class<?> clazz, Class<? extends Anno
}
// have to resolve it.
methods = new ArrayList<Method>();

// get list of all annotated methods on this class..
for( Method method : findAllAnnotatedMethods( clazz)) {
if (method.isAnnotationPresent(annotationType)) {
methods.add(method);
}
}

sortByPriority(methods, annotationType);

// store it in cache
classAndAnnotation2Methods.put( key, methods);

return methods;
}
}

private void sortByPriority(List<Method> methods, final Class<? extends Annotation> annotationType) {
try {
final Method priority = annotationType.getMethod("priority");
sort(methods, new Comparator<Method>() {
@Override public int compare(Method m1, Method m2) {
try {
Integer priority1 = (Integer) priority.invoke(m1.getAnnotation(annotationType));
Integer priority2 = (Integer) priority.invoke(m2.getAnnotation(annotationType));
return priority1.compareTo(priority2);
}
catch (Exception e) {
// should not happen
throw new RuntimeException(e);
}
}
});
}
catch (NoSuchMethodException e) {
// no need to sort - this annotation doesn't have priority() method
}
}

/**
* Find all annotated method from a class
* @param clazz The class
Expand Down

0 comments on commit 1d2c220

Please sign in to comment.