summaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
authorMuga Nishizawa <muga@f11vm.(none)>2010-10-07 15:27:11 +0900
committerMuga Nishizawa <muga@f11vm.(none)>2010-10-07 15:27:11 +0900
commit4bbfb3f9a398e569a560034e8d8eb58f7fb22c7f (patch)
treefd12c85a1279c2514792ff88e6a893eba5ccb26e /java
parent98eec72522d39d7e5e3757205bc6de256c4cf14e (diff)
downloadmsgpack-python-4bbfb3f9a398e569a560034e8d8eb58f7fb22c7f.tar.gz
java: refactor DynamicCodeGen.java
Diffstat (limited to 'java')
-rw-r--r--java/src/main/java/org/msgpack/util/codegen/BasicConstants.java14
-rw-r--r--java/src/main/java/org/msgpack/util/codegen/DynamicCodeGen.java84
-rw-r--r--java/src/main/java/org/msgpack/util/codegen/DynamicCodeGenBase.java90
3 files changed, 88 insertions, 100 deletions
diff --git a/java/src/main/java/org/msgpack/util/codegen/BasicConstants.java b/java/src/main/java/org/msgpack/util/codegen/BasicConstants.java
index 8448c02..f169bcd 100644
--- a/java/src/main/java/org/msgpack/util/codegen/BasicConstants.java
+++ b/java/src/main/java/org/msgpack/util/codegen/BasicConstants.java
@@ -88,6 +88,20 @@ public interface BasicConstants {
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_PUT = "put";
diff --git a/java/src/main/java/org/msgpack/util/codegen/DynamicCodeGen.java b/java/src/main/java/org/msgpack/util/codegen/DynamicCodeGen.java
index 1b4cb91..909bef6 100644
--- a/java/src/main/java/org/msgpack/util/codegen/DynamicCodeGen.java
+++ b/java/src/main/java/org/msgpack/util/codegen/DynamicCodeGen.java
@@ -254,18 +254,18 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
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
- int mod = f.getModifiers();
+ int mod = field.getModifiers();
if ((!(Modifier.isPublic(mod))) || Modifier.isStatic(mod)
|| Modifier.isFinal(mod) || Modifier.isTransient(mod)
- || f.isSynthetic()) {
- throwFieldValidationException(f);
+ || field.isSynthetic()) {
+ throwFieldValidationException(field);
}
// check same name
- for (Field f0 : fs) {
- if (f0.getName().equals(f.getName())) {
- throwFieldValidationException(f);
+ for (Field f : fields) {
+ if (f.getName().equals(field.getName())) {
+ throwFieldValidationException(field);
}
}
}
@@ -486,39 +486,10 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
String castType = null;
String rawValueGetter = null;
if (type.isPrimitive()) {
- if (type.equals(byte.class)) {
- castType = "(Byte)";
- 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());
- }
+ castType = "(" + primitiveTypeToWrapperType(type).getName() + ")";
+ rawValueGetter = getPrimTypeValueMethodName(type);
} else if (type.isArray()) {
- Class<?> ct = type.getComponentType();
- if (ct.equals(byte.class)) {
- castType = "(byte[])";
- } else {
- throw new UnsupportedOperationException("Not supported yet: "
- + type.getName());
- }
+ castType = "(" + arrayTypeToString(type) + ")";
} else {
castType = "(" + type.getName() + ")";
}
@@ -696,39 +667,10 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
String castType = null;
String rawValueGetter = null;
if (type.isPrimitive()) {
- if (type.equals(byte.class)) {
- castType = "(Byte)";
- 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());
- }
+ castType = "(" + primitiveTypeToWrapperType(type).getName() + ")";
+ rawValueGetter = getPrimTypeValueMethodName(type);
} else if (type.isArray()) {
- Class<?> ct = type.getComponentType();
- if (ct.equals(byte.class)) {
- castType = "(byte[])";
- } else {
- throw new UnsupportedOperationException("Not supported yet: "
- + type.getName());
- }
+ castType = "(" + arrayTypeToString(type) + ")";
} else {
castType = "(" + type.getName() + ")";
}
diff --git a/java/src/main/java/org/msgpack/util/codegen/DynamicCodeGenBase.java b/java/src/main/java/org/msgpack/util/codegen/DynamicCodeGenBase.java
index 533a8bc..af7c909 100644
--- a/java/src/main/java/org/msgpack/util/codegen/DynamicCodeGenBase.java
+++ b/java/src/main/java/org/msgpack/util/codegen/DynamicCodeGenBase.java
@@ -9,6 +9,7 @@ import java.util.Map;
import org.msgpack.CustomConverter;
import org.msgpack.CustomMessage;
+import org.msgpack.MessageTypeException;
import org.msgpack.Template;
import org.msgpack.Templates;
import org.msgpack.annotation.MessagePackDelegate;
@@ -21,10 +22,10 @@ public class DynamicCodeGenBase implements BasicConstants {
public static interface TemplateAccessor {
void setTemplates(Template[] templates);
}
-
+
public static class TemplateAccessorImpl implements TemplateAccessor {
public Template[] _$$_templates;
-
+
public void setTemplates(Template[] _$$_tmpls) {
_$$_templates = _$$_tmpls;
}
@@ -122,19 +123,17 @@ public class DynamicCodeGenBase implements BasicConstants {
public void insertLocalVariableDecl(StringBuilder sb, Class<?> type,
String name, int dim) {
// int[] lv
- if (type.equals(byte[].class)) {
- sb.append("byte[]");
- } else {
- sb.append(type.getName());
- }
- for (int i = 0; i < dim; ++i) {
+ int dim0 = dim + getArrayDim(type);
+ Class<?> type0 = getArrayBaseType(type);
+ sb.append(type0.getName());
+ for (int i = 0; i < dim0; ++i) {
sb.append(CHAR_NAME_LEFT_SQUARE_BRACKET);
sb.append(CHAR_NAME_RIGHT_SQUARE_BRACKET);
}
sb.append(CHAR_NAME_SPACE);
sb.append(name);
}
-
+
static int getArrayDim(Class<?> type) {
if (type.isArray()) {
return 1 + getArrayDim(type.getComponentType());
@@ -150,6 +149,18 @@ public class DynamicCodeGenBase implements BasicConstants {
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) {
// = expr
@@ -235,25 +246,7 @@ public class DynamicCodeGenBase implements BasicConstants {
public void insertTypeConvToObjectType(StringBuilder sb, Class<?> type,
String expr) throws DynamicCodeGenException {
if (type.isPrimitive()) { // primitive type
- if (type.equals(boolean.class)) {
- // 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());
- }
+ insertConsCall(sb, primitiveTypeToWrapperType(type), expr);
} else { // reference type
sb.append(expr);
}
@@ -285,7 +278,46 @@ public class DynamicCodeGenBase implements BasicConstants {
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)
throws DynamicCodeGenException {
if (c.equals(boolean.class) || c.equals(Boolean.class)) {