mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-22 08:26:35 +01:00
java: Refactored template builders again
This commit is contained in:
parent
b96b62f2ac
commit
58c0fe0f91
@ -1,7 +1,7 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
// 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.
|
||||
@ -23,5 +23,4 @@ public abstract class AbstractTemplate implements Template {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return convert(pac.unpackObject(), to);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
// 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.
|
||||
@ -18,5 +18,4 @@
|
||||
package org.msgpack;
|
||||
|
||||
public interface Template extends MessagePacker, MessageUnpacker, MessageConverter {
|
||||
}
|
||||
|
||||
}
|
@ -20,12 +20,7 @@ package org.msgpack.template;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.GenericArrayType;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.annotation.Annotation;
|
||||
import org.msgpack.annotation.MessagePackMessage;
|
||||
import org.msgpack.annotation.MessagePackDelegate;
|
||||
import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||
import org.msgpack.template.builder.BuilderSelectorRegistry;
|
||||
import org.msgpack.template.builder.CustomTemplateBuilder;
|
||||
import org.msgpack.template.builder.TemplateBuilder;
|
||||
@ -44,18 +39,13 @@ public class TemplateRegistry {
|
||||
builderSelectorRegistry = BuilderSelectorRegistry.getInstance();
|
||||
}
|
||||
|
||||
public static void register(Class<?> target) { // auto-detect
|
||||
public static void register(Class<?> target) {
|
||||
TemplateBuilder builder = builderSelectorRegistry.select(target);
|
||||
if(builder != null){
|
||||
register(target,builder.buildTemplate(target));
|
||||
}else{
|
||||
register(target,builderSelectorRegistry.getForceBuilder().buildTemplate(target));
|
||||
}
|
||||
/*if(target.isEnum()) {
|
||||
register(target, TemplateBuilder.buildOrdinalEnum(target));
|
||||
} else {
|
||||
register(target, TemplateBuilder.build(target));
|
||||
}*/
|
||||
}
|
||||
|
||||
public static void register(Class<?> target, FieldOption implicitOption) {
|
||||
@ -63,7 +53,7 @@ public class TemplateRegistry {
|
||||
if(builder != null && builder instanceof CustomTemplateBuilder){
|
||||
register(target, ((CustomTemplateBuilder)builder).buildTemplate(target, implicitOption));
|
||||
}else{
|
||||
throw new TemplateBuildException("cannot build template with filed option");
|
||||
throw new TemplateBuildException("Cannot build template with filed option");
|
||||
}
|
||||
}
|
||||
|
||||
@ -72,7 +62,7 @@ public class TemplateRegistry {
|
||||
if(builder != null && builder instanceof CustomTemplateBuilder){
|
||||
register(target, ((CustomTemplateBuilder)builder).buildTemplate(target, flist));
|
||||
}else{
|
||||
throw new TemplateBuildException("cannot build template with filed list");
|
||||
throw new TemplateBuildException("Cannot build template with filed list");
|
||||
}
|
||||
}
|
||||
|
||||
@ -123,50 +113,21 @@ public class TemplateRegistry {
|
||||
return tmpl;
|
||||
}
|
||||
|
||||
/*if(targetType instanceof GenericArrayType) {
|
||||
// GenericArrayType is not a Class<?>
|
||||
tmpl = TemplateBuilder.buildArray(targetType);
|
||||
register(targetType, tmpl);
|
||||
return tmpl;
|
||||
}
|
||||
|
||||
Class<?> target = (Class<?>)targetType;
|
||||
|
||||
Class<?> tmplClass = TemplateBuilder.load(target);
|
||||
if (tmplClass != null) {
|
||||
tmpl = TemplateBuilder.initialize(target, tmplClass);
|
||||
register(target, tmpl);
|
||||
return tmpl;
|
||||
}
|
||||
|
||||
if(target.isArray()) {
|
||||
// FIXME can't distinguish type-erased T<>[]?
|
||||
tmpl = TemplateBuilder.buildArray(target);
|
||||
register(target, tmpl);
|
||||
return tmpl;
|
||||
}
|
||||
|
||||
if(isAnnotated(target, MessagePackMessage.class)) {
|
||||
tmpl = TemplateBuilder.build(target);
|
||||
register(target, tmpl);
|
||||
return tmpl;
|
||||
} else if(isAnnotated(target, MessagePackDelegate.class)) {
|
||||
// TODO DelegateTemplate
|
||||
throw new UnsupportedOperationException("not supported yet. : " + target.getName());
|
||||
} else if(isAnnotated(target, MessagePackOrdinalEnum.class)) {
|
||||
tmpl = TemplateBuilder.buildOrdinalEnum(target);
|
||||
register(target, tmpl);
|
||||
return tmpl;
|
||||
}*/
|
||||
// find match TemplateBuilder
|
||||
TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(targetType);
|
||||
if(builder != null){
|
||||
tmpl = builder.loadTemplate(targetType);
|
||||
if (tmpl != null) {
|
||||
return tmpl;
|
||||
}
|
||||
|
||||
tmpl = builder.buildTemplate(targetType);
|
||||
register(targetType,tmpl);
|
||||
return tmpl;
|
||||
if (tmpl != null) {
|
||||
register(targetType, tmpl);
|
||||
return tmpl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Class<?> target = (Class<?>)targetType;
|
||||
|
||||
for(Class<?> i : target.getInterfaces()) {
|
||||
@ -212,7 +173,7 @@ public class TemplateRegistry {
|
||||
}
|
||||
return new DefaultTemplate((Class<?>)parameterizedType.getRawType(), parameterizedType);
|
||||
} else {
|
||||
throw new IllegalArgumentException("actual types of the generic type are erased: "+targetType);
|
||||
throw new IllegalArgumentException("Actual types of the generic type are erased: "+targetType);
|
||||
}
|
||||
}
|
||||
|
||||
@ -231,10 +192,5 @@ public class TemplateRegistry {
|
||||
|
||||
return gtmpl.build(tmpls);
|
||||
}
|
||||
|
||||
private static boolean isAnnotated(Class<?> ao, Class<? extends Annotation> with) {
|
||||
return ao.getAnnotation(with) != null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -151,6 +151,7 @@ public class ArrayTemplateBuilder implements TemplateBuilder {
|
||||
return toTemplate(arrayType, baseType, baseClass, dim);
|
||||
|
||||
}
|
||||
|
||||
private Template toTemplate(Type arrayType, Type genericBaseType, Class<?> baseClass, int dim) {
|
||||
if(dim == 1) {
|
||||
if(baseClass == boolean.class) {
|
||||
@ -181,4 +182,13 @@ public class ArrayTemplateBuilder implements TemplateBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTemplate(Type targetType, String directoryName) {
|
||||
throw new UnsupportedOperationException(targetType.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Template loadTemplate(Type targetType) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ public class BeansBuildContext extends BuildContextBase<BeansFieldEntry> {
|
||||
|
||||
protected void setSuperClass() throws CannotCompileException, NotFoundException {
|
||||
this.tmplCtClass.setSuperclass(
|
||||
director.getCtClass(JavassistTemplate.class.getName()));
|
||||
director.getCtClass(JavassistTemplateBuilder.JavassistTemplate.class.getName()));
|
||||
}
|
||||
|
||||
protected void buildConstructor() throws CannotCompileException, NotFoundException {
|
||||
@ -272,4 +272,14 @@ public class BeansBuildContext extends BuildContextBase<BeansFieldEntry> {
|
||||
return getBuiltString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTemplate(Class<?> targetClass, BeansFieldEntry[] entries,
|
||||
Template[] templates, String directoryName) {
|
||||
throw new UnsupportedOperationException(targetClass.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Template loadTemplate(Class<?> targetClass, BeansFieldEntry[] entries, Template[] templates) {
|
||||
throw new UnsupportedOperationException(targetClass.getName());
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@
|
||||
package org.msgpack.template.builder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import org.msgpack.AbstractTemplate;
|
||||
import org.msgpack.MessagePackObject;
|
||||
@ -299,7 +300,6 @@ public class BeansTemplateBuilder extends CustomTemplateBuilder{
|
||||
|
||||
@Override
|
||||
public Template buildTemplate(Class<?> targetClass, IFieldEntry[] entries) {
|
||||
|
||||
ReflectionEntry[] refEntries = new ReflectionEntry[entries.length];
|
||||
for(int i = 0;i < entries.length;i++){
|
||||
BeansFieldEntry e = (BeansFieldEntry)entries[i];
|
||||
@ -323,10 +323,6 @@ public class BeansTemplateBuilder extends CustomTemplateBuilder{
|
||||
refEntries[i] = new ObjectFieldEntry(e, tmpl);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return new BeansReflectionTemplate(targetClass,refEntries);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ public class BuildContext extends BuildContextBase<FieldEntry> {
|
||||
|
||||
protected void setSuperClass() throws CannotCompileException, NotFoundException {
|
||||
this.tmplCtClass.setSuperclass(
|
||||
director.getCtClass(JavassistTemplate.class.getName()));
|
||||
director.getCtClass(JavassistTemplateBuilder.JavassistTemplate.class.getName()));
|
||||
}
|
||||
|
||||
protected void buildConstructor() throws CannotCompileException, NotFoundException {
|
||||
@ -272,4 +272,22 @@ public class BuildContext extends BuildContextBase<FieldEntry> {
|
||||
return getBuiltString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTemplate(Class<?> targetClass, FieldEntry[] entries,
|
||||
Template[] templates, String directoryName) {
|
||||
this.entries = entries;
|
||||
this.templates = templates;
|
||||
this.origClass = targetClass;
|
||||
this.origName = this.origClass.getName();
|
||||
write(this.origName, directoryName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Template loadTemplate(Class<?> targetClass, FieldEntry[] entries, Template[] templates) {
|
||||
this.entries = entries;
|
||||
this.templates = templates;
|
||||
this.origClass = targetClass;
|
||||
this.origName = this.origClass.getName();
|
||||
return load(this.origName);
|
||||
}
|
||||
}
|
@ -42,6 +42,8 @@ public abstract class BuildContextBase<T extends IFieldEntry> {
|
||||
|
||||
protected CtClass tmplCtClass;
|
||||
|
||||
protected abstract Template buildTemplate(Class<?> targetClass, T[] entries, Template[] templates);
|
||||
|
||||
protected abstract void setSuperClass() throws CannotCompileException, NotFoundException;
|
||||
|
||||
protected abstract void buildConstructor() throws CannotCompileException, NotFoundException;
|
||||
@ -54,17 +56,21 @@ public abstract class BuildContextBase<T extends IFieldEntry> {
|
||||
|
||||
protected abstract String buildConvertMethodBody();
|
||||
|
||||
protected abstract Template buildInstance(Class<?> c) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException;
|
||||
protected abstract Template buildInstance(Class<?> c)
|
||||
throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException;
|
||||
|
||||
protected abstract void writeTemplate(Class<?> targetClass, T[] entries,
|
||||
Template[] templates, String directoryName);
|
||||
|
||||
protected abstract Template loadTemplate(Class<?> targetClass, T[] entries, Template[] templates);
|
||||
|
||||
public BuildContextBase(JavassistTemplateBuilder director) {
|
||||
this.director = director;
|
||||
}
|
||||
|
||||
public abstract Template buildTemplate(Class<?> targetClass, T[] entries, Template[] templates);
|
||||
|
||||
protected Template build(final String className) {
|
||||
try {
|
||||
reset(className);
|
||||
reset(className, false);
|
||||
buildClass();
|
||||
buildConstructor();
|
||||
buildMethodInit();
|
||||
@ -76,15 +82,20 @@ public abstract class BuildContextBase<T extends IFieldEntry> {
|
||||
String code = getBuiltString();
|
||||
if(code != null) {
|
||||
LOG.error("builder: " + code, e);
|
||||
throw new TemplateBuildException("cannot compile: " + code, e);
|
||||
throw new TemplateBuildException("Cannot compile: " + code, e);
|
||||
} else {
|
||||
throw new TemplateBuildException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void reset(String className) {
|
||||
tmplName = className + "_$$_Template" + director.nextSeqId();
|
||||
protected void reset(String className, boolean isWritten) {
|
||||
String tmplName = null;
|
||||
if (!isWritten) {
|
||||
tmplName = className + "_$$_Template" + director.nextSeqId();
|
||||
} else {
|
||||
tmplName = className + "_$$_Template";
|
||||
}
|
||||
tmplCtClass = director.makeCtClass(tmplName);
|
||||
}
|
||||
|
||||
@ -151,6 +162,10 @@ public abstract class BuildContextBase<T extends IFieldEntry> {
|
||||
return (Class<?>) tmplCtClass.toClass(null, null);
|
||||
}
|
||||
|
||||
protected void saveClass(final String directoryName) throws CannotCompileException, IOException {
|
||||
tmplCtClass.writeFile(directoryName);
|
||||
}
|
||||
|
||||
protected StringBuilder stringBuilder = null;
|
||||
|
||||
protected void resetStringBuilder() {
|
||||
@ -171,7 +186,7 @@ public abstract class BuildContextBase<T extends IFieldEntry> {
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
|
||||
protected String primitivePackName(Class<?> type) {
|
||||
if(type == boolean.class) {
|
||||
return "packBoolean";
|
||||
@ -228,4 +243,43 @@ public abstract class BuildContextBase<T extends IFieldEntry> {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void write(final String className, final String directoryName) {
|
||||
try {
|
||||
reset(className, true);
|
||||
buildClass();
|
||||
buildConstructor();
|
||||
buildMethodInit();
|
||||
buildPackMethod();
|
||||
buildUnpackMethod();
|
||||
buildConvertMethod();
|
||||
saveClass(directoryName);
|
||||
} catch (Exception e) {
|
||||
String code = getBuiltString();
|
||||
if(code != null) {
|
||||
LOG.error("builder: " + code, e);
|
||||
throw new TemplateBuildException("Cannot compile: " + code, e);
|
||||
} else {
|
||||
throw new TemplateBuildException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected Template load(final String className) {
|
||||
String tmplName = className + "_$$_Template";
|
||||
try {
|
||||
Class<?> tmplClass = getClass().getClassLoader().loadClass(tmplName);
|
||||
return buildInstance(tmplClass);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
String code = getBuiltString();
|
||||
if(code != null) {
|
||||
LOG.error("builder: " + code, e);
|
||||
throw new TemplateBuildException("Cannot compile: " + code, e);
|
||||
} else {
|
||||
throw new TemplateBuildException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -31,12 +31,13 @@ public abstract class CustomTemplateBuilder implements TemplateBuilder {
|
||||
public abstract IFieldEntryReader getFieldEntryReader();
|
||||
|
||||
public abstract Template buildTemplate(Class<?> targetClass , IFieldEntry[] entries);
|
||||
|
||||
public Template buildTemplate(Class<?> targetClass ,FieldOption implicitOption ){
|
||||
|
||||
public Template buildTemplate(Class<?> targetClass, FieldOption implicitOption ){
|
||||
checkValidation(targetClass);
|
||||
return buildTemplate(targetClass,
|
||||
getFieldEntryReader().readFieldEntries(targetClass, implicitOption));
|
||||
}
|
||||
|
||||
public Template buildTemplate(Class<?> targetClass, FieldList flist) throws NoSuchFieldException {
|
||||
checkValidation(targetClass);
|
||||
return buildTemplate(targetClass, getFieldEntryReader().convertFieldEntries(targetClass, flist));
|
||||
@ -48,20 +49,29 @@ public abstract class CustomTemplateBuilder implements TemplateBuilder {
|
||||
IFieldEntryReader reader = getFieldEntryReader();
|
||||
FieldOption implicitOption = reader.readImplicitFieldOption(targetClass);
|
||||
checkValidation(targetClass);
|
||||
|
||||
IFieldEntry[] entries = reader.readFieldEntries(targetClass, implicitOption);
|
||||
|
||||
return buildTemplate(targetClass,entries);
|
||||
return buildTemplate(targetClass, entries);
|
||||
}
|
||||
private void checkValidation(Class<?> targetClass) {
|
||||
|
||||
protected void checkValidation(Class<?> targetClass) {
|
||||
if(targetClass.isInterface()) {
|
||||
throw new TemplateBuildException("cannot build template of interface");
|
||||
throw new TemplateBuildException("Cannot build template of interface");
|
||||
}
|
||||
if(targetClass.isArray()) {
|
||||
throw new TemplateBuildException("cannot build template of array class");
|
||||
throw new TemplateBuildException("Cannot build template of array class");
|
||||
}
|
||||
if(targetClass.isPrimitive()) {
|
||||
throw new TemplateBuildException("cannot build template of primitive type");
|
||||
throw new TemplateBuildException("Cannot build template of primitive type");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTemplate(Type targetType, String directoryName) {
|
||||
throw new UnsupportedOperationException(targetType.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Template loadTemplate(Type targetType) {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
//
|
||||
// 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.template.builder;
|
||||
|
||||
import org.msgpack.AbstractTemplate;
|
||||
import org.msgpack.Template;
|
||||
|
||||
public abstract class JavassistTemplate extends AbstractTemplate {
|
||||
public Class<?> targetClass;
|
||||
public Template[] templates;
|
||||
|
||||
public JavassistTemplate(Class<?> targetClass, Template[] templates) {
|
||||
this.targetClass = targetClass;
|
||||
this.templates = templates;
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@
|
||||
package org.msgpack.template.builder;
|
||||
|
||||
import java.lang.Thread;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import org.msgpack.*;
|
||||
|
||||
@ -29,11 +30,22 @@ import javassist.NotFoundException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.msgpack.template.FieldEntryReader;
|
||||
import org.msgpack.template.FieldOption;
|
||||
import org.msgpack.template.IFieldEntry;
|
||||
import org.msgpack.template.IFieldEntryReader;
|
||||
import org.msgpack.template.TemplateRegistry;
|
||||
|
||||
public class JavassistTemplateBuilder extends CustomTemplateBuilder {
|
||||
public static abstract class JavassistTemplate extends AbstractTemplate {
|
||||
public Class<?> targetClass;
|
||||
public Template[] templates;
|
||||
|
||||
public JavassistTemplate(Class<?> targetClass, Template[] templates) {
|
||||
this.targetClass = targetClass;
|
||||
this.templates = templates;
|
||||
}
|
||||
}
|
||||
|
||||
private static Logger LOG = LoggerFactory.getLogger(JavassistTemplateBuilder.class);
|
||||
|
||||
private static JavassistTemplateBuilder instance;
|
||||
@ -49,27 +61,24 @@ public class JavassistTemplateBuilder extends CustomTemplateBuilder {
|
||||
getInstance().pool.appendClassPath(new LoaderClassPath(cl));
|
||||
}
|
||||
|
||||
|
||||
IFieldEntryReader reader = new FieldEntryReader();
|
||||
|
||||
|
||||
public void setFieldEntryReader(IFieldEntryReader reader){
|
||||
this.reader = reader;
|
||||
}
|
||||
|
||||
|
||||
BuildContextFactory buildContextFactory = new BuildContextFactory() {
|
||||
|
||||
@Override
|
||||
public BuildContextBase createBuildContext(JavassistTemplateBuilder builder) {
|
||||
|
||||
return new BuildContext(builder);
|
||||
}
|
||||
};
|
||||
|
||||
public void setBuildContextFactory(BuildContextFactory factory){
|
||||
this.buildContextFactory = factory;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public JavassistTemplateBuilder() {
|
||||
pool = new ClassPool();
|
||||
boolean appended = false;
|
||||
@ -96,6 +105,7 @@ public class JavassistTemplateBuilder extends CustomTemplateBuilder {
|
||||
pool.appendSystemPath();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace FieldEntryReader and BuilderContextFactory.
|
||||
* you can replace field entry rules and generated codes easily.
|
||||
@ -108,7 +118,6 @@ public class JavassistTemplateBuilder extends CustomTemplateBuilder {
|
||||
this.buildContextFactory = buildContextFactory;
|
||||
}
|
||||
|
||||
|
||||
protected ClassPool pool;
|
||||
|
||||
private int seqId = 0;
|
||||
@ -125,7 +134,6 @@ public class JavassistTemplateBuilder extends CustomTemplateBuilder {
|
||||
return seqId++;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Template buildTemplate(Class<?> targetClass, IFieldEntry[] entries) {
|
||||
// FIXME private / packagefields
|
||||
@ -136,10 +144,15 @@ public class JavassistTemplateBuilder extends CustomTemplateBuilder {
|
||||
// f.setAccessible(true);
|
||||
// }
|
||||
//}
|
||||
Template[] tmpls = toTemplate(entries);
|
||||
BuildContextBase bc = getBuildContextFacotry().createBuildContext(this);
|
||||
return bc.buildTemplate(targetClass, entries, tmpls);
|
||||
}
|
||||
|
||||
Template[] tmpls = new Template[entries.length];
|
||||
for(int i=0; i < entries.length; i++) {
|
||||
IFieldEntry e = entries[i];
|
||||
private static Template[] toTemplate(IFieldEntry[] from) {
|
||||
Template[] tmpls = new Template[from.length];
|
||||
for(int i=0; i < from.length; i++) {
|
||||
IFieldEntry e = from[i];
|
||||
if(!e.isAvailable()) {
|
||||
tmpls[i] = null;
|
||||
} else {
|
||||
@ -147,9 +160,7 @@ public class JavassistTemplateBuilder extends CustomTemplateBuilder {
|
||||
tmpls[i] = tmpl;
|
||||
}
|
||||
}
|
||||
|
||||
BuildContextBase bc = getBuildContextFacotry().createBuildContext(this);
|
||||
return bc.buildTemplate(targetClass, entries, tmpls);
|
||||
return tmpls;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -161,60 +172,36 @@ public class JavassistTemplateBuilder extends CustomTemplateBuilder {
|
||||
return buildContextFactory;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
static class JavassistOrdinalEnumTemplate extends ReflectionTemplateBuilder.ReflectionOrdinalEnumTemplate {
|
||||
JavassistOrdinalEnumTemplate(Enum<?>[] entries) {
|
||||
super(entries);
|
||||
}
|
||||
@Override
|
||||
public void writeTemplate(Type targetType, String directoryName) {
|
||||
Class<?> targetClass = (Class<?>)targetType;
|
||||
IFieldEntryReader reader = getFieldEntryReader();
|
||||
FieldOption implicitOption = reader.readImplicitFieldOption(targetClass);
|
||||
checkValidation(targetClass);
|
||||
IFieldEntry[] entries = reader.readFieldEntries(targetClass, implicitOption);
|
||||
writeTemplate(targetClass, entries, directoryName);
|
||||
}
|
||||
|
||||
private void writeTemplate(Class<?> targetClass, IFieldEntry[] entries, String directoryName) {
|
||||
Template[] tmpls = toTemplate(entries);
|
||||
BuildContextBase bc = getBuildContextFacotry().createBuildContext(this);
|
||||
bc.writeTemplate(targetClass, entries, tmpls, directoryName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeOrdinalEnumTemplateClass(Class<?> targetClass, Enum<?>[] entires, String directoryName) {
|
||||
throw new UnsupportedOperationException("not supported yet.");// TODO
|
||||
public Template loadTemplate(Type targetType) {
|
||||
Class<?> targetClass = (Class<?>)targetType;
|
||||
IFieldEntryReader reader = getFieldEntryReader();
|
||||
FieldOption implicitOption = reader.readImplicitFieldOption(targetClass);
|
||||
checkValidation(targetClass);
|
||||
IFieldEntry[] entries = reader.readFieldEntries(targetClass, implicitOption);
|
||||
return loadTemplate(targetClass, entries);
|
||||
}
|
||||
|
||||
public Template buildOrdinalEnumTemplate(Class<?> targetClass, Enum<?>[] entries) {
|
||||
return new JavassistOrdinalEnumTemplate(entries);
|
||||
private Template loadTemplate(Class<?> targetClass, IFieldEntry[] entries) {
|
||||
Template[] tmpls = toTemplate(entries);
|
||||
BuildContextBase bc = getBuildContextFacotry().createBuildContext(this);
|
||||
return bc.loadTemplate(targetClass, entries, tmpls);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeArrayTemplateClass(Type arrayType, Type genericBaseType,
|
||||
Class<?> baseClass, int dim, String directoryName) {
|
||||
throw new UnsupportedOperationException("not supported yet.");//TODO
|
||||
}
|
||||
|
||||
public Template buildArrayTemplate(Type arrayType, Type genericBaseType, Class<?> baseClass, int dim) {
|
||||
if(dim == 1) {
|
||||
if(baseClass == boolean.class) {
|
||||
return BooleanArrayTemplate.getInstance();
|
||||
} else if(baseClass == short.class) {
|
||||
return ShortArrayTemplate.getInstance();
|
||||
} else if(baseClass == int.class) {
|
||||
return IntArrayTemplate.getInstance();
|
||||
} else if(baseClass == long.class) {
|
||||
return LongArrayTemplate.getInstance();
|
||||
} else if(baseClass == float.class) {
|
||||
return FloatArrayTemplate.getInstance();
|
||||
} else if(baseClass == double.class) {
|
||||
return DoubleArrayTemplate.getInstance();
|
||||
} else {
|
||||
// FIXME
|
||||
Template baseTemplate = TemplateRegistry.lookup(genericBaseType);
|
||||
return new ReflectionTemplateBuilder.ReflectionObjectArrayTemplate(baseClass, baseTemplate);
|
||||
}
|
||||
} else if(dim == 2) {
|
||||
// FIXME
|
||||
Class<?> componentClass = Array.newInstance(baseClass, 0).getClass();
|
||||
Template componentTemplate = buildArrayTemplate(arrayType, genericBaseType, baseClass, dim-1);
|
||||
return new ReflectionTemplateBuilder.ReflectionMultidimentionalArrayTemplate(componentClass, componentTemplate);
|
||||
} else {
|
||||
// FIXME
|
||||
ReflectionTemplateBuilder.ReflectionMultidimentionalArrayTemplate componentTemplate = (ReflectionTemplateBuilder.ReflectionMultidimentionalArrayTemplate)
|
||||
buildArrayTemplate(arrayType, genericBaseType, baseClass, dim-1);
|
||||
Class<?> componentClass = Array.newInstance(componentTemplate.getComponentClass(), 0).getClass();
|
||||
return new ReflectionTemplateBuilder.ReflectionMultidimentionalArrayTemplate(componentClass, componentTemplate);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
|
@ -77,10 +77,20 @@ public class OrdinalEnumTemplateBuilder implements TemplateBuilder{
|
||||
|
||||
return new ReflectionOrdinalEnumTemplate(entries);
|
||||
}
|
||||
|
||||
private void checkOrdinalEnumValidation(Class<?> targetClass) {
|
||||
if(!targetClass.isEnum()) {
|
||||
throw new TemplateBuildException("tried to build ordinal enum template of non-enum class");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTemplate(Type targetType, String directoryName) {
|
||||
throw new UnsupportedOperationException(targetType.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Template loadTemplate(Type targetType) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||
|
||||
public class OrdinalEnumTemplateBuilderSelector implements BuilderSelector {
|
||||
|
||||
public static final String NAME = "OrdinalEnumBuilder";
|
||||
public static final String NAME = "OrdinalEnumTemplateBuilder";
|
||||
|
||||
OrdinalEnumTemplateBuilder builder = new OrdinalEnumTemplateBuilder();
|
||||
|
||||
|
@ -21,7 +21,12 @@ import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import org.msgpack.*;
|
||||
import org.msgpack.AbstractTemplate;
|
||||
import org.msgpack.MessagePackObject;
|
||||
import org.msgpack.MessageTypeException;
|
||||
import org.msgpack.Packer;
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
import org.msgpack.template.FieldEntry;
|
||||
import org.msgpack.template.FieldEntryReader;
|
||||
import org.msgpack.template.IFieldEntry;
|
||||
@ -412,7 +417,6 @@ public class ReflectionTemplateBuilder extends CustomTemplateBuilder {
|
||||
res[i] = new ObjectFieldEntry(e, tmpl);
|
||||
}
|
||||
}
|
||||
|
||||
return new ReflectionTemplate(targetClass, res);
|
||||
}
|
||||
}
|
||||
|
@ -23,5 +23,9 @@ import org.msgpack.Template;
|
||||
|
||||
public interface TemplateBuilder {
|
||||
Template buildTemplate(Type targetType);
|
||||
|
||||
void writeTemplate(Type targetType, String directoryName);
|
||||
|
||||
Template loadTemplate(Type targetType);
|
||||
}
|
||||
|
||||
|
@ -17,56 +17,58 @@
|
||||
//
|
||||
package org.msgpack.util;
|
||||
|
||||
import org.msgpack.template.TemplateBuildException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.msgpack.template.builder.BuilderSelectorRegistry;
|
||||
import org.msgpack.template.builder.TemplateBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class TemplatePrecompiler {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(TemplatePrecompiler.class);
|
||||
|
||||
public static void write(Class<?> target, String directoryName) {
|
||||
if (target.isEnum()) {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
} else {
|
||||
//TemplateBuilder.writeClass(target, directoryName);
|
||||
}
|
||||
LOG.info("finished writing .class file of template class for " + target.getName());
|
||||
}
|
||||
//private static final String SRC = "msgpack.template.srcdir";
|
||||
|
||||
private String directoryName;
|
||||
private static final String DIST = "msgpack.template.distdir";
|
||||
|
||||
private String[] classNames;
|
||||
//private static final String DEFAULT_SRC = ".";
|
||||
|
||||
private static final String DEFAULT_DIST = ".";
|
||||
|
||||
private TemplatePrecompiler() {
|
||||
}
|
||||
|
||||
private void parseOpts(final String[] args) {// TODO
|
||||
if (args.length == 0) {
|
||||
usage();
|
||||
}
|
||||
public void saveTemplates(final String[] classFileNames) throws IOException {
|
||||
throw new UnsupportedOperationException("Not supported yet.");// TODO
|
||||
}
|
||||
|
||||
private void usage() {// TODO
|
||||
System.err.println("java org.msgpack.template.TemplateClassWriter ");
|
||||
System.err.println("");
|
||||
public void saveTemplateClass(Class<?> targetClass) throws IOException {
|
||||
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);
|
||||
}
|
||||
LOG.info("Saved .class file of template class of " + targetClass.getName());
|
||||
}
|
||||
|
||||
private void writeTemplateClasses() {
|
||||
ClassLoader cl = this.getClass().getClassLoader();// TODO
|
||||
for (String className : classNames) {
|
||||
Class<?> origClass = null;
|
||||
try {
|
||||
origClass = cl.loadClass(className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new TemplateBuildException(e);
|
||||
}
|
||||
write(origClass, directoryName);
|
||||
private 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 writer = new TemplatePrecompiler();
|
||||
writer.parseOpts(args);
|
||||
writer.writeTemplateClasses();
|
||||
TemplatePrecompiler compiler = new TemplatePrecompiler();// TODO
|
||||
compiler.saveTemplates(args);
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,8 @@ import java.lang.Class
|
||||
import collection.immutable.{ListMap, TreeMap}
|
||||
import java.lang.reflect.{Type, Modifier, Method, Field}
|
||||
import java.lang.annotation.{Annotation => JavaAnnotation}
|
||||
import builder.{JavassistTemplateBuilder, JavassistTemplate, BuildContextBase, BuildContext}
|
||||
import builder.{JavassistTemplateBuilder, BuildContextBase, BuildContext}
|
||||
import builder.JavassistTemplateBuilder.JavassistTemplate
|
||||
import scala.collection.JavaConverters._
|
||||
;
|
||||
/*
|
||||
@ -26,6 +27,23 @@ import scala.collection.JavaConverters._
|
||||
var templates : Array[Template] = null
|
||||
var minimumArrayLength : Int = 0
|
||||
|
||||
def writeTemplate(targetClass : Class[_] , entries : Array[IFieldEntry],
|
||||
templates : Array[Template], directoryName : String) = {
|
||||
this.entries = entries;
|
||||
this.templates = templates;
|
||||
this.origClass = targetClass;
|
||||
this.origName = this.origClass.getName();
|
||||
write(this.origName, directoryName);
|
||||
}
|
||||
|
||||
def loadTemplate(targetClass : Class[_] , entries : Array[IFieldEntry], templates : Array[Template]) = {
|
||||
this.entries = entries;
|
||||
this.templates = templates;
|
||||
this.origClass = targetClass;
|
||||
this.origName = this.origClass.getName();
|
||||
load(this.origName);
|
||||
}
|
||||
|
||||
def buildTemplate(targetClass : Class[_] , entries : Array[IFieldEntry], templates : Array[Template]) = {
|
||||
this.entries = entries;
|
||||
this.templates = templates;
|
||||
|
Loading…
x
Reference in New Issue
Block a user