Browse Source

改用 MethodFilter 机制过滤无需代理的方法,可以生成阶段处理完毕。避免在每次调用时过滤,进一步提升性能

James 2 years ago
parent
commit
5a74bcbefd
1 changed files with 32 additions and 5 deletions
  1. 32 5
      src/main/java/com/jfinal/ext/proxy/JavassistProxyFactory.java

+ 32 - 5
src/main/java/com/jfinal/ext/proxy/JavassistProxyFactory.java

@@ -16,8 +16,12 @@
 
 package com.jfinal.ext.proxy;
 
+import java.lang.reflect.Method;
 import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;
 import com.jfinal.proxy.ProxyFactory;
+import javassist.util.proxy.MethodFilter;
 import javassist.util.proxy.ProxyObject;
 
 /**
@@ -34,6 +38,7 @@ public class JavassistProxyFactory extends ProxyFactory {
     
     protected HashMap<Class<?>, Class<?>> cache = new HashMap<>(1024, 0.25F);
     protected JavassistCallback callback = new JavassistCallback();
+    protected JavassistMethodFilter methodFilter = new JavassistMethodFilter();
     
     @SuppressWarnings("unchecked")
     public <T> T get(Class<T> target) {
@@ -63,10 +68,37 @@ public class JavassistProxyFactory extends ProxyFactory {
             return (Class<T>) cache.computeIfAbsent(target, key -> {
                 javassist.util.proxy.ProxyFactory factory = new javassist.util.proxy.ProxyFactory();
                 factory.setSuperclass(key);
+                factory.setFilter(methodFilter);
                 return factory.createClass();
             });
         }
     }
+    
+    /**
+     * 过滤不需要代理的方法,参考资料:
+     * https://github.com/jboss-javassist/javassist/blob/master/src/test/testproxy/ProxyTester.java
+     */
+    private static class JavassistMethodFilter implements MethodFilter {
+        
+        private static final Set<String> excludedMethodName = buildExcludedMethodName();
+        
+        @Override
+        public boolean isHandled(Method method) {
+            return ! excludedMethodName.contains(method.getName());
+        }
+        
+        private static final Set<String> buildExcludedMethodName() {
+            Set<String> excludedMethodName = new HashSet<String>(64, 0.25F);
+            Method[] methods = Object.class.getDeclaredMethods();
+            for (Method m : methods) {
+                excludedMethodName.add(m.getName());
+            }
+            // getClass() registerNatives() can not be enhanced
+            // excludedMethodName.remove("getClass");   
+            // excludedMethodName.remove("registerNatives");
+            return excludedMethodName;
+        }
+    }
 }
 
 
@@ -74,8 +106,3 @@ public class JavassistProxyFactory extends ProxyFactory {
 
 
 
-
-
-
-
-