Browse Source

jfinal 3.5

James 7 years ago
parent
commit
28bc35c97b

+ 1 - 1
src/main/java/com/jfinal/core/Action.java

@@ -45,7 +45,7 @@ public class Action {
 		this.interceptors = interceptors;
 		this.interceptors = interceptors;
 		this.viewPath = viewPath;
 		this.viewPath = viewPath;
 		
 		
-		this.parameterGetter = ParaProcessorBuilder.me().build(controllerClass, method);
+		this.parameterGetter = ParaProcessorBuilder.me.build(controllerClass, method);
 	}
 	}
 	
 	
 	public Class<? extends Controller> getControllerClass() {
 	public Class<? extends Controller> getControllerClass() {

+ 48 - 0
src/main/java/com/jfinal/core/paragetter/NullParaProcessor.java

@@ -0,0 +1,48 @@
+/**
+ * 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.core.paragetter;
+
+import com.jfinal.core.Action;
+import com.jfinal.core.Controller;
+
+/**
+ * 无参 action 共享同一个 NullParaProcessor 对象,节省空间
+ * 
+ * 其它所有 ParaProcessor 对象的 get(Action action, Controller c)
+ * 内部不必进行 null 值判断,节省时间
+ */
+public class NullParaProcessor extends ParaProcessor {
+	
+	private static final Object[] NULL_ARGS = new Object[0];
+	
+	public static final NullParaProcessor me = new NullParaProcessor(0);
+	
+	private NullParaProcessor(int paraCount) {
+		super(paraCount);
+	}
+	
+	@Override
+	public Object[] get(Action action, Controller c) {
+		return NULL_ARGS;
+	}
+}
+
+
+
+
+
+

+ 39 - 30
src/main/java/com/jfinal/core/paragetter/ParaProcessor.java

@@ -1,5 +1,5 @@
 /**
 /**
- * Copyright (c) 2011-2017, 玛雅牛 (myaniu AT gmail.com).
+ * Copyright (c) 2011-2019, 玛雅牛 (myaniu AT gmail.com).
  *
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * you may not use this file except in compliance with the License.
@@ -15,47 +15,56 @@
  */
  */
 package com.jfinal.core.paragetter;
 package com.jfinal.core.paragetter;
 
 
-import java.util.ArrayList;
-import java.util.List;
-
 import com.jfinal.core.Action;
 import com.jfinal.core.Action;
 import com.jfinal.core.Controller;
 import com.jfinal.core.Controller;
 
 
-public class ParaProcessor implements IParaGetter<Object[]>{
-	
-	private final List<IParaGetter<?>> paraGetters;
+public class ParaProcessor implements IParaGetter<Object[]> {
 	
 	
-	private static final Object[] NULL_ARGS = new Object[0];
+	private int fileParaIndex = -1;
+	private IParaGetter<?>[] paraGetters;
 	
 	
-	public ParaProcessor(int paramCount){
-		if( paramCount > 0){
-			this.paraGetters = new ArrayList<IParaGetter<?>>(paramCount);
-		}else{
-			this.paraGetters = null;
-		}
-	}
-	public void addParaGetterToHeader(IParaGetter<?> paraGetter){
-		if(this.paraGetters != null){
-			this.paraGetters.add(0, paraGetter);
-		}
+	public ParaProcessor(int paraCount) {
+		paraGetters = paraCount > 0 ? new IParaGetter<?>[paraCount] : null;
 	}
 	}
 	
 	
-	public void addParaGetter(IParaGetter<?> paraGetter){
-		if(this.paraGetters != null){
-			this.paraGetters.add(paraGetter);
+	public void addParaGetter(int index, IParaGetter<?> paraGetter) {
+		// fileParaIndex 记录第一个 File、UploadFile 的数组下标
+		if (	fileParaIndex == -1 &&
+				(paraGetter instanceof FileGetter || paraGetter instanceof UploadFileGetter)) {
+			fileParaIndex = index;
 		}
 		}
+		
+		paraGetters[index] = paraGetter;
 	}
 	}
+	
 	@Override
 	@Override
 	public Object[] get(Action action, Controller c) {
 	public Object[] get(Action action, Controller c) {
-		if(this.paraGetters != null){
-			List<Object> parameters = new ArrayList<Object>(this.paraGetters.size());
-			for(IParaGetter<?> paraGetter : this.paraGetters ){
-			    Object obj = paraGetter.get(action,c);
-				parameters.add(obj);
+		int len = paraGetters.length;
+		Object[] ret = new Object[len];
+		
+		// 没有 File、UploadFile 参数的 action
+		if (fileParaIndex == -1) {
+			for (int i=0; i<len; i++) {
+				ret[i] = paraGetters[i].get(action, c);
+			}
+			return ret;
+		}
+		
+		// 有 File、UploadFile 参数的 action,优先获取 File、UploadFile 对象
+		Object fileRet = paraGetters[fileParaIndex].get(action, c);
+		for (int i=0; i<len; i++) {
+			if (i != fileParaIndex) {
+				ret[i] = paraGetters[i].get(action, c);	
+			} else {
+				ret[i] = fileRet;
 			}
 			}
-			return parameters.toArray();
-		}else{
-			return NULL_ARGS;
 		}
 		}
+		return ret;
 	}
 	}
 }
 }
+
+
+
+
+
+

+ 16 - 19
src/main/java/com/jfinal/core/paragetter/ParaProcessorBuilder.java

@@ -1,5 +1,5 @@
 /**
 /**
- * Copyright (c) 2011-2017, 玛雅牛 (myaniu AT gmail.com).
+ * Copyright (c) 2011-2019, 玛雅牛 (myaniu AT gmail.com).
  *
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@ import com.jfinal.log.Log;
 
 
 public class ParaProcessorBuilder {
 public class ParaProcessorBuilder {
 
 
-	private final static ParaProcessorBuilder me = new ParaProcessorBuilder();
+	public static final ParaProcessorBuilder me = new ParaProcessorBuilder();
 	private Map<String, Holder> typeMap = new HashMap<String, Holder>();
 	private Map<String, Holder> typeMap = new HashMap<String, Holder>();
 	private static final Log log = Log.getLog(ParaProcessorBuilder.class);
 	private static final Log log = Log.getLog(ParaProcessorBuilder.class);
 
 
@@ -57,10 +57,6 @@ public class ParaProcessorBuilder {
 		regist(com.jfinal.core.paragetter.RawPostData.class, RawPostDataGetter.class, null);
 		regist(com.jfinal.core.paragetter.RawPostData.class, RawPostDataGetter.class, null);
 		
 		
 	}
 	}
-
-	public static ParaProcessorBuilder me() {
-		return me;
-	}
 	
 	
 	/**
 	/**
 	 * 注册一个类型对应的参数获取器 
 	 * 注册一个类型对应的参数获取器 
@@ -74,21 +70,22 @@ public class ParaProcessorBuilder {
 	}
 	}
 
 
 	public ParaProcessor build(Class<? extends Controller> controllerClass, Method method) {
 	public ParaProcessor build(Class<? extends Controller> controllerClass, Method method) {
-		final int parameterCount = method.getParameterCount();
-		ParaProcessor opag = new ParaProcessor(parameterCount);
-		if (0 == parameterCount) {
-			return opag;
+		final int paraCount = method.getParameterCount();
+		
+		// 无参 action 共享同一个对象,该分支以外的所有 ParaProcessor 都是有参 action,不必进行 null 值判断
+		if (paraCount == 0) {
+			return NullParaProcessor.me;
 		}
 		}
-		for (Parameter p : method.getParameters()) {
-			IParaGetter<?> pg = createParaGetter(controllerClass, method, p);
-			//存在文件的情况下,文件需要优先获取才行
-			if (pg instanceof FileGetter) {
-				opag.addParaGetterToHeader(pg);
-			} else {
-				opag.addParaGetter(pg);
-			}
+		
+		ParaProcessor ret = new ParaProcessor(paraCount);
+		
+		Parameter[] paras = method.getParameters();
+		for (int i=0; i<paraCount; i++) {
+			IParaGetter<?> pg = createParaGetter(controllerClass, method, paras[i]);
+			ret.addParaGetter(i, pg);
 		}
 		}
-		return opag;
+		
+		return ret;
 	}
 	}
 
 
 	@SuppressWarnings({ "unchecked", "rawtypes" })
 	@SuppressWarnings({ "unchecked", "rawtypes" })