Browse Source

jfinal 4.0

James 6 years ago
parent
commit
51996a026b

+ 0 - 5
pom.xml

@@ -53,11 +53,6 @@
 			<scope>test</scope>
 		</dependency>
 		
-		<dependency>
-			<groupId>cglib</groupId>
-			<artifactId>cglib-nodep</artifactId>
-			<version>3.2.5</version>
-		</dependency>
 		
 		<dependency>
 			<groupId>com.jfinal</groupId>

+ 0 - 85
src/main/java/com/jfinal/aop/Callback.java

@@ -1,85 +0,0 @@
-/**
- * Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.com).
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.jfinal.aop;
-
-import java.lang.reflect.Method;
-import java.util.HashSet;
-import java.util.Set;
-import net.sf.cglib.proxy.MethodInterceptor;
-import net.sf.cglib.proxy.MethodProxy;
-import static com.jfinal.aop.InterceptorManager.NULL_INTERS;
-
-/**
- * Callback.
- */
-class Callback implements MethodInterceptor {
-	
-	private final Interceptor[] injectInters;
-	
-	private static final Set<String> excludedMethodName = buildExcludedMethodName();
-	private static final InterceptorManager interMan = InterceptorManager.me();
-	
-	public Callback() {
-		this.injectInters = NULL_INTERS;
-	}
-	
-	public Callback(Interceptor... injectInters) {
-		checkInjectInterceptors(injectInters);
-		this.injectInters = injectInters;
-	}
-	
-	private void checkInjectInterceptors(Interceptor... injectInters) {
-		if (injectInters == null) {
-			throw new IllegalArgumentException("injectInters can not be null.");
-		}
-		for (Interceptor inter : injectInters) {
-			if (inter == null) {
-				throw new IllegalArgumentException("interceptor in injectInters can not be null.");
-			}
-		}
-	}
-	
-	public Object intercept(Object target, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
-		if (excludedMethodName.contains(method.getName())) {
-			return methodProxy.invokeSuper(target, args);
-		}
-		
-		Class<?> targetClass = target.getClass();
-		if (targetClass.getName().indexOf("$$EnhancerBy") != -1) {
-			targetClass = targetClass.getSuperclass();
-		}
-		
-		Interceptor[] finalInters = interMan.buildServiceMethodInterceptor(injectInters, targetClass, method);
-		Invocation invocation = new Invocation(target, method, args, methodProxy, finalInters);
-		invocation.invoke();
-		return invocation.getReturnValue();
-	}
-	
-	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;
-	}
-}
-
-

+ 5 - 3
src/main/java/com/jfinal/aop/Duang.java

@@ -16,6 +16,8 @@
 
 package com.jfinal.aop;
 
+import com.jfinal.proxy.Proxy;
+
 /**
  * duang duang duang
  * 
@@ -36,16 +38,16 @@ public class Duang {
 	private Duang() {}
 	
 	public static <T> T duang(Class<T> targetClass) {
-		return (T)Enhancer.enhance(targetClass);
+		// return (T)Enhancer.enhance(targetClass);
+		return Proxy.get(targetClass);
 	}
 	
 	/**
 	 * 下一个版本的 aop 将不再支持 inject interceptor,所以本方法被 Deprecated
-	 */
 	@Deprecated
 	public static <T> T duang(Class<T> targetClass, Interceptor... injectInters) {
 		return (T)Enhancer.enhance(targetClass, injectInters);
-	}
+	} */
 }
 
 

+ 6 - 4
src/main/java/com/jfinal/aop/Enhancer.java

@@ -16,6 +16,8 @@
 
 package com.jfinal.aop;
 
+import com.jfinal.proxy.Proxy;
+
 /**
  * Enhancer
  * 
@@ -31,22 +33,22 @@ package com.jfinal.aop;
  * 所以删除了 Enhancer 中所有带 singletonKey 参数的方法
  * </pre>
  */
-@SuppressWarnings("unchecked")
+@Deprecated
 public class Enhancer {
 	
 	private Enhancer() {}
 	
 	public static <T> T enhance(Class<T> targetClass) {
-		return (T)net.sf.cglib.proxy.Enhancer.create(targetClass, new Callback());
+		// return (T)net.sf.cglib.proxy.Enhancer.create(targetClass, new Callback());
+		return Proxy.get(targetClass);
 	}
 	
 	/**
 	 * 下一个版本的 aop 将不再支持 inject interceptor,所以本方法被 Deprecated
-	 */
 	@Deprecated
 	public static <T> T enhance(Class<T> targetClass, Interceptor... injectInters) {
 		return (T)net.sf.cglib.proxy.Enhancer.create(targetClass, new Callback(injectInters));
-	}
+	} */
 }
 
 

+ 32 - 18
src/main/java/com/jfinal/aop/Invocation.java

@@ -18,9 +18,11 @@ package com.jfinal.aop;
 
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import com.jfinal.proxy.Callback;
+import com.jfinal.proxy.ProxyMethod;
+import com.jfinal.proxy.ProxyMethodCache;
 import com.jfinal.core.Action;
 import com.jfinal.core.Controller;
-import net.sf.cglib.proxy.MethodProxy;
 
 /**
  * Invocation is used to invoke the interceptors and the target method
@@ -28,18 +30,42 @@ import net.sf.cglib.proxy.MethodProxy;
 @SuppressWarnings("unchecked")
 public class Invocation {
 	
-	private Action action;
-	// private static final Object[] NULL_ARGS = new Object[0];	// Prevent new Object[0] by jvm for paras of action invocation.
+	private static final Object[] NULL_ARGS = new Object[0];	// Prevent new Object[0] by jvm for args of method invoking
 	
+	private Action action;
 	private Object target;
 	private Method method;
 	private Object[] args;
-	private MethodProxy methodProxy;
+	private Callback callback;
 	private Interceptor[] inters;
 	private Object returnValue;
 	
 	private int index = 0;
 	
+	
+	// ----------------------------------------------------------------
+	
+	
+	public Invocation(Object target, Long proxyMethodKey, Callback callback, Object... args) {
+		this.action = null;
+		this.target = target;
+		
+		ProxyMethod proxyMethod = ProxyMethodCache.get(proxyMethodKey);
+		this.method = proxyMethod.getMethod();
+		this.inters = proxyMethod.getInterceptors();
+		
+		this.callback = callback;
+		this.args = args;
+	}
+	
+	
+	public Invocation(Object target, Long proxyMethodKey, Callback callback) {
+		this(target, proxyMethodKey, callback, NULL_ARGS);
+	}
+	
+	// ----------------------------------------------------------------
+	
+	
 	// InvocationWrapper need this constructor
 	protected Invocation() {
 		this.action = null;
@@ -54,15 +80,6 @@ public class Invocation {
 		this.args = action.getParameterGetter().get(action, controller);
 	}
 	
-	public Invocation(Object target, Method method, Object[] args, MethodProxy methodProxy, Interceptor[] inters) {
-		this.action = null;
-		this.target = target;
-		this.method = method;
-		this.args = args;
-		this.methodProxy = methodProxy;
-		this.inters = inters;
-	}
-	
 	public void invoke() {
 		if (index < inters.length) {
 			inters[index++].intercept(this);
@@ -73,12 +90,9 @@ public class Invocation {
 				if (action != null) {
 					returnValue = action.getMethod().invoke(target, args);
 				}
-				// Invoke the method
+				// Invoke the callback
 				else {
-					// if (!Modifier.isAbstract(method.getModifiers()))
-						// returnValue = methodProxy.invokeSuper(target, args);
-					
-					returnValue = methodProxy.invokeSuper(target, args);
+					returnValue = callback.call(args);
 				}
 			}
 			catch (InvocationTargetException e) {