forked from msgpack/msgpack-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplatePrecompiler.java
More file actions
163 lines (146 loc) · 6.17 KB
/
TemplatePrecompiler.java
File metadata and controls
163 lines (146 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// MessagePack for Java
//
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
//
// 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 org.msgpack.util;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;
import org.msgpack.template.TemplateRegistry;
import org.msgpack.template.builder.JavassistTemplateBuilder;
/**
* This class is a template precompiler, which is used for saving templates that
* <code>TemplateBuilder</code> generated. It saves templates as .class files.
* Application enables to load the .class files and use templates.
*
*/
public class TemplatePrecompiler {
private static final Logger LOG = Logger.getLogger(TemplatePrecompiler.class.getName());
public static final String DEST = "msgpack.template.destdir";
public static final String DEFAULT_DEST = ".";
public static void saveTemplates(final String[] classNames)
throws IOException, ClassNotFoundException {
// TODO #MN
TemplateRegistry registry = new TemplateRegistry(null);
List<String> ret = new ArrayList<String>();
for (String className : classNames) {
matchClassNames(ret, className);
}
List<Class<?>> ret0 = toClass(ret);
for (Class<?> c : ret0) {
saveTemplateClass(registry, c);
}
}
@SuppressWarnings("serial")
private static void matchClassNames(List<String> ret, final String className)
throws IOException {
String packageName = className.substring(0, className.lastIndexOf('.'));
String relativedName = className.substring(
className.lastIndexOf('.') + 1, className.length());
String patName = relativedName.replace("*", "(\\w+)");
Pattern pat = Pattern.compile(patName);
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
JavaFileManager fm = compiler.getStandardFileManager(
new DiagnosticCollector<JavaFileObject>(), null, null);
HashSet<JavaFileObject.Kind> kind = new HashSet<JavaFileObject.Kind>() {
{
add(JavaFileObject.Kind.CLASS);
}
};
for (JavaFileObject f : fm.list(StandardLocation.PLATFORM_CLASS_PATH, packageName, kind, false)) {
String relatived0 = f.getName();
String name0 = relatived0.substring(0, relatived0.length() - ".class".length());
Matcher m = pat.matcher(name0);
if (m.matches()) {
String name = packageName + '.' + name0;
if (!ret.contains(name)) {
ret.add(name);
}
}
}
}
private static List<Class<?>> toClass(List<String> classNames)
throws ClassNotFoundException {
List<Class<?>> ret = new ArrayList<Class<?>>(classNames.size());
ClassLoader cl = TemplatePrecompiler.class.getClassLoader();
for (String className : classNames) {
Class<?> c = cl.loadClass(className);
ret.add(c);
}
return ret;
}
public static void saveTemplateClasses(TemplateRegistry registry, Class<?>[] targetClasses)
throws IOException {
for (Class<?> c : targetClasses) {
saveTemplateClass(registry, c);
}
}
public static void saveTemplateClass(TemplateRegistry registry, Class<?> targetClass)
throws IOException {
LOG.info("Saving template of " + targetClass.getName() + "...");
Properties props = System.getProperties();
String distDirName = getDirName(props, DEST, DEFAULT_DEST);
if (targetClass.isEnum()) {
throw new UnsupportedOperationException(
"Not supported enum type yet: " + targetClass.getName());
} else {
new JavassistTemplateBuilder(registry).writeTemplate(targetClass,
distDirName);
}
LOG.info("Saved .class file of template class of " + targetClass.getName());
}
public static boolean deleteTemplateClass(Class<?> targetClass)
throws IOException {
LOG.info("Deleting template of " + targetClass.getName() + "...");
Properties props = System.getProperties();
String distDirName = getDirName(props, DEST, DEFAULT_DEST);
String targetClassName = targetClass.getName();
String targetClassFileName = targetClassName.replace('.',
File.separatorChar) + "_$$_Template.class";
File targetFile = new File(distDirName + File.separatorChar
+ targetClassFileName);
boolean deleted = false;
if (!targetFile.isDirectory() && targetFile.exists()) {
deleted = targetFile.delete();
}
LOG.info("Deleted .class file of template class of " + targetClass.getName());
return deleted;
}
private static String getDirName(Properties props, String dirName, String defaultDirName)
throws IOException {
String dName = props.getProperty(dirName, defaultDirName);
File d = new File(dName);
if (!d.isDirectory() && !d.exists()) {
throw new IOException("Directory not exists: " + dName);
}
return d.getAbsolutePath();
}
public static void main(final String[] args) throws Exception {
TemplatePrecompiler.saveTemplates(args);
}
}