Skip to content

Commit 21dc52f

Browse files
xiaoma20082008xiaoma20082008
authored andcommitted
<fix>: remove ASTReference impl in CompiledVisitor
<fix>: remove all logger abstraction <feat>: add testcases
1 parent 42eca55 commit 21dc52f

27 files changed

Lines changed: 181 additions & 637 deletions

velocity-engine-core/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
```
22
+----------+ |
33
| | nowContext +---------+ |
4-
| | -----------> | Context | <--+ run
4+
| | -----------> | Context | <--+ render
55
init | | +---------+ |
66
----->| Velocity | v
77
| | getTemplate +------------------+

velocity-engine-core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@
295295
</build>
296296

297297
<dependencies>
298+
<dependency>
299+
<groupId>org.javassist</groupId>
300+
<artifactId>javassist</artifactId>
301+
<version>3.28.0-GA</version>
302+
<scope>provided</scope>
303+
</dependency>
298304
<dependency>
299305
<groupId>org.apache.commons</groupId>
300306
<artifactId>commons-lang3</artifactId>

velocity-engine-core/src/main/java/org/apache/velocity/app/VelocityEngine.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
* under the License.
2020
*/
2121

22+
import java.io.Reader;
23+
import java.io.Writer;
24+
import java.util.Objects;
25+
import java.util.Properties;
2226
import org.apache.velocity.Template;
2327
import org.apache.velocity.context.Context;
2428
import org.apache.velocity.exception.MethodInvocationException;
@@ -28,10 +32,6 @@
2832
import org.apache.velocity.runtime.RuntimeInstance;
2933
import org.slf4j.Logger;
3034

31-
import java.io.Reader;
32-
import java.io.Writer;
33-
import java.util.Properties;
34-
3535
/**
3636
* <p>
3737
* This class provides a separate new-able instance of the
@@ -51,14 +51,19 @@
5151
*/
5252
public class VelocityEngine implements RuntimeConstants
5353
{
54-
private RuntimeInstance ri = new RuntimeInstance();
54+
private final RuntimeInstance ri;
5555

5656
/**
5757
* Init-less CTOR
5858
*/
5959
public VelocityEngine()
6060
{
61-
// do nothing
61+
this(new RuntimeInstance());
62+
}
63+
64+
public VelocityEngine(RuntimeInstance ri) // for testable
65+
{
66+
this.ri = Objects.requireNonNull(ri, "RuntimeInstance is null");
6267
}
6368

6469
/**
@@ -68,6 +73,7 @@ public VelocityEngine()
6873
*/
6974
public VelocityEngine(String propsFilename)
7075
{
76+
this();
7177
ri.setProperties(propsFilename);
7278
}
7379

@@ -77,6 +83,7 @@ public VelocityEngine(String propsFilename)
7783
*/
7884
public VelocityEngine(Properties p)
7985
{
86+
this();
8087
ri.setProperties(p);
8188
}
8289

velocity-engine-core/src/main/java/org/apache/velocity/spi/Loader.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,11 @@
2020
*/
2121

2222
import java.io.IOException;
23-
import java.util.List;
2423
import java.util.Locale;
2524
import org.apache.velocity.api.Resource;
2625

2726
public interface Loader {
2827

29-
/**
30-
* @param suffix resource suffix
31-
* @return resource names.
32-
*/
33-
List<String> list(String suffix) throws IOException;
34-
3528
boolean exists(String name, Locale locale);
3629

3730
Resource load(String name, Locale locale, String encoding) throws IOException;

velocity-engine-core/src/main/java/org/apache/velocity/spi/Logger.java

Lines changed: 0 additions & 139 deletions
This file was deleted.

velocity-engine-core/src/main/java/org/apache/velocity/spi/caches/MapCache.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@
2222
import java.util.Collection;
2323
import java.util.Map;
2424
import java.util.Set;
25+
import java.util.concurrent.ConcurrentHashMap;
2526
import java.util.concurrent.ConcurrentMap;
2627
import org.apache.velocity.spi.Cache;
2728

2829
public class MapCache<K, V> implements Cache<K, V> {
2930

30-
private ConcurrentMap<K, V> map;
31+
private final ConcurrentMap<K, V> map;
32+
33+
public MapCache() {
34+
this.map = new ConcurrentHashMap<>();
35+
}
3136

3237
@Override
3338
public int size() {
@@ -51,61 +56,61 @@ public boolean containsValue(Object value) {
5156

5257
@Override
5358
public V get(Object key) {
54-
return null;
59+
return map.get(key);
5560
}
5661

5762
@Override
5863
public V put(K key, V value) {
59-
return null;
64+
return map.put(key, value);
6065
}
6166

6267
@Override
6368
public V remove(Object key) {
64-
return null;
69+
return map.remove(key);
6570
}
6671

6772
@Override
6873
public void putAll(Map<? extends K, ? extends V> m) {
69-
74+
map.putAll(m);
7075
}
7176

7277
@Override
7378
public void clear() {
74-
79+
map.clear();
7580
}
7681

7782
@Override
7883
public Set<K> keySet() {
79-
return null;
84+
return map.keySet();
8085
}
8186

8287
@Override
8388
public Collection<V> values() {
84-
return null;
89+
return map.values();
8590
}
8691

8792
@Override
8893
public Set<Entry<K, V>> entrySet() {
89-
return null;
94+
return map.entrySet();
9095
}
9196

9297
@Override
9398
public V putIfAbsent(K key, V value) {
94-
return null;
99+
return map.putIfAbsent(key, value);
95100
}
96101

97102
@Override
98103
public boolean remove(Object key, Object value) {
99-
return false;
104+
return map.remove(key, value);
100105
}
101106

102107
@Override
103108
public boolean replace(K key, V oldValue, V newValue) {
104-
return false;
109+
return map.replace(key, oldValue, newValue);
105110
}
106111

107112
@Override
108113
public V replace(K key, V value) {
109-
return null;
114+
return map.replace(key, value);
110115
}
111116
}

velocity-engine-core/src/main/java/org/apache/velocity/spi/compilers/BaseCompiler.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929
import org.apache.velocity.exception.VelocityException;
3030
import org.apache.velocity.spi.Cache;
3131
import org.apache.velocity.spi.Compiler;
32-
import org.apache.velocity.spi.Logger;
32+
import org.slf4j.Logger;
33+
import org.slf4j.LoggerFactory;
3334

3435
public abstract class BaseCompiler implements Compiler {
3536

3637
private static final Pattern PACKAGE_PATTERN = Pattern.compile("package\\s+([_a-zA-Z][_a-zA-Z0-9\\.]*);");
37-
private static final Pattern CLASS_PATTERN = Pattern.compile("class\\s+([_a-zA-Z][_a-zA-Z0-9]*)\\s+");
38+
private static final Pattern CLASS_PATTERN = Pattern.compile("public\\s+class\\s+([_a-zA-Z][_a-zA-Z0-9]*)\\s+");
3839

39-
protected Logger logger;
40+
protected final Logger logger = LoggerFactory.getLogger(getClass());
4041
protected Cache<String, Object> cache;
4142
protected File compileDirectory;
4243
private volatile boolean first = true;
@@ -80,10 +81,6 @@ public Class<?> compile(String code) throws VelocityException {
8081

8182
protected abstract Class<?> doCompile(String name, String source) throws Exception;
8283

83-
public void setLogger(Logger logger) {
84-
this.logger = logger;
85-
}
86-
8784
public void setCache(Cache<String, Object> cache) {
8885
this.cache = cache;
8986
}
@@ -102,7 +99,7 @@ protected void saveBytecode(String name, byte[] bytecode) {
10299
if (first) {
103100
first = false;
104101
if (logger != null && logger.isInfoEnabled()) {
105-
logger.info("Compile httl template classes to directory " + compileDirectory.getAbsolutePath());
102+
logger.info("Compile template classes to directory " + compileDirectory.getAbsolutePath());
106103
}
107104
}
108105
} catch (IOException e) {

velocity-engine-core/src/main/java/org/apache/velocity/spi/compilers/JavacCompiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public JavacCompiler() {
6565
compiler = ToolProvider.getSystemJavaCompiler();
6666
if (compiler == null) {
6767
throw new IllegalStateException(
68-
"Can not get system java compiler. Please run with JDK (NOT JVM), or configure the httl.properties: compiler=httl.spi.compilers.JavassistCompiler, and add javassist.jar.");
68+
"Can not get system java compiler.");
6969
}
7070
diagnosticCollector = new DiagnosticCollector<JavaFileObject>();
7171
standardJavaFileManager = compiler.getStandardFileManager(diagnosticCollector, null, null);

velocity-engine-core/src/main/java/org/apache/velocity/spi/compilers/JavassistCompiler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
* under the License.
2020
*/
2121

22+
import javassist.ClassPool;
23+
2224
public class JavassistCompiler extends BaseCompiler {
2325

2426
@Override
2527
protected Class<?> doCompile(String name, String source) throws Exception {
26-
return null;
28+
return ClassPool.getDefault().makeClass(source).toClass();
2729
}
2830
}

0 commit comments

Comments
 (0)