8 Commits

Author SHA1 Message Date
James
04332bab30 enjoy 4.8 release ^_^ 2019-12-10 01:33:19 +08:00
James
a0dfe76bb1 enjoy 4.8 release ^_^ 2019-12-10 01:28:18 +08:00
James
468f75b39e enjoy 4.8 2019-11-09 17:54:10 +08:00
James
b4af3f62f7 enjoy 4.8 2019-11-07 18:01:03 +08:00
James
0f27710991 enjoy 4.7 release ^_^ 2019-10-30 00:06:13 +08:00
James
c4ef9561bc enjoy 4.6 2019-10-30 00:05:01 +08:00
James
1996b12013 enjoy 4.7 release ^_^ 2019-10-30 00:01:20 +08:00
James
4069806028 enjoy 4.6 release ^_^ 2019-10-13 22:36:49 +08:00
15 changed files with 174 additions and 53 deletions

View File

@@ -8,7 +8,7 @@ Enjoy 是基于 Java 语言的极轻量极魔板引擎。极轻量级仅 227 KB
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>enjoy</artifactId>
<version>4.5</version>
<version>4.8</version>
</dependency>
```
@@ -25,7 +25,49 @@ Enjoy 是基于 Java 语言的极轻量极魔板引擎。极轻量级仅 227 KB
#### 简单示例:
**1. 在 spring 中的配置**
**1. 与 Spring boot 整合**
```java
@Configuration
public class SpringBootConfig {
@Bean(name = "jfinalViewResolver")
public JFinalViewResolver getJFinalViewResolver() {
// 创建用于整合 spring boot 的 ViewResolver 扩展对象
JFinalViewResolver jfr = new JFinalViewResolver();
// 对 spring boot 进行配置
jfr.setSuffix(".html");
jfr.setContentType("text/html;charset=UTF-8");
jfr.setOrder(0);
// 获取 engine 对象,对 enjoy 模板引擎进行配置,配置方式与前面章节完全一样
Engine engine = JFinalViewResolver.engine;
// 热加载配置能对后续配置产生影响,需要放在最前面
engine.setDevMode(true);
// 使用 ClassPathSourceFactory 从 class path 与 jar 包中加载模板文件
engine.setToClassPathSourceFactory();
// 在使用 ClassPathSourceFactory 时要使用 setBaseTemplatePath
// 代替 jfr.setPrefix("/view/")
engine.setBaseTemplatePath("/view/");
// 添加模板函数
engine.addSharedFunction("/common/_layout.html");
engine.addSharedFunction("/common/_paginate.html");
// 更多配置与前面章节完全一样
// engine.addDirective(...)
// engine.addSharedMethod(...);
return jfr;
}
}
```
**2. 与 Spring MVC 整合**
```java
<bean id="viewResolver" class="com.jfinal.template.ext.spring.JFinalViewResolver">
@@ -43,7 +85,7 @@ Enjoy 是基于 Java 语言的极轻量极魔板引擎。极轻量级仅 227 KB
</bean>
```
**2.详细使用方法见 jfinal 手册**
**3.详细使用方法见官方文档**
read me 正在补充,详细使用文档见官网:[https://www.jfinal.com/doc/6-1](https://www.jfinal.com/doc/6-1)

View File

@@ -4,7 +4,7 @@
<groupId>com.jfinal</groupId>
<artifactId>enjoy</artifactId>
<version>4.5</version>
<version>4.8</version>
<packaging>jar</packaging>

View File

@@ -128,7 +128,8 @@ public class ProxyCompiler {
public void compile(ProxyClass proxyClass) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new RuntimeException("Can not get javax.tools.JavaCompiler, check whether \"tools.jar\" is in the environment variable CLASSPATH");
throw new RuntimeException("Can not get javax.tools.JavaCompiler, check whether \"tools.jar\" is in the environment variable CLASSPATH \n" +
"Visit https://jfinal.com/doc/4-8 for details \n");
}
DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<>();

View File

@@ -102,6 +102,14 @@ public class Template {
}
}
/**
* 支持无 data 参数,渲染到 String 中去 <br>
* 适用于数据在模板中通过表达式和语句直接计算得出等等应用场景
*/
public String renderToString() {
return renderToString(null);
}
/**
* 渲染到 StringBuilder 中去
*/

View File

@@ -79,7 +79,7 @@ public class Method extends Expr {
try {
MethodInfo methodInfo = MethodKit.getMethod(target.getClass(), methodName, argValues);
if (methodInfo != null) {
if (methodInfo.notNull()) {
return methodInfo.invoke(target, argValues);
}

View File

@@ -93,6 +93,29 @@ public class MethodInfo {
}
return ret.append(")").toString();
}
// --------- 以下代码仅用于支持 NullMethodInfo
/**
* 仅供 NullMethodInfo 继承使用
*/
protected MethodInfo() {
this.key = null;
this.clazz = null;
this.method = null;
this.isVarArgs = false;
this.paraTypes = null;
}
/**
* 仅仅 NullMethodInfo 会覆盖此方法并返回 false
*
* 1MethodKit.getMethod(...) 消除 instanceof 判断
* 2Method.exec(...) 消除 null 值判断
*/
public boolean notNull() {
return true;
}
}

View File

@@ -41,7 +41,7 @@ public class MethodKit {
private static final Set<String> forbiddenMethods = new HashSet<String>(64);
private static final Set<Class<?>> forbiddenClasses = new HashSet<Class<?>>(64);
private static final Map<Class<?>, Class<?>> primitiveMap = new HashMap<Class<?>, Class<?>>(64);
private static final SyncWriteMap<Long, Object> methodCache = new SyncWriteMap<Long, Object>(2048, 0.25F);
private static final SyncWriteMap<Long, MethodInfo> methodCache = new SyncWriteMap<Long, MethodInfo>(2048, 0.25F);
// 初始化在模板中调用 method 时所在的被禁止使用类
static {
@@ -49,7 +49,9 @@ public class MethodKit {
System.class, Runtime.class, Thread.class, Class.class, ClassLoader.class, File.class,
Compiler.class, InheritableThreadLocal.class, Package.class, Process.class,
RuntimePermission.class, SecurityManager.class, ThreadGroup.class, ThreadLocal.class,
java.lang.reflect.Method.class
java.lang.reflect.Method.class,
java.lang.reflect.Proxy.class
};
for (Class<?> c : cs) {
forbiddenClasses.add(c);
@@ -62,8 +64,8 @@ public class MethodKit {
"getClass", "getDeclaringClass", "forName", "newInstance", "getClassLoader",
"invoke", // "getMethod", "getMethods", // "getField", "getFields",
"notify", "notifyAll", "wait",
"load", "exit", "loadLibrary", "halt",
"stop", "suspend", "resume", "setDaemon", "setPriority",
"exit", "loadLibrary", "halt", // "load",
"stop", "suspend", "resume" // "setDaemon", "setPriority"
};
for (String m : ms) {
forbiddenMethods.add(m);
@@ -122,35 +124,16 @@ public class MethodKit {
public static MethodInfo getMethod(Class<?> targetClass, String methodName, Object[] argValues) {
Class<?>[] argTypes = getArgTypes(argValues);
Long key = getMethodKey(targetClass, methodName, argTypes);
Object method = methodCache.get(key);
MethodInfo method = methodCache.get(key);
if (method == null) {
// 已确保不会返回 null对于不存在的 Method只进行一次获取操作
// 提升 null safe 表达式性能,未来需要考虑内存泄漏风险
method = doGetMethod(key, targetClass, methodName, argTypes);
if (method != null) {
methodCache.putIfAbsent(key, method);
} else {
// 对于不存在的 Method只进行一次获取操作主要为了支持 null safe未来需要考虑内存泄漏风险
methodCache.putIfAbsent(key, Void.class);
}
}
return method instanceof MethodInfo ? (MethodInfo)method : null;
}
/**
* 获取 getter 方法
* 使用与 Field 相同的 key避免生成两次 key值
* ---> jfinal 3.5 已将此功能转移至 FieldKit
public static MethodInfo getGetterMethod(Long key, Class<?> targetClass, String methodName) {
Object getterMethod = methodCache.get(key);
if (getterMethod == null) {
getterMethod = doGetMethod(key, targetClass, methodName, NULL_ARG_TYPES);
if (getterMethod != null) {
methodCache.putIfAbsent(key, getterMethod);
} else {
methodCache.putIfAbsent(key, Void.class);
return method;
}
}
return getterMethod instanceof MethodInfo ? (MethodInfo)getterMethod : null;
} */
static Class<?>[] getArgTypes(Object[] argValues) {
if (argValues == null || argValues.length == 0) {
@@ -167,7 +150,9 @@ public class MethodKit {
if (forbiddenClasses.contains(targetClass)) {
throw new RuntimeException("Forbidden class: " + targetClass.getName());
}
// 仅开启 forbiddenClasses 检测
// Method、SharedMethod、StaticMethod 已用 MethodKit.isForbiddenMethod(...) 检测
// if (forbiddenMethods.contains(methodName)) {
// throw new RuntimeException("Forbidden method: " + methodName);
// }
@@ -184,7 +169,8 @@ public class MethodKit {
}
}
}
return null;
return NullMethodInfo.me;
}
static boolean matchFixedArgTypes(Class<?>[] paraTypes, Class<?>[] argTypes) {

View File

@@ -0,0 +1,37 @@
/**
* 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.template.expr.ast;
/**
* NullMethodInfo
*
* 1MethodKit.getMethod(...) 消除 instanceof 判断
* 2Method.exec(...) 消除 null 值判断
*/
public class NullMethodInfo extends MethodInfo {
public static final NullMethodInfo me = new NullMethodInfo();
public boolean notNull() {
return false;
}
public Object invoke(Object target, Object... args) throws ReflectiveOperationException {
throw new RuntimeException("The method invoke(Object, Object...) of NullMethodInfo should not be invoked");
}
}

View File

@@ -50,6 +50,14 @@ public class StaticMethod extends Expr {
} catch (Exception e) {
throw new ParseException(e.getMessage(), location, e);
}
if (MethodKit.isForbiddenClass(this.clazz)) {
throw new ParseException("Forbidden class: " + this.clazz.getName(), location);
}
if (MethodKit.isForbiddenMethod(methodName)) {
throw new ParseException("Forbidden method: " + methodName, location);
}
this.methodName = methodName;
this.exprList = exprList;
this.location = location;
@@ -61,7 +69,7 @@ public class StaticMethod extends Expr {
try {
MethodInfo methodInfo = MethodKit.getMethod(clazz, methodName, argValues);
if (methodInfo != null) {
if (methodInfo.notNull()) {
if (methodInfo.isStatic()) {
return methodInfo.invoke(null, argValues);
} else {

View File

@@ -53,10 +53,9 @@ public class NumberDirective extends Directive {
private Expr valueExpr;
private Expr patternExpr;
private int paraNum;
public void setExprList(ExprList exprList) {
this.paraNum = exprList.length();
int paraNum = exprList.length();
if (paraNum == 0) {
throw new ParseException("The parameter of #number directive can not be blank", location);
}
@@ -64,13 +63,8 @@ public class NumberDirective extends Directive {
throw new ParseException("Wrong number parameter of #number directive, two parameters allowed at most", location);
}
if (paraNum == 1) {
this.valueExpr = exprList.getExpr(0);
this.patternExpr = null;
} else if (paraNum == 2) {
this.valueExpr = exprList.getExpr(0);
this.patternExpr = exprList.getExpr(1);
}
valueExpr = exprList.getExpr(0);
patternExpr = (paraNum == 1 ? null : exprList.getExpr(1));
}
public void exec(Env env, Scope scope, Writer writer) {
@@ -79,9 +73,9 @@ public class NumberDirective extends Directive {
return ;
}
if (paraNum == 1) {
if (patternExpr == null) {
outputWithoutPattern(writer, value);
} else if (paraNum == 2) {
} else {
outputWithPattern(scope, writer, value);
}
}

View File

@@ -194,6 +194,14 @@ public class JFinalViewResolver extends AbstractTemplateViewResolver {
engine.setSourceFactory(sourceFactory);
}
/**
* 设置为 ClassPathSourceFactory 的快捷方法
* ClassPathSourceFactory 将从 CLASSPATH 与 jar 包中读取模板
*/
public void setToClassPathSourceFactory() {
engine.setToClassPathSourceFactory();
}
/**
* 设置模板基础路径
*/

View File

@@ -78,6 +78,7 @@ public class Scope {
if (cur.data == null) { // 支持顶层 data 为 null 值
cur.data = new HashMap();
}
cur.data.put(key, value);
return ;
}
@@ -159,6 +160,10 @@ public class Scope {
public void setGlobal(Object key, Object value) {
for (Scope cur=this; true; cur=cur.parent) {
if (cur.parent == null) {
if (cur.data == null) {
cur.data = new HashMap();
}
cur.data.put(key, value);
return ;
}
@@ -172,7 +177,7 @@ public class Scope {
public Object getGlobal(Object key) {
for (Scope cur=this; true; cur=cur.parent) {
if (cur.parent == null) {
return cur.data.get(key);
return cur.data != null ? cur.data.get(key) : null;
}
}
}
@@ -184,7 +189,10 @@ public class Scope {
public void removeGlobal(Object key) {
for (Scope cur=this; true; cur=cur.parent) {
if (cur.parent == null) {
if (cur.data != null) {
cur.data.remove(key);
}
return ;
}
}

View File

@@ -20,6 +20,7 @@ import com.jfinal.template.Env;
import com.jfinal.template.expr.ast.Assign;
import com.jfinal.template.expr.ast.Expr;
import com.jfinal.template.expr.ast.ExprList;
import com.jfinal.template.expr.ast.IncDec;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Location;
import com.jfinal.template.stat.ParseException;
@@ -44,10 +45,11 @@ public class Set extends Stat {
}
for (Expr expr : exprList.getExprArray()) {
if ( !(expr instanceof Assign) ) {
if ( !(expr instanceof Assign || expr instanceof IncDec) ) {
throw new ParseException("#set directive only supports assignment expressions", location);
}
}
this.expr = exprList.getActualExpr();
}

View File

@@ -20,6 +20,7 @@ import com.jfinal.template.Env;
import com.jfinal.template.expr.ast.Assign;
import com.jfinal.template.expr.ast.Expr;
import com.jfinal.template.expr.ast.ExprList;
import com.jfinal.template.expr.ast.IncDec;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Ctrl;
import com.jfinal.template.stat.Location;
@@ -41,10 +42,11 @@ public class SetGlobal extends Stat {
}
for (Expr expr : exprList.getExprArray()) {
if ( !(expr instanceof Assign) ) {
if ( !(expr instanceof Assign || expr instanceof IncDec) ) {
throw new ParseException("#setGlobal directive only supports assignment expressions", location);
}
}
this.expr = exprList.getActualExpr();
}

View File

@@ -20,6 +20,7 @@ import com.jfinal.template.Env;
import com.jfinal.template.expr.ast.Assign;
import com.jfinal.template.expr.ast.Expr;
import com.jfinal.template.expr.ast.ExprList;
import com.jfinal.template.expr.ast.IncDec;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Ctrl;
import com.jfinal.template.stat.Location;
@@ -42,10 +43,11 @@ public class SetLocal extends Stat {
}
for (Expr expr : exprList.getExprArray()) {
if ( !(expr instanceof Assign) ) {
if ( !(expr instanceof Assign || expr instanceof IncDec) ) {
throw new ParseException("#setLocal directive only supports assignment expressions", location);
}
}
this.expr = exprList.getActualExpr();
}