java: refactor DynamicCodeGen.java

This commit is contained in:
Muga Nishizawa 2010-10-07 15:27:11 +09:00
parent 98eec72522
commit 4bbfb3f9a3
3 changed files with 88 additions and 100 deletions

View File

@ -88,6 +88,20 @@ public interface BasicConstants {
String METHOD_NAME_VALUEOF = "valueOf"; String METHOD_NAME_VALUEOF = "valueOf";
String METHOD_NAME_BOOLEANVALUE = "booleanValue";
String METHOD_NAME_BYTEVALUE = "byteValue";
String METHOD_NAME_SHORTVALUE = "shortValue";
String METHOD_NAME_INTVALUE = "intValue";
String METHOD_NAME_FLOATVALUE = "floatValue";
String METHOD_NAME_LONGVALUE = "longValue";
String METHOD_NAME_DOUBLEVALUE = "doubleValue";
String METHOD_NAME_ADD = "add"; String METHOD_NAME_ADD = "add";
String METHOD_NAME_PUT = "put"; String METHOD_NAME_PUT = "put";

View File

@ -254,18 +254,18 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
return allFields.toArray(new Field[0]); return allFields.toArray(new Field[0]);
} }
private void checkFieldValidation(Field f, List<Field> fs) { private void checkFieldValidation(Field field, List<Field> fields) {
// check that it has a public modifier // check that it has a public modifier
int mod = f.getModifiers(); int mod = field.getModifiers();
if ((!(Modifier.isPublic(mod))) || Modifier.isStatic(mod) if ((!(Modifier.isPublic(mod))) || Modifier.isStatic(mod)
|| Modifier.isFinal(mod) || Modifier.isTransient(mod) || Modifier.isFinal(mod) || Modifier.isTransient(mod)
|| f.isSynthetic()) { || field.isSynthetic()) {
throwFieldValidationException(f); throwFieldValidationException(field);
} }
// check same name // check same name
for (Field f0 : fs) { for (Field f : fields) {
if (f0.getName().equals(f.getName())) { if (f.getName().equals(field.getName())) {
throwFieldValidationException(f); throwFieldValidationException(field);
} }
} }
} }
@ -486,39 +486,10 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
String castType = null; String castType = null;
String rawValueGetter = null; String rawValueGetter = null;
if (type.isPrimitive()) { if (type.isPrimitive()) {
if (type.equals(byte.class)) { castType = "(" + primitiveTypeToWrapperType(type).getName() + ")";
castType = "(Byte)"; rawValueGetter = getPrimTypeValueMethodName(type);
rawValueGetter = "byteValue";
} else if (type.equals(boolean.class)) {
castType = "(Boolean)";
rawValueGetter = "booleanValue";
} else if (type.equals(short.class)) {
castType = "(Short)";
rawValueGetter = "shortValue";
} else if (type.equals(int.class)) {
castType = "(Integer)";
rawValueGetter = "intValue";
} else if (type.equals(long.class)) {
castType = "(Long)";
rawValueGetter = "longValue";
} else if (type.equals(float.class)) {
castType = "(Float)";
rawValueGetter = "floatValue";
} else if (type.equals(double.class)) {
castType = "(Double)";
rawValueGetter = "doubleValue";
} else {
throw new DynamicCodeGenException("Fatal error: "
+ type.getName());
}
} else if (type.isArray()) { } else if (type.isArray()) {
Class<?> ct = type.getComponentType(); castType = "(" + arrayTypeToString(type) + ")";
if (ct.equals(byte.class)) {
castType = "(byte[])";
} else {
throw new UnsupportedOperationException("Not supported yet: "
+ type.getName());
}
} else { } else {
castType = "(" + type.getName() + ")"; castType = "(" + type.getName() + ")";
} }
@ -696,39 +667,10 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
String castType = null; String castType = null;
String rawValueGetter = null; String rawValueGetter = null;
if (type.isPrimitive()) { if (type.isPrimitive()) {
if (type.equals(byte.class)) { castType = "(" + primitiveTypeToWrapperType(type).getName() + ")";
castType = "(Byte)"; rawValueGetter = getPrimTypeValueMethodName(type);
rawValueGetter = "byteValue";
} else if (type.equals(boolean.class)) {
castType = "(Boolean)";
rawValueGetter = "booleanValue";
} else if (type.equals(short.class)) {
castType = "(Short)";
rawValueGetter = "shortValue";
} else if (type.equals(int.class)) {
castType = "(Integer)";
rawValueGetter = "intValue";
} else if (type.equals(long.class)) {
castType = "(Long)";
rawValueGetter = "longValue";
} else if (type.equals(float.class)) {
castType = "(Float)";
rawValueGetter = "floatValue";
} else if (type.equals(double.class)) {
castType = "(Double)";
rawValueGetter = "doubleValue";
} else {
throw new DynamicCodeGenException("Fatal error: "
+ type.getName());
}
} else if (type.isArray()) { } else if (type.isArray()) {
Class<?> ct = type.getComponentType(); castType = "(" + arrayTypeToString(type) + ")";
if (ct.equals(byte.class)) {
castType = "(byte[])";
} else {
throw new UnsupportedOperationException("Not supported yet: "
+ type.getName());
}
} else { } else {
castType = "(" + type.getName() + ")"; castType = "(" + type.getName() + ")";
} }

View File

@ -9,6 +9,7 @@ import java.util.Map;
import org.msgpack.CustomConverter; import org.msgpack.CustomConverter;
import org.msgpack.CustomMessage; import org.msgpack.CustomMessage;
import org.msgpack.MessageTypeException;
import org.msgpack.Template; import org.msgpack.Template;
import org.msgpack.Templates; import org.msgpack.Templates;
import org.msgpack.annotation.MessagePackDelegate; import org.msgpack.annotation.MessagePackDelegate;
@ -21,10 +22,10 @@ public class DynamicCodeGenBase implements BasicConstants {
public static interface TemplateAccessor { public static interface TemplateAccessor {
void setTemplates(Template[] templates); void setTemplates(Template[] templates);
} }
public static class TemplateAccessorImpl implements TemplateAccessor { public static class TemplateAccessorImpl implements TemplateAccessor {
public Template[] _$$_templates; public Template[] _$$_templates;
public void setTemplates(Template[] _$$_tmpls) { public void setTemplates(Template[] _$$_tmpls) {
_$$_templates = _$$_tmpls; _$$_templates = _$$_tmpls;
} }
@ -122,19 +123,17 @@ public class DynamicCodeGenBase implements BasicConstants {
public void insertLocalVariableDecl(StringBuilder sb, Class<?> type, public void insertLocalVariableDecl(StringBuilder sb, Class<?> type,
String name, int dim) { String name, int dim) {
// int[] lv // int[] lv
if (type.equals(byte[].class)) { int dim0 = dim + getArrayDim(type);
sb.append("byte[]"); Class<?> type0 = getArrayBaseType(type);
} else { sb.append(type0.getName());
sb.append(type.getName()); for (int i = 0; i < dim0; ++i) {
}
for (int i = 0; i < dim; ++i) {
sb.append(CHAR_NAME_LEFT_SQUARE_BRACKET); sb.append(CHAR_NAME_LEFT_SQUARE_BRACKET);
sb.append(CHAR_NAME_RIGHT_SQUARE_BRACKET); sb.append(CHAR_NAME_RIGHT_SQUARE_BRACKET);
} }
sb.append(CHAR_NAME_SPACE); sb.append(CHAR_NAME_SPACE);
sb.append(name); sb.append(name);
} }
static int getArrayDim(Class<?> type) { static int getArrayDim(Class<?> type) {
if (type.isArray()) { if (type.isArray()) {
return 1 + getArrayDim(type.getComponentType()); return 1 + getArrayDim(type.getComponentType());
@ -150,6 +149,18 @@ public class DynamicCodeGenBase implements BasicConstants {
return type; return type;
} }
} }
public String arrayTypeToString(Class<?> type) {
StringBuilder sb = new StringBuilder();
int dim = getArrayDim(type);
Class<?> t = getArrayBaseType(type);
sb.append(t.getName());
for (int i = 0; i < dim; ++i) {
sb.append(CHAR_NAME_LEFT_SQUARE_BRACKET);
sb.append(CHAR_NAME_RIGHT_SQUARE_BRACKET);
}
return sb.toString();
}
public void insertValueInsertion(StringBuilder sb, String expr) { public void insertValueInsertion(StringBuilder sb, String expr) {
// = expr // = expr
@ -235,25 +246,7 @@ public class DynamicCodeGenBase implements BasicConstants {
public void insertTypeConvToObjectType(StringBuilder sb, Class<?> type, public void insertTypeConvToObjectType(StringBuilder sb, Class<?> type,
String expr) throws DynamicCodeGenException { String expr) throws DynamicCodeGenException {
if (type.isPrimitive()) { // primitive type if (type.isPrimitive()) { // primitive type
if (type.equals(boolean.class)) { insertConsCall(sb, primitiveTypeToWrapperType(type), expr);
// new Boolean(expr)
insertConsCall(sb, Boolean.class, expr);
} else if (type.equals(byte.class)) {
insertConsCall(sb, Byte.class, expr);
} else if (type.equals(short.class)) {
insertConsCall(sb, Short.class, expr);
} else if (type.equals(int.class)) {
insertConsCall(sb, Integer.class, expr);
} else if (type.equals(long.class)) {
insertConsCall(sb, Long.class, expr);
} else if (type.equals(float.class)) {
insertConsCall(sb, Float.class, expr);
} else if (type.equals(double.class)) {
insertConsCall(sb, Double.class, expr);
} else {
throw new DynamicCodeGenException("Type error: "
+ type.getName());
}
} else { // reference type } else { // reference type
sb.append(expr); sb.append(expr);
} }
@ -285,7 +278,46 @@ public class DynamicCodeGenBase implements BasicConstants {
sb.append(CHAR_NAME_SPACE); sb.append(CHAR_NAME_SPACE);
} }
} }
public Class<?> primitiveTypeToWrapperType(Class<?> type) {
if (type.equals(boolean.class)) {
return Boolean.class;
} else if (type.equals(byte.class)) {
return Byte.class;
} else if (type.equals(short.class)) {
return Short.class;
} else if (type.equals(int.class)) {
return Integer.class;
} else if (type.equals(long.class)) {
return Long.class;
} else if (type.equals(float.class)) {
return Float.class;
} else if (type.equals(double.class)) {
return Double.class;
} else {
throw new MessageTypeException("Type error: " + type.getName());
}
}
public String getPrimTypeValueMethodName(Class<?> type) {
if (type.equals(boolean.class)) {
return METHOD_NAME_BOOLEANVALUE;
} else if (type.equals(byte.class)) {
return METHOD_NAME_BYTEVALUE;
} else if (type.equals(short.class)) {
return METHOD_NAME_SHORTVALUE;
} else if (type.equals(int.class)) {
return METHOD_NAME_INTVALUE;
} else if (type.equals(long.class)) {
return METHOD_NAME_LONGVALUE;
} else if (type.equals(float.class)) {
return METHOD_NAME_FLOATVALUE;
} else if (type.equals(double.class)) {
return METHOD_NAME_DOUBLEVALUE;
} else {
throw new MessageTypeException("Type error: " + type.getName());
}
}
public String getUnpackMethodName(Class<?> c) public String getUnpackMethodName(Class<?> c)
throws DynamicCodeGenException { throws DynamicCodeGenException {
if (c.equals(boolean.class) || c.equals(Boolean.class)) { if (c.equals(boolean.class) || c.equals(Boolean.class)) {