Compare commits
74 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
04332bab30 | ||
|
a0dfe76bb1 | ||
|
468f75b39e | ||
|
b4af3f62f7 | ||
|
0f27710991 | ||
|
c4ef9561bc | ||
|
1996b12013 | ||
|
4069806028 | ||
|
8cf09eac35 | ||
|
550ddaba53 | ||
|
260dd4b438 | ||
|
442a920366 | ||
|
6156051e16 | ||
|
75e7caf0ec | ||
|
4a5cfe5ed5 | ||
|
8bdd4719c8 | ||
|
1939fb4cc0 | ||
|
c88f7baad9 | ||
|
5649576dde | ||
|
2224e0d212 | ||
|
1a6006fe36 | ||
|
12c3b5fdb5 | ||
|
f065faf4e7 | ||
|
b4e277fcc0 | ||
|
bbc1e24b87 | ||
|
34c1a9e53a | ||
|
f6a855b6bf | ||
|
4377d19e2f | ||
|
820f2806ec | ||
|
808bdf6079 | ||
|
eac1d8d055 | ||
|
d5a88b8be4 | ||
|
fd5d554171 | ||
|
6d18be3df8 | ||
|
869824e2bb | ||
|
84573be584 | ||
|
3cc94a5b32 | ||
|
3a4f4f4495 | ||
|
d250b431a4 | ||
|
5e133e7de5 | ||
|
ef39843a25 | ||
|
0e5f3b7249 | ||
|
6f5cd47376 | ||
|
b23a1a9133 | ||
|
4c63d00157 | ||
|
972c7e7673 | ||
|
6d7d0af2b2 | ||
|
6bf9ae3f59 | ||
|
d46ca53a34 | ||
|
985b02177e | ||
|
b9cc8a58e0 | ||
|
31ca22d21d | ||
|
13f2d302c3 | ||
|
49d53e9f55 | ||
|
8d88d0bba4 | ||
|
462c70b692 | ||
|
1ce7068072 | ||
|
3e89651aa4 | ||
|
1aef12eb94 | ||
|
6f615bc1a8 | ||
|
0c3e7790b7 | ||
|
a1e88a4a4c | ||
|
d12d3d8abb | ||
|
ef7b0da917 | ||
|
2ed806c296 | ||
|
998df7b907 | ||
|
3e93fc970e | ||
|
010a7f3d8a | ||
|
99f024f546 | ||
|
ca3361703e | ||
|
1840d49e0a | ||
|
70d1e5a9eb | ||
|
e0abc218b1 | ||
|
bd1949fc56 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -50,3 +50,7 @@ integration-repo
|
|||||||
a_little_config_pro.txt
|
a_little_config_pro.txt
|
||||||
|
|
||||||
dev_plan.txt
|
dev_plan.txt
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
3
LICENSE
3
LICENSE
@@ -189,3 +189,6 @@ third-party archives.
|
|||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
See the License for the specific language governing permissions and
|
See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
73
README.md
73
README.md
@@ -1,21 +1,73 @@
|
|||||||
### Enjoy
|
### Enjoy
|
||||||
|
|
||||||
Enjoy 是基于 Java 语言的极轻量极魔板引擎。极轻量级仅 171K 并且不依赖任何第三方。极简设计仅 if、for、set、define、include、render 六个核心指令,让学习成本低到极致。独创 DKFF(Dynamic Key Feature Forward) 词法分析算法与 DLRD (Double Layer Recursive Descent)语法分析算法,避免使用 javacc、antlr、jflex 生成器,令代码量少到极致。
|
Enjoy 是基于 Java 语言的极轻量极魔板引擎。极轻量级仅 227 KB 并且不依赖任何第三方。极简设计仅 if、for、switch、set、define、include、render 七个核心指令,让学习成本低到极致。独创 DKFF(Dynamic Key Feature Forward) 词法分析算法与 DLRD (Double Layer Recursive Descent)语法分析算法,避免使用 javacc、antlr、jflex 生成器,令代码量少到极致。
|
||||||
|
|
||||||
|
#### Maven 坐标
|
||||||
|
|
||||||
|
```java
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.jfinal</groupId>
|
||||||
|
<artifactId>enjoy</artifactId>
|
||||||
|
<version>4.8</version>
|
||||||
|
</dependency>
|
||||||
|
```
|
||||||
|
|
||||||
#### Enjoy 主要特点
|
#### Enjoy 主要特点
|
||||||
- 消灭传统模板引擎中大量繁杂概念,仅六个核心指令,学习成本极低
|
- 消灭传统模板引擎中大量繁杂概念,仅七个核心指令,学习成本极低
|
||||||
- 独创 DKFF 词法分析算法与 DLRD 语法分析算法,避免使用 javacc、antlr
|
- 独创 DKFF 词法分析算法与 DLRD 语法分析算法,避免使用 javacc、antlr
|
||||||
- 功能强大,极为简单覆盖掉 freemarker、velocity 的核心功能
|
|
||||||
- 扩展性强,支持多种扩展方式,且是唯一支持指令级扩展的模板引擎
|
|
||||||
- 与 java 打通式设计,在模板中与 java 交互极为方便
|
- 与 java 打通式设计,在模板中与 java 交互极为方便
|
||||||
- 贴近 java 使用直觉,为 java 开发者量身打造
|
- 贴近 java 使用直觉,为 java 开发者量身打造
|
||||||
|
- 功能强大,极为简单覆盖掉 freemarker、velocity 的核心功能
|
||||||
|
- 扩展性强,支持多种扩展方式,且是唯一支持指令级扩展的模板引擎
|
||||||
- 回归模板引擎渲染 View 数据的本质,采用指令式设计,避免 view 层表达复杂逻辑
|
- 回归模板引擎渲染 View 数据的本质,采用指令式设计,避免 view 层表达复杂逻辑
|
||||||
- 体积小,仅 171K,且不依赖于任何第三方
|
- 体积小,仅 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
|
```java
|
||||||
<bean id="viewResolver" class="com.jfinal.template.ext.spring.JFinalViewResolver">
|
<bean id="viewResolver" class="com.jfinal.template.ext.spring.JFinalViewResolver">
|
||||||
@@ -33,10 +85,13 @@ Enjoy 是基于 Java 语言的极轻量极魔板引擎。极轻量级仅 171K
|
|||||||
</bean>
|
</bean>
|
||||||
```
|
```
|
||||||
|
|
||||||
**2.详细使用方法见 jfinal 手册**
|
**3.详细使用方法见官方文档**
|
||||||
read me 正在补充,详细使用文档请下载 jfinal.com 官网的 jfinal 手册[http://www.jfinal.com](http://www.jfinal.com)
|
|
||||||
|
read me 正在补充,详细使用文档见官网:[https://www.jfinal.com/doc/6-1](https://www.jfinal.com/doc/6-1)
|
||||||
|
|
||||||
|
**JFinal Enjoy 官方文档:[https://www.jfinal.com/doc/6-1](https://www.jfinal.com/doc/6-1)**
|
||||||
|
|
||||||
|
|
||||||
**JFinal 官方网站:[http://www.jfinal.com](http://www.jfinal.com)**
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
59
pom.xml
59
pom.xml
@@ -1,12 +1,16 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>com.jfinal</groupId>
|
<groupId>com.jfinal</groupId>
|
||||||
<artifactId>enjoy</artifactId>
|
<artifactId>enjoy</artifactId>
|
||||||
|
<version>4.8</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
|
||||||
<name>enjoy</name>
|
<name>enjoy</name>
|
||||||
<version>3.3</version>
|
|
||||||
<url>http://www.jfinal.com</url>
|
|
||||||
<description>Enjoy is a simple, light, rapid, independent, extensible Java Template Engine.</description>
|
<description>Enjoy is a simple, light, rapid, independent, extensible Java Template Engine.</description>
|
||||||
|
<url>http://www.jfinal.com</url>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
@@ -15,14 +19,16 @@
|
|||||||
|
|
||||||
<issueManagement>
|
<issueManagement>
|
||||||
<system>Git Issue</system>
|
<system>Git Issue</system>
|
||||||
<url>http://git.oschina.net/jfinal/enjoy/issues</url>
|
<url>https://gitee.com/jfinal/enjoy/issues</url>
|
||||||
</issueManagement>
|
</issueManagement>
|
||||||
|
|
||||||
<licenses>
|
<licenses>
|
||||||
<license>
|
<license>
|
||||||
<name>The Apache Software License, Version 2.0</name>
|
<name>The Apache Software License, Version 2.0</name>
|
||||||
<url>http://apache.org/licenses/LICENSE-2.0.txt</url>
|
<url>http://apache.org/licenses/LICENSE-2.0.txt</url>
|
||||||
</license>
|
</license>
|
||||||
</licenses>
|
</licenses>
|
||||||
|
|
||||||
<developers>
|
<developers>
|
||||||
<developer>
|
<developer>
|
||||||
<id>jfinal</id>
|
<id>jfinal</id>
|
||||||
@@ -31,20 +37,27 @@
|
|||||||
<url>http://jfinal.com/user/1</url>
|
<url>http://jfinal.com/user/1</url>
|
||||||
</developer>
|
</developer>
|
||||||
</developers>
|
</developers>
|
||||||
|
|
||||||
<scm>
|
<scm>
|
||||||
<connection>scm:git:git@git.oschina.net:jfinal/enjoy.git</connection>
|
<connection>scm:git:git@gitee.com:jfinal/enjoy.git</connection>
|
||||||
<developerConnection>scm:git:git@git.oschina.net:jfinal/enjoy.git</developerConnection>
|
<developerConnection>scm:git:git@gitee.com:jfinal/enjoy.git</developerConnection>
|
||||||
<url>git@git.oschina.net:jfinal/enjoy.git</url>
|
<url>git@gitee.com:jfinal/enjoy.git</url>
|
||||||
</scm>
|
</scm>
|
||||||
|
|
||||||
<parent>
|
<distributionManagement>
|
||||||
<groupId>org.sonatype.oss</groupId>
|
<snapshotRepository>
|
||||||
<artifactId>oss-parent</artifactId>
|
<id>ossrh</id>
|
||||||
<version>7</version>
|
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
|
||||||
</parent>
|
</snapshotRepository>
|
||||||
|
<repository>
|
||||||
|
<id>ossrh</id>
|
||||||
|
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
|
||||||
|
</repository>
|
||||||
|
</distributionManagement>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
@@ -73,22 +86,29 @@
|
|||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.6.1</version>
|
<version>3.6.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>1.6</source>
|
<source>1.8</source>
|
||||||
<target>1.6</target>
|
<target>1.8</target>
|
||||||
|
<encoding>UTF-8</encoding>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
<version>2.10.3</version>
|
<version>2.10.4</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<!-- 解决 java8 发布到 maven 异常 -->
|
<!-- 解决 java8 发布到 maven 异常 -->
|
||||||
<additionalparam>-Xdoclint:none</additionalparam>
|
<additionalparam>-Xdoclint:none</additionalparam>
|
||||||
<encoding>UTF-8</encoding>
|
<encoding>UTF-8</encoding>
|
||||||
<outputDirectory>${basedir}</outputDirectory>
|
|
||||||
<reportOutputDirectory>${basedir}</reportOutputDirectory>
|
|
||||||
</configuration>
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>attach-javadocs</id>
|
||||||
|
<goals>
|
||||||
|
<goal>jar</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<!-- 安装源码到本地仓库 -->
|
<!-- 安装源码到本地仓库 -->
|
||||||
@@ -110,7 +130,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-gpg-plugin</artifactId>
|
<artifactId>maven-gpg-plugin</artifactId>
|
||||||
<version>1.1</version>
|
<version>1.6</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>sign-artifacts</id>
|
<id>sign-artifacts</id>
|
||||||
@@ -120,11 +140,10 @@
|
|||||||
</goals>
|
</goals>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
<configuration>
|
|
||||||
<skip>false</skip>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</project>
|
</project>
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -45,7 +45,7 @@ public class ElKit {
|
|||||||
engine.addDirective("eval", InnerEvalDirective.class);
|
engine.addDirective("eval", InnerEvalDirective.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Engine getEngine() {
|
public static Engine getEngine() {
|
||||||
return engine;
|
return engine;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -30,11 +30,6 @@ import java.util.Map;
|
|||||||
@SuppressWarnings({"serial", "rawtypes", "unchecked"})
|
@SuppressWarnings({"serial", "rawtypes", "unchecked"})
|
||||||
public class Kv extends HashMap {
|
public class Kv extends HashMap {
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
private static final String STATE_OK = "isOk";
|
|
||||||
@Deprecated
|
|
||||||
private static final String STATE_FAIL = "isFail";
|
|
||||||
|
|
||||||
public Kv() {
|
public Kv() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,73 +41,25 @@ public class Kv extends HashMap {
|
|||||||
return new Kv();
|
return new Kv();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static Kv ok() {
|
|
||||||
return new Kv().setOk();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static Kv ok(Object key, Object value) {
|
|
||||||
return ok().set(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static Kv fail() {
|
|
||||||
return new Kv().setFail();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static Kv fail(Object key, Object value) {
|
|
||||||
return fail().set(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public Kv setOk() {
|
|
||||||
super.put(STATE_OK, Boolean.TRUE);
|
|
||||||
super.put(STATE_FAIL, Boolean.FALSE);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public Kv setFail() {
|
|
||||||
super.put(STATE_FAIL, Boolean.TRUE);
|
|
||||||
super.put(STATE_OK, Boolean.FALSE);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public boolean isOk() {
|
|
||||||
Boolean isOk = (Boolean)get(STATE_OK);
|
|
||||||
if (isOk != null) {
|
|
||||||
return isOk;
|
|
||||||
}
|
|
||||||
Boolean isFail = (Boolean)get(STATE_FAIL);
|
|
||||||
if (isFail != null) {
|
|
||||||
return !isFail;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new IllegalStateException("调用 isOk() 之前,必须先调用 ok()、fail() 或者 setOk()、setFail() 方法");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public boolean isFail() {
|
|
||||||
Boolean isFail = (Boolean)get(STATE_FAIL);
|
|
||||||
if (isFail != null) {
|
|
||||||
return isFail;
|
|
||||||
}
|
|
||||||
Boolean isOk = (Boolean)get(STATE_OK);
|
|
||||||
if (isOk != null) {
|
|
||||||
return !isOk;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new IllegalStateException("调用 isFail() 之前,必须先调用 ok()、fail() 或者 setOk()、setFail() 方法");
|
|
||||||
}
|
|
||||||
|
|
||||||
public Kv set(Object key, Object value) {
|
public Kv set(Object key, Object value) {
|
||||||
super.put(key, value);
|
super.put(key, value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Kv setIfNotBlank(Object key, String value) {
|
||||||
|
if (StrKit.notBlank(value)) {
|
||||||
|
set(key, value);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Kv setIfNotNull(Object key, Object value) {
|
||||||
|
if (value != null) {
|
||||||
|
set(key, value);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Kv set(Map map) {
|
public Kv set(Map map) {
|
||||||
super.putAll(map);
|
super.putAll(map);
|
||||||
return this;
|
return this;
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -16,6 +16,9 @@
|
|||||||
|
|
||||||
package com.jfinal.kit;
|
package com.jfinal.kit;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Parameter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 反射工具类
|
* 反射工具类
|
||||||
*/
|
*/
|
||||||
@@ -24,11 +27,50 @@ public class ReflectKit {
|
|||||||
public static Object newInstance(Class<?> clazz) {
|
public static Object newInstance(Class<?> clazz) {
|
||||||
try {
|
try {
|
||||||
return clazz.newInstance();
|
return clazz.newInstance();
|
||||||
} catch (Exception e) {
|
} catch (ReflectiveOperationException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getMethodSignature(Method method) {
|
||||||
|
StringBuilder ret = new StringBuilder()
|
||||||
|
.append(method.getDeclaringClass().getName())
|
||||||
|
.append(".")
|
||||||
|
.append(method.getName())
|
||||||
|
.append("(");
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
Parameter[] paras = method.getParameters();
|
||||||
|
for (Parameter p : paras) {
|
||||||
|
if (index++ > 0) {
|
||||||
|
ret.append(", ");
|
||||||
|
}
|
||||||
|
ret.append(p.getParameterizedType().getTypeName());
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret.append(")").toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
public static String getMethodSignature(Method method) {
|
||||||
|
StringBuilder ret = new StringBuilder()
|
||||||
|
.append(method.getDeclaringClass().getName())
|
||||||
|
.append(".")
|
||||||
|
.append(method.getName())
|
||||||
|
.append("(");
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
java.lang.reflect.Type[] paraTypes = method.getGenericParameterTypes();
|
||||||
|
for (java.lang.reflect.Type type : paraTypes) {
|
||||||
|
if (index++ > 0) {
|
||||||
|
ret.append(", ");
|
||||||
|
}
|
||||||
|
ret.append(type.getTypeName());
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret.append(")").toString();
|
||||||
|
}*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -102,6 +102,10 @@ public class StrKit {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String defaultIfBlank(String str, String defaultValue) {
|
||||||
|
return isBlank(str) ? defaultValue : str;
|
||||||
|
}
|
||||||
|
|
||||||
public static String toCamelCase(String stringWithUnderline) {
|
public static String toCamelCase(String stringWithUnderline) {
|
||||||
if (stringWithUnderline.indexOf('_') == -1) {
|
if (stringWithUnderline.indexOf('_') == -1) {
|
||||||
return stringWithUnderline;
|
return stringWithUnderline;
|
||||||
@@ -145,6 +149,17 @@ public class StrKit {
|
|||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String join(java.util.List<String> list, String separator) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (int i=0, len=list.size(); i<len; i++) {
|
||||||
|
if (i > 0) {
|
||||||
|
sb.append(separator);
|
||||||
|
}
|
||||||
|
sb.append(list.get(i));
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean slowEquals(String a, String b) {
|
public static boolean slowEquals(String a, String b) {
|
||||||
byte[] aBytes = (a != null ? a.getBytes() : null);
|
byte[] aBytes = (a != null ? a.getBytes() : null);
|
||||||
byte[] bBytes = (b != null ? b.getBytes() : null);
|
byte[] bBytes = (b != null ? b.getBytes() : null);
|
||||||
|
92
src/main/java/com/jfinal/kit/SyncWriteMap.java
Normal file
92
src/main/java/com/jfinal/kit/SyncWriteMap.java
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
/**
|
||||||
|
* 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.kit;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SyncWriteMap 同步写 HashMap
|
||||||
|
* 创建原因是 HashMap扩容时,遇到并发修改可能造成 100% CPU 占用
|
||||||
|
*
|
||||||
|
* SyncWriteMap 拥有 HashMap 的性能,但不保障并发访问的线程安全
|
||||||
|
* 只用于读多写少且不用保障线程安全的场景
|
||||||
|
*
|
||||||
|
* 例如 MethodKit 中用于缓存 MethodInfo 的 cache,被写入的数据
|
||||||
|
* 不用保障是单例,读取之后会做 null 值判断
|
||||||
|
*
|
||||||
|
* ActionMapping 中的 HashMap 是系统启动时在独立线程内初始化的,
|
||||||
|
* 不存在并发写,只存在并发读的情况,所以仍然可以使用 HashMap
|
||||||
|
*/
|
||||||
|
public class SyncWriteMap<K, V> extends HashMap<K, V> {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -7287230891751869148L;
|
||||||
|
|
||||||
|
public SyncWriteMap() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public SyncWriteMap(int initialCapacity) {
|
||||||
|
super(initialCapacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SyncWriteMap(int initialCapacity, float loadFactor) {
|
||||||
|
super(initialCapacity, loadFactor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SyncWriteMap(Map<? extends K, ? extends V> m) {
|
||||||
|
super(m);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V put(K key, V value) {
|
||||||
|
synchronized (this) {
|
||||||
|
return super.put(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V putIfAbsent(K key, V value) {
|
||||||
|
synchronized (this) {
|
||||||
|
return super.putIfAbsent(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void putAll(Map<? extends K, ? extends V> m) {
|
||||||
|
synchronized (this) {
|
||||||
|
super.putAll(m);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V remove(Object key) {
|
||||||
|
synchronized (this) {
|
||||||
|
return super.remove(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
synchronized (this) {
|
||||||
|
super.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
100
src/main/java/com/jfinal/proxy/ProxyClass.java
Normal file
100
src/main/java/com/jfinal/proxy/ProxyClass.java
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
/**
|
||||||
|
* 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.proxy;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ProxyClass
|
||||||
|
*/
|
||||||
|
public class ProxyClass {
|
||||||
|
|
||||||
|
// 被代理的目标
|
||||||
|
private Class<?> target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 以下是代理类信息
|
||||||
|
*/
|
||||||
|
private String pkg; // 包名
|
||||||
|
private String name; // 类名
|
||||||
|
private String sourceCode; // 源代码
|
||||||
|
private Map<String, byte[]> byteCode; // 字节码
|
||||||
|
private Class<?> clazz; // 字节码被 loadClass 后的 Class
|
||||||
|
// private List<ProxyMethod> proxyMethodList = new ArrayList<>();
|
||||||
|
|
||||||
|
public ProxyClass(Class<?> target) {
|
||||||
|
this.target = target;
|
||||||
|
this.pkg = target.getPackage().getName();
|
||||||
|
this.name = target.getSimpleName() + "$$EnhancerByJFinal";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否需要代理
|
||||||
|
*/
|
||||||
|
// public boolean needProxy() {
|
||||||
|
// return proxyMethodList.size() > 0;
|
||||||
|
// }
|
||||||
|
|
||||||
|
public Class<?> getTarget() {
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPkg() {
|
||||||
|
return pkg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSourceCode() {
|
||||||
|
return sourceCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSourceCode(String sourceCode) {
|
||||||
|
this.sourceCode = sourceCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, byte[]> getByteCode() {
|
||||||
|
return byteCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setByteCode(Map<String, byte[]> byteCode) {
|
||||||
|
this.byteCode = byteCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class<?> getClazz() {
|
||||||
|
return clazz;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setClazz(Class<?> clazz) {
|
||||||
|
this.clazz = clazz;
|
||||||
|
}
|
||||||
|
|
||||||
|
// public void addProxyMethod(ProxyMethod proxyMethod) {
|
||||||
|
// proxyMethodList.add(proxyMethod);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public List<ProxyMethod> getProxyMethodList() {
|
||||||
|
// return proxyMethodList;
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
70
src/main/java/com/jfinal/proxy/ProxyClassLoader.java
Normal file
70
src/main/java/com/jfinal/proxy/ProxyClassLoader.java
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
/**
|
||||||
|
* 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.proxy;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ProxyClassLoader
|
||||||
|
*/
|
||||||
|
public class ProxyClassLoader extends ClassLoader {
|
||||||
|
|
||||||
|
protected Map<String, byte[]> byteCodeMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
static {
|
||||||
|
registerAsParallelCapable();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProxyClassLoader() {
|
||||||
|
super(getParentClassLoader());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static ClassLoader getParentClassLoader() {
|
||||||
|
ClassLoader ret = Thread.currentThread().getContextClassLoader();
|
||||||
|
return ret != null ? ret : ProxyClassLoader.class.getClassLoader();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class<?> loadProxyClass(ProxyClass proxyClass) {
|
||||||
|
for (Entry<String, byte[]> e : proxyClass.getByteCode().entrySet()) {
|
||||||
|
byteCodeMap.putIfAbsent(e.getKey(), e.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return loadClass(proxyClass.getPkg() + "." + proxyClass.getName());
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
||||||
|
byte[] bytes = byteCodeMap.get(name);
|
||||||
|
if (bytes != null) {
|
||||||
|
Class<?> ret = defineClass(name, bytes, 0, bytes.length);
|
||||||
|
byteCodeMap.remove(name);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.findClass(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
239
src/main/java/com/jfinal/proxy/ProxyCompiler.java
Normal file
239
src/main/java/com/jfinal/proxy/ProxyCompiler.java
Normal file
@@ -0,0 +1,239 @@
|
|||||||
|
/**
|
||||||
|
* 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.proxy;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLClassLoader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Objects;
|
||||||
|
import javax.tools.DiagnosticCollector;
|
||||||
|
import javax.tools.FileObject;
|
||||||
|
import javax.tools.ForwardingJavaFileManager;
|
||||||
|
import javax.tools.JavaCompiler;
|
||||||
|
import javax.tools.JavaFileManager;
|
||||||
|
import javax.tools.JavaFileObject;
|
||||||
|
import javax.tools.SimpleJavaFileObject;
|
||||||
|
import javax.tools.ToolProvider;
|
||||||
|
// import com.jfinal.log.Log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ProxyCompiler
|
||||||
|
*
|
||||||
|
* https://www.programcreek.com/java-api-examples/?api=javax.tools.JavaCompiler
|
||||||
|
*/
|
||||||
|
public class ProxyCompiler {
|
||||||
|
|
||||||
|
// protected static final Log log = Log.getLog(ProxyCompiler.class);
|
||||||
|
|
||||||
|
// protected List<String> options = Arrays.asList("-target", "1.8" /*, "-parameters"*/);
|
||||||
|
protected volatile List<String> options = null;
|
||||||
|
|
||||||
|
protected List<String> getOptions() {
|
||||||
|
if (options != null) {
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized (this) {
|
||||||
|
if (options != null) {
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> ret = new ArrayList<>();
|
||||||
|
ret.add("-target");
|
||||||
|
ret.add("1.8");
|
||||||
|
|
||||||
|
String cp = getClassPath();
|
||||||
|
if (cp != null && cp.trim().length() != 0) {
|
||||||
|
ret.add("-classpath");
|
||||||
|
ret.add(cp);
|
||||||
|
}
|
||||||
|
|
||||||
|
options = ret;
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 兼容 tomcat 丢失 class path,否则无法编译
|
||||||
|
*/
|
||||||
|
protected String getClassPath() {
|
||||||
|
URLClassLoader classLoader = getURLClassLoader();
|
||||||
|
if (classLoader == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
boolean isWindows = isWindows();
|
||||||
|
StringBuilder ret = new StringBuilder();
|
||||||
|
for (URL url : classLoader.getURLs()) {
|
||||||
|
if (index++ > 0) {
|
||||||
|
ret.append(File.pathSeparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
String path = url.getFile();
|
||||||
|
|
||||||
|
// 如果是 windows 系统,去除前缀字符 '/'
|
||||||
|
if (isWindows && path.startsWith("/")) {
|
||||||
|
path = path.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 去除后缀字符 '/'
|
||||||
|
if (path.length() > 1 && (path.endsWith("/") || path.endsWith(File.separator))) {
|
||||||
|
path = path.substring(0, path.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
ret.append(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean isWindows() {
|
||||||
|
String osName = System.getProperty("os.name", "unknown");
|
||||||
|
return osName.toLowerCase().indexOf("windows") != -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected URLClassLoader getURLClassLoader() {
|
||||||
|
ClassLoader ret = Thread.currentThread().getContextClassLoader();
|
||||||
|
if (ret == null) {
|
||||||
|
ret = ProxyCompiler.class.getClassLoader();
|
||||||
|
}
|
||||||
|
return (ret instanceof URLClassLoader) ? (URLClassLoader)ret : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 \n" +
|
||||||
|
"Visit https://jfinal.com/doc/4-8 for details \n");
|
||||||
|
}
|
||||||
|
|
||||||
|
DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<>();
|
||||||
|
try (MyJavaFileManager javaFileManager = new MyJavaFileManager(compiler.getStandardFileManager(collector, null, null))) {
|
||||||
|
|
||||||
|
MyJavaFileObject javaFileObject = new MyJavaFileObject(proxyClass.getName(), proxyClass.getSourceCode());
|
||||||
|
Boolean result = compiler.getTask(null, javaFileManager, collector, getOptions(), null, Arrays.asList(javaFileObject)).call();
|
||||||
|
outputCompileError(result, collector);
|
||||||
|
|
||||||
|
Map<String, byte[]> ret = new HashMap<>();
|
||||||
|
for (Entry<String, MyJavaFileObject> e : javaFileManager.fileObjects.entrySet()) {
|
||||||
|
ret.put(e.getKey(), e.getValue().getByteCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
proxyClass.setByteCode(ret);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void outputCompileError(Boolean result, DiagnosticCollector<JavaFileObject> collector) {
|
||||||
|
if (! result) {
|
||||||
|
// collector.getDiagnostics().forEach(item -> log.error(item.toString()));
|
||||||
|
collector.getDiagnostics().forEach(item -> System.out.println(item.toString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProxyCompiler setCompileOptions(List<String> options) {
|
||||||
|
Objects.requireNonNull(options, "options can not be null");
|
||||||
|
this.options = options;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProxyCompiler addCompileOption(String option) {
|
||||||
|
Objects.requireNonNull(option, "option can not be null");
|
||||||
|
options.add(option);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class MyJavaFileObject extends SimpleJavaFileObject {
|
||||||
|
|
||||||
|
private String source;
|
||||||
|
private ByteArrayOutputStream outPutStream;
|
||||||
|
|
||||||
|
public MyJavaFileObject(String name, String source) {
|
||||||
|
super(URI.create("String:///" + name + Kind.SOURCE.extension), Kind.SOURCE);
|
||||||
|
this.source = source;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyJavaFileObject(String name, Kind kind) {
|
||||||
|
super(URI.create("String:///" + name + kind.extension), kind);
|
||||||
|
source = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
|
||||||
|
if (source == null) {
|
||||||
|
throw new IllegalStateException("source field can not be null");
|
||||||
|
}
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public OutputStream openOutputStream() throws IOException {
|
||||||
|
outPutStream = new ByteArrayOutputStream();
|
||||||
|
return outPutStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getByteCode() {
|
||||||
|
return outPutStream.toByteArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class MyJavaFileManager extends ForwardingJavaFileManager<JavaFileManager> {
|
||||||
|
|
||||||
|
public Map<String, MyJavaFileObject> fileObjects = new HashMap<>();
|
||||||
|
|
||||||
|
public MyJavaFileManager(JavaFileManager fileManager) {
|
||||||
|
super(fileManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JavaFileObject getJavaFileForOutput(Location location, String qualifiedClassName, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
|
||||||
|
MyJavaFileObject javaFileObject = new MyJavaFileObject(qualifiedClassName, kind);
|
||||||
|
fileObjects.put(qualifiedClassName, javaFileObject);
|
||||||
|
return javaFileObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 是否在编译时依赖另一个类的情况下用到本方法 ?
|
||||||
|
@Override
|
||||||
|
public JavaFileObject getJavaFileForInput(Location location, String className, JavaFileObject.Kind kind) throws IOException {
|
||||||
|
JavaFileObject javaFileObject = fileObjects.get(className);
|
||||||
|
if (javaFileObject == null) {
|
||||||
|
javaFileObject = super.getJavaFileForInput(location, className, kind);
|
||||||
|
}
|
||||||
|
return javaFileObject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -21,10 +21,17 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import com.jfinal.kit.HashKit;
|
import com.jfinal.kit.HashKit;
|
||||||
import com.jfinal.kit.StrKit;
|
import com.jfinal.kit.StrKit;
|
||||||
|
import com.jfinal.kit.SyncWriteMap;
|
||||||
|
import com.jfinal.template.expr.ast.FieldGetter;
|
||||||
|
import com.jfinal.template.expr.ast.FieldKeyBuilder;
|
||||||
|
import com.jfinal.template.expr.ast.FieldKit;
|
||||||
import com.jfinal.template.expr.ast.MethodKit;
|
import com.jfinal.template.expr.ast.MethodKit;
|
||||||
|
import com.jfinal.template.io.EncoderFactory;
|
||||||
|
import com.jfinal.template.source.ClassPathSourceFactory;
|
||||||
import com.jfinal.template.source.ISource;
|
import com.jfinal.template.source.ISource;
|
||||||
import com.jfinal.template.source.ISourceFactory;
|
import com.jfinal.template.source.ISourceFactory;
|
||||||
import com.jfinal.template.source.StringSource;
|
import com.jfinal.template.source.StringSource;
|
||||||
|
import com.jfinal.template.stat.OutputDirectiveFactory;
|
||||||
import com.jfinal.template.stat.Parser;
|
import com.jfinal.template.stat.Parser;
|
||||||
import com.jfinal.template.stat.ast.Stat;
|
import com.jfinal.template.stat.ast.Stat;
|
||||||
|
|
||||||
@@ -40,7 +47,7 @@ public class Engine {
|
|||||||
public static final String MAIN_ENGINE_NAME = "main";
|
public static final String MAIN_ENGINE_NAME = "main";
|
||||||
|
|
||||||
private static Engine MAIN_ENGINE;
|
private static Engine MAIN_ENGINE;
|
||||||
private static Map<String, Engine> engineMap = new HashMap<String, Engine>();
|
private static Map<String, Engine> engineMap = new HashMap<String, Engine>(64, 0.5F);
|
||||||
|
|
||||||
// Create main engine
|
// Create main engine
|
||||||
static {
|
static {
|
||||||
@@ -53,7 +60,7 @@ public class Engine {
|
|||||||
private EngineConfig config = new EngineConfig();
|
private EngineConfig config = new EngineConfig();
|
||||||
private ISourceFactory sourceFactory = config.getSourceFactory();
|
private ISourceFactory sourceFactory = config.getSourceFactory();
|
||||||
|
|
||||||
private Map<String, Template> templateCache = new HashMap<String, Template>();
|
private Map<String, Template> templateCache = new SyncWriteMap<String, Template>(2048, 0.5F);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create engine without management of JFinal
|
* Create engine without management of JFinal
|
||||||
@@ -123,7 +130,7 @@ public class Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get template with file name
|
* Get template by file name
|
||||||
*/
|
*/
|
||||||
public Template getTemplate(String fileName) {
|
public Template getTemplate(String fileName) {
|
||||||
if (fileName.charAt(0) != '/') {
|
if (fileName.charAt(0) != '/') {
|
||||||
@@ -161,14 +168,6 @@ public class Engine {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get template by string content and do not cache the template
|
* Get template by string content and do not cache the template
|
||||||
*
|
|
||||||
* 重要:StringSource 中的 key = HashKit.md5(content),也即 key
|
|
||||||
* 与 content 有紧密的对应关系,当 content 发生变化时 key 值也相应变化
|
|
||||||
* 因此,原先 key 所对应的 Template 缓存对象已无法被获取,当 getTemplateByString(String)
|
|
||||||
* 的 String 参数的数量不确定时会引发内存泄漏
|
|
||||||
*
|
|
||||||
* 当 getTemplateByString(String, boolean) 中的 String 参数的
|
|
||||||
* 数量可控并且确定时,才可对其使用缓存
|
|
||||||
*/
|
*/
|
||||||
public Template getTemplateByString(String content) {
|
public Template getTemplateByString(String content) {
|
||||||
return getTemplateByString(content, false);
|
return getTemplateByString(content, false);
|
||||||
@@ -176,6 +175,15 @@ public class Engine {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get template by string content
|
* Get template by string content
|
||||||
|
*
|
||||||
|
* 重要:StringSource 中的 cacheKey = HashKit.md5(content),也即 cacheKey
|
||||||
|
* 与 content 有紧密的对应关系,当 content 发生变化时 cacheKey 值也相应变化
|
||||||
|
* 因此,原先 cacheKey 所对应的 Template 缓存对象已无法被获取,当 getTemplateByString(String)
|
||||||
|
* 的 String 参数的数量不确定时会引发内存泄漏
|
||||||
|
*
|
||||||
|
* 当 getTemplateByString(String, boolean) 中的 String 参数的
|
||||||
|
* 数量可控并且确定时,才可对其使用缓存
|
||||||
|
*
|
||||||
* @param content 模板内容
|
* @param content 模板内容
|
||||||
* @param cache true 则缓存 Template,否则不缓存
|
* @param cache true 则缓存 Template,否则不缓存
|
||||||
*/
|
*/
|
||||||
@@ -184,37 +192,37 @@ public class Engine {
|
|||||||
return buildTemplateBySource(new StringSource(content, cache));
|
return buildTemplateBySource(new StringSource(content, cache));
|
||||||
}
|
}
|
||||||
|
|
||||||
String key = HashKit.md5(content);
|
String cacheKey = HashKit.md5(content);
|
||||||
Template template = templateCache.get(key);
|
Template template = templateCache.get(cacheKey);
|
||||||
if (template == null) {
|
if (template == null) {
|
||||||
template = buildTemplateBySource(new StringSource(content, cache));
|
template = buildTemplateBySource(new StringSource(content, cache));
|
||||||
templateCache.put(key, template);
|
templateCache.put(cacheKey, template);
|
||||||
} else if (devMode) {
|
} else if (devMode) {
|
||||||
if (template.isModified()) {
|
if (template.isModified()) {
|
||||||
template = buildTemplateBySource(new StringSource(content, cache));
|
template = buildTemplateBySource(new StringSource(content, cache));
|
||||||
templateCache.put(key, template);
|
templateCache.put(cacheKey, template);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return template;
|
return template;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get template with implementation of ISource
|
* Get template by implementation of ISource
|
||||||
*/
|
*/
|
||||||
public Template getTemplate(ISource source) {
|
public Template getTemplate(ISource source) {
|
||||||
String key = source.getKey();
|
String cacheKey = source.getCacheKey();
|
||||||
if (key == null) { // key 为 null 则不缓存,详见 ISource.getKey() 注释
|
if (cacheKey == null) { // cacheKey 为 null 则不缓存,详见 ISource.getCacheKey() 注释
|
||||||
return buildTemplateBySource(source);
|
return buildTemplateBySource(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
Template template = templateCache.get(key);
|
Template template = templateCache.get(cacheKey);
|
||||||
if (template == null) {
|
if (template == null) {
|
||||||
template = buildTemplateBySource(source);
|
template = buildTemplateBySource(source);
|
||||||
templateCache.put(key, template);
|
templateCache.put(cacheKey, template);
|
||||||
} else if (devMode) {
|
} else if (devMode) {
|
||||||
if (template.isModified()) {
|
if (template.isModified()) {
|
||||||
template = buildTemplateBySource(source);
|
template = buildTemplateBySource(source);
|
||||||
templateCache.put(key, template);
|
templateCache.put(cacheKey, template);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return template;
|
return template;
|
||||||
@@ -232,7 +240,7 @@ public class Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add shared function with file
|
* Add shared function by file
|
||||||
*/
|
*/
|
||||||
public Engine addSharedFunction(String fileName) {
|
public Engine addSharedFunction(String fileName) {
|
||||||
config.addSharedFunction(fileName);
|
config.addSharedFunction(fileName);
|
||||||
@@ -248,7 +256,7 @@ public class Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add shared function with files
|
* Add shared function by files
|
||||||
*/
|
*/
|
||||||
public Engine addSharedFunction(String... fileNames) {
|
public Engine addSharedFunction(String... fileNames) {
|
||||||
config.addSharedFunction(fileNames);
|
config.addSharedFunction(fileNames);
|
||||||
@@ -271,32 +279,45 @@ public class Engine {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Engine removeSharedObject(String name) {
|
||||||
|
config.removeSharedObject(name);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set output directive factory
|
* Set output directive factory
|
||||||
*/
|
*/
|
||||||
public Engine setOutputDirectiveFactory(IOutputDirectiveFactory outputDirectiveFactory) {
|
public Engine setOutputDirectiveFactory(OutputDirectiveFactory outputDirectiveFactory) {
|
||||||
config.setOutputDirectiveFactory(outputDirectiveFactory);
|
config.setOutputDirectiveFactory(outputDirectiveFactory);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add directive
|
* 添加自定义指令
|
||||||
|
*
|
||||||
|
* 建议添加自定义指令时明确指定 keepLineBlank 变量值,其规则如下:
|
||||||
|
* 1:keepLineBlank 为 true 时, 该指令所在行的前后空白字符以及末尾字符 '\n' 将会被保留
|
||||||
|
* 一般用于具有输出值的指令,例如 #date、#para 等指令
|
||||||
|
*
|
||||||
|
* 2:keepLineBlank 为 false 时,该指令所在行的前后空白字符以及末尾字符 '\n' 将会被删除
|
||||||
|
* 一般用于没有输出值的指令,例如 #for、#if、#else、#end 这种性质的指令
|
||||||
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* 示例:
|
* 示例:
|
||||||
* addDirective("now", NowDirective.class)
|
* addDirective("now", NowDirective.class, true)
|
||||||
* </pre>
|
* </pre>
|
||||||
*/
|
*/
|
||||||
public Engine addDirective(String directiveName, Class<? extends Directive> directiveClass) {
|
public Engine addDirective(String directiveName, Class<? extends Directive> directiveClass, boolean keepLineBlank) {
|
||||||
config.addDirective(directiveName, directiveClass);
|
config.addDirective(directiveName, directiveClass, keepLineBlank);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 该方法已被 addDirective(String, Class<? extends Directive>) 所代替
|
* 添加自定义指令,keepLineBlank 使用默认值
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
public Engine addDirective(String directiveName, Class<? extends Directive> directiveClass) {
|
||||||
public Engine addDirective(String directiveName, Directive directive) {
|
config.addDirective(directiveName, directiveClass);
|
||||||
return addDirective(directiveName, directive.getClass());
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -332,7 +353,7 @@ public class Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove shared Method with method name
|
* Remove shared Method by method name
|
||||||
*/
|
*/
|
||||||
public Engine removeSharedMethod(String methodName) {
|
public Engine removeSharedMethod(String methodName) {
|
||||||
config.removeSharedMethod(methodName);
|
config.removeSharedMethod(methodName);
|
||||||
@@ -356,10 +377,10 @@ public class Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove template cache with template key
|
* Remove template cache by cache key
|
||||||
*/
|
*/
|
||||||
public void removeTemplateCache(String templateKey) {
|
public void removeTemplateCache(String cacheKey) {
|
||||||
templateCache.remove(templateKey);
|
templateCache.remove(cacheKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -426,6 +447,13 @@ public class Engine {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置为 ClassPathSourceFactory 的快捷方法
|
||||||
|
*/
|
||||||
|
public Engine setToClassPathSourceFactory() {
|
||||||
|
return setSourceFactory(new ClassPathSourceFactory());
|
||||||
|
}
|
||||||
|
|
||||||
public ISourceFactory getSourceFactory() {
|
public ISourceFactory getSourceFactory() {
|
||||||
return sourceFactory;
|
return sourceFactory;
|
||||||
}
|
}
|
||||||
@@ -457,6 +485,25 @@ public class Engine {
|
|||||||
return config.getEncoding();
|
return config.getEncoding();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enjoy 模板引擎对 UTF-8 的 encoding 做过性能优化,某些罕见字符
|
||||||
|
* 无法被编码,可以配置为 JdkEncoderFactory 解决问题:
|
||||||
|
* engine.setEncoderFactory(new JdkEncoderFactory());
|
||||||
|
*/
|
||||||
|
public Engine setEncoderFactory(EncoderFactory encoderFactory) {
|
||||||
|
config.setEncoderFactory(encoderFactory);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置为 JdkEncoderFactory,支持 utf8mb4,支持 emoji 表情字符,
|
||||||
|
* 支持各种罕见字符编码
|
||||||
|
*/
|
||||||
|
public Engine setToJdkEncoderFactory() {
|
||||||
|
config.setEncoderFactory(new com.jfinal.template.io.JdkEncoderFactory());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Engine setWriterBufferSize(int bufferSize) {
|
public Engine setWriterBufferSize(int bufferSize) {
|
||||||
config.setWriterBufferSize(bufferSize);
|
config.setWriterBufferSize(bufferSize);
|
||||||
return this;
|
return this;
|
||||||
@@ -489,6 +536,56 @@ public class Engine {
|
|||||||
public static void removeExtensionMethod(Class<?> targetClass, Class<?> extensionClass) {
|
public static void removeExtensionMethod(Class<?> targetClass, Class<?> extensionClass) {
|
||||||
MethodKit.removeExtensionMethod(targetClass, extensionClass);
|
MethodKit.removeExtensionMethod(targetClass, extensionClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加 FieldGetter 实现类到指定的位置
|
||||||
|
*
|
||||||
|
* 系统当前默认 FieldGetter 实现类及其位置如下:
|
||||||
|
* GetterMethodFieldGetter ---> 调用 getter 方法取值
|
||||||
|
* RealFieldGetter ---> 直接获取 public 型的 object.field 值
|
||||||
|
* ModelFieldGetter ---> 调用 Model.get(String) 方法取值
|
||||||
|
* RecordFieldGetter ---> 调用 Record.get(String) 方法取值
|
||||||
|
* MapFieldGetter ---> 调用 Map.get(String) 方法取值
|
||||||
|
* ArrayLengthGetter ---> 获取数组长度
|
||||||
|
*
|
||||||
|
* 根据以上次序,如果要插入 IsMethodFieldGetter 到 GetterMethodFieldGetter
|
||||||
|
* 之后的代码如下:
|
||||||
|
* Engine.addFieldGetter(1, new IsMethodFieldGetter());
|
||||||
|
*
|
||||||
|
* 注:IsMethodFieldGetter 系统已经提供,只是默认没有启用。该实现类通过调用
|
||||||
|
* target.isXxx() 方法获取 target.xxx 表达式的值,其中 isXxx() 返回值
|
||||||
|
* 必须是 Boolean/boolean 类型才会被调用
|
||||||
|
*/
|
||||||
|
public static void addFieldGetter(int index, FieldGetter fieldGetter) {
|
||||||
|
FieldKit.addFieldGetter(index, fieldGetter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addFieldGetterToLast(FieldGetter fieldGetter) {
|
||||||
|
FieldKit.addFieldGetterToLast(fieldGetter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addFieldGetterToFirst(FieldGetter fieldGetter) {
|
||||||
|
FieldKit.addFieldGetterToFirst(fieldGetter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void removeFieldGetter(Class<? extends FieldGetter> fieldGetterClass) {
|
||||||
|
FieldKit.removeFieldGetter(fieldGetterClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setFastFieldKeyBuilder(boolean enable) {
|
||||||
|
FieldKeyBuilder.setFastFieldKeyBuilder(enable);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置极速模式
|
||||||
|
*
|
||||||
|
* 极速模式将生成代理对象来消除 java.lang.reflect.Method.invoke(...) 调用,
|
||||||
|
* 性能提升 12.9%
|
||||||
|
*/
|
||||||
|
public static void setFastMode(boolean fastMode) {
|
||||||
|
FieldKit.setFastMode(fastMode);
|
||||||
|
FieldKeyBuilder.setFastFieldKeyBuilder(fastMode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -19,9 +19,11 @@ package com.jfinal.template;
|
|||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Set;
|
||||||
import com.jfinal.kit.StrKit;
|
import com.jfinal.kit.StrKit;
|
||||||
import com.jfinal.template.expr.ast.ExprList;
|
import com.jfinal.template.expr.ast.ExprList;
|
||||||
import com.jfinal.template.expr.ast.SharedMethodKit;
|
import com.jfinal.template.expr.ast.SharedMethodKit;
|
||||||
@@ -35,6 +37,7 @@ import com.jfinal.template.source.ISource;
|
|||||||
import com.jfinal.template.source.ISourceFactory;
|
import com.jfinal.template.source.ISourceFactory;
|
||||||
import com.jfinal.template.source.StringSource;
|
import com.jfinal.template.source.StringSource;
|
||||||
import com.jfinal.template.stat.Location;
|
import com.jfinal.template.stat.Location;
|
||||||
|
import com.jfinal.template.stat.OutputDirectiveFactory;
|
||||||
import com.jfinal.template.stat.Parser;
|
import com.jfinal.template.stat.Parser;
|
||||||
import com.jfinal.template.stat.ast.Define;
|
import com.jfinal.template.stat.ast.Define;
|
||||||
import com.jfinal.template.stat.ast.Output;
|
import com.jfinal.template.stat.ast.Output;
|
||||||
@@ -48,16 +51,19 @@ public class EngineConfig {
|
|||||||
|
|
||||||
WriterBuffer writerBuffer = new WriterBuffer();
|
WriterBuffer writerBuffer = new WriterBuffer();
|
||||||
|
|
||||||
private Map<String, Define> sharedFunctionMap = new HashMap<String, Define>();
|
private Map<String, Define> sharedFunctionMap = createSharedFunctionMap(); // new HashMap<String, Define>(512, 0.25F);
|
||||||
private List<ISource> sharedFunctionSourceList = new ArrayList<ISource>(); // for devMode only
|
private List<ISource> sharedFunctionSourceList = new ArrayList<ISource>(); // for devMode only
|
||||||
|
|
||||||
Map<String, Object> sharedObjectMap = null;
|
Map<String, Object> sharedObjectMap = null;
|
||||||
|
|
||||||
private IOutputDirectiveFactory outputDirectiveFactory = OutputDirectiveFactory.me;
|
private OutputDirectiveFactory outputDirectiveFactory = OutputDirectiveFactory.me;
|
||||||
private ISourceFactory sourceFactory = new FileSourceFactory();
|
private ISourceFactory sourceFactory = new FileSourceFactory();
|
||||||
private Map<String, Class<? extends Directive>> directiveMap = new HashMap<String, Class<? extends Directive>>();
|
private Map<String, Class<? extends Directive>> directiveMap = new HashMap<String, Class<? extends Directive>>(64, 0.5F);
|
||||||
private SharedMethodKit sharedMethodKit = new SharedMethodKit();
|
private SharedMethodKit sharedMethodKit = new SharedMethodKit();
|
||||||
|
|
||||||
|
// 保留指令所在行空白字符的指令
|
||||||
|
private Set<String> keepLineBlankDirectives = new HashSet<>();
|
||||||
|
|
||||||
private boolean devMode = false;
|
private boolean devMode = false;
|
||||||
private boolean reloadModifiedSharedFunctionInDevMode = true;
|
private boolean reloadModifiedSharedFunctionInDevMode = true;
|
||||||
private String baseTemplatePath = null;
|
private String baseTemplatePath = null;
|
||||||
@@ -65,16 +71,21 @@ public class EngineConfig {
|
|||||||
private String datePattern = "yyyy-MM-dd HH:mm";
|
private String datePattern = "yyyy-MM-dd HH:mm";
|
||||||
|
|
||||||
public EngineConfig() {
|
public EngineConfig() {
|
||||||
|
// 内置指令 #() 与 #include() 需要配置,保留指令所在行前后空白字符以及行尾换行字符 '\n'
|
||||||
|
setKeepLineBlank("output", true);
|
||||||
|
setKeepLineBlank("include", true);
|
||||||
|
|
||||||
// Add official directive of Template Engine
|
// Add official directive of Template Engine
|
||||||
addDirective("render", RenderDirective.class);
|
addDirective("render", RenderDirective.class, true);
|
||||||
addDirective("date", DateDirective.class);
|
addDirective("date", DateDirective.class, true);
|
||||||
addDirective("escape", EscapeDirective.class);
|
addDirective("escape", EscapeDirective.class, true);
|
||||||
addDirective("string", StringDirective.class);
|
addDirective("random", RandomDirective.class, true);
|
||||||
addDirective("random", RandomDirective.class);
|
addDirective("number", NumberDirective.class, true);
|
||||||
addDirective("number", NumberDirective.class);
|
|
||||||
|
addDirective("call", CallDirective.class, false);
|
||||||
|
addDirective("string", StringDirective.class, false);
|
||||||
|
|
||||||
// Add official shared method of Template Engine
|
// Add official shared method of Template Engine
|
||||||
// addSharedMethod(new Json());
|
|
||||||
addSharedMethod(new SharedMethodLib());
|
addSharedMethod(new SharedMethodLib());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,6 +93,7 @@ public class EngineConfig {
|
|||||||
* Add shared function with file
|
* Add shared function with file
|
||||||
*/
|
*/
|
||||||
public void addSharedFunction(String fileName) {
|
public void addSharedFunction(String fileName) {
|
||||||
|
fileName = fileName.replace("\\", "/");
|
||||||
// FileSource fileSource = new FileSource(baseTemplatePath, fileName, encoding);
|
// FileSource fileSource = new FileSource(baseTemplatePath, fileName, encoding);
|
||||||
ISource source = sourceFactory.getSource(baseTemplatePath, fileName, encoding);
|
ISource source = sourceFactory.getSource(baseTemplatePath, fileName, encoding);
|
||||||
doAddSharedFunction(source, fileName);
|
doAddSharedFunction(source, fileName);
|
||||||
@@ -179,7 +191,7 @@ public class EngineConfig {
|
|||||||
* 开发者可直接使用模板注释功能将不需要的 function 直接注释掉
|
* 开发者可直接使用模板注释功能将不需要的 function 直接注释掉
|
||||||
*/
|
*/
|
||||||
private synchronized void reloadSharedFunctionSourceList() {
|
private synchronized void reloadSharedFunctionSourceList() {
|
||||||
Map<String, Define> newMap = new HashMap<String, Define>();
|
Map<String, Define> newMap = createSharedFunctionMap();
|
||||||
for (int i = 0, size = sharedFunctionSourceList.size(); i < size; i++) {
|
for (int i = 0, size = sharedFunctionSourceList.size(); i < size; i++) {
|
||||||
ISource source = sharedFunctionSourceList.get(i);
|
ISource source = sharedFunctionSourceList.get(i);
|
||||||
String fileName = source instanceof FileSource ? ((FileSource)source).getFileName() : null;
|
String fileName = source instanceof FileSource ? ((FileSource)source).getFileName() : null;
|
||||||
@@ -194,23 +206,33 @@ public class EngineConfig {
|
|||||||
this.sharedFunctionMap = newMap;
|
this.sharedFunctionMap = newMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Map<String, Define> createSharedFunctionMap() {
|
||||||
|
return new HashMap<String, Define>(512, 0.25F);
|
||||||
|
}
|
||||||
|
|
||||||
public synchronized void addSharedObject(String name, Object object) {
|
public synchronized void addSharedObject(String name, Object object) {
|
||||||
if (sharedObjectMap == null) {
|
if (sharedObjectMap == null) {
|
||||||
sharedObjectMap = new HashMap<String, Object>();
|
sharedObjectMap = new HashMap<String, Object>(64, 0.25F);
|
||||||
} else if (sharedObjectMap.containsKey(name)) {
|
} else if (sharedObjectMap.containsKey(name)) {
|
||||||
throw new IllegalArgumentException("Shared object already exists: " + name);
|
throw new IllegalArgumentException("Shared object already exists: " + name);
|
||||||
}
|
}
|
||||||
sharedObjectMap.put(name, object);
|
sharedObjectMap.put(name, object);
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, Object> getSharedObjectMap() {
|
public Map<String, Object> getSharedObjectMap() {
|
||||||
return sharedObjectMap;
|
return sharedObjectMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized void removeSharedObject(String name) {
|
||||||
|
if (sharedObjectMap != null) {
|
||||||
|
sharedObjectMap.remove(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set output directive factory
|
* Set output directive factory
|
||||||
*/
|
*/
|
||||||
public void setOutputDirectiveFactory(IOutputDirectiveFactory outputDirectiveFactory) {
|
public void setOutputDirectiveFactory(OutputDirectiveFactory outputDirectiveFactory) {
|
||||||
if (outputDirectiveFactory == null) {
|
if (outputDirectiveFactory == null) {
|
||||||
throw new IllegalArgumentException("outputDirectiveFactory can not be null");
|
throw new IllegalArgumentException("outputDirectiveFactory can not be null");
|
||||||
}
|
}
|
||||||
@@ -256,8 +278,9 @@ public class EngineConfig {
|
|||||||
throw new IllegalArgumentException("baseTemplatePath can not be blank");
|
throw new IllegalArgumentException("baseTemplatePath can not be blank");
|
||||||
}
|
}
|
||||||
baseTemplatePath = baseTemplatePath.trim();
|
baseTemplatePath = baseTemplatePath.trim();
|
||||||
|
baseTemplatePath = baseTemplatePath.replace("\\", "/");
|
||||||
if (baseTemplatePath.length() > 1) {
|
if (baseTemplatePath.length() > 1) {
|
||||||
if (baseTemplatePath.endsWith("/") || baseTemplatePath.endsWith("\\")) {
|
if (baseTemplatePath.endsWith("/")) {
|
||||||
baseTemplatePath = baseTemplatePath.substring(0, baseTemplatePath.length() - 1);
|
baseTemplatePath = baseTemplatePath.substring(0, baseTemplatePath.length() - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -305,12 +328,7 @@ public class EngineConfig {
|
|||||||
this.reloadModifiedSharedFunctionInDevMode = reloadModifiedSharedFunctionInDevMode;
|
this.reloadModifiedSharedFunctionInDevMode = reloadModifiedSharedFunctionInDevMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
public synchronized void addDirective(String directiveName, Class<? extends Directive> directiveClass, boolean keepLineBlank) {
|
||||||
public void addDirective(String directiveName, Directive directive) {
|
|
||||||
addDirective(directiveName, directive.getClass());
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void addDirective(String directiveName, Class<? extends Directive> directiveClass) {
|
|
||||||
if (StrKit.isBlank(directiveName)) {
|
if (StrKit.isBlank(directiveName)) {
|
||||||
throw new IllegalArgumentException("directive name can not be blank");
|
throw new IllegalArgumentException("directive name can not be blank");
|
||||||
}
|
}
|
||||||
@@ -320,7 +338,15 @@ public class EngineConfig {
|
|||||||
if (directiveMap.containsKey(directiveName)) {
|
if (directiveMap.containsKey(directiveName)) {
|
||||||
throw new IllegalArgumentException("directive already exists : " + directiveName);
|
throw new IllegalArgumentException("directive already exists : " + directiveName);
|
||||||
}
|
}
|
||||||
|
|
||||||
directiveMap.put(directiveName, directiveClass);
|
directiveMap.put(directiveName, directiveClass);
|
||||||
|
if (keepLineBlank) {
|
||||||
|
keepLineBlankDirectives.add(directiveName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addDirective(String directiveName, Class<? extends Directive> directiveClass) {
|
||||||
|
addDirective(directiveName, directiveClass, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Class<? extends Directive> getDirective(String directiveName) {
|
public Class<? extends Directive> getDirective(String directiveName) {
|
||||||
@@ -329,6 +355,19 @@ public class EngineConfig {
|
|||||||
|
|
||||||
public void removeDirective(String directiveName) {
|
public void removeDirective(String directiveName) {
|
||||||
directiveMap.remove(directiveName);
|
directiveMap.remove(directiveName);
|
||||||
|
keepLineBlankDirectives.remove(directiveName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKeepLineBlank(String directiveName, boolean keepLineBlank) {
|
||||||
|
if (keepLineBlank) {
|
||||||
|
keepLineBlankDirectives.add(directiveName);
|
||||||
|
} else {
|
||||||
|
keepLineBlankDirectives.remove(directiveName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<String> getKeepLineBlankDirectives() {
|
||||||
|
return keepLineBlankDirectives;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -35,7 +35,7 @@ import com.jfinal.template.stat.ast.Define;
|
|||||||
public class Env {
|
public class Env {
|
||||||
|
|
||||||
protected EngineConfig engineConfig;
|
protected EngineConfig engineConfig;
|
||||||
protected Map<String, Define> functionMap = new HashMap<String, Define>();
|
protected Map<String, Define> functionMap = new HashMap<String, Define>(16, 0.5F);
|
||||||
|
|
||||||
// 代替 Template 持有该属性,便于在 #include 指令中调用 Env.addSource()
|
// 代替 Template 持有该属性,便于在 #include 指令中调用 Env.addSource()
|
||||||
protected List<ISource> sourceList = null;
|
protected List<ISource> sourceList = null;
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -16,6 +16,9 @@
|
|||||||
|
|
||||||
package com.jfinal.template;
|
package com.jfinal.template;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -99,13 +102,41 @@ public class Template {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 支持无 data 参数,渲染到 String 中去 <br>
|
||||||
|
* 适用于数据在模板中通过表达式和语句直接计算得出等等应用场景
|
||||||
|
*/
|
||||||
|
public String renderToString() {
|
||||||
|
return renderToString(null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 渲染到 StringBuilder 中去
|
* 渲染到 StringBuilder 中去
|
||||||
*/
|
*/
|
||||||
public StringBuilder renderToStringBuilder(Map<?, ?> data) {
|
public StringBuilder renderToStringBuilder(Map<?, ?> data) {
|
||||||
FastStringWriter fsw = new FastStringWriter();
|
FastStringWriter fsw = new FastStringWriter();
|
||||||
render(data, fsw);
|
render(data, fsw);
|
||||||
return fsw.getBuffer();
|
return fsw.toStringBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 渲染到 File 中去
|
||||||
|
* 适用于代码生成器类似应用场景
|
||||||
|
*/
|
||||||
|
public void render(Map<?, ?> data, File file) {
|
||||||
|
try (FileOutputStream fos = new FileOutputStream(file)) {
|
||||||
|
render(data, fos);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 渲染到 String fileName 参数所指定的文件中去
|
||||||
|
* 适用于代码生成器类似应用场景
|
||||||
|
*/
|
||||||
|
public void render(Map<?, ?> data, String fileName) {
|
||||||
|
render(data, new File(fileName));
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isModified() {
|
public boolean isModified() {
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -28,8 +28,8 @@ public class TemplateException extends RuntimeException {
|
|||||||
super(loc != null ? msg + loc : msg);
|
super(loc != null ? msg + loc : msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public TemplateException(String msg, Location loc, Throwable t) {
|
public TemplateException(String msg, Location loc, Throwable cause) {
|
||||||
super(loc != null ? msg + loc : msg, t);
|
super(loc != null ? msg + loc : msg, cause);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -21,28 +21,7 @@ import java.util.LinkedHashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import com.jfinal.template.EngineConfig;
|
import com.jfinal.template.EngineConfig;
|
||||||
import com.jfinal.template.expr.Sym;
|
import com.jfinal.template.expr.Sym;
|
||||||
import com.jfinal.template.expr.ast.Arith;
|
import com.jfinal.template.expr.ast.*;
|
||||||
import com.jfinal.template.expr.ast.Array;
|
|
||||||
import com.jfinal.template.expr.ast.Assign;
|
|
||||||
import com.jfinal.template.expr.ast.Compare;
|
|
||||||
import com.jfinal.template.expr.ast.Const;
|
|
||||||
import com.jfinal.template.expr.ast.Expr;
|
|
||||||
import com.jfinal.template.expr.ast.ExprList;
|
|
||||||
import com.jfinal.template.expr.ast.Field;
|
|
||||||
import com.jfinal.template.expr.ast.ForCtrl;
|
|
||||||
import com.jfinal.template.expr.ast.Id;
|
|
||||||
import com.jfinal.template.expr.ast.IncDec;
|
|
||||||
import com.jfinal.template.expr.ast.Index;
|
|
||||||
import com.jfinal.template.expr.ast.Logic;
|
|
||||||
import com.jfinal.template.expr.ast.Map;
|
|
||||||
import com.jfinal.template.expr.ast.Method;
|
|
||||||
import com.jfinal.template.expr.ast.NullSafe;
|
|
||||||
import com.jfinal.template.expr.ast.RangeArray;
|
|
||||||
import com.jfinal.template.expr.ast.SharedMethod;
|
|
||||||
import com.jfinal.template.expr.ast.StaticField;
|
|
||||||
import com.jfinal.template.expr.ast.StaticMethod;
|
|
||||||
import com.jfinal.template.expr.ast.Ternary;
|
|
||||||
import com.jfinal.template.expr.ast.Unary;
|
|
||||||
import com.jfinal.template.stat.Location;
|
import com.jfinal.template.stat.Location;
|
||||||
import com.jfinal.template.stat.ParaToken;
|
import com.jfinal.template.stat.ParaToken;
|
||||||
import com.jfinal.template.stat.ParseException;
|
import com.jfinal.template.stat.ParseException;
|
||||||
@@ -292,7 +271,7 @@ public class ExprParser {
|
|||||||
case ADD:
|
case ADD:
|
||||||
case SUB:
|
case SUB:
|
||||||
move();
|
move();
|
||||||
return new Unary(tok.sym, unary(), location);
|
return new Unary(tok.sym, unary(), location).toConstIfPossible();
|
||||||
case INC:
|
case INC:
|
||||||
case DEC:
|
case DEC:
|
||||||
move();
|
move();
|
||||||
@@ -468,21 +447,26 @@ public class ExprParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* mapEntry : (ID | STR) ':' expr
|
* mapEntry : (ID | STR | INT | LONG | FLOAT | DOUBLE | TRUE | FALSE | NULL) ':' expr
|
||||||
|
* 设计目标为 map 定义与初始化,所以 ID 仅当成 STR 不进行求值
|
||||||
*/
|
*/
|
||||||
void buildMapEntry(LinkedHashMap<Object, Expr> map) {
|
void buildMapEntry(LinkedHashMap<Object, Expr> map) {
|
||||||
Tok tok = peek();
|
Expr keyExpr = expr();
|
||||||
if (tok.sym == Sym.ID || tok.sym == Sym.STR) {
|
Object key;
|
||||||
move();
|
if (keyExpr instanceof Id) {
|
||||||
|
key = ((Id)keyExpr).getId();
|
||||||
|
} else if (keyExpr instanceof Const) {
|
||||||
|
key = ((Const)keyExpr).getValue();
|
||||||
|
} else {
|
||||||
|
throw new ParseException("Expression error: the value of map key must be identifier, String, Boolean, null or Number", location);
|
||||||
|
}
|
||||||
|
|
||||||
match(Sym.COLON);
|
match(Sym.COLON);
|
||||||
Expr value = expr();
|
Expr value = expr();
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
throw new ParseException("Expression error: the value on the right side of map entry can not be blank", location);
|
throw new ParseException("Expression error: the value on the right side of map entry can not be blank", location);
|
||||||
}
|
}
|
||||||
map.put(tok.value(), value);
|
map.put(key, value);
|
||||||
return ;
|
|
||||||
}
|
|
||||||
throw new ParseException("Expression error: the value of map key must be identifier or String", location);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -528,12 +512,14 @@ public class ExprParser {
|
|||||||
move();
|
move();
|
||||||
return new Id(tok.value());
|
return new Id(tok.value());
|
||||||
case STR:
|
case STR:
|
||||||
|
move();
|
||||||
|
return new Const(tok.sym, tok.value());
|
||||||
case INT:
|
case INT:
|
||||||
case LONG:
|
case LONG:
|
||||||
case FLOAT:
|
case FLOAT:
|
||||||
case DOUBLE:
|
case DOUBLE:
|
||||||
move();
|
move();
|
||||||
return new Const(tok.sym, tok.value());
|
return new Const(tok.sym, ((NumTok)tok).getNumberValue());
|
||||||
case TRUE:
|
case TRUE:
|
||||||
move();
|
move();
|
||||||
return Const.TRUE;
|
return Const.TRUE;
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -43,7 +43,7 @@ import com.jfinal.template.stat.ParseException;
|
|||||||
*/
|
*/
|
||||||
public class NumTok extends Tok {
|
public class NumTok extends Tok {
|
||||||
|
|
||||||
private Object value;
|
private Number value;
|
||||||
|
|
||||||
NumTok(Sym sym, String s, int radix, boolean isScientificNotation, Location location) {
|
NumTok(Sym sym, String s, int radix, boolean isScientificNotation, Location location) {
|
||||||
super(sym, location.getRow());
|
super(sym, location.getRow());
|
||||||
@@ -101,3 +101,8 @@ public class NumTok extends Tok {
|
|||||||
return sym.value() + " : " + value;
|
return sym.value() + " : " + value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -134,11 +134,12 @@ public class Compare extends Expr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (leftValue instanceof Comparable &&
|
if (leftValue instanceof Comparable &&
|
||||||
|
rightValue != null &&
|
||||||
leftValue.getClass() == rightValue.getClass()) {
|
leftValue.getClass() == rightValue.getClass()) {
|
||||||
return ((Comparable)leftValue).compareTo((Comparable)rightValue) > 0;
|
return ((Comparable)leftValue).compareTo((Comparable)rightValue) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return checkType(leftValue, rightValue);
|
return checkComparisonValue(leftValue, rightValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
@@ -166,11 +167,12 @@ public class Compare extends Expr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (leftValue instanceof Comparable &&
|
if (leftValue instanceof Comparable &&
|
||||||
|
rightValue != null &&
|
||||||
leftValue.getClass() == rightValue.getClass()) {
|
leftValue.getClass() == rightValue.getClass()) {
|
||||||
return ((Comparable)leftValue).compareTo((Comparable)rightValue) >= 0;
|
return ((Comparable)leftValue).compareTo((Comparable)rightValue) >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return checkType(leftValue, rightValue);
|
return checkComparisonValue(leftValue, rightValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
@@ -198,11 +200,12 @@ public class Compare extends Expr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (leftValue instanceof Comparable &&
|
if (leftValue instanceof Comparable &&
|
||||||
|
rightValue != null &&
|
||||||
leftValue.getClass() == rightValue.getClass()) {
|
leftValue.getClass() == rightValue.getClass()) {
|
||||||
return ((Comparable)leftValue).compareTo((Comparable)rightValue) < 0;
|
return ((Comparable)leftValue).compareTo((Comparable)rightValue) < 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return checkType(leftValue, rightValue);
|
return checkComparisonValue(leftValue, rightValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
@@ -230,11 +233,12 @@ public class Compare extends Expr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (leftValue instanceof Comparable &&
|
if (leftValue instanceof Comparable &&
|
||||||
|
rightValue != null &&
|
||||||
leftValue.getClass() == rightValue.getClass()) {
|
leftValue.getClass() == rightValue.getClass()) {
|
||||||
return ((Comparable)leftValue).compareTo((Comparable)rightValue) <= 0;
|
return ((Comparable)leftValue).compareTo((Comparable)rightValue) <= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return checkType(leftValue, rightValue);
|
return checkComparisonValue(leftValue, rightValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getMaxType(Number obj1, Number obj2) {
|
private int getMaxType(Number obj1, Number obj2) {
|
||||||
@@ -270,7 +274,7 @@ public class Compare extends Expr {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Boolean checkType(Object leftValue, Object rightValue) {
|
private Boolean checkComparisonValue(Object leftValue, Object rightValue) {
|
||||||
if (leftValue == null) {
|
if (leftValue == null) {
|
||||||
throw new TemplateException("The operation target on the left side of \"" + op.value() + "\" can not be null", location);
|
throw new TemplateException("The operation target on the left side of \"" + op.value() + "\" can not be null", location);
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -20,54 +20,25 @@ import com.jfinal.template.expr.Sym;
|
|||||||
import com.jfinal.template.stat.Scope;
|
import com.jfinal.template.stat.Scope;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* STR INT LONG FLOAT DOUBLE true false null
|
* STR INT LONG FLOAT DOUBLE TRUE FALSE NULL
|
||||||
*/
|
*/
|
||||||
public class Const extends Expr {
|
public class Const extends Expr {
|
||||||
|
|
||||||
public static final Const TRUE = new Const(Boolean.TRUE, Sym.TRUE);
|
public static final Const TRUE = new Const(Sym.TRUE, Boolean.TRUE);
|
||||||
public static final Const FALSE = new Const(Boolean.FALSE, Sym.FALSE);
|
public static final Const FALSE = new Const(Sym.FALSE, Boolean.FALSE);
|
||||||
public static final Const NULL = new Const(null, Sym.NULL);
|
public static final Const NULL = new Const(Sym.NULL, null);
|
||||||
|
|
||||||
private Sym type;
|
private final Sym type;
|
||||||
private Object value;
|
private final Object value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造 TRUE FALSE NULL 常量,无需对 value 进行转换
|
* INT LONG FLOAT DOUBLE 常量已在 NumTok 中转换成了确切的类型,无需再次转换
|
||||||
*/
|
*/
|
||||||
private Const(Object value, Sym type) {
|
public Const(Sym type, Object value) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Const(Sym type, String value) {
|
|
||||||
this.type = type;
|
|
||||||
this.value = typeConvert(type, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Object typeConvert(Sym type, String value) {
|
|
||||||
switch (type) {
|
|
||||||
case STR:
|
|
||||||
return value;
|
|
||||||
case INT:
|
|
||||||
return Integer.parseInt(value);
|
|
||||||
case LONG:
|
|
||||||
return Long.parseLong(value);
|
|
||||||
case FLOAT:
|
|
||||||
return Float.parseFloat(value);
|
|
||||||
case DOUBLE:
|
|
||||||
return Double.parseDouble(value);
|
|
||||||
/*
|
|
||||||
case TRUE:
|
|
||||||
case FALSE:
|
|
||||||
return Boolean.parseBoolean(value);
|
|
||||||
case NULL:
|
|
||||||
return null;
|
|
||||||
*/
|
|
||||||
default:
|
|
||||||
throw new RuntimeException("never happend");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object eval(Scope scope) {
|
public Object eval(Scope scope) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
@@ -108,6 +79,10 @@ public class Const extends Expr {
|
|||||||
return type == Sym.DOUBLE;
|
return type == Sym.DOUBLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isNumber() {
|
||||||
|
return value instanceof Number;
|
||||||
|
}
|
||||||
|
|
||||||
public Object getValue() {
|
public Object getValue() {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
@@ -136,6 +111,10 @@ public class Const extends Expr {
|
|||||||
return (Double)value;
|
return (Double)value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Number getNumber() {
|
||||||
|
return (Number)value;
|
||||||
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return value != null ? value.toString() : "null";
|
return value != null ? value.toString() : "null";
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
181
src/main/java/com/jfinal/template/expr/ast/FastFieldGetter.java
Normal file
181
src/main/java/com/jfinal/template/expr/ast/FastFieldGetter.java
Normal file
@@ -0,0 +1,181 @@
|
|||||||
|
package com.jfinal.template.expr.ast;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import com.jfinal.kit.StrKit;
|
||||||
|
import com.jfinal.proxy.ProxyClassLoader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用 jfinal proxy 机制消除 java.lang.reflect.Method.invoke(...)
|
||||||
|
* 提升性能,并且同时支持动态类型的 field 表达式取值
|
||||||
|
*/
|
||||||
|
public class FastFieldGetter extends FieldGetter {
|
||||||
|
|
||||||
|
protected static ProxyGenerator generator = new ProxyGenerator();
|
||||||
|
protected static ProxyCompiler compiler = new ProxyCompiler();
|
||||||
|
protected static ProxyClassLoader classLoader = new ProxyClassLoader();
|
||||||
|
protected static Map<Class<?>, Proxy> cache = new ConcurrentHashMap<>(512, 0.25F);
|
||||||
|
|
||||||
|
protected static boolean outputCompileError = false;
|
||||||
|
|
||||||
|
protected Proxy proxy;
|
||||||
|
protected java.lang.reflect.Method getterMethod;
|
||||||
|
|
||||||
|
public FastFieldGetter(Proxy proxy, java.lang.reflect.Method getterMethod) {
|
||||||
|
this.proxy = proxy;
|
||||||
|
this.getterMethod = getterMethod;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 仅用于配置 Engine.addFieldGetter(0, new FastFieldGetter());
|
||||||
|
*/
|
||||||
|
public FastFieldGetter() {
|
||||||
|
this(null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FieldGetter takeOver(Class<?> targetClass, String fieldName) {
|
||||||
|
if (MethodKit.isForbiddenClass(targetClass)) {
|
||||||
|
throw new RuntimeException("Forbidden class: " + targetClass.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
String getterName = "get" + StrKit.firstCharToUpperCase(fieldName);
|
||||||
|
java.lang.reflect.Method[] methodArray = targetClass.getMethods();
|
||||||
|
for (java.lang.reflect.Method method : methodArray) {
|
||||||
|
if (method.getName().equals(getterName) && method.getParameterCount() == 0) {
|
||||||
|
|
||||||
|
Proxy proxy = cache.get(targetClass);
|
||||||
|
if (proxy == null) {
|
||||||
|
synchronized (targetClass) {
|
||||||
|
proxy = cache.get(targetClass);
|
||||||
|
if (proxy == null) {
|
||||||
|
try {
|
||||||
|
proxy = createProxy(targetClass, fieldName);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
cache.putIfAbsent(targetClass, proxy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new FastFieldGetter(proxy, method);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object get(Object target, String fieldName) throws Exception {
|
||||||
|
// return getterMethod.invoke(target, ExprList.NULL_OBJECT_ARRAY);
|
||||||
|
return proxy.getValue(target, fieldName);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Proxy createProxy(Class<?> targetClass, String fieldName) {
|
||||||
|
ProxyClass proxyClass = new ProxyClass(targetClass);
|
||||||
|
String sourceCode = generator.generate(proxyClass);
|
||||||
|
// System.out.println(sourceCode);
|
||||||
|
|
||||||
|
proxyClass.setSourceCode(sourceCode);
|
||||||
|
compiler.compile(proxyClass);
|
||||||
|
Class<?> retClass = classLoader.loadProxyClass(proxyClass);
|
||||||
|
proxyClass.setClazz(retClass);
|
||||||
|
try {
|
||||||
|
return (Proxy)retClass.newInstance();
|
||||||
|
} catch (ReflectiveOperationException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return getterMethod.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 代理接口
|
||||||
|
*/
|
||||||
|
public static interface Proxy {
|
||||||
|
public Object getValue(Object target, String fieldName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 代理类
|
||||||
|
*/
|
||||||
|
static class ProxyClass extends com.jfinal.proxy.ProxyClass {
|
||||||
|
|
||||||
|
private String name; // 类名
|
||||||
|
|
||||||
|
public ProxyClass(Class<?> target) {
|
||||||
|
super(target);
|
||||||
|
|
||||||
|
name = target.getSimpleName() + "$$EnhancerByJFinal_FieldGetter";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 代理生成器
|
||||||
|
*/
|
||||||
|
static class ProxyGenerator {
|
||||||
|
|
||||||
|
String generate(ProxyClass proxyClass) {
|
||||||
|
StringBuilder ret = new StringBuilder(1024);
|
||||||
|
|
||||||
|
Class<?> targetClass = proxyClass.getTarget();
|
||||||
|
String className = proxyClass.getName();
|
||||||
|
|
||||||
|
ret.append("package ").append(proxyClass.getPkg()).append(";\n\n");
|
||||||
|
ret.append("import com.jfinal.template.expr.ast.FastFieldGetter.Proxy;\n\n");
|
||||||
|
ret.append("public class ").append(className).append(" implements Proxy {\n\n");
|
||||||
|
ret.append("\tpublic Object getValue(Object target, String fieldName) {\n");
|
||||||
|
ret.append("\t\tint hash = fieldName.hashCode();\n");
|
||||||
|
ret.append("\t\tswitch (hash) {\n");
|
||||||
|
|
||||||
|
java.lang.reflect.Method[] methodArray = targetClass.getMethods();
|
||||||
|
for (java.lang.reflect.Method method : methodArray) {
|
||||||
|
String mn = method.getName();
|
||||||
|
if (method.getParameterCount() == 0 && mn.startsWith("get") && (!mn.equals("getClass"))) {
|
||||||
|
String fieldName = StrKit.firstCharToLowerCase(mn.substring(3));
|
||||||
|
ret.append("\t\tcase ").append(fieldName.hashCode()).append(" :\n");
|
||||||
|
ret.append("\t\t\treturn ((").append(targetClass.getName()).append(")target).").append(mn).append("();\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ret.append("\t\tdefault :\n");
|
||||||
|
ret.append("\t\t\tthrow new RuntimeException(\"Can not access the field \\\"\" + target.getClass().getName() + \".\" + fieldName + \"\\\"\");\n");
|
||||||
|
|
||||||
|
ret.append("\t\t}\n");
|
||||||
|
ret.append("\t}\n");
|
||||||
|
ret.append("}\n");
|
||||||
|
|
||||||
|
return ret.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------
|
||||||
|
|
||||||
|
public static void setOutputCompileError(boolean outputCompileError) {
|
||||||
|
FastFieldGetter.outputCompileError = outputCompileError;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 代理编译器
|
||||||
|
*/
|
||||||
|
static class ProxyCompiler extends com.jfinal.proxy.ProxyCompiler {
|
||||||
|
@Override
|
||||||
|
protected void outputCompileError(Boolean result, javax.tools.DiagnosticCollector<javax.tools.JavaFileObject> collector) {
|
||||||
|
if (outputCompileError) {
|
||||||
|
super.outputCompileError(result, collector);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -16,11 +16,8 @@
|
|||||||
|
|
||||||
package com.jfinal.template.expr.ast;
|
package com.jfinal.template.expr.ast;
|
||||||
|
|
||||||
import java.lang.reflect.Array;
|
|
||||||
import com.jfinal.kit.HashKit;
|
import com.jfinal.kit.HashKit;
|
||||||
import com.jfinal.kit.StrKit;
|
import com.jfinal.kit.StrKit;
|
||||||
// import com.jfinal.plugin.activerecord.Model;
|
|
||||||
// import com.jfinal.plugin.activerecord.Record;
|
|
||||||
import com.jfinal.template.TemplateException;
|
import com.jfinal.template.TemplateException;
|
||||||
import com.jfinal.template.stat.Location;
|
import com.jfinal.template.stat.Location;
|
||||||
import com.jfinal.template.stat.ParseException;
|
import com.jfinal.template.stat.ParseException;
|
||||||
@@ -31,10 +28,10 @@ import com.jfinal.template.stat.Scope;
|
|||||||
*
|
*
|
||||||
* field 表达式取值优先次序,以 user.name 为例
|
* field 表达式取值优先次序,以 user.name 为例
|
||||||
* 1:假如 user.getName() 存在,则优先调用
|
* 1:假如 user.getName() 存在,则优先调用
|
||||||
* 2:假如 user 为 Model 子类,则调用 user.get("name")
|
* 2:假如 user 具有 public name 属性,则取 user.name 属性值
|
||||||
* 3:假如 user 为 Record,则调用 user.get("name")
|
* 3:假如 user 为 Model 子类,则调用 user.get("name")
|
||||||
* 4:假如 user 为 Map,则调用 user.get("name")
|
* 4:假如 user 为 Record,则调用 user.get("name")
|
||||||
* 5:假如 user 具有 public name 属性,则取 user.name 属性值
|
* 5:假如 user 为 Map,则调用 user.get("name")
|
||||||
*/
|
*/
|
||||||
public class Field extends Expr {
|
public class Field extends Expr {
|
||||||
|
|
||||||
@@ -68,59 +65,34 @@ public class Field extends Expr {
|
|||||||
throw new TemplateException("Can not accessed by \"" + fieldName + "\" field from null target", location);
|
throw new TemplateException("Can not accessed by \"" + fieldName + "\" field from null target", location);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
Class<?> targetClass = target.getClass();
|
Class<?> targetClass = target.getClass();
|
||||||
Long key = buildFieldKey(targetClass);
|
Object key = FieldKeyBuilder.instance.getFieldKey(targetClass, getterNameHash);
|
||||||
|
FieldGetter fieldGetter = FieldKit.getFieldGetter(key, targetClass, fieldName);
|
||||||
MethodInfo getter;
|
if (fieldGetter.notNull()) {
|
||||||
try {
|
return fieldGetter.get(target, fieldName);
|
||||||
getter = MethodKit.getGetterMethod(key, targetClass, getterName);
|
}
|
||||||
|
} catch (TemplateException | ParseException e) {
|
||||||
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new TemplateException(e.getMessage(), location, e);
|
throw new TemplateException(e.getMessage(), location, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
if (getter != null) {
|
|
||||||
return getter.invoke(target, ExprList.NULL_OBJECT_ARRAY);
|
|
||||||
}
|
|
||||||
// if (target instanceof Model) {
|
|
||||||
// return ((Model<?>)target).get(fieldName);
|
|
||||||
// }
|
|
||||||
// if (target instanceof Record) {
|
|
||||||
// return ((Record)target).get(fieldName);
|
|
||||||
// }
|
|
||||||
if (target instanceof java.util.Map) {
|
|
||||||
return ((java.util.Map<?, ?>)target).get(fieldName);
|
|
||||||
}
|
|
||||||
// if (target instanceof com.jfinal.kit.Ret) {
|
|
||||||
// return ((com.jfinal.kit.Ret)target).get(fieldName);
|
|
||||||
// }
|
|
||||||
java.lang.reflect.Field field = FieldKit.getField(key, targetClass, fieldName);
|
|
||||||
if (field != null) {
|
|
||||||
return field.get(target);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 支持获取数组长度: array.length
|
|
||||||
if ("length".equals(fieldName) && target.getClass().isArray()) {
|
|
||||||
return Array.getLength(target);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new TemplateException(e.getMessage(), location, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (scope.getCtrl().isNullSafe()) {
|
if (scope.getCtrl().isNullSafe()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (expr instanceof Id) {
|
if (expr instanceof Id) {
|
||||||
String id = ((Id)expr).getId();
|
String id = ((Id)expr).getId();
|
||||||
throw new TemplateException("Field not found: \"" + id + "." + fieldName + "\" and getter method not found: \"" + id + "." + getterName + "()\"", location);
|
throw new TemplateException("public field not found: \"" + id + "." + fieldName + "\" and public getter method not found: \"" + id + "." + getterName + "()\"", location);
|
||||||
}
|
}
|
||||||
throw new TemplateException("Field not found: \"" + fieldName + "\" and getter method not found: \"" + getterName + "()\"", location);
|
throw new TemplateException("public field not found: \"" + fieldName + "\" and public getter method not found: \"" + getterName + "()\"", location);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Long buildFieldKey(Class<?> targetClass) {
|
// private Long buildFieldKey(Class<?> targetClass) {
|
||||||
return targetClass.getName().hashCode() ^ getterNameHash;
|
// return targetClass.getName().hashCode() ^ getterNameHash;
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
58
src/main/java/com/jfinal/template/expr/ast/FieldGetter.java
Normal file
58
src/main/java/com/jfinal/template/expr/ast/FieldGetter.java
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FieldGetter 用于支持 target.field 表达式的取值,
|
||||||
|
* 以及支持用户扩展自定义的 FieldGetter 实现方式
|
||||||
|
*/
|
||||||
|
public abstract class FieldGetter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 接管 target.fieldName 表达式,如果可以接管则返回接管对象,否则返回 null
|
||||||
|
* @param targetClass target.fieldName 表达式中 target 的 Class 类型
|
||||||
|
* @param fieldName target.fieldName 表达式中的 fieldName 部分
|
||||||
|
* @return 如果可以接管 targetClass.fieldName 则返回接管对象,否则返回 null
|
||||||
|
*/
|
||||||
|
public abstract FieldGetter takeOver(Class<?> targetClass, String fieldName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 target.fieldName 表达式的值
|
||||||
|
* @param target 目标对象
|
||||||
|
* @param fieldName 字段名称
|
||||||
|
* @return target.fieldName 表达式的值
|
||||||
|
*/
|
||||||
|
public abstract Object get(Object target, String fieldName) throws Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 仅仅 NullFieldGetter 会覆盖此方法并返回 false
|
||||||
|
*
|
||||||
|
* 用于消除 FieldKit.getFieldGetter(...) 中的 instanceof 判断,
|
||||||
|
* 并且让 Map fieldGetterCache 的 value 值不必使用 Object 类型,
|
||||||
|
* 还消除了 Field.exec(...) 中的 null 值判断
|
||||||
|
*/
|
||||||
|
public boolean notNull() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
270
src/main/java/com/jfinal/template/expr/ast/FieldGetters.java
Normal file
270
src/main/java/com/jfinal/template/expr/ast/FieldGetters.java
Normal file
@@ -0,0 +1,270 @@
|
|||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import java.lang.reflect.Array;
|
||||||
|
import com.jfinal.kit.StrKit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FieldGetters 封装官方默认 FieldGetter 实现
|
||||||
|
*/
|
||||||
|
public class FieldGetters {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NullFieldGetter
|
||||||
|
*
|
||||||
|
* 用于消除 FieldKit.getFieldGetter(...) 中的 instanceof 判断,并且让 Map fieldGetterCache
|
||||||
|
* 中的 value 不必使用 Object 类型。还消除了 Field.exec(...) 中的 null 值判断
|
||||||
|
*/
|
||||||
|
public static class NullFieldGetter extends FieldGetter {
|
||||||
|
|
||||||
|
public static final NullFieldGetter me = new NullFieldGetter();
|
||||||
|
|
||||||
|
public boolean notNull() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FieldGetter takeOver(Class<?> targetClass, String fieldName) {
|
||||||
|
throw new RuntimeException("The method takeOver(Class, String) of NullFieldGetter should not be invoked");
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object get(Object target, String fieldName) throws Exception {
|
||||||
|
throw new RuntimeException("The method get(Object, String) of NullFieldGetter should not be invoked");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GetterMethodFieldGetter
|
||||||
|
*
|
||||||
|
* 使用 getter 方法获取 target.field 表达式的值
|
||||||
|
*/
|
||||||
|
public static class GetterMethodFieldGetter extends FieldGetter {
|
||||||
|
|
||||||
|
protected java.lang.reflect.Method getterMethod;
|
||||||
|
|
||||||
|
public GetterMethodFieldGetter(java.lang.reflect.Method getterMethod) {
|
||||||
|
this.getterMethod = getterMethod;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FieldGetter takeOver(Class<?> targetClass, String fieldName) {
|
||||||
|
if (MethodKit.isForbiddenClass(targetClass)) {
|
||||||
|
throw new RuntimeException("Forbidden class: " + targetClass.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
String getterName = "get" + StrKit.firstCharToUpperCase(fieldName);
|
||||||
|
java.lang.reflect.Method[] methodArray = targetClass.getMethods();
|
||||||
|
for (java.lang.reflect.Method method : methodArray) {
|
||||||
|
if (method.getName().equals(getterName) && method.getParameterCount() == 0) {
|
||||||
|
// if (MethodKit.isForbiddenMethod(getterName)) {
|
||||||
|
// throw new RuntimeException("Forbidden method: " + getterName);
|
||||||
|
// }
|
||||||
|
|
||||||
|
return new GetterMethodFieldGetter(method);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object get(Object target, String fieldName) throws Exception {
|
||||||
|
return getterMethod.invoke(target, ExprList.NULL_OBJECT_ARRAY);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return getterMethod.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IsMethodFieldGetter
|
||||||
|
*
|
||||||
|
* 使用 target.isXxx() 方法获取值,默认不启用该功能,用户可以通过如下方式启用:
|
||||||
|
* Engine.addLastFieldGetter(new FieldGetters.IsMethodFieldGetter());
|
||||||
|
*/
|
||||||
|
public static class IsMethodFieldGetter extends FieldGetter {
|
||||||
|
|
||||||
|
protected java.lang.reflect.Method isMethod;
|
||||||
|
|
||||||
|
// 此构造方法仅为了方便在 Engine.addFieldGetter(...) 添加时不用为构造方法传参
|
||||||
|
public IsMethodFieldGetter() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public IsMethodFieldGetter(java.lang.reflect.Method isMethod) {
|
||||||
|
this.isMethod = isMethod;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FieldGetter takeOver(Class<?> targetClass, String fieldName) {
|
||||||
|
if (MethodKit.isForbiddenClass(targetClass)) {
|
||||||
|
throw new RuntimeException("Forbidden class: " + targetClass.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
String isMethodName = "is" + StrKit.firstCharToUpperCase(fieldName);
|
||||||
|
java.lang.reflect.Method[] methodArray = targetClass.getMethods();
|
||||||
|
for (java.lang.reflect.Method method : methodArray) {
|
||||||
|
if (method.getName().equals(isMethodName) && method.getParameterCount() == 0) {
|
||||||
|
Class<?> returnType = method.getReturnType();
|
||||||
|
if (returnType == Boolean.class || returnType == boolean.class) {
|
||||||
|
return new IsMethodFieldGetter(method);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object get(Object target, String fieldName) throws Exception {
|
||||||
|
return isMethod.invoke(target, ExprList.NULL_OBJECT_ARRAY);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return isMethod.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ModelFieldGetter
|
||||||
|
*
|
||||||
|
* 使用 Model.get(String) 获取值
|
||||||
|
|
||||||
|
public static class ModelFieldGetter extends FieldGetter {
|
||||||
|
|
||||||
|
// 所有 Model 可以共享 ModelFieldGetter 获取属性
|
||||||
|
static final ModelFieldGetter singleton = new ModelFieldGetter();
|
||||||
|
|
||||||
|
public FieldGetter takeOver(Class<?> targetClass, String fieldName) {
|
||||||
|
if (com.jfinal.plugin.activerecord.Model.class.isAssignableFrom(targetClass)) {
|
||||||
|
return singleton;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object get(Object target, String fieldName) throws Exception {
|
||||||
|
return ((com.jfinal.plugin.activerecord.Model<?>)target).get(fieldName);
|
||||||
|
}
|
||||||
|
} */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RecordFieldGetter
|
||||||
|
*
|
||||||
|
* 使用 Record.get(String) 获取值
|
||||||
|
|
||||||
|
public static class RecordFieldGetter extends FieldGetter {
|
||||||
|
|
||||||
|
// 所有 Record 可以共享 RecordFieldGetter 获取属性
|
||||||
|
static final RecordFieldGetter singleton = new RecordFieldGetter();
|
||||||
|
|
||||||
|
public FieldGetter takeOver(Class<?> targetClass, String fieldName) {
|
||||||
|
if (com.jfinal.plugin.activerecord.Record.class.isAssignableFrom(targetClass)) {
|
||||||
|
return singleton;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object get(Object target, String fieldName) throws Exception {
|
||||||
|
return ((com.jfinal.plugin.activerecord.Record)target).get(fieldName);
|
||||||
|
}
|
||||||
|
} */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MapFieldGetter
|
||||||
|
*
|
||||||
|
* 使用 Map.get(Object) 获取值
|
||||||
|
*/
|
||||||
|
public static class MapFieldGetter extends FieldGetter {
|
||||||
|
|
||||||
|
// 所有 Map 可以共享 MapFieldGetter 获取属性
|
||||||
|
static final MapFieldGetter singleton = new MapFieldGetter();
|
||||||
|
|
||||||
|
public FieldGetter takeOver(Class<?> targetClass, String fieldName) {
|
||||||
|
if (java.util.Map.class.isAssignableFrom(targetClass)) {
|
||||||
|
return singleton;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object get(Object target, String fieldName) throws Exception {
|
||||||
|
return ((java.util.Map<?, ?>)target).get(fieldName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RealFieldGetter
|
||||||
|
*
|
||||||
|
* 使用 target.field 获取值
|
||||||
|
*/
|
||||||
|
public static class RealFieldGetter extends FieldGetter {
|
||||||
|
|
||||||
|
protected java.lang.reflect.Field field;
|
||||||
|
|
||||||
|
public RealFieldGetter(java.lang.reflect.Field field) {
|
||||||
|
this.field = field;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FieldGetter takeOver(Class<?> targetClass, String fieldName) {
|
||||||
|
java.lang.reflect.Field[] fieldArray = targetClass.getFields();
|
||||||
|
for (java.lang.reflect.Field field : fieldArray) {
|
||||||
|
if (field.getName().equals(fieldName)) {
|
||||||
|
return new RealFieldGetter(field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object get(Object target, String fieldName) throws Exception {
|
||||||
|
return field.get(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return field.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ArrayLengthGetter
|
||||||
|
*
|
||||||
|
* 获取数组长度: array.length
|
||||||
|
*/
|
||||||
|
public static class ArrayLengthGetter extends FieldGetter {
|
||||||
|
|
||||||
|
// 所有数组可以共享 ArrayLengthGetter 获取属性
|
||||||
|
static final ArrayLengthGetter singleton = new ArrayLengthGetter();
|
||||||
|
|
||||||
|
public FieldGetter takeOver(Class<?> targetClass, String fieldName) {
|
||||||
|
if ("length".equals(fieldName) && targetClass.isArray()) {
|
||||||
|
return singleton;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object get(Object target, String fieldName) throws Exception {
|
||||||
|
return Array.getLength(target);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
132
src/main/java/com/jfinal/template/expr/ast/FieldKeyBuilder.java
Normal file
132
src/main/java/com/jfinal/template/expr/ast/FieldKeyBuilder.java
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FieldKeyBuilder
|
||||||
|
*
|
||||||
|
* 用于生成缓存 FieldGetter 的 key
|
||||||
|
*/
|
||||||
|
public abstract class FieldKeyBuilder {
|
||||||
|
|
||||||
|
public abstract Object getFieldKey(Class<?> targetClass, long fieldFnv1a64Hash);
|
||||||
|
|
||||||
|
// 假定是超大规模项目,并且假定其 Map/Model/Record + field 组合数量超级庞大,默认使用 StrictFieldKeyBuilder
|
||||||
|
static FieldKeyBuilder instance = new StrictFieldKeyBuilder();
|
||||||
|
|
||||||
|
public static FieldKeyBuilder getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开启 FastFieldKeyBuilder,性能更高
|
||||||
|
*/
|
||||||
|
public static void setFastFieldKeyBuilder(boolean enable) {
|
||||||
|
if (enable) {
|
||||||
|
instance = new FastFieldKeyBuilder();
|
||||||
|
} else {
|
||||||
|
instance = new StrictFieldKeyBuilder();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置为自定义 FieldKeyBuilder
|
||||||
|
*/
|
||||||
|
public static void setFieldKeyBuilder(FieldKeyBuilder fieldKeyBuilder) {
|
||||||
|
if (fieldKeyBuilder == null) {
|
||||||
|
throw new IllegalArgumentException("fieldKeyBuilder can not be null");
|
||||||
|
}
|
||||||
|
instance = fieldKeyBuilder;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FastFieldKeyBuilder
|
||||||
|
*/
|
||||||
|
public static class FastFieldKeyBuilder extends FieldKeyBuilder {
|
||||||
|
public Object getFieldKey(Class<?> targetClass, long fieldFnv1a64Hash) {
|
||||||
|
return targetClass.getName().hashCode() ^ fieldFnv1a64Hash;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* StrictFieldKeyBuilder
|
||||||
|
*/
|
||||||
|
public static class StrictFieldKeyBuilder extends FieldKeyBuilder {
|
||||||
|
public Object getFieldKey(Class<?> targetClass, long fieldFnv1a64Hash) {
|
||||||
|
return new FieldKey(targetClass.getName().hashCode(), fieldFnv1a64Hash);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FieldKey
|
||||||
|
*
|
||||||
|
* FieldKey 用于封装 targetClass、fieldName 这两部分的 hash 值,
|
||||||
|
* 确保不会出现 key 值碰撞
|
||||||
|
*
|
||||||
|
* 这两部分 hash 值在不同 class 与 field 的组合下出现碰撞的
|
||||||
|
* 概率完全可以忽略不计
|
||||||
|
*
|
||||||
|
* 备忘:
|
||||||
|
* 可以考虑用 ThreadLocal 重用 FieldKey 对象,但要注意放入 Map fieldGetterCache
|
||||||
|
* 中的 FieldKey 对象需要 clone 出来,确保线程安全。由于 FieldKey 占用空间不大,
|
||||||
|
* 所以 ThreadLocal 方案大概率并无优势,从 ThreadLocal 中获取数据时,除了耗时也无法
|
||||||
|
* 避免创建对象
|
||||||
|
*/
|
||||||
|
public static class FieldKey {
|
||||||
|
|
||||||
|
final long classHash;
|
||||||
|
final long fieldHash;
|
||||||
|
|
||||||
|
public FieldKey(long classHash, long fieldHash) {
|
||||||
|
this.classHash = classHash;
|
||||||
|
this.fieldHash = fieldHash;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int hashCode() {
|
||||||
|
return (int)(classHash ^ fieldHash);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FieldKey 的核心价值在于此 equals 方法通过比较两部分 hash 值,避免超大规模场景下可能的 key 值碰撞
|
||||||
|
*
|
||||||
|
* 不必判断 if (fieldKey instanceof FieldKey),因为所有 key 类型必须要相同
|
||||||
|
* 不必判断 if (this == fieldKey),因为每次用于取值的 FieldKey 都是新建的
|
||||||
|
*/
|
||||||
|
public boolean equals(Object fieldKey) {
|
||||||
|
FieldKey fk = (FieldKey)fieldKey;
|
||||||
|
return classHash == fk.classHash && fieldHash == fk.fieldHash;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "classHash = " + classHash + "\nfieldHash = " + fieldHash;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -16,38 +16,159 @@
|
|||||||
|
|
||||||
package com.jfinal.template.expr.ast;
|
package com.jfinal.template.expr.ast;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import com.jfinal.kit.SyncWriteMap;
|
||||||
|
import com.jfinal.template.expr.ast.FieldGetters.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FieldKit
|
* FieldKit
|
||||||
*/
|
*/
|
||||||
public class FieldKit {
|
public class FieldKit {
|
||||||
|
|
||||||
private static final HashMap<Long, Object> fieldCache = new HashMap<Long, Object>();
|
private static FieldGetter[] getters = init();
|
||||||
|
|
||||||
public static Field getField(Long key, Class<?> targetClass, String fieldName) {
|
private static final HashMap<Object, FieldGetter> fieldGetterCache = new SyncWriteMap<Object, FieldGetter>(1024, 0.25F);
|
||||||
Object field = fieldCache.get(key);
|
|
||||||
if (field == null) {
|
/**
|
||||||
field = doGetField(targetClass, fieldName);
|
* 初始化官方默认 FieldGetter
|
||||||
if (field != null) {
|
*
|
||||||
fieldCache.put(key, field);
|
* 注意:
|
||||||
|
* 默认不启用 IsMethodFieldGetter,用户可以通过下面的代码启用:
|
||||||
|
* Engine.addLastFieldGetter(new FieldGetters.IsMethodFieldGetter());
|
||||||
|
*
|
||||||
|
* 也可以通过直接调用 target.isXxx() 方法来达到与 target.xxx 表达式相同的目的
|
||||||
|
*/
|
||||||
|
private static FieldGetter[] init() {
|
||||||
|
LinkedList<FieldGetter> ret = new LinkedList<FieldGetter>();
|
||||||
|
|
||||||
|
ret.addLast(new GetterMethodFieldGetter(null));
|
||||||
|
ret.addLast(new RealFieldGetter(null));
|
||||||
|
// ret.addLast(new ModelFieldGetter());
|
||||||
|
// ret.addLast(new RecordFieldGetter());
|
||||||
|
ret.addLast(new MapFieldGetter());
|
||||||
|
|
||||||
|
// 挪到第二的位置,addSharedObject(..., modelObj) 用法可以获取到 model 中的 public 属性
|
||||||
|
// ret.addLast(new RealFieldGetter(null));
|
||||||
|
ret.addLast(new ArrayLengthGetter());
|
||||||
|
// ret.addLast(new IsMethodFieldGetter());
|
||||||
|
|
||||||
|
return ret.toArray(new FieldGetter[ret.size()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FieldGetter getFieldGetter(Object key, Class<?> targetClass, String fieldName) {
|
||||||
|
FieldGetter fieldGetter = fieldGetterCache.get(key);
|
||||||
|
if (fieldGetter == null) {
|
||||||
|
fieldGetter = doGetFieldGetter(targetClass, fieldName); // 已确保不会返回 null
|
||||||
|
fieldGetterCache.putIfAbsent(key, fieldGetter);
|
||||||
|
}
|
||||||
|
return fieldGetter;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static FieldGetter doGetFieldGetter(Class<?> targetClass, String fieldName) {
|
||||||
|
FieldGetter ret;
|
||||||
|
for (FieldGetter fieldGetter : getters) {
|
||||||
|
ret = fieldGetter.takeOver(targetClass, fieldName);
|
||||||
|
if (ret != null) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NullFieldGetter.me;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addFieldGetter(int index, FieldGetter fieldGetter) {
|
||||||
|
addFieldGetter(fieldGetter, index, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addFieldGetterToLast(FieldGetter fieldGetter) {
|
||||||
|
addFieldGetter(fieldGetter, null, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addFieldGetterToFirst(FieldGetter fieldGetter) {
|
||||||
|
addFieldGetter(fieldGetter, null, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 当 Integer index 不为 null 时,boolean addLast 为无效参数
|
||||||
|
private static synchronized void addFieldGetter(FieldGetter fieldGetter, Integer index, boolean addLast) {
|
||||||
|
checkParameter(fieldGetter);
|
||||||
|
|
||||||
|
LinkedList<FieldGetter> ret = getCurrentFieldGetters();
|
||||||
|
if (index != null) {
|
||||||
|
ret.add(index, fieldGetter);
|
||||||
} else {
|
} else {
|
||||||
// 对于不存在的 Field,只进行一次获取操作,主要为了支持 null safe,未来需要考虑内存泄漏风险
|
if (addLast) {
|
||||||
fieldCache.put(key, Boolean.FALSE);
|
ret.addLast(fieldGetter);
|
||||||
|
} else {
|
||||||
|
ret.addFirst(fieldGetter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return field instanceof Field ? (Field)field : null;
|
getters = ret.toArray(new FieldGetter[ret.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Field doGetField(Class<?> targetClass, String fieldName) {
|
private static LinkedList<FieldGetter> getCurrentFieldGetters() {
|
||||||
Field[] fs = targetClass.getFields();
|
LinkedList<FieldGetter> ret = new LinkedList<FieldGetter>();
|
||||||
for (Field f : fs) {
|
for (FieldGetter fieldGetter : getters) {
|
||||||
if (f.getName().equals(fieldName)) {
|
ret.add(fieldGetter);
|
||||||
return f;
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void checkParameter(FieldGetter fieldGetter) {
|
||||||
|
if (fieldGetter == null) {
|
||||||
|
throw new IllegalArgumentException("The parameter fieldGetter can not be null");
|
||||||
|
}
|
||||||
|
for (FieldGetter fg : getters) {
|
||||||
|
if (fg.getClass() == fieldGetter.getClass()) {
|
||||||
|
throw new RuntimeException("FieldGetter already exists : " + fieldGetter.getClass().getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
}
|
||||||
|
|
||||||
|
public static synchronized void removeFieldGetter(Class<? extends FieldGetter> fieldGetterClass) {
|
||||||
|
LinkedList<FieldGetter> ret = getCurrentFieldGetters();
|
||||||
|
|
||||||
|
for (Iterator<FieldGetter> it = ret.iterator(); it.hasNext();) {
|
||||||
|
if (it.next().getClass() == fieldGetterClass) {
|
||||||
|
it.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getters = ret.toArray(new FieldGetter[ret.size()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void clearCache() {
|
||||||
|
fieldGetterCache.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置极速模式
|
||||||
|
*
|
||||||
|
* 极速模式将生成代理对象来消除 java.lang.reflect.Method.invoke(...) 调用,
|
||||||
|
* 性能提升 12.9%
|
||||||
|
*/
|
||||||
|
public static synchronized void setFastMode(boolean fastMode) {
|
||||||
|
if (fastMode) {
|
||||||
|
if ( !contains(FastFieldGetter.class) ) {
|
||||||
|
addFieldGetterToFirst(new FastFieldGetter());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (contains(FastFieldGetter.class)) {
|
||||||
|
removeFieldGetter(FastFieldGetter.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断是否包含某个 FieldGetter
|
||||||
|
*/
|
||||||
|
public static boolean contains(Class<? extends FieldGetter> fieldGetterClass) {
|
||||||
|
for (FieldGetter fg : getters) {
|
||||||
|
if (fg.getClass() == fieldGetterClass) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,3 +176,6 @@ public class FieldKit {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -23,7 +23,7 @@ import com.jfinal.template.stat.Scope;
|
|||||||
*/
|
*/
|
||||||
public class Id extends Expr {
|
public class Id extends Expr {
|
||||||
|
|
||||||
private String id;
|
private final String id;
|
||||||
|
|
||||||
public Id(String id) {
|
public Id(String id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -43,8 +43,8 @@ public class Index extends Expr {
|
|||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
public Object eval(Scope scope) {
|
public Object eval(Scope scope) {
|
||||||
Object array = expr.eval(scope);
|
Object target = expr.eval(scope);
|
||||||
if (array == null) {
|
if (target == null) {
|
||||||
if (scope.getCtrl().isNullSafe()) {
|
if (scope.getCtrl().isNullSafe()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -56,25 +56,30 @@ public class Index extends Expr {
|
|||||||
if (scope.getCtrl().isNullSafe()) {
|
if (scope.getCtrl().isNullSafe()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
throw new TemplateException("The index of list/array and the key of map can not be null", location);
|
|
||||||
|
if (target instanceof java.util.Map) {
|
||||||
|
// Map 的 key 可以是 null,不能抛异常
|
||||||
|
} else {
|
||||||
|
throw new TemplateException("The index of list and array can not be null", location);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (array instanceof List) {
|
if (target instanceof List) {
|
||||||
if (idx instanceof Integer) {
|
if (idx instanceof Integer) {
|
||||||
return ((List<?>)array).get((Integer)idx);
|
return ((List<?>)target).get((Integer)idx);
|
||||||
}
|
}
|
||||||
throw new TemplateException("The index of list can only be integer", location);
|
throw new TemplateException("The index of list must be integer", location);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (array instanceof java.util.Map) {
|
if (target instanceof java.util.Map) {
|
||||||
return ((java.util.Map)array).get(idx);
|
return ((java.util.Map)target).get(idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (array.getClass().isArray()) {
|
if (target.getClass().isArray()) {
|
||||||
if (idx instanceof Integer) {
|
if (idx instanceof Integer) {
|
||||||
return java.lang.reflect.Array.get(array, (Integer)idx);
|
return java.lang.reflect.Array.get(target, (Integer)idx);
|
||||||
}
|
}
|
||||||
throw new TemplateException("The index of array can only be integer", location);
|
throw new TemplateException("The index of array must be integer", location);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new TemplateException("Only the list array and map is supported by index access", location);
|
throw new TemplateException("Only the list array and map is supported by index access", location);
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -24,8 +24,10 @@ import com.jfinal.template.stat.Scope;
|
|||||||
* Map
|
* Map
|
||||||
*
|
*
|
||||||
* 1:定义 map 常量
|
* 1:定义 map 常量
|
||||||
* {k1:123, k2:"abc", 'k3':true, "k4":[1,2,3], k5:1+2}
|
* {k1:123, k2:"abc", 'k3':true, "k4":[1,2,3], k5:1+2, 1:12, true:"Y", null:"abc"}
|
||||||
* 如上所示,map定义的 key 可以为 String 或者 id 标识符,而右侧的 value 可以是任意的常量与表达式
|
* 如上所示,map定义的 key 可以为 id 标识符或者 String、Integer、Long、Boolean、null
|
||||||
|
* 等常量值 (详见 ExprParser.buildMapEntry(...) 方法),
|
||||||
|
* 右侧的 value 可以是任意的常量与表达式
|
||||||
*
|
*
|
||||||
* 2:取值
|
* 2:取值
|
||||||
* 先将 Map 常量赋值给某个变量: #set(map = {...})
|
* 先将 Map 常量赋值给某个变量: #set(map = {...})
|
||||||
@@ -35,6 +37,10 @@ import com.jfinal.template.stat.Scope;
|
|||||||
* map.get("k1")
|
* map.get("k1")
|
||||||
* map.k1
|
* map.k1
|
||||||
*
|
*
|
||||||
|
* 3:不通过中间变量取值
|
||||||
|
* {1:'自买', 2:'跟买'}.get(1)
|
||||||
|
* {1:'自买', 2:'跟买'}[2]
|
||||||
|
*
|
||||||
* 如上所示,当以下标方式取值时,下标参数可以是 string 与 expr,而 expr 求值以后的值必须也为 string类型
|
* 如上所示,当以下标方式取值时,下标参数可以是 string 与 expr,而 expr 求值以后的值必须也为 string类型
|
||||||
* 当用 map.k1 这类 field 字段取值形式时,则是使用 id 标识符,而不是 string 形参数
|
* 当用 map.k1 这类 field 字段取值形式时,则是使用 id 标识符,而不是 string 形参数
|
||||||
*
|
*
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -24,6 +24,17 @@ import com.jfinal.template.stat.Scope;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Method : expr '.' ID '(' exprList? ')'
|
* Method : expr '.' ID '(' exprList? ')'
|
||||||
|
*
|
||||||
|
* 每次通过 MethodKit.getMethod(...) 取 MethodInfo 而不是用属性持有其对象
|
||||||
|
* 是为了支持 target 对象的动态类型,MethodInfo 中的 Method 被调用 15 次以后
|
||||||
|
* 会被 JDK 动态生成 GeneratedAccessorXXX 字节码,性能不是问题
|
||||||
|
* 唯一的性能损耗是从 HashMap 中获取 MethodInfo 对象,可以忽略不计
|
||||||
|
*
|
||||||
|
* 如果在未来通过结合 #dynamic(boolean) 指令来优化,需要在 Ctrl 中引入一个
|
||||||
|
* boolean dynamic = false 变量,而不能在 Env、Scope 引入该变量
|
||||||
|
*
|
||||||
|
* 还需要引入一个 NullMethodInfo 以及 notNull() 方法,此优化复杂度提高不少,
|
||||||
|
* 暂时不做此优化
|
||||||
*/
|
*/
|
||||||
public class Method extends Expr {
|
public class Method extends Expr {
|
||||||
|
|
||||||
@@ -65,28 +76,24 @@ public class Method extends Expr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Object[] argValues = exprList.evalExprList(scope);
|
Object[] argValues = exprList.evalExprList(scope);
|
||||||
MethodInfo methodInfo;
|
|
||||||
try {
|
try {
|
||||||
methodInfo = MethodKit.getMethod(target.getClass(), methodName, argValues);
|
|
||||||
} catch (Exception e) {
|
MethodInfo methodInfo = MethodKit.getMethod(target.getClass(), methodName, argValues);
|
||||||
throw new TemplateException(e.getMessage(), location, e);
|
if (methodInfo.notNull()) {
|
||||||
|
return methodInfo.invoke(target, argValues);
|
||||||
}
|
}
|
||||||
if (methodInfo == null) {
|
|
||||||
if (scope.getCtrl().isNullSafe()) {
|
if (scope.getCtrl().isNullSafe()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
throw new TemplateException(buildMethodNotFoundSignature("Method not found: " + target.getClass().getName() + ".", methodName, argValues), location);
|
throw new TemplateException(buildMethodNotFoundSignature("public method not found: " + target.getClass().getName() + ".", methodName, argValues), location);
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
} catch (TemplateException | ParseException e) {
|
||||||
return methodInfo.invoke(target, argValues);
|
throw e;
|
||||||
} catch (InvocationTargetException e) {
|
} catch (InvocationTargetException e) {
|
||||||
Throwable t = e.getTargetException();
|
Throwable t = e.getTargetException();
|
||||||
if (t != null) {
|
if (t == null) {t = e;}
|
||||||
throw new TemplateException(t.getMessage(), location, t);
|
throw new TemplateException(t.getMessage(), location, t);
|
||||||
} else {
|
|
||||||
throw new TemplateException(e.getMessage(), location, e);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new TemplateException(e.getMessage(), location, e);
|
throw new TemplateException(e.getMessage(), location, e);
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -17,7 +17,6 @@
|
|||||||
package com.jfinal.template.expr.ast;
|
package com.jfinal.template.expr.ast;
|
||||||
|
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
|
|
||||||
@@ -41,7 +40,7 @@ public class MethodInfo {
|
|||||||
this.paraTypes = method.getParameterTypes();
|
this.paraTypes = method.getParameterTypes();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object invoke(Object target, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
public Object invoke(Object target, Object... args) throws ReflectiveOperationException {
|
||||||
if (isVarArgs) {
|
if (isVarArgs) {
|
||||||
return invokeVarArgsMethod(target, args);
|
return invokeVarArgsMethod(target, args);
|
||||||
} else {
|
} else {
|
||||||
@@ -49,7 +48,7 @@ public class MethodInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Object invokeVarArgsMethod(Object target, Object[] argValues) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
protected Object invokeVarArgsMethod(Object target, Object[] argValues) throws ReflectiveOperationException {
|
||||||
Object[] finalArgValues = new Object[paraTypes.length];
|
Object[] finalArgValues = new Object[paraTypes.length];
|
||||||
|
|
||||||
int fixedParaLength = paraTypes.length - 1;
|
int fixedParaLength = paraTypes.length - 1;
|
||||||
@@ -94,6 +93,29 @@ public class MethodInfo {
|
|||||||
}
|
}
|
||||||
return ret.append(")").toString();
|
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
|
||||||
|
*
|
||||||
|
* 1:MethodKit.getMethod(...) 消除 instanceof 判断
|
||||||
|
* 2:Method.exec(...) 消除 null 值判断
|
||||||
|
*/
|
||||||
|
public boolean notNull() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package com.jfinal.template.expr.ast;
|
package com.jfinal.template.expr.ast;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -37,7 +36,7 @@ public class MethodInfoExt extends MethodInfo {
|
|||||||
// this.paraTypes = newParaTypes;
|
// this.paraTypes = newParaTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object invoke(Object target, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
public Object invoke(Object target, Object... args) throws ReflectiveOperationException {
|
||||||
Object[] finalArgs = new Object[args.length + 1];
|
Object[] finalArgs = new Object[args.length + 1];
|
||||||
finalArgs[0] = target;
|
finalArgs[0] = target;
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -43,17 +43,20 @@ public abstract class MethodKeyBuilder {
|
|||||||
* 如果希望将 configEngine(Engine me) 中的 Engine 切换到 StrictMethodKeyBuilder,
|
* 如果希望将 configEngine(Engine me) 中的 Engine 切换到 StrictMethodKeyBuilder,
|
||||||
* 需要在 YourJFinalConfig extends JFinalConfig 中利用如下代码块才能生效:
|
* 需要在 YourJFinalConfig extends JFinalConfig 中利用如下代码块才能生效:
|
||||||
* static {
|
* static {
|
||||||
* MethodKeyBuilder.useStrictMethodKeyBuilder();
|
* MethodKeyBuilder.setToStrictMethodKeyBuilder();
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* 原因是在 com.jfinal.core.Config 中 new Engine() 时 useStrictMethodKeyBuilder()
|
* 原因是在 com.jfinal.core.Config 中 new Engine() 时 setToStrictMethodKeyBuilder()
|
||||||
* 方法并未生效,所以 extension method 生成 method key 时仍然使用的是 FastMethodKeyBuilder
|
* 方法并未生效,所以 extension method 生成 method key 时仍然使用的是 FastMethodKeyBuilder
|
||||||
* 以至于在运行时,使用 StrictMethodKeyBuilder 生成的 key 找不到 extension method
|
* 以至于在运行时,使用 StrictMethodKeyBuilder 生成的 key 找不到 extension method
|
||||||
*
|
*
|
||||||
|
* 后续版本考虑在调用 setToStrictMethodKeyBuilder() 以后重新初始化一下 MethodKit 中的变量
|
||||||
|
* 原先的 static 初始化方式重构出 synchronized void init() 方法来方便调用
|
||||||
|
*
|
||||||
* </pre>
|
* </pre>
|
||||||
*/
|
*/
|
||||||
public static void useStrictMethodKeyBuilder() {
|
public static void setToStrictMethodKeyBuilder() {
|
||||||
MethodKeyBuilder.instance = new StrictMethodKeyBuilder();
|
instance = new StrictMethodKeyBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -63,7 +66,7 @@ public abstract class MethodKeyBuilder {
|
|||||||
if (methodKeyBuilder == null) {
|
if (methodKeyBuilder == null) {
|
||||||
throw new IllegalArgumentException("methodKeyBuilder can not be null");
|
throw new IllegalArgumentException("methodKeyBuilder can not be null");
|
||||||
}
|
}
|
||||||
MethodKeyBuilder.instance = methodKeyBuilder;
|
instance = methodKeyBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -87,6 +90,9 @@ public abstract class MethodKeyBuilder {
|
|||||||
if (type != null) {
|
if (type != null) {
|
||||||
hash ^= type.getName().hashCode();
|
hash ^= type.getName().hashCode();
|
||||||
hash *= HashKit.FNV_PRIME_64;
|
hash *= HashKit.FNV_PRIME_64;
|
||||||
|
} else {
|
||||||
|
hash ^= "null".hashCode();
|
||||||
|
hash *= HashKit.FNV_PRIME_64;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -110,6 +116,8 @@ public abstract class MethodKeyBuilder {
|
|||||||
Class<?> type = argTypes[i];
|
Class<?> type = argTypes[i];
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
hash = fnv1a64(hash, type.getName());
|
hash = fnv1a64(hash, type.getName());
|
||||||
|
} else {
|
||||||
|
hash = fnv1a64(hash, "null");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -23,6 +23,7 @@ import java.util.HashSet;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import com.jfinal.kit.ReflectKit;
|
import com.jfinal.kit.ReflectKit;
|
||||||
|
import com.jfinal.kit.SyncWriteMap;
|
||||||
import com.jfinal.template.ext.extensionmethod.ByteExt;
|
import com.jfinal.template.ext.extensionmethod.ByteExt;
|
||||||
import com.jfinal.template.ext.extensionmethod.DoubleExt;
|
import com.jfinal.template.ext.extensionmethod.DoubleExt;
|
||||||
import com.jfinal.template.ext.extensionmethod.FloatExt;
|
import com.jfinal.template.ext.extensionmethod.FloatExt;
|
||||||
@@ -37,17 +38,20 @@ import com.jfinal.template.ext.extensionmethod.StringExt;
|
|||||||
public class MethodKit {
|
public class MethodKit {
|
||||||
|
|
||||||
private static final Class<?>[] NULL_ARG_TYPES = new Class<?>[0];
|
private static final Class<?>[] NULL_ARG_TYPES = new Class<?>[0];
|
||||||
private static final Set<String> forbiddenMethods = new HashSet<String>();
|
private static final Set<String> forbiddenMethods = new HashSet<String>(64);
|
||||||
private static final Set<Class<?>> forbiddenClasses = new HashSet<Class<?>>();
|
private static final Set<Class<?>> forbiddenClasses = new HashSet<Class<?>>(64);
|
||||||
private static final Map<Class<?>, Class<?>> primitiveMap = new HashMap<Class<?>, Class<?>>();
|
private static final Map<Class<?>, Class<?>> primitiveMap = new HashMap<Class<?>, Class<?>>(64);
|
||||||
private static final HashMap<Long, Object> methodCache = new HashMap<Long, Object>();
|
private static final SyncWriteMap<Long, MethodInfo> methodCache = new SyncWriteMap<Long, MethodInfo>(2048, 0.25F);
|
||||||
|
|
||||||
// 初始化在模板中调用 method 时所在的被禁止使用类
|
// 初始化在模板中调用 method 时所在的被禁止使用类
|
||||||
static {
|
static {
|
||||||
Class<?>[] cs = {
|
Class<?>[] cs = {
|
||||||
System.class, Runtime.class, Thread.class, Class.class, ClassLoader.class, File.class,
|
System.class, Runtime.class, Thread.class, Class.class, ClassLoader.class, File.class,
|
||||||
Compiler.class, InheritableThreadLocal.class, Package.class, Process.class,
|
Compiler.class, InheritableThreadLocal.class, Package.class, Process.class,
|
||||||
RuntimePermission.class, SecurityManager.class, ThreadGroup.class, ThreadLocal.class
|
RuntimePermission.class, SecurityManager.class, ThreadGroup.class, ThreadLocal.class,
|
||||||
|
|
||||||
|
java.lang.reflect.Method.class,
|
||||||
|
java.lang.reflect.Proxy.class
|
||||||
};
|
};
|
||||||
for (Class<?> c : cs) {
|
for (Class<?> c : cs) {
|
||||||
forbiddenClasses.add(c);
|
forbiddenClasses.add(c);
|
||||||
@@ -58,10 +62,10 @@ public class MethodKit {
|
|||||||
static {
|
static {
|
||||||
String[] ms = {
|
String[] ms = {
|
||||||
"getClass", "getDeclaringClass", "forName", "newInstance", "getClassLoader",
|
"getClass", "getDeclaringClass", "forName", "newInstance", "getClassLoader",
|
||||||
"getMethod", "getMethods", "getField", "getFields",
|
"invoke", // "getMethod", "getMethods", // "getField", "getFields",
|
||||||
"notify", "notifyAll", "wait",
|
"notify", "notifyAll", "wait",
|
||||||
"load", "exit", "loadLibrary", "halt",
|
"exit", "loadLibrary", "halt", // "load",
|
||||||
"stop", "suspend", "resume", "setDaemon", "setPriority",
|
"stop", "suspend", "resume" // "setDaemon", "setPriority"
|
||||||
};
|
};
|
||||||
for (String m : ms) {
|
for (String m : ms) {
|
||||||
forbiddenMethods.add(m);
|
forbiddenMethods.add(m);
|
||||||
@@ -93,6 +97,14 @@ public class MethodKit {
|
|||||||
return forbiddenClasses.contains(clazz);
|
return forbiddenClasses.contains(clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void addForbiddenClass(Class<?> clazz) {
|
||||||
|
forbiddenClasses.add(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void removeForbiddenClass(Class<?> clazz) {
|
||||||
|
forbiddenClasses.remove(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean isForbiddenMethod(String methodName) {
|
public static boolean isForbiddenMethod(String methodName) {
|
||||||
return forbiddenMethods.contains(methodName);
|
return forbiddenMethods.contains(methodName);
|
||||||
}
|
}
|
||||||
@@ -101,37 +113,26 @@ public class MethodKit {
|
|||||||
forbiddenMethods.add(methodName);
|
forbiddenMethods.add(methodName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void removeForbiddenMethod(String methodName) {
|
||||||
|
forbiddenMethods.remove(methodName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void clearCache() {
|
||||||
|
methodCache.clear();
|
||||||
|
}
|
||||||
|
|
||||||
public static MethodInfo getMethod(Class<?> targetClass, String methodName, Object[] argValues) {
|
public static MethodInfo getMethod(Class<?> targetClass, String methodName, Object[] argValues) {
|
||||||
Class<?>[] argTypes = getArgTypes(argValues);
|
Class<?>[] argTypes = getArgTypes(argValues);
|
||||||
Long key = getMethodKey(targetClass, methodName, argTypes);
|
Long key = getMethodKey(targetClass, methodName, argTypes);
|
||||||
Object method = methodCache.get(key);
|
MethodInfo method = methodCache.get(key);
|
||||||
if (method == null) {
|
if (method == null) {
|
||||||
|
// 已确保不会返回 null,对于不存在的 Method,只进行一次获取操作
|
||||||
|
// 提升 null safe 表达式性能,未来需要考虑内存泄漏风险
|
||||||
method = doGetMethod(key, targetClass, methodName, argTypes);
|
method = doGetMethod(key, targetClass, methodName, argTypes);
|
||||||
if (method != null) {
|
methodCache.putIfAbsent(key, method);
|
||||||
methodCache.put(key, method);
|
|
||||||
} else {
|
|
||||||
// 对于不存在的 Method,只进行一次获取操作,主要为了支持 null safe,未来需要考虑内存泄漏风险
|
|
||||||
methodCache.put(key, Boolean.FALSE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return method instanceof MethodInfo ? (MethodInfo)method : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
return method;
|
||||||
* 获取 getter 方法
|
|
||||||
* 使用与 Field 相同的 key,避免生成两次 key值
|
|
||||||
*/
|
|
||||||
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.put(key, getterMethod);
|
|
||||||
} else {
|
|
||||||
methodCache.put(key, Boolean.FALSE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return getterMethod instanceof MethodInfo ? (MethodInfo)getterMethod : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Class<?>[] getArgTypes(Object[] argValues) {
|
static Class<?>[] getArgTypes(Object[] argValues) {
|
||||||
@@ -149,7 +150,9 @@ public class MethodKit {
|
|||||||
if (forbiddenClasses.contains(targetClass)) {
|
if (forbiddenClasses.contains(targetClass)) {
|
||||||
throw new RuntimeException("Forbidden class: " + targetClass.getName());
|
throw new RuntimeException("Forbidden class: " + targetClass.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 仅开启 forbiddenClasses 检测
|
// 仅开启 forbiddenClasses 检测
|
||||||
|
// Method、SharedMethod、StaticMethod 已用 MethodKit.isForbiddenMethod(...) 检测
|
||||||
// if (forbiddenMethods.contains(methodName)) {
|
// if (forbiddenMethods.contains(methodName)) {
|
||||||
// throw new RuntimeException("Forbidden method: " + methodName);
|
// throw new RuntimeException("Forbidden method: " + methodName);
|
||||||
// }
|
// }
|
||||||
@@ -166,7 +169,8 @@ public class MethodKit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
return NullMethodInfo.me;
|
||||||
}
|
}
|
||||||
|
|
||||||
static boolean matchFixedArgTypes(Class<?>[] paraTypes, Class<?>[] argTypes) {
|
static boolean matchFixedArgTypes(Class<?>[] paraTypes, Class<?>[] argTypes) {
|
||||||
@@ -279,7 +283,7 @@ public class MethodKit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
MethodInfoExt mie = new MethodInfoExt(objectOfExtensionClass, key, extensionClass/* targetClass */, method);
|
MethodInfoExt mie = new MethodInfoExt(objectOfExtensionClass, key, extensionClass/* targetClass */, method);
|
||||||
methodCache.put(key, mie);
|
methodCache.putIfAbsent(key, mie);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -307,7 +311,7 @@ public class MethodKit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Map<Class<?>, Class<?>> primitiveToBoxedMap = new HashMap<Class<?>, Class<?>>();
|
private static final Map<Class<?>, Class<?>> primitiveToBoxedMap = new HashMap<Class<?>, Class<?>>(64);
|
||||||
|
|
||||||
// 初始化 primitive type 到 boxed type 的映射
|
// 初始化 primitive type 到 boxed type 的映射
|
||||||
static {
|
static {
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -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
|
||||||
|
*
|
||||||
|
* 1:MethodKit.getMethod(...) 消除 instanceof 判断
|
||||||
|
* 2:Method.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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -26,9 +26,14 @@ import com.jfinal.template.stat.Scope;
|
|||||||
* SharedMethod
|
* SharedMethod
|
||||||
*
|
*
|
||||||
* 用法:
|
* 用法:
|
||||||
* engine.addSharedMethod(object);
|
* engine.addSharedMethod(new StrKit());
|
||||||
* engine.addSharedStaticMethod(Xxx.class);
|
* engine.addSharedStaticMethod(MyKit.class);
|
||||||
* #(method(para))
|
*
|
||||||
|
* #if (notBlank(para))
|
||||||
|
* ....
|
||||||
|
* #end
|
||||||
|
*
|
||||||
|
* 上面代码中的 notBlank 方法来自 StrKit
|
||||||
*/
|
*/
|
||||||
public class SharedMethod extends Expr {
|
public class SharedMethod extends Expr {
|
||||||
|
|
||||||
@@ -48,14 +53,18 @@ public class SharedMethod extends Expr {
|
|||||||
|
|
||||||
public Object eval(Scope scope) {
|
public Object eval(Scope scope) {
|
||||||
Object[] argValues = exprList.evalExprList(scope);
|
Object[] argValues = exprList.evalExprList(scope);
|
||||||
SharedMethodInfo sharedMethodInfo = sharedMethodKit.getSharedMethodInfo(methodName, argValues);
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
SharedMethodInfo sharedMethodInfo = sharedMethodKit.getSharedMethodInfo(methodName, argValues);
|
||||||
|
if (sharedMethodInfo != null) {
|
||||||
|
return sharedMethodInfo.invoke(argValues);
|
||||||
|
} else {
|
||||||
// ShareMethod 相当于是固定的静态的方法,不支持 null safe,null safe 只支持具有动态特征的用法
|
// ShareMethod 相当于是固定的静态的方法,不支持 null safe,null safe 只支持具有动态特征的用法
|
||||||
if (sharedMethodInfo == null) {
|
|
||||||
throw new TemplateException(Method.buildMethodNotFoundSignature("Shared method not found: ", methodName, argValues), location);
|
throw new TemplateException(Method.buildMethodNotFoundSignature("Shared method not found: ", methodName, argValues), location);
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
return sharedMethodInfo.invoke(argValues);
|
} catch (TemplateException | ParseException e) {
|
||||||
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new TemplateException(e.getMessage(), location, e);
|
throw new TemplateException(e.getMessage(), location, e);
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -22,11 +22,11 @@ import java.util.Iterator;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import com.jfinal.kit.HashKit;
|
import com.jfinal.kit.HashKit;
|
||||||
import com.jfinal.kit.ReflectKit;
|
import com.jfinal.kit.ReflectKit;
|
||||||
|
import com.jfinal.kit.SyncWriteMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SharedMethodKit
|
* SharedMethodKit
|
||||||
@@ -44,7 +44,7 @@ public class SharedMethodKit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private final List<SharedMethodInfo> sharedMethodList = new ArrayList<SharedMethodInfo>();
|
private final List<SharedMethodInfo> sharedMethodList = new ArrayList<SharedMethodInfo>();
|
||||||
private final HashMap<Long, SharedMethodInfo> methodCache = new HashMap<Long, SharedMethodInfo>();
|
private final HashMap<Long, SharedMethodInfo> methodCache = new SyncWriteMap<Long, SharedMethodInfo>(512, 0.25F);
|
||||||
|
|
||||||
public SharedMethodInfo getSharedMethodInfo(String methodName, Object[] argValues) {
|
public SharedMethodInfo getSharedMethodInfo(String methodName, Object[] argValues) {
|
||||||
Class<?>[] argTypes = MethodKit.getArgTypes(argValues);
|
Class<?>[] argTypes = MethodKit.getArgTypes(argValues);
|
||||||
@@ -53,9 +53,9 @@ public class SharedMethodKit {
|
|||||||
if (method == null) {
|
if (method == null) {
|
||||||
method = doGetSharedMethodInfo(methodName, argTypes);
|
method = doGetSharedMethodInfo(methodName, argTypes);
|
||||||
if (method != null) {
|
if (method != null) {
|
||||||
methodCache.put(key, method);
|
methodCache.putIfAbsent(key, method);
|
||||||
}
|
}
|
||||||
// shared method 不支持 null safe,不缓存: methodCache.put(key, Boolean.FALSE)
|
// shared method 不支持 null safe,不缓存: methodCache.putIfAbsent(key, Void.class)
|
||||||
}
|
}
|
||||||
return method;
|
return method;
|
||||||
}
|
}
|
||||||
@@ -156,6 +156,9 @@ public class SharedMethodKit {
|
|||||||
if (type != null) {
|
if (type != null) {
|
||||||
hash ^= type.getName().hashCode();
|
hash ^= type.getName().hashCode();
|
||||||
hash *= HashKit.FNV_PRIME_64;
|
hash *= HashKit.FNV_PRIME_64;
|
||||||
|
} else {
|
||||||
|
hash ^= "null".hashCode();
|
||||||
|
hash *= HashKit.FNV_PRIME_64;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -170,7 +173,7 @@ public class SharedMethodKit {
|
|||||||
this.target = target;
|
this.target = target;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object invoke(Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
public Object invoke(Object... args) throws ReflectiveOperationException {
|
||||||
return super.invoke(target, args);
|
return super.invoke(target, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -50,6 +50,14 @@ public class StaticMethod extends Expr {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ParseException(e.getMessage(), location, 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.methodName = methodName;
|
||||||
this.exprList = exprList;
|
this.exprList = exprList;
|
||||||
this.location = location;
|
this.location = location;
|
||||||
@@ -57,23 +65,23 @@ public class StaticMethod extends Expr {
|
|||||||
|
|
||||||
public Object eval(Scope scope) {
|
public Object eval(Scope scope) {
|
||||||
Object[] argValues = exprList.evalExprList(scope);
|
Object[] argValues = exprList.evalExprList(scope);
|
||||||
MethodInfo methodInfo;
|
|
||||||
try {
|
|
||||||
methodInfo = MethodKit.getMethod(clazz, methodName, argValues);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new TemplateException(e.getMessage(), location, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// StaticMethod 是固定的存在,不支持 null safe,null safe 只支持具有动态特征的用法
|
try {
|
||||||
if (methodInfo == null) {
|
MethodInfo methodInfo = MethodKit.getMethod(clazz, methodName, argValues);
|
||||||
throw new TemplateException(Method.buildMethodNotFoundSignature("public static method not found: " + clazz.getName() + "::", methodName, argValues), location);
|
|
||||||
}
|
if (methodInfo.notNull()) {
|
||||||
if (!methodInfo.isStatic()) {
|
if (methodInfo.isStatic()) {
|
||||||
|
return methodInfo.invoke(null, argValues);
|
||||||
|
} else {
|
||||||
throw new TemplateException(Method.buildMethodNotFoundSignature("Not public static method: " + clazz.getName() + "::", methodName, argValues), location);
|
throw new TemplateException(Method.buildMethodNotFoundSignature("Not public static method: " + clazz.getName() + "::", methodName, argValues), location);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// StaticMethod 是固定的存在,不支持 null safe,null safe 只支持具有动态特征的用法
|
||||||
|
throw new TemplateException(Method.buildMethodNotFoundSignature("public static method not found: " + clazz.getName() + "::", methodName, argValues), location);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
} catch (TemplateException | ParseException e) {
|
||||||
return methodInfo.invoke(null, argValues);
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new TemplateException(e.getMessage(), location, e);
|
throw new TemplateException(e.getMessage(), location, e);
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -83,6 +83,51 @@ public class Unary extends Expr {
|
|||||||
throw new TemplateException("Unsupported operator: " + op.value(), location);
|
throw new TemplateException("Unsupported operator: " + op.value(), location);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果可能的话,将 Unary 表达式转化成 Const 表达式,类似于 ExprParser.buildMapEntry() 需要这种转化来简化实现
|
||||||
|
* 除了可简化程序外,还起到一定的性能优化作用
|
||||||
|
*
|
||||||
|
* Number : +123 -456 +3.14 -0.12
|
||||||
|
* Boolean : !true !false
|
||||||
|
*
|
||||||
|
* 特别注意:
|
||||||
|
* Boolean 的支持并不需要,!true、!false 已在 ExprParser 中被 Logic 表达式接管,在此仅为逻辑上的完备性而添加
|
||||||
|
*/
|
||||||
|
public Expr toConstIfPossible() {
|
||||||
|
if (expr instanceof Const && (op == Sym.SUB || op == Sym.ADD || op == Sym.NOT)) {
|
||||||
|
} else {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Expr ret = this;
|
||||||
|
Const c = (Const)expr;
|
||||||
|
if (op == Sym.SUB) {
|
||||||
|
if (c.isInt()) {
|
||||||
|
ret = new Const(Sym.INT, -c.getInt());
|
||||||
|
} else if (c.isLong()) {
|
||||||
|
ret = new Const(Sym.LONG, -c.getLong());
|
||||||
|
} else if (c.isFloat()) {
|
||||||
|
ret = new Const(Sym.FLOAT, -c.getFloat());
|
||||||
|
} else if (c.isDouble()) {
|
||||||
|
ret = new Const(Sym.DOUBLE, -c.getDouble());
|
||||||
|
}
|
||||||
|
} else if (op == Sym.ADD) {
|
||||||
|
if (c.isNumber()) {
|
||||||
|
ret = c;
|
||||||
|
}
|
||||||
|
} else if (op == Sym.NOT) {
|
||||||
|
if (c.isBoolean()) {
|
||||||
|
ret = c.isTrue() ? Const.FALSE : Const.TRUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return op.toString() + expr.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -0,0 +1,109 @@
|
|||||||
|
/**
|
||||||
|
* 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.ext.directive;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import com.jfinal.template.Directive;
|
||||||
|
import com.jfinal.template.Env;
|
||||||
|
import com.jfinal.template.TemplateException;
|
||||||
|
import com.jfinal.template.expr.ast.Const;
|
||||||
|
import com.jfinal.template.expr.ast.Expr;
|
||||||
|
import com.jfinal.template.expr.ast.ExprList;
|
||||||
|
import com.jfinal.template.io.Writer;
|
||||||
|
import com.jfinal.template.stat.ParseException;
|
||||||
|
import com.jfinal.template.stat.Scope;
|
||||||
|
import com.jfinal.template.stat.ast.Define;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CallDirective 动态调用模板函数
|
||||||
|
*
|
||||||
|
* 模板函数的名称与参数都可以动态指定,提升模板函数调用的灵活性
|
||||||
|
*
|
||||||
|
* 例如:
|
||||||
|
* #call(funcName, p1, p2, ..., pn)
|
||||||
|
* 其中 funcName,为函数名,p1、p2、pn 为被调用函数所使用的参数
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* 如果希望模板函数不存在时忽略其调用,添加常量值 true 在第一个参数位置即可
|
||||||
|
* 例如:
|
||||||
|
* #call(true, funcName, p1, p2, ..., pn)
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* TODO 后续优化看一下 ast.Call.java
|
||||||
|
*/
|
||||||
|
public class CallDirective extends Directive {
|
||||||
|
|
||||||
|
protected Expr funcNameExpr;
|
||||||
|
protected ExprList paraExpr;
|
||||||
|
|
||||||
|
protected boolean nullSafe = false; // 是否支持函数名不存在时跳过
|
||||||
|
|
||||||
|
public void setExprList(ExprList exprList) {
|
||||||
|
int len = exprList.length();
|
||||||
|
if (len == 0) {
|
||||||
|
throw new ParseException("模板函数名不能缺失", location);
|
||||||
|
}
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
Expr expr = exprList.getExpr(index);
|
||||||
|
if (expr instanceof Const && ((Const)expr).isBoolean()) {
|
||||||
|
if (len == 1) {
|
||||||
|
throw new ParseException("模板函数名不能缺失", location);
|
||||||
|
}
|
||||||
|
|
||||||
|
nullSafe = ((Const)expr).getBoolean();
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
funcNameExpr = exprList.getExpr(index++);
|
||||||
|
|
||||||
|
ArrayList<Expr> list = new ArrayList<Expr>();
|
||||||
|
for (int i=index; i<len; i++) {
|
||||||
|
list.add(exprList.getExpr(i));
|
||||||
|
}
|
||||||
|
paraExpr = new ExprList(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void exec(Env env, Scope scope, Writer writer) {
|
||||||
|
Object funcNameValue = funcNameExpr.eval(scope);
|
||||||
|
if (funcNameValue == null) {
|
||||||
|
if (nullSafe) {
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
throw new TemplateException("模板函数名为 null", location);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(funcNameValue instanceof String)) {
|
||||||
|
throw new TemplateException("模板函数名必须是字符串", location);
|
||||||
|
}
|
||||||
|
|
||||||
|
Define func = env.getFunction(funcNameValue.toString());
|
||||||
|
|
||||||
|
if (func == null) {
|
||||||
|
if (nullSafe) {
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
throw new TemplateException("模板函数未找到 : " + funcNameValue, location);
|
||||||
|
}
|
||||||
|
|
||||||
|
func.call(env, scope, paraExpr, writer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -79,23 +79,25 @@ public class DateDirective extends Directive {
|
|||||||
|
|
||||||
private void outputWithoutDatePattern(Env env, Scope scope, Writer writer) {
|
private void outputWithoutDatePattern(Env env, Scope scope, Writer writer) {
|
||||||
Object value = valueExpr.eval(scope);
|
Object value = valueExpr.eval(scope);
|
||||||
if (value != null) {
|
if (value instanceof Date) {
|
||||||
write(writer, (Date)value, env.getEngineConfig().getDatePattern());
|
write(writer, (Date)value, env.getEngineConfig().getDatePattern());
|
||||||
|
} else if (value != null) {
|
||||||
|
throw new TemplateException("The first parameter date of #date directive must be Date type", location);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void outputWithDatePattern(Env env, Scope scope, Writer writer) {
|
private void outputWithDatePattern(Env env, Scope scope, Writer writer) {
|
||||||
Object value = valueExpr.eval(scope);
|
Object value = valueExpr.eval(scope);
|
||||||
if (value == null) {
|
if (value instanceof Date) {
|
||||||
return ;
|
|
||||||
}
|
|
||||||
|
|
||||||
Object datePattern = this.datePatternExpr.eval(scope);
|
Object datePattern = this.datePatternExpr.eval(scope);
|
||||||
if ( !(datePattern instanceof String) ) {
|
if (datePattern instanceof String) {
|
||||||
|
write(writer, (Date)value, (String)datePattern);
|
||||||
|
} else {
|
||||||
throw new TemplateException("The sencond parameter datePattern of #date directive must be String", location);
|
throw new TemplateException("The sencond parameter datePattern of #date directive must be String", location);
|
||||||
}
|
}
|
||||||
|
} else if (value != null) {
|
||||||
write(writer, (Date)value, (String)datePattern);
|
throw new TemplateException("The first parameter date of #date directive must be Date type", location);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void write(Writer writer, Date date, String datePattern) {
|
private void write(Writer writer, Date date, String datePattern) {
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -16,9 +16,12 @@
|
|||||||
|
|
||||||
package com.jfinal.template.ext.directive;
|
package com.jfinal.template.ext.directive;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import com.jfinal.template.Directive;
|
import com.jfinal.template.Directive;
|
||||||
import com.jfinal.template.Env;
|
import com.jfinal.template.Env;
|
||||||
|
import com.jfinal.template.TemplateException;
|
||||||
import com.jfinal.template.io.Writer;
|
import com.jfinal.template.io.Writer;
|
||||||
|
import com.jfinal.template.stat.ParseException;
|
||||||
import com.jfinal.template.stat.Scope;
|
import com.jfinal.template.stat.Scope;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -29,44 +32,62 @@ import com.jfinal.template.stat.Scope;
|
|||||||
public class EscapeDirective extends Directive {
|
public class EscapeDirective extends Directive {
|
||||||
|
|
||||||
public void exec(Env env, Scope scope, Writer writer) {
|
public void exec(Env env, Scope scope, Writer writer) {
|
||||||
|
try {
|
||||||
Object value = exprList.eval(scope);
|
Object value = exprList.eval(scope);
|
||||||
if (value != null) {
|
|
||||||
write(writer, escape(value.toString()));
|
if (value instanceof String) {
|
||||||
|
escape((String)value, writer);
|
||||||
|
} else if (value instanceof Number) {
|
||||||
|
Class<?> c = value.getClass();
|
||||||
|
if (c == Integer.class) {
|
||||||
|
writer.write((Integer)value);
|
||||||
|
} else if (c == Long.class) {
|
||||||
|
writer.write((Long)value);
|
||||||
|
} else if (c == Double.class) {
|
||||||
|
writer.write((Double)value);
|
||||||
|
} else if (c == Float.class) {
|
||||||
|
writer.write((Float)value);
|
||||||
|
} else if (c == Short.class) {
|
||||||
|
writer.write((Short)value);
|
||||||
|
} else {
|
||||||
|
writer.write(value.toString());
|
||||||
|
}
|
||||||
|
} else if (value != null) {
|
||||||
|
escape(value.toString(), writer);
|
||||||
|
}
|
||||||
|
} catch(TemplateException | ParseException e) {
|
||||||
|
throw e;
|
||||||
|
} catch(Exception e) {
|
||||||
|
throw new TemplateException(e.getMessage(), location, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO 挪到 StrKit 中
|
private void escape(String str, Writer w) throws IOException {
|
||||||
private String escape(String str) {
|
for (int i = 0, len = str.length(); i < len; i++) {
|
||||||
if (str == null || str.length() == 0) {
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
int len = str.length();
|
|
||||||
StringBuilder ret = new StringBuilder(len * 2);
|
|
||||||
for (int i = 0; i < len; i++) {
|
|
||||||
char cur = str.charAt(i);
|
char cur = str.charAt(i);
|
||||||
switch (cur) {
|
switch (cur) {
|
||||||
case '<':
|
case '<':
|
||||||
ret.append("<");
|
w.write("<");
|
||||||
break;
|
break;
|
||||||
case '>':
|
case '>':
|
||||||
ret.append(">");
|
w.write(">");
|
||||||
break;
|
break;
|
||||||
case '\"':
|
case '"':
|
||||||
ret.append(""");
|
w.write(""");
|
||||||
break;
|
break;
|
||||||
case '\'':
|
case '\'':
|
||||||
ret.append("'"); // IE 不支持 ' 考虑 '
|
// w.write("'"); // IE 不支持 ' 考虑 '
|
||||||
|
w.write("'");
|
||||||
break;
|
break;
|
||||||
case '&':
|
case '&':
|
||||||
ret.append("&");
|
w.write("&");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
ret.append(cur);
|
w.write(str, i, 1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return ret.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -32,7 +32,7 @@ import com.jfinal.template.stat.Scope;
|
|||||||
*/
|
*/
|
||||||
public class NowDirective extends Directive {
|
public class NowDirective extends Directive {
|
||||||
|
|
||||||
public void setExrpList(ExprList exprList) {
|
public void setExprList(ExprList exprList) {
|
||||||
if (exprList.length() > 1) {
|
if (exprList.length() > 1) {
|
||||||
throw new ParseException("#now directive support one parameter only", location);
|
throw new ParseException("#now directive support one parameter only", location);
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -53,10 +53,9 @@ public class NumberDirective extends Directive {
|
|||||||
|
|
||||||
private Expr valueExpr;
|
private Expr valueExpr;
|
||||||
private Expr patternExpr;
|
private Expr patternExpr;
|
||||||
private int paraNum;
|
|
||||||
|
|
||||||
public void setExprList(ExprList exprList) {
|
public void setExprList(ExprList exprList) {
|
||||||
this.paraNum = exprList.length();
|
int paraNum = exprList.length();
|
||||||
if (paraNum == 0) {
|
if (paraNum == 0) {
|
||||||
throw new ParseException("The parameter of #number directive can not be blank", location);
|
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);
|
throw new ParseException("Wrong number parameter of #number directive, two parameters allowed at most", location);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (paraNum == 1) {
|
valueExpr = exprList.getExpr(0);
|
||||||
this.valueExpr = exprList.getExpr(0);
|
patternExpr = (paraNum == 1 ? null : exprList.getExpr(1));
|
||||||
this.patternExpr = null;
|
|
||||||
} else if (paraNum == 2) {
|
|
||||||
this.valueExpr = exprList.getExpr(0);
|
|
||||||
this.patternExpr = exprList.getExpr(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void exec(Env env, Scope scope, Writer writer) {
|
public void exec(Env env, Scope scope, Writer writer) {
|
||||||
@@ -79,9 +73,9 @@ public class NumberDirective extends Directive {
|
|||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (paraNum == 1) {
|
if (patternExpr == null) {
|
||||||
outputWithoutPattern(writer, value);
|
outputWithoutPattern(writer, value);
|
||||||
} else if (paraNum == 2) {
|
} else {
|
||||||
outputWithPattern(scope, writer, value);
|
outputWithPattern(scope, writer, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -16,22 +16,29 @@
|
|||||||
|
|
||||||
package com.jfinal.template.ext.directive;
|
package com.jfinal.template.ext.directive;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import com.jfinal.template.Directive;
|
import com.jfinal.template.Directive;
|
||||||
import com.jfinal.template.Env;
|
import com.jfinal.template.Env;
|
||||||
|
import com.jfinal.template.TemplateException;
|
||||||
import com.jfinal.template.io.Writer;
|
import com.jfinal.template.io.Writer;
|
||||||
import com.jfinal.template.stat.Scope;
|
import com.jfinal.template.stat.Scope;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 输出随机数
|
* 输出 int 型随机数
|
||||||
*/
|
*/
|
||||||
public class RandomDirective extends Directive {
|
public class RandomDirective extends Directive {
|
||||||
|
|
||||||
private java.util.Random random = new java.util.Random();
|
private java.util.Random random = new java.util.Random();
|
||||||
|
|
||||||
public void exec(Env env, Scope scope, Writer writer) {
|
public void exec(Env env, Scope scope, Writer writer) {
|
||||||
write(writer, String.valueOf(random.nextInt()));
|
try {
|
||||||
|
writer.write(random.nextInt());
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new TemplateException(e.getMessage(), location, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -16,8 +16,8 @@
|
|||||||
|
|
||||||
package com.jfinal.template.ext.directive;
|
package com.jfinal.template.ext.directive;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import com.jfinal.kit.SyncWriteMap;
|
||||||
import com.jfinal.template.Directive;
|
import com.jfinal.template.Directive;
|
||||||
import com.jfinal.template.EngineConfig;
|
import com.jfinal.template.EngineConfig;
|
||||||
import com.jfinal.template.Env;
|
import com.jfinal.template.Env;
|
||||||
@@ -60,7 +60,7 @@ import com.jfinal.template.stat.ast.StatList;
|
|||||||
public class RenderDirective extends Directive {
|
public class RenderDirective extends Directive {
|
||||||
|
|
||||||
private String parentFileName;
|
private String parentFileName;
|
||||||
private Map<String, StatInfo> statInfoCache = new HashMap<String,StatInfo>();
|
private Map<String, SubStat> subStatCache = new SyncWriteMap<String, SubStat>(16, 0.5F);
|
||||||
|
|
||||||
public void setExprList(ExprList exprList) {
|
public void setExprList(ExprList exprList) {
|
||||||
int len = exprList.length();
|
int len = exprList.length();
|
||||||
@@ -70,7 +70,7 @@ public class RenderDirective extends Directive {
|
|||||||
if (len > 1) {
|
if (len > 1) {
|
||||||
for (int i = 1; i < len; i++) {
|
for (int i = 1; i < len; i++) {
|
||||||
if (!(exprList.getExpr(i) instanceof Assign)) {
|
if (!(exprList.getExpr(i) instanceof Assign)) {
|
||||||
throw new ParseException("The " + i + "th parameter of #render directive must be an assignment expression", location);
|
throw new ParseException("The " + (i + 1) + "th parameter of #render directive must be an assignment expression", location);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,62 +108,68 @@ public class RenderDirective extends Directive {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String subFileName = Include.getSubFileName((String)value, parentFileName);
|
String subFileName = Include.getSubFileName((String)value, parentFileName);
|
||||||
StatInfo statInfo = statInfoCache.get(subFileName);
|
SubStat subStat = subStatCache.get(subFileName);
|
||||||
if (statInfo == null) {
|
if (subStat == null) {
|
||||||
statInfo = parseStatInfo(env, subFileName);
|
subStat = parseSubStat(env, subFileName);
|
||||||
statInfoCache.put(subFileName, statInfo);
|
subStatCache.put(subFileName, subStat);
|
||||||
} else if (env.isDevMode()) {
|
} else if (env.isDevMode()) {
|
||||||
// statInfo.env.isSourceListModified() 逻辑可以支持 #render 子模板中的 #include 过来的子模板在 devMode 下在修改后可被重加载
|
// subStat.env.isSourceListModified() 逻辑可以支持 #render 子模板中的 #include 过来的子模板在 devMode 下在修改后可被重加载
|
||||||
if (statInfo.source.isModified() || statInfo.env.isSourceListModified()) {
|
if (subStat.source.isModified() || subStat.env.isSourceListModified()) {
|
||||||
statInfo = parseStatInfo(env, subFileName);
|
subStat = parseSubStat(env, subFileName);
|
||||||
statInfoCache.put(subFileName, statInfo);
|
subStatCache.put(subFileName, subStat);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
statInfo.stat.exec(statInfo.env, scope, writer);
|
subStat.exec(null, scope, writer); // subStat.stat.exec(subStat.env, scope, writer);
|
||||||
|
|
||||||
scope.getCtrl().setJumpNone();
|
scope.getCtrl().setJumpNone();
|
||||||
}
|
}
|
||||||
|
|
||||||
private StatInfo parseStatInfo(Env env, String subFileName) {
|
private SubStat parseSubStat(Env env, String subFileName) {
|
||||||
EngineConfig config = env.getEngineConfig();
|
EngineConfig config = env.getEngineConfig();
|
||||||
// FileSource fileSource = new FileSource(config.getBaseTemplatePath(), subFileName, config.getEncoding());
|
// FileSource subFileSource = new FileSource(config.getBaseTemplatePath(), subFileName, config.getEncoding());
|
||||||
ISource fileSource = config.getSourceFactory().getSource(config.getBaseTemplatePath(), subFileName, config.getEncoding());
|
ISource subFileSource = config.getSourceFactory().getSource(config.getBaseTemplatePath(), subFileName, config.getEncoding());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
EnvSub envSub = new EnvSub(env);
|
SubEnv subEnv = new SubEnv(env);
|
||||||
StatList statList = new Parser(envSub, fileSource.getContent(), subFileName).parse();
|
StatList subStatList = new Parser(subEnv, subFileSource.getContent(), subFileName).parse();
|
||||||
return new StatInfo(envSub, statList.getActualStat(), fileSource);
|
return new SubStat(subEnv, subStatList.getActualStat(), subFileSource);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ParseException(e.getMessage(), location, e);
|
throw new ParseException(e.getMessage(), location, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class StatInfo {
|
public static class SubStat extends Stat {
|
||||||
EnvSub env;
|
public SubEnv env;
|
||||||
Stat stat;
|
public Stat stat;
|
||||||
ISource source;
|
public ISource source;
|
||||||
|
|
||||||
StatInfo(EnvSub env, Stat stat, ISource source) {
|
public SubStat(SubEnv env, Stat stat, ISource source) {
|
||||||
this.env = env;
|
this.env = env;
|
||||||
this.stat = stat;
|
this.stat = stat;
|
||||||
this.source = source;
|
this.source = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void exec(Env env, Scope scope, Writer writer) {
|
||||||
|
stat.exec(this.env, scope, writer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* EnvSub 用于将子模板与父模板中的模板函数隔离开来,
|
* SubEnv 用于将子模板与父模板中的模板函数隔离开来,
|
||||||
* 否则在子模板被修改并被重新解析时会再次添加子模板中的
|
* 否则在子模板被修改并被重新解析时会再次添加子模板中的
|
||||||
* 模板函数,从而抛出异常
|
* 模板函数,从而抛出异常
|
||||||
*
|
*
|
||||||
* EnvSub 也可以使子模板中定义的模板函数不与上层产生冲突,
|
* SubEnv 也可以使子模板中定义的模板函数不与上层产生冲突,
|
||||||
* 有利于动态型模板渲染的模块化
|
* 有利于动态型模板渲染的模块化
|
||||||
*
|
*
|
||||||
* 注意: #render 子模板中定义的模板函数无法被上层调用
|
* 注意: #render 子模板中定义的模板函数无法在父模板中调用
|
||||||
*/
|
*/
|
||||||
private static class EnvSub extends Env {
|
public static class SubEnv extends Env {
|
||||||
Env parentEnv;
|
public Env parentEnv;
|
||||||
|
|
||||||
public EnvSub(Env parentEnv) {
|
public SubEnv(Env parentEnv) {
|
||||||
super(parentEnv.getEngineConfig());
|
super(parentEnv.getEngineConfig());
|
||||||
this.parentEnv = parentEnv;
|
this.parentEnv = parentEnv;
|
||||||
}
|
}
|
||||||
@@ -171,6 +177,7 @@ public class RenderDirective extends Directive {
|
|||||||
/**
|
/**
|
||||||
* 接管父类 getFunction(),先从子模板中找模板函数,找不到再去父模板中找
|
* 接管父类 getFunction(),先从子模板中找模板函数,找不到再去父模板中找
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public Define getFunction(String functionName) {
|
public Define getFunction(String functionName) {
|
||||||
Define func = functionMap.get(functionName);
|
Define func = functionMap.get(functionName);
|
||||||
return func != null ? func : parentEnv.getFunction(functionName);
|
return func != null ? func : parentEnv.getFunction(functionName);
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -43,6 +43,14 @@ public class ByteExt {
|
|||||||
public Double toDouble(Byte self) {
|
public Double toDouble(Byte self) {
|
||||||
return self.doubleValue();
|
return self.doubleValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Short toShort(Byte self) {
|
||||||
|
return self.shortValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Byte toByte(Byte self) {
|
||||||
|
return self;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -43,6 +43,14 @@ public class DoubleExt {
|
|||||||
public Double toDouble(Double self) {
|
public Double toDouble(Double self) {
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Short toShort(Double self) {
|
||||||
|
return self.shortValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Byte toByte(Double self) {
|
||||||
|
return self.byteValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -43,6 +43,14 @@ public class FloatExt {
|
|||||||
public Double toDouble(Float self) {
|
public Double toDouble(Float self) {
|
||||||
return self.doubleValue();
|
return self.doubleValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Short toShort(Float self) {
|
||||||
|
return self.shortValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Byte toByte(Float self) {
|
||||||
|
return self.byteValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -63,6 +63,14 @@ public class IntegerExt {
|
|||||||
public Double toDouble(Integer self) {
|
public Double toDouble(Integer self) {
|
||||||
return self.doubleValue();
|
return self.doubleValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Short toShort(Integer self) {
|
||||||
|
return self.shortValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Byte toByte(Integer self) {
|
||||||
|
return self.byteValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -43,6 +43,14 @@ public class LongExt {
|
|||||||
public Double toDouble(Long self) {
|
public Double toDouble(Long self) {
|
||||||
return self.doubleValue();
|
return self.doubleValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Short toShort(Long self) {
|
||||||
|
return self.shortValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Byte toByte(Long self) {
|
||||||
|
return self.byteValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -43,6 +43,14 @@ public class ShortExt {
|
|||||||
public Double toDouble(Short self) {
|
public Double toDouble(Short self) {
|
||||||
return self.doubleValue();
|
return self.doubleValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Short toShort(Short self) {
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Byte toByte(Short self) {
|
||||||
|
return self.byteValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -79,6 +79,14 @@ public class StringExt {
|
|||||||
public Double toDouble(String self) {
|
public Double toDouble(String self) {
|
||||||
return StrKit.isBlank(self) ? null : Double.parseDouble(self);
|
return StrKit.isBlank(self) ? null : Double.parseDouble(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Short toShort(String self) {
|
||||||
|
return StrKit.isBlank(self) ? null : Short.parseShort(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Byte toByte(String self) {
|
||||||
|
return StrKit.isBlank(self) ? null : Byte.parseByte(self);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package com.jfinal.template.ext.spring;
|
package com.jfinal.template.ext.spring;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -56,13 +57,24 @@ public class JFinalView extends AbstractTemplateView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
OutputStream os = response.getOutputStream();
|
OutputStream os = response.getOutputStream();
|
||||||
JFinalViewResolver.engine.getTemplate(getUrl()).render(model, os);
|
JFinalViewResolver.engine.getTemplate(getUrl()).render(model, os);
|
||||||
|
} catch (Exception e) { // 捕获 ByteWriter.close() 抛出的 RuntimeException
|
||||||
|
Throwable cause = e.getCause();
|
||||||
|
if (cause instanceof IOException) { // ClientAbortException、EofException 直接或间接继承自 IOException
|
||||||
|
String name = cause.getClass().getSimpleName();
|
||||||
|
if ("ClientAbortException".equals(name) || "EofException".equals(name)) {
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
|
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
|
||||||
class InnerSession extends HashMap<Object, Object> implements HttpSession {
|
public static class InnerSession extends HashMap<Object, Object> implements HttpSession {
|
||||||
|
|
||||||
private static final long serialVersionUID = -8679493647540628009L;
|
private static final long serialVersionUID = -8679493647540628009L;
|
||||||
private HttpSession session;
|
private HttpSession session;
|
||||||
@@ -155,6 +167,11 @@ class InnerSession extends HashMap<Object, Object> implements HttpSession {
|
|||||||
public void setMaxInactiveInterval(int maxInactiveInterval) {
|
public void setMaxInactiveInterval(int maxInactiveInterval) {
|
||||||
session.setMaxInactiveInterval(maxInactiveInterval);
|
session.setMaxInactiveInterval(maxInactiveInterval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return session != null ? session.toString() : "null";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -162,4 +179,3 @@ class InnerSession extends HashMap<Object, Object> implements HttpSession {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -194,6 +194,14 @@ public class JFinalViewResolver extends AbstractTemplateViewResolver {
|
|||||||
engine.setSourceFactory(sourceFactory);
|
engine.setSourceFactory(sourceFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置为 ClassPathSourceFactory 的快捷方法
|
||||||
|
* ClassPathSourceFactory 将从 CLASSPATH 与 jar 包中读取模板
|
||||||
|
*/
|
||||||
|
public void setToClassPathSourceFactory() {
|
||||||
|
engine.setToClassPathSourceFactory();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置模板基础路径
|
* 设置模板基础路径
|
||||||
*/
|
*/
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -46,27 +46,21 @@ public class ByteWriter extends Writer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
try {
|
|
||||||
if (out != null) {
|
|
||||||
out.flush();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
out = null;
|
out = null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void write(String str, int offset, int len) throws IOException {
|
public void write(String str, int offset, int len) throws IOException {
|
||||||
while (len > chars.length) {
|
int size, byteLen;
|
||||||
write(str, offset, chars.length);
|
while (len > 0) {
|
||||||
offset += chars.length;
|
size = (len > chars.length ? chars.length : len);
|
||||||
len -= chars.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
str.getChars(offset, offset + len, chars, 0);
|
str.getChars(offset, offset + size, chars, 0);
|
||||||
int byteLen = encoder.encode(chars, 0, len, bytes);
|
byteLen = encoder.encode(chars, 0, size, bytes);
|
||||||
out.write(bytes, 0, byteLen);
|
out.write(bytes, 0, byteLen);
|
||||||
|
|
||||||
|
offset += size;
|
||||||
|
len -= size;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(String str) throws IOException {
|
public void write(String str) throws IOException {
|
||||||
@@ -74,15 +68,17 @@ public class ByteWriter extends Writer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void write(StringBuilder stringBuilder, int offset, int len) throws IOException {
|
public void write(StringBuilder stringBuilder, int offset, int len) throws IOException {
|
||||||
while (len > chars.length) {
|
int size, byteLen;
|
||||||
write(stringBuilder, offset, chars.length);
|
while (len > 0) {
|
||||||
offset += chars.length;
|
size = (len > chars.length ? chars.length : len);
|
||||||
len -= chars.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
stringBuilder.getChars(offset, offset + len, chars, 0);
|
stringBuilder.getChars(offset, offset + size, chars, 0);
|
||||||
int byteLen = encoder.encode(chars, 0, len, bytes);
|
byteLen = encoder.encode(chars, 0, size, bytes);
|
||||||
out.write(bytes, 0, byteLen);
|
out.write(bytes, 0, byteLen);
|
||||||
|
|
||||||
|
offset += size;
|
||||||
|
len -= size;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(StringBuilder stringBuilder) throws IOException {
|
public void write(StringBuilder stringBuilder) throws IOException {
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -40,26 +40,20 @@ public class CharWriter extends Writer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
try {
|
|
||||||
if (out != null) {
|
|
||||||
out.flush();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
out = null;
|
out = null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void write(String str, int offset, int len) throws IOException {
|
public void write(String str, int offset, int len) throws IOException {
|
||||||
while (len > chars.length) {
|
int size;
|
||||||
write(str, offset, chars.length);
|
while (len > 0) {
|
||||||
offset += chars.length;
|
size = (len > chars.length ? chars.length : len);
|
||||||
len -= chars.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
str.getChars(offset, offset + len, chars, 0);
|
str.getChars(offset, offset + size, chars, 0);
|
||||||
out.write(chars, 0, len);
|
out.write(chars, 0, size);
|
||||||
|
|
||||||
|
offset += size;
|
||||||
|
len -= size;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(String str) throws IOException {
|
public void write(String str) throws IOException {
|
||||||
@@ -67,14 +61,16 @@ public class CharWriter extends Writer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void write(StringBuilder stringBuilder, int offset, int len) throws IOException {
|
public void write(StringBuilder stringBuilder, int offset, int len) throws IOException {
|
||||||
while (len > chars.length) {
|
int size;
|
||||||
write(stringBuilder, offset, chars.length);
|
while (len > 0) {
|
||||||
offset += chars.length;
|
size = (len > chars.length ? chars.length : len);
|
||||||
len -= chars.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
stringBuilder.getChars(offset, offset + len, chars, 0);
|
stringBuilder.getChars(offset, offset + size, chars, 0);
|
||||||
out.write(chars, 0, len);
|
out.write(chars, 0, size);
|
||||||
|
|
||||||
|
offset += size;
|
||||||
|
len -= size;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(StringBuilder stringBuilder) throws IOException {
|
public void write(StringBuilder stringBuilder) throws IOException {
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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 java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public class DateFormats {
|
public class DateFormats {
|
||||||
|
|
||||||
private Map<String, SimpleDateFormat> map = new HashMap<String, SimpleDateFormat>();
|
private Map<String, SimpleDateFormat> map = new HashMap<String, SimpleDateFormat>(16, 0.25F);
|
||||||
|
|
||||||
public SimpleDateFormat getDateFormat(String datePattern) {
|
public SimpleDateFormat getDateFormat(String datePattern) {
|
||||||
SimpleDateFormat ret = map.get(datePattern);
|
SimpleDateFormat ret = map.get(datePattern);
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -16,101 +16,152 @@
|
|||||||
|
|
||||||
package com.jfinal.template.io;
|
package com.jfinal.template.io;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FastStringWriter
|
* FastStringWriter
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* 由 JDK 中 StringWriter 改造而来,在其基础之上做了如下改变:
|
* 由 JDK 中 Writer 改造而来,在其基础之上做了如下改变:
|
||||||
* 1:StringBuffer 属性改为 StringBuilder,避免了前者的 synchronized 操作
|
* 1:添加 char[] value 直接保存 char 值
|
||||||
* 2:添加了 MAX_SIZE 属性
|
* 2:添加 int len 记录数据长度
|
||||||
* 3:去掉了 close() 方法声明中的 throws IOException,并添加了代码,原先该方法中无任何代码
|
* 3:去掉 synchronized 操作
|
||||||
|
* 4:添加 MAX_BUFFER_SIZE,限定 value 被重用的最大长度
|
||||||
|
* 5:去掉了 close() 方法声明中的 throws IOException,并添加缓存回收逻辑
|
||||||
* </pre>
|
* </pre>
|
||||||
*/
|
*/
|
||||||
public class FastStringWriter extends Writer {
|
public class FastStringWriter extends Writer {
|
||||||
|
|
||||||
private StringBuilder buf;
|
private char[] value;
|
||||||
|
private int len;
|
||||||
|
|
||||||
|
private static int MAX_BUFFER_SIZE = 1024 * 256; // 1024 * 64;
|
||||||
|
|
||||||
|
public static void setMaxBufferSize(int maxBufferSize) {
|
||||||
|
int min = 256;
|
||||||
|
if (maxBufferSize < min) {
|
||||||
|
throw new IllegalArgumentException("maxBufferSize must more than " + min);
|
||||||
|
}
|
||||||
|
MAX_BUFFER_SIZE = maxBufferSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() /* throws IOException */ {
|
||||||
|
len = 0;
|
||||||
|
|
||||||
|
// 释放空间占用过大的缓存
|
||||||
|
if (value.length > MAX_BUFFER_SIZE) {
|
||||||
|
value = new char[Math.max(256, MAX_BUFFER_SIZE / 2)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return new String(value, 0, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
public StringBuilder toStringBuilder() {
|
||||||
|
return new StringBuilder(len + 64).append(value, 0, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FastStringWriter(int capacity) {
|
||||||
|
value = new char[capacity];
|
||||||
|
}
|
||||||
|
|
||||||
public FastStringWriter() {
|
public FastStringWriter() {
|
||||||
buf = new StringBuilder();
|
this(128);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FastStringWriter(int initialSize) {
|
/**
|
||||||
if (initialSize < 0) {
|
* 扩容
|
||||||
throw new IllegalArgumentException("Negative buffer size");
|
*/
|
||||||
|
protected void expandCapacity(int newLen) {
|
||||||
|
int newCapacity = Math.max(newLen, value.length * 2);
|
||||||
|
char[] newValue = new char[newCapacity];
|
||||||
|
|
||||||
|
// 复制 value 中的值到 newValue
|
||||||
|
if (len > 0) {
|
||||||
|
System.arraycopy(value, 0, newValue, 0, len);
|
||||||
}
|
}
|
||||||
buf = new StringBuilder(initialSize);
|
value = newValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(int c) {
|
@Override
|
||||||
buf.append((char) c);
|
public void write(char buffer[], int offset, int len) throws IOException {
|
||||||
|
int newLen = this.len + len;
|
||||||
|
if (newLen > value.length) {
|
||||||
|
expandCapacity(newLen);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(char cbuf[], int off, int len) {
|
System.arraycopy(buffer, offset, value, this.len, len);
|
||||||
if ((off < 0) || (off > cbuf.length) || (len < 0) ||
|
this.len = newLen;
|
||||||
((off + len) > cbuf.length) || ((off + len) < 0)) {
|
|
||||||
throw new IndexOutOfBoundsException();
|
|
||||||
} else if (len == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
buf.append(cbuf, off, len);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(String str) {
|
@Override
|
||||||
buf.append(str);
|
public void write(String str, int offset, int len) throws IOException {
|
||||||
|
int newLen = this.len + len;
|
||||||
|
if (newLen > value.length) {
|
||||||
|
expandCapacity(newLen);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(String str, int off, int len) {
|
str.getChars(offset, (offset + len), value, this.len);
|
||||||
buf.append(str.substring(off, off + len));
|
this.len = newLen;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FastStringWriter append(CharSequence csq) {
|
@Override
|
||||||
if (csq == null) {
|
public void write(int c) throws IOException {
|
||||||
write("null");
|
char[] buffer = {(char)c};
|
||||||
} else {
|
write(buffer, 0, 1);
|
||||||
write(csq.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(char buffer[]) throws IOException {
|
||||||
|
write(buffer, 0, buffer.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(String str) throws IOException {
|
||||||
|
write(str, 0, str.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Writer append(CharSequence csq) throws IOException {
|
||||||
|
if (csq instanceof String) {
|
||||||
|
String str = (String)csq;
|
||||||
|
write(str, 0, str.length());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (csq == null)
|
||||||
|
write("null");
|
||||||
|
else
|
||||||
|
write(csq.toString());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Writer append(CharSequence csq, int start, int end) throws IOException {
|
||||||
|
if (csq instanceof String) {
|
||||||
|
String str = (String)csq;
|
||||||
|
write(str, start, (end - start));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FastStringWriter append(CharSequence csq, int start, int end) {
|
|
||||||
CharSequence cs = (csq == null ? "null" : csq);
|
CharSequence cs = (csq == null ? "null" : csq);
|
||||||
write(cs.subSequence(start, end).toString());
|
write(cs.subSequence(start, end).toString());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FastStringWriter append(char c) {
|
@Override
|
||||||
write(c);
|
public Writer append(char c) throws IOException {
|
||||||
|
char[] buffer = {c};
|
||||||
|
write(buffer, 0, 1);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
@Override
|
||||||
return buf.toString();
|
public void flush() throws IOException {
|
||||||
}
|
|
||||||
|
|
||||||
public StringBuilder getBuffer() {
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void flush() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static int MAX_SIZE = 1024 * 128;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 由 StringWriter.close() 改造而来,原先该方法中无任何代码 ,改造如下:
|
|
||||||
* 1:去掉 throws IOException
|
|
||||||
* 2:添加 buf 空间释放处理逻辑
|
|
||||||
* 3:添加 buf.setLength(0),以便于配合 ThreadLocal 回收利用
|
|
||||||
*/
|
|
||||||
public void close() {
|
|
||||||
if (buf.length() > MAX_SIZE) {
|
|
||||||
buf = new StringBuilder(); // 释放空间占用过大的 buf
|
|
||||||
} else {
|
|
||||||
buf.setLength(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -14,21 +14,25 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.jfinal.template;
|
package com.jfinal.template.io;
|
||||||
|
|
||||||
import com.jfinal.template.expr.ast.ExprList;
|
|
||||||
import com.jfinal.template.stat.Location;
|
|
||||||
import com.jfinal.template.stat.ast.Output;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* OutputDirectiveFactory
|
* JdkEncoderFactory
|
||||||
|
*
|
||||||
|
* 支持 utf8mb4,支持 emoji 表情字符,支持各种罕见字符编码
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* 配置方法:
|
||||||
|
* engine.setToJdkEncoderFactory();
|
||||||
|
* </pre>
|
||||||
*/
|
*/
|
||||||
public class OutputDirectiveFactory implements IOutputDirectiveFactory {
|
public class JdkEncoderFactory extends EncoderFactory {
|
||||||
|
|
||||||
public static final OutputDirectiveFactory me = new OutputDirectiveFactory();
|
@Override
|
||||||
|
public Encoder getEncoder() {
|
||||||
public Output getOutputDirective(ExprList exprList, Location location) {
|
return new JdkEncoder(charset);
|
||||||
return new Output(exprList, location);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
package com.jfinal.template.io;
|
package com.jfinal.template.io;
|
||||||
|
|
||||||
import java.nio.charset.MalformedInputException;
|
// import java.nio.charset.MalformedInputException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utf8Encoder
|
* Utf8Encoder
|
||||||
@@ -62,12 +62,16 @@ public class Utf8Encoder extends Encoder {
|
|||||||
if (Character.isLowSurrogate(d)) {
|
if (Character.isLowSurrogate(d)) {
|
||||||
uc = Character.toCodePoint(c, d);
|
uc = Character.toCodePoint(c, d);
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException("encode UTF8 error", new MalformedInputException(1));
|
// throw new RuntimeException("encode UTF8 error", new MalformedInputException(1));
|
||||||
|
bytes[dp++] = (byte) '?';
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (Character.isLowSurrogate(c)) {
|
if (Character.isLowSurrogate(c)) {
|
||||||
throw new RuntimeException("encode UTF8 error", new MalformedInputException(1));
|
// throw new RuntimeException("encode UTF8 error", new MalformedInputException(1));
|
||||||
|
bytes[dp++] = (byte) '?';
|
||||||
|
continue;
|
||||||
} else {
|
} else {
|
||||||
uc = c;
|
uc = c;
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -17,12 +17,10 @@
|
|||||||
package com.jfinal.template.source;
|
package com.jfinal.template.source;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.File;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.net.JarURLConnection;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
|
||||||
import com.jfinal.template.EngineConfig;
|
import com.jfinal.template.EngineConfig;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -65,24 +63,19 @@ public class ClassPathSource implements ISource {
|
|||||||
this.classLoader = getClassLoader();
|
this.classLoader = getClassLoader();
|
||||||
this.url = classLoader.getResource(finalFileName);
|
this.url = classLoader.getResource(finalFileName);
|
||||||
if (url == null) {
|
if (url == null) {
|
||||||
throw new IllegalArgumentException("File not found : \"" + finalFileName + "\"");
|
throw new IllegalArgumentException("File not found in CLASSPATH or JAR : \"" + finalFileName + "\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
processIsInJarAndlastModified();
|
processIsInJarAndlastModified();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void processIsInJarAndlastModified() {
|
protected void processIsInJarAndlastModified() {
|
||||||
try {
|
if ("file".equalsIgnoreCase(url.getProtocol())) {
|
||||||
URLConnection conn = url.openConnection();
|
isInJar = false;
|
||||||
if ("jar".equals(url.getProtocol()) || conn instanceof JarURLConnection) {
|
lastModified = new File(url.getFile()).lastModified();
|
||||||
|
} else {
|
||||||
isInJar = true;
|
isInJar = true;
|
||||||
lastModified = -1;
|
lastModified = -1;
|
||||||
} else {
|
|
||||||
isInJar = false;
|
|
||||||
lastModified = conn.getLastModified();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +104,7 @@ public class ClassPathSource implements ISource {
|
|||||||
return finalFileName;
|
return finalFileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getKey() {
|
public String getCacheKey() {
|
||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,12 +113,7 @@ public class ClassPathSource implements ISource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected long getLastModified() {
|
protected long getLastModified() {
|
||||||
try {
|
return new File(url.getFile()).lastModified();
|
||||||
URLConnection conn = url.openConnection();
|
|
||||||
return conn.getLastModified();
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -152,9 +140,8 @@ public class ClassPathSource implements ISource {
|
|||||||
|
|
||||||
public static StringBuilder loadFile(InputStream inputStream, String encoding) {
|
public static StringBuilder loadFile(InputStream inputStream, String encoding) {
|
||||||
StringBuilder ret = new StringBuilder();
|
StringBuilder ret = new StringBuilder();
|
||||||
BufferedReader br = null;
|
|
||||||
try {
|
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, encoding))) {
|
||||||
br = new BufferedReader(new InputStreamReader(inputStream, encoding));
|
|
||||||
// br = new BufferedReader(new FileReader(fileName));
|
// br = new BufferedReader(new FileReader(fileName));
|
||||||
String line = br.readLine();
|
String line = br.readLine();
|
||||||
if (line != null) {
|
if (line != null) {
|
||||||
@@ -170,16 +157,6 @@ public class ClassPathSource implements ISource {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
if (br != null) {
|
|
||||||
try {
|
|
||||||
br.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
// com.jfinal.kit.LogKit.error(e.getMessage(), e);
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -19,7 +19,6 @@ package com.jfinal.template.source;
|
|||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import com.jfinal.template.EngineConfig;
|
import com.jfinal.template.EngineConfig;
|
||||||
|
|
||||||
@@ -48,7 +47,7 @@ public class FileSource implements ISource {
|
|||||||
return lastModified != new File(finalFileName).lastModified();
|
return lastModified != new File(finalFileName).lastModified();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getKey() {
|
public String getCacheKey() {
|
||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,6 +76,9 @@ public class FileSource implements ISource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String buildFinalFileName(String baseTemplatePath, String fileName) {
|
private String buildFinalFileName(String baseTemplatePath, String fileName) {
|
||||||
|
if (baseTemplatePath == null) {
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
char firstChar = fileName.charAt(0);
|
char firstChar = fileName.charAt(0);
|
||||||
String finalFileName;
|
String finalFileName;
|
||||||
if (firstChar == '/' || firstChar == '\\') {
|
if (firstChar == '/' || firstChar == '\\') {
|
||||||
@@ -89,9 +91,8 @@ public class FileSource implements ISource {
|
|||||||
|
|
||||||
public static StringBuilder loadFile(File file, String encoding) {
|
public static StringBuilder loadFile(File file, String encoding) {
|
||||||
StringBuilder ret = new StringBuilder((int)file.length() + 3);
|
StringBuilder ret = new StringBuilder((int)file.length() + 3);
|
||||||
BufferedReader br = null;
|
|
||||||
try {
|
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding))) {
|
||||||
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
|
|
||||||
// br = new BufferedReader(new FileReader(fileName));
|
// br = new BufferedReader(new FileReader(fileName));
|
||||||
String line = br.readLine();
|
String line = br.readLine();
|
||||||
if (line != null) {
|
if (line != null) {
|
||||||
@@ -107,16 +108,6 @@ public class FileSource implements ISource {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
if (br != null) {
|
|
||||||
try {
|
|
||||||
br.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
// com.jfinal.kit.LogKit.error(e.getMessage(), e);
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -27,12 +27,12 @@ public interface ISource {
|
|||||||
boolean isModified();
|
boolean isModified();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* key used to cache, return null if do not cache the template
|
* cache key used to cache, return null if do not cache the template
|
||||||
*
|
*
|
||||||
* 注意:如果不希望缓存从该 ISource 解析出来的 Template 对象
|
* 注意:如果不希望缓存从该 ISource 解析出来的 Template 对象
|
||||||
* 让 getKey() 返回 null 值即可
|
* 让 getCacheKey() 返回 null 值即可
|
||||||
*/
|
*/
|
||||||
String getKey();
|
String getCacheKey();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* content of ISource
|
* content of ISource
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.template.EngineConfig;
|
|||||||
*/
|
*/
|
||||||
public class StringSource implements ISource {
|
public class StringSource implements ISource {
|
||||||
|
|
||||||
private String key;
|
private String cacheKey;
|
||||||
private StringBuilder content;
|
private StringBuilder content;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -38,7 +38,7 @@ public class StringSource implements ISource {
|
|||||||
throw new IllegalArgumentException("content can not be blank");
|
throw new IllegalArgumentException("content can not be blank");
|
||||||
}
|
}
|
||||||
this.content = new StringBuilder(content);
|
this.content = new StringBuilder(content);
|
||||||
this.key = cache ? HashKit.md5(content) : null; // 不缓存只要将 key 值赋为 null 即可
|
this.cacheKey = cache ? HashKit.md5(content) : null; // 不缓存只要将 cacheKey 值赋为 null 即可
|
||||||
}
|
}
|
||||||
|
|
||||||
public StringSource(StringBuilder content, boolean cache) {
|
public StringSource(StringBuilder content, boolean cache) {
|
||||||
@@ -46,15 +46,15 @@ public class StringSource implements ISource {
|
|||||||
throw new IllegalArgumentException("content can not be blank");
|
throw new IllegalArgumentException("content can not be blank");
|
||||||
}
|
}
|
||||||
this.content = content;
|
this.content = content;
|
||||||
this.key = cache ? HashKit.md5(content.toString()) : null; // 不缓存只要将 key 值赋为 null 即可
|
this.cacheKey = cache ? HashKit.md5(content.toString()) : null; // 不缓存只要将 cacheKey 值赋为 null 即可
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isModified() {
|
public boolean isModified() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getKey() {
|
public String getCacheKey() {
|
||||||
return key;
|
return cacheKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public StringBuilder getContent() {
|
public StringBuilder getContent() {
|
||||||
@@ -67,8 +67,8 @@ public class StringSource implements ISource {
|
|||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("Key : ").append(key).append("\n");
|
sb.append("cacheKey : ").append(cacheKey).append("\n");
|
||||||
sb.append("Content : ").append(content).append("\n");
|
sb.append("content : ").append(content).append("\n");
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.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.
|
||||||
@@ -18,6 +18,7 @@ package com.jfinal.template.stat;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DKFF(Dynamic Key Feature Forward) Lexer
|
* DKFF(Dynamic Key Feature Forward) Lexer
|
||||||
@@ -35,10 +36,14 @@ class Lexer {
|
|||||||
int forwardRow = 1;
|
int forwardRow = 1;
|
||||||
TextToken previousTextToken = null;
|
TextToken previousTextToken = null;
|
||||||
|
|
||||||
List<Token> tokens = new ArrayList<Token>();
|
|
||||||
String fileName;
|
String fileName;
|
||||||
|
Set<String> keepLineBlankDirectives;
|
||||||
|
|
||||||
|
List<Token> tokens = new ArrayList<Token>();
|
||||||
|
|
||||||
|
public Lexer(StringBuilder content, String fileName, Set<String> keepLineBlankDirectives) {
|
||||||
|
this.keepLineBlankDirectives = keepLineBlankDirectives;
|
||||||
|
|
||||||
public Lexer(StringBuilder content, String fileName) {
|
|
||||||
int len = content.length();
|
int len = content.length();
|
||||||
buf = new char[len + 1];
|
buf = new char[len + 1];
|
||||||
content.getChars(0, content.length(), buf, 0);
|
content.getChars(0, content.length(), buf, 0);
|
||||||
@@ -110,7 +115,7 @@ class Lexer {
|
|||||||
para = scanPara("");
|
para = scanPara("");
|
||||||
idToken = new Token(Symbol.OUTPUT, beginRow);
|
idToken = new Token(Symbol.OUTPUT, beginRow);
|
||||||
paraToken = new ParaToken(para, beginRow);
|
paraToken = new ParaToken(para, beginRow);
|
||||||
return addOutputToken(idToken, paraToken);
|
return addIdParaToken(idToken, paraToken);
|
||||||
}
|
}
|
||||||
if (CharTable.isLetter(peek())) { // # id
|
if (CharTable.isLetter(peek())) { // # id
|
||||||
state = 10;
|
state = 10;
|
||||||
@@ -472,31 +477,6 @@ class Lexer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 输出指令不对前后空白与换行进行任何处理,直接调用 tokens.add(...)
|
|
||||||
boolean addOutputToken(Token idToken, Token paraToken) {
|
|
||||||
tokens.add(idToken);
|
|
||||||
tokens.add(paraToken);
|
|
||||||
previousTextToken = null;
|
|
||||||
return prepareNextScan(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 向前看后续是否跟随的是空白 + 换行或者是空白 + EOF,是则表示当前指令后续没有其它有用内容
|
|
||||||
boolean lookForwardLineFeedAndEof() {
|
|
||||||
int forwardBak = forward;
|
|
||||||
int forwardRowBak = forwardRow;
|
|
||||||
for (char c=peek(); true; c=next()) {
|
|
||||||
if (CharTable.isBlank(c)) {
|
|
||||||
continue ;
|
|
||||||
}
|
|
||||||
if (c == '\n' || c == EOF) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
forward = forwardBak;
|
|
||||||
forwardRow = forwardRowBak;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 带参指令处于独立行时删除前后空白字符,并且再删除一个后续的换行符
|
* 带参指令处于独立行时删除前后空白字符,并且再删除一个后续的换行符
|
||||||
* 处于独立行是指:向前看无有用内容,在前面情况成立的基础之上
|
* 处于独立行是指:向前看无有用内容,在前面情况成立的基础之上
|
||||||
@@ -509,32 +489,68 @@ class Lexer {
|
|||||||
tokens.add(idToken);
|
tokens.add(idToken);
|
||||||
tokens.add(paraToken);
|
tokens.add(paraToken);
|
||||||
|
|
||||||
|
skipFollowingComment();
|
||||||
|
|
||||||
|
// 保留指令所在行空白字符
|
||||||
|
// #define xxx() 模板函数名、#@xxx() 模板函数名,可以与指令同名,需要排除掉这三种 Symbol
|
||||||
|
if (keepLineBlankDirectives.contains(idToken.value())
|
||||||
|
&& idToken.symbol != Symbol.DEFINE
|
||||||
|
&& idToken.symbol != Symbol.CALL
|
||||||
|
&& idToken.symbol != Symbol.CALL_IF_DEFINED
|
||||||
|
) {
|
||||||
|
|
||||||
|
prepareNextScan(0);
|
||||||
|
} else {
|
||||||
|
trimLineBlank();
|
||||||
|
}
|
||||||
|
|
||||||
|
previousTextToken = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// #set 这类指令,处在独立一行时,需要删除当前行的前后空白字符以及行尾字符 '\n'
|
||||||
|
void trimLineBlank() {
|
||||||
// if (lookForwardLineFeed() && (deletePreviousTextTokenBlankTails() || lexemeBegin == 0)) {
|
// if (lookForwardLineFeed() && (deletePreviousTextTokenBlankTails() || lexemeBegin == 0)) {
|
||||||
if (lookForwardLineFeedAndEof() && deletePreviousTextTokenBlankTails()) {
|
if (lookForwardLineFeedAndEof() && deletePreviousTextTokenBlankTails()) {
|
||||||
prepareNextScan(peek() != EOF ? 1 : 0);
|
prepareNextScan(peek() != EOF ? 1 : 0);
|
||||||
} else {
|
} else {
|
||||||
prepareNextScan(0);
|
prepareNextScan(0);
|
||||||
}
|
}
|
||||||
previousTextToken = null;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理前后空白的逻辑与 addIdParaToken() 基本一样,仅仅多了一个对于紧随空白的 next() 操作
|
// 无参指令无条件调用 trimLineBlank()
|
||||||
boolean addNoParaToken(Token noParaToken) {
|
boolean addNoParaToken(Token noParaToken) {
|
||||||
tokens.add(noParaToken);
|
tokens.add(noParaToken);
|
||||||
|
|
||||||
|
skipFollowingComment();
|
||||||
|
|
||||||
if (CharTable.isBlank(peek())) {
|
if (CharTable.isBlank(peek())) {
|
||||||
next(); // 无参指令之后紧随的一个空白字符仅为分隔符,不参与后续扫描
|
next(); // 无参指令之后紧随的一个空白字符仅为分隔符,不参与后续扫描
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lookForwardLineFeedAndEof() && deletePreviousTextTokenBlankTails()) {
|
trimLineBlank();
|
||||||
prepareNextScan(peek() != EOF ? 1 : 0);
|
|
||||||
} else {
|
|
||||||
prepareNextScan(0);
|
|
||||||
}
|
|
||||||
previousTextToken = null;
|
previousTextToken = null;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 向前看后续是否跟随的是空白 + 换行或者是空白 + EOF,是则表示当前指令后续没有其它有用内容
|
||||||
|
boolean lookForwardLineFeedAndEof() {
|
||||||
|
int fp = forward;
|
||||||
|
for (char c=buf[fp]; true; c=buf[++fp]) {
|
||||||
|
if (CharTable.isBlank(c)) {
|
||||||
|
continue ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c == '\n' || c == EOF) {
|
||||||
|
forward = fp;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 1:当前指令前方仍然是指令 (previousTextToken 为 null),直接返回 true
|
* 1:当前指令前方仍然是指令 (previousTextToken 为 null),直接返回 true
|
||||||
* 2:当前指令前方为 TextToken 时的处理逻辑与返回值完全依赖于 TextToken.deleteBlankTails()
|
* 2:当前指令前方为 TextToken 时的处理逻辑与返回值完全依赖于 TextToken.deleteBlankTails()
|
||||||
@@ -543,6 +559,54 @@ class Lexer {
|
|||||||
// return previousTextToken != null ? previousTextToken.deleteBlankTails() : false;
|
// return previousTextToken != null ? previousTextToken.deleteBlankTails() : false;
|
||||||
return previousTextToken == null || previousTextToken.deleteBlankTails();
|
return previousTextToken == null || previousTextToken.deleteBlankTails();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 跳过指令后方跟随的注释,以便正确处理各类换行逻辑
|
||||||
|
*/
|
||||||
|
void skipFollowingComment() {
|
||||||
|
int fp = forward;
|
||||||
|
for (char c=buf[fp]; true; c=buf[++fp]) {
|
||||||
|
if (CharTable.isBlank(c)) {
|
||||||
|
continue ;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 勿使用 next()
|
||||||
|
if (c == '#') {
|
||||||
|
if (buf[fp + 1] == '#' && buf[fp + 2] == '#') {
|
||||||
|
forward = fp;
|
||||||
|
skipFollowingSingleLineComment();
|
||||||
|
} else if (buf[fp + 1] == '-' && buf[fp + 2] == '-') {
|
||||||
|
forward = fp;
|
||||||
|
skipFollowingMultiLineComment();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void skipFollowingSingleLineComment() {
|
||||||
|
forward = forward + 3;
|
||||||
|
for (char c=peek(); true; c=next()) {
|
||||||
|
if (c == '\n' || c == EOF) {
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void skipFollowingMultiLineComment() {
|
||||||
|
forward = forward + 3;
|
||||||
|
for (char c=peek(); true; c=next()) {
|
||||||
|
if (c == '-' && buf[forward + 1] == '-' && buf[forward + 2] == '#') {
|
||||||
|
forward = forward + 3;
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c == EOF) {
|
||||||
|
throw new ParseException("The multiline comment start block \"#--\" can not match the end block: \"--#\"", new Location(fileName, beginRow));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user