Permalink
Browse files

JsonMetaクラスがほぼ動くようになった

  • Loading branch information...
1 parent fc37328 commit 05ffd734c4a6e6f4bf1dafc5cd91b315e87d1b5d @vvakame committed May 8, 2012
View
4 ...parser-apt/src/main/java/net/vvakame/util/jsonpullparser/factory/ClassGenerateHelper.java
@@ -130,14 +130,12 @@ public void write() throws IOException {
{
Filer filer = processingEnv.getFiler();
String generateClassName = g.getPackageName() + "." + g.getTarget() + postfix;
- Log.d("generateClassName=" + generateClassName);
- Log.d("classElement=" + classElement.getSimpleName().toString());
JavaFileObject fileObject = filer.createSourceFile(generateClassName, classElement);
Template.writeGen(fileObject, g);
}
if (g.isBuilder()) {
Filer filer = processingEnv.getFiler();
- String generateClassName = classElement.asType().toString() + "JsonMeta";
+ String generateClassName = g.getPackageName() + "." + g.getTarget() + "JsonMeta";
JavaFileObject fileObject = filer.createSourceFile(generateClassName, classElement);
Template.writeJsonMeta(fileObject, g);
}
View
38 jsonpullparser-apt/src/main/resources/JsonModelMeta.java.mvel
@@ -15,7 +15,7 @@ import net.vvakame.util.jsonpullparser.builder.JsonPropertyMeta;
import net.vvakame.util.jsonpullparser.util.JsonUtil;
/**
- * Generates JSON-formatted data from {@link @{target}} with {@link JsonPullParser}.<br>
+ * Generates JSON-formatted data from {@link @{targetNew}} with {@link JsonPullParser}.<br>
* NB: This class is auto-generated by APT; please do not edit by hand.
*/
public class @{target}JsonMeta {
@@ -24,7 +24,7 @@ public class @{target}JsonMeta {
public static @{target}JsonMeta meta = new @{target}JsonMeta();
@foreach{obj : allElements}
/** Metaclass for @{obj.originalName} */
- public final JsonPropertyMeta<@{target}> @{obj.originalName} = new JsonPropertyMeta<@{target}>(Property_@{obj.key}.class, "@{obj.key}");
+ public final JsonPropertyMeta<@{targetNew}> @{obj.originalName} = new JsonPropertyMeta<@{targetNew}>(Property_@{obj.key}.class, "@{obj.key}");
@end{}
/**
@@ -41,34 +41,34 @@ public class @{target}JsonMeta {
* @return The brand new Builder instance.
* @author vvakame
*/
- public JsonModelBuilder<@{target}> newBuilder() {
- return new @{target}Builder(@{target}.class, true);
+ public JsonModelBuilder<@{targetNew}> newBuilder() {
+ return new @{target}Builder(@{targetNew}.class, true);
}
- class @{target}Builder extends JsonModelBuilder<@{target}> {
+ class @{target}Builder extends JsonModelBuilder<@{targetNew}> {
- public @{target}Builder(Class<@{target}> baseClass, boolean treatUnknownKeyAsError) {
+ public @{target}Builder(Class<@{targetNew}> baseClass, boolean treatUnknownKeyAsError) {
super(baseClass, treatUnknownKeyAsError);
}
@Override
- public JsonModelBuilder<@{target}> addAll() {
+ public JsonModelBuilder<@{targetNew}> addAll() {
@foreach{obj : allElements}
add(@{obj.originalName});
@end{}
return this;
}
@Override
- public JsonModelCoder<@{target}> fix() {
- Map<String, JsonPropertyCoder<@{target}>> properties = new LinkedHashMap<String, JsonPropertyCoder<@{target}>>();
+ public JsonModelCoder<@{targetNew}> fix() {
+ Map<String, JsonPropertyCoder<@{targetNew}>> properties = new LinkedHashMap<String, JsonPropertyCoder<@{targetNew}>>();
for (String key : map.keySet()) {
- JsonPropertyBuilder<@{target}> builder = map.get(key);
- JsonPropertyCoder<@{target}> fixed = builder.fix();
+ JsonPropertyBuilder<@{targetNew}> builder = map.get(key);
+ JsonPropertyCoder<@{targetNew}> fixed = builder.fix();
properties.put(key, fixed);
}
- JsonModelCoder<@{target}> fixed = new JsonModelCoder<@{target}>(baseClass, treatUnknownKeyAsError, properties);
+ JsonModelCoder<@{targetNew}> fixed = new JsonModelCoder<@{targetNew}>(baseClass, treatUnknownKeyAsError, properties);
return fixed;
}
}
@@ -78,10 +78,10 @@ public class @{target}JsonMeta {
* JSON property coder for @{obj.key}.
* @author vvakame
*/
- public static class Property_@{obj.key} extends JsonPropertyCoder<@{target}> {
+ public static class Property_@{obj.key} extends JsonPropertyCoder<@{targetNew}> {
@Override
- public void encode(Writer writer, @{target} obj) throws IOException {
+ public void encode(Writer writer, @{targetNew} obj) throws IOException {
@if{obj.kind == "LIST"}
@if{obj.modelName == "java.lang.Boolean"}
JsonUtil.putBooleanList(writer, obj.@{obj.getter}());
@@ -117,7 +117,7 @@ public class @{target}JsonMeta {
JsonUtil.putEnumList(writer, obj.@{obj.getter}());
@else{}
- @{obj.modelName}@{postfix}.encodeListNullToNull(writer, obj.@{obj.getter}());
+ @{obj.genName}@{postfix}.encodeListNullToNull(writer, obj.@{obj.getter}());
@end{}
@else{obj.kind == "BOOLEAN_WRAPPER"}
@@ -151,7 +151,7 @@ public class @{target}JsonMeta {
JsonUtil.put(writer, (Object) null);
}
@else{obj.kind == "MODEL"}
- @{obj.modelName}@{postfix}.encodeNullToNull(writer, obj.@{obj.getter}());
+ @{obj.genName}@{postfix}.encodeNullToNull(writer, obj.@{obj.getter}());
@else{obj.kind == "CONVERTER"}
@{obj.converter}.getInstance().encodeNullToNull(writer, obj.@{obj.getter}());
@else{}
@@ -160,7 +160,7 @@ public class @{target}JsonMeta {
}
@Override
- public void decode(JsonPullParser parser, @{target} obj) throws IOException,
+ public void decode(JsonPullParser parser, @{targetNew} obj) throws IOException,
JsonFormatException {
@if{obj.kind == "BOOLEAN"}
@@ -214,7 +214,7 @@ public class @{target}JsonMeta {
@else{obj.subKind == "ENUM"}
obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserEnumList(parser, @{obj.modelName}.class));
@else{}
- obj.@{obj.setter}(@{obj.modelName}@{postfix}.getList(parser, null));
+ obj.@{obj.setter}(@{obj.genName}@{postfix}.getList(parser, null));
@end{}
@else{obj.kind == "BOOLEAN_WRAPPER"}
obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserBoolean(parser));
@@ -249,7 +249,7 @@ public class @{target}JsonMeta {
@else{obj.kind == "JSON_HASH"}
obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonHash.fromParser(parser));
@else{obj.kind == "MODEL"}
- @{obj.modelName} tmp = @{obj.modelName}@{postfix}.get(parser);
+ @{obj.modelName} tmp = @{obj.genName}@{postfix}.get(parser);
obj.@{obj.setter}(tmp);
@else{obj.kind == "CONVERTER"}
@{obj.converter} converter = @{obj.converter}.getInstance();
View
6 jsonpullparser-apt/src/test/java/net/vvakame/sample/ForInnerClass.java
@@ -10,7 +10,7 @@
* Test for @JsonModel at inner class.
* @author vvakame
*/
-@JsonModel
+@JsonModel(builder = true)
public class ForInnerClass {
@JsonKey
@@ -67,7 +67,7 @@ public void setB(int b) {
* Test for @JsonModel at inner class. (grandchild)
* @author vvakame
*/
- @JsonModel
+ @JsonModel(builder = true)
public static class InnerClass2 {
@JsonKey
@@ -96,7 +96,7 @@ public void setC(int c) {
* Test for @JsonModel at inner class. (field has inner class)
* @author vvakame
*/
- @JsonModel
+ @JsonModel(builder = true)
public static class InnerClass3 {
@JsonKey
View
2 ...pt/src/test/java/net/vvakame/util/jsonpullparser/factory/JsonAnnotationProcessorTest.java
@@ -189,8 +189,6 @@ public void testForInnerClass() throws Exception {
compile();
- System.out.println(getGeneratedSource("net.vvakame.sample.InnerClass3Gen"));
-
assertThat(getCompiledResult(), is(true));
}
View
6 jsonpullparser-usage/src/main/java/net/vvakame/sample/ForInnerClassData.java
@@ -10,7 +10,7 @@
* Generate class for inner class.
* @author vvakame
*/
-@JsonModel
+@JsonModel(builder = true)
public class ForInnerClassData {
@JsonKey
@@ -66,7 +66,7 @@ public void setB(int b) {
* Generate class for inner class.
* @author vvakame
*/
- @JsonModel
+ @JsonModel(builder = true)
public static class InnerClassAB {
@JsonKey
@@ -95,7 +95,7 @@ public void setC(int c) {
* Generate class for inner class.
* @author vvakame
*/
- @JsonModel
+ @JsonModel(builder = true)
public static class InnerClassB {
@JsonKey
View
56 ...ge/src/test/java/net/vvakame/util/jsonpullparser/factory/JsonAnnotationProcessorTest.java
@@ -43,8 +43,11 @@
import net.vvakame.sample.ForInnerClassData.InnerClassA.InnerClassAB;
import net.vvakame.sample.ForInnerClassData.InnerClassB;
import net.vvakame.sample.ForInnerClassDataGenerated;
+import net.vvakame.sample.ForInnerClassDataJsonMeta;
import net.vvakame.sample.InnerClassABGenerated;
+import net.vvakame.sample.InnerClassABJsonMeta;
import net.vvakame.sample.InnerClassBGenerated;
+import net.vvakame.sample.InnerClassBJsonMeta;
import net.vvakame.sample.MiniData;
import net.vvakame.sample.MiniDataGenerated;
import net.vvakame.sample.PrimitiveTypeData;
@@ -58,6 +61,7 @@
import net.vvakame.sample.twitter.TweetGenerated;
import net.vvakame.util.jsonpullparser.JsonFormatException;
import net.vvakame.util.jsonpullparser.JsonPullParser;
+import net.vvakame.util.jsonpullparser.builder.JsonModelCoder;
import net.vvakame.util.jsonpullparser.util.JsonArray;
import net.vvakame.util.jsonpullparser.util.JsonHash;
@@ -465,14 +469,14 @@ public void primitiveWrapperList() throws IOException, IllegalStateException,
/**
* TODO テストを適当な所に移す.<br>
- * Tests for the inner class.
+ * Tests for the inner class by Gen.
* @author vvakame
* @throws IOException
* @throws JsonFormatException
* @throws IllegalStateException
*/
@Test
- public void innerClass() throws IOException, JsonFormatException {
+ public void innerClassGen() throws IOException, JsonFormatException {
{ // normal
ForInnerClassData data = new ForInnerClassData();
data.setA(1);
@@ -508,6 +512,54 @@ public void innerClass() throws IOException, JsonFormatException {
}
/**
+ * TODO テストを適当な所に移す.<br>
+ * Tests for the inner class by JsonMeta.
+ * @author vvakame
+ * @throws IOException
+ * @throws JsonFormatException
+ * @throws IllegalStateException
+ */
+ @Test
+ public void innerClassMeta() throws IOException, JsonFormatException {
+ { // normal
+ ForInnerClassData data = new ForInnerClassData();
+ data.setA(1);
+ StringWriter writer = new StringWriter();
+ ForInnerClassDataJsonMeta.get().newBuilder().addAll().fix().encode(writer, data);
+ assertThat("{\"a\":1}", is(writer.toString()));
+ }
+ { // grandchild
+ JsonModelCoder<InnerClassAB> coder =
+ InnerClassABJsonMeta.get().newBuilder().addAll().fix();
+ InnerClassAB data = new InnerClassAB();
+ data.setC(2);
+ StringWriter writer = new StringWriter();
+ coder.encode(writer, data);
+ assertThat("{\"c\":2}", is(writer.toString()));
+
+ data = coder.get(writer.toString());
+ assertThat(data.getC(), is(2));
+ }
+ { // child
+ JsonModelCoder<InnerClassB> coder =
+ InnerClassBJsonMeta.get().newBuilder().addAll().fix();
+ InnerClassB data = new InnerClassB();
+ data.setD(new InnerClassAB());
+ List<InnerClassAB> list = new ArrayList<InnerClassAB>();
+ list.add(new InnerClassAB());
+ data.setE(list);
+ StringWriter writer = new StringWriter();
+ coder.encode(writer, data);
+ System.out.println(writer.toString());
+ assertThat("{\"d\":{\"c\":0},\"e\":[{\"c\":0}]}", is(writer.toString()));
+
+ data = coder.get(writer.toString());
+ assertThat(data.getD(), notNullValue());
+ assertThat(data.getE().size(), is(1));
+ }
+ }
+
+ /**
* We can read Twitter timelines as well! #1: to POJO
* @throws IOException
* @throws JsonFormatException

0 comments on commit 05ffd73

Please sign in to comment.