java: add test program for TemplatePrecompiler

This commit is contained in:
Muga Nishizawa
2011-04-10 02:33:46 +09:00
parent 58c0fe0f91
commit 65ddd1a455
4 changed files with 154 additions and 33 deletions
@@ -29,7 +29,6 @@ import org.msgpack.Template;
public class TemplateRegistry {
private static Map<Type, Template> map;
private static Map<Type, GenericTemplate> genericMap;
private static BuilderSelectorRegistry builderSelectorRegistry;
static {
@@ -41,38 +40,43 @@ public class TemplateRegistry {
public static void register(Class<?> target) {
TemplateBuilder builder = builderSelectorRegistry.select(target);
if(builder != null){
if (builder != null) {
register(target,builder.buildTemplate(target));
}else{
} else {
register(target,builderSelectorRegistry.getForceBuilder().buildTemplate(target));
}
}
public static void register(Class<?> target, FieldOption implicitOption) {
TemplateBuilder builder = builderSelectorRegistry.select(target);
if(builder != null && builder instanceof CustomTemplateBuilder){
if (builder != null && builder instanceof CustomTemplateBuilder) {
register(target, ((CustomTemplateBuilder)builder).buildTemplate(target, implicitOption));
}else{
} else {
throw new TemplateBuildException("Cannot build template with filed option");
}
}
public static void register(Class<?> target, FieldList flist) throws NoSuchFieldException {
TemplateBuilder builder = builderSelectorRegistry.select(target);
if(builder != null && builder instanceof CustomTemplateBuilder){
if (builder != null && builder instanceof CustomTemplateBuilder) {
register(target, ((CustomTemplateBuilder)builder).buildTemplate(target, flist));
}else{
} else {
throw new TemplateBuildException("Cannot build template with filed list");
}
}
public static synchronized void register(Type rawType, Template tmpl) {
if(rawType instanceof ParameterizedType) {
if (rawType instanceof ParameterizedType) {
rawType = ((ParameterizedType)rawType).getRawType();
}
map.put(rawType, tmpl);
}
public static boolean unregister(Class<?> target) {
Template tmpl = map.remove(target);
return tmpl != null;
}
public static synchronized void registerGeneric(Type rawType, GenericTemplate gtmpl) {
if(rawType instanceof ParameterizedType) {
rawType = ((ParameterizedType)rawType).getRawType();
@@ -81,22 +85,27 @@ public class TemplateRegistry {
}
public static synchronized Template lookup(Type targetType) {
return lookupImpl(targetType, false, true);
return lookupImpl(targetType, true, false, true);
}
public static synchronized Template lookup(Type targetType, boolean forceBuild) {
return lookupImpl(targetType, forceBuild, true);
return lookupImpl(targetType, true, forceBuild, true);
}
public static synchronized Template lookup(Type targetType, boolean forceLoad, boolean forceBuild) {
return lookupImpl(targetType, forceLoad, forceBuild, true);
}
public static synchronized Template tryLookup(Type targetType) {
return lookupImpl(targetType, false, false);
return lookupImpl(targetType, true, false, false);
}
public static synchronized Template tryLookup(Type targetType, boolean forceBuild) {
return lookupImpl(targetType, forceBuild, false);
return lookupImpl(targetType, true, forceBuild, false);
}
private static synchronized Template lookupImpl(Type targetType, boolean forceBuild, boolean fallbackDefault) {
private static synchronized Template lookupImpl(Type targetType,
boolean forceLoad, boolean forceBuild, boolean fallbackDefault) {
Template tmpl;
if(targetType instanceof ParameterizedType) {
@@ -115,10 +124,13 @@ public class TemplateRegistry {
// find match TemplateBuilder
TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(targetType);
if(builder != null){
tmpl = builder.loadTemplate(targetType);
if (tmpl != null) {
return tmpl;
if (builder != null) {
if (forceLoad) {
tmpl = builder.loadTemplate(targetType);
if (tmpl != null) {
register(targetType, tmpl);
return tmpl;
}
}
tmpl = builder.buildTemplate(targetType);
@@ -22,6 +22,7 @@ import java.io.IOException;
import java.util.Properties;
import org.msgpack.template.builder.BuilderSelectorRegistry;
import org.msgpack.template.builder.JavassistTemplateBuilder;
import org.msgpack.template.builder.TemplateBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -29,35 +30,39 @@ import org.slf4j.LoggerFactory;
public class TemplatePrecompiler {
private static final Logger LOG = LoggerFactory.getLogger(TemplatePrecompiler.class);
//private static final String SRC = "msgpack.template.srcdir";
//public static final String SRC = "msgpack.template.srcdir";
private static final String DIST = "msgpack.template.distdir";
public static final String DIST = "msgpack.template.distdir";
//private static final String DEFAULT_SRC = ".";
//public static final String DEFAULT_SRC = ".";
private static final String DEFAULT_DIST = ".";
public static final String DEFAULT_DIST = ".";
private static TemplatePrecompiler INSTANCE = null;
private TemplatePrecompiler() {
}
public void saveTemplates(final String[] classFileNames) throws IOException {
public static void saveTemplates(final String[] classFileNames) throws IOException {
throw new UnsupportedOperationException("Not supported yet.");// TODO
}
public void saveTemplateClass(Class<?> targetClass) throws IOException {
public static void saveTemplateClass(Class<?> targetClass) throws IOException {
if (INSTANCE != null) {
INSTANCE = new TemplatePrecompiler();
}
LOG.info("Saving template of " + targetClass.getName() + "...");
Properties props = System.getProperties();
String distDirName = getDirName(props, DIST, DEFAULT_DIST);
if (targetClass.isEnum()) {
throw new UnsupportedOperationException("Enum not supported yet: " + targetClass.getName());
} else {
TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(targetClass);
builder.writeTemplate(targetClass, distDirName);
new JavassistTemplateBuilder().writeTemplate(targetClass, distDirName);
}
LOG.info("Saved .class file of template class of " + targetClass.getName());
}
private String getDirName(Properties props, String dirName, String defaultDirName)
private static String getDirName(Properties props, String dirName, String defaultDirName)
throws IOException {
String dName = props.getProperty(dirName, defaultDirName);
File d = new File(dName);
@@ -68,7 +73,6 @@ public class TemplatePrecompiler {
}
public static void main(final String[] args) throws Exception {
TemplatePrecompiler compiler = new TemplatePrecompiler();// TODO
compiler.saveTemplates(args);
TemplatePrecompiler.saveTemplates(args);
}
}