Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Otimize assertIsNotOnBaseObject for performance #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -23,6 +24,20 @@
*/
public class DefaultAccessControl implements AccessControl
{
private static final Map<String, List<Class<?>[]>> OBJECT_METHODS_CACHE = new HashMap<String, List<Class<?>[]>>();

static {
final Method[] methods = Object.class.getMethods();
for (Method m : methods) {
List<Class<?>[]> pTypesList = OBJECT_METHODS_CACHE.get(m.getName());
if(pTypesList == null) {
pTypesList = new ArrayList<Class<?>[]>();
OBJECT_METHODS_CACHE.put(m.getName(), pTypesList);
}
pTypesList.add(m.getParameterTypes());
}
}

/* (non-Javadoc)
* @see org.directwebremoting.extend.AccessControl#assertGeneralExecutionIsPossible(java.lang.String, org.directwebremoting.extend.MethodDeclaration)
*/
Expand Down Expand Up @@ -216,11 +231,14 @@ protected static void assertIsMethodPublic(Method method)
*/
protected static void assertIsNotOnBaseObject(Method method)
{
try {
Method objectMethod = Object.class.getMethod(method.getName(), method.getParameterTypes());
throw new SecurityException("Methods defined in java.lang.Object are not accessible (" + objectMethod.getName() + ").");
} catch(NoSuchMethodException ex) {
// All is well, method not found in Object
final List<Class<?>[]> pTypesList = OBJECT_METHODS_CACHE.get(method.getName());
if (pTypesList == null) {
return;
}
for (Class<?>[] pTypes : pTypesList) {
if (Arrays.equals(pTypes, method.getParameterTypes())) {
throw new SecurityException("Methods defined in java.lang.Object are not accessible (" + method.getName() + ").");
}
}
}

Expand Down