Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'feature/id/6/InnerClass' into develop

  • Loading branch information...
commit 48e31832910f79a698dd2d09f7e53db9553e284c 2 parents ecd44dc + 05ffd73
@vvakame authored
View
38 jsonpullparser-apt/src/main/java/net/vvakame/apt/AptUtil.java
@@ -30,6 +30,7 @@
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;
+import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
/**
@@ -161,17 +162,29 @@ public static TypeElement getTypeElement(Types typeUtils, Element element) {
/**
* Returns the package name of the given element.
* NB: This method requires the given element has the kind of {@link ElementKind#CLASS}.
+ * @param elementUtils
* @param element
* @return the package name
* @author vvakame
*/
- public static String getPackageName(Element element) {
+ public static String getPackageName(Elements elementUtils, Element element) {
+ return elementUtils.getPackageOf(element).getQualifiedName().toString();
+ }
+
+ /**
+ * Returns unqualified class name (e.g. String, if java.lang.String)
+ * NB: This method requires the given element has the kind of {@link ElementKind#CLASS}.
+ * @param element
+ * @return unqualified class name
+ * @author vvakame
+ */
+ public static String getSimpleName(Element element) {
if (element.getKind() != ElementKind.CLASS) {
throw new IllegalStateException();
}
String str = element.asType().toString();
int i = str.lastIndexOf(".");
- return str.substring(0, i);
+ return str.substring(i + 1);
}
/**
@@ -181,13 +194,26 @@ public static String getPackageName(Element element) {
* @return unqualified class name
* @author vvakame
*/
- public static String getSimpleName(Element element) {
+ public static String getNameForNew(Element element) {
if (element.getKind() != ElementKind.CLASS) {
throw new IllegalStateException();
}
- String str = element.asType().toString();
- int i = str.lastIndexOf(".");
- return str.substring(i + 1);
+ return getNameForNew("", element);
+ }
+
+ static String getNameForNew(String current, Element element) {
+ if (element.getKind() == ElementKind.PACKAGE) {
+ return current;
+ } else {
+ String str = element.asType().toString();
+ int i = str.lastIndexOf(".");
+ String now = str.substring(i + 1);
+ if ("".equals(current)) {
+ return getNameForNew(now, element.getEnclosingElement());
+ } else {
+ return getNameForNew(now + "." + current, element.getEnclosingElement());
+ }
+ }
}
/**
View
19 ...parser-apt/src/main/java/net/vvakame/util/jsonpullparser/factory/ClassGenerateHelper.java
@@ -37,6 +37,7 @@
import javax.lang.model.type.PrimitiveType;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.WildcardType;
+import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.JavaFileObject;
@@ -94,8 +95,10 @@ public static ClassGenerateHelper newInstance(Element element) {
public ClassGenerateHelper(Element element) {
classElement = element;
- g.setPackageName(getPackageName(element));
+ Elements elementUtils = processingEnv.getElementUtils();
+ g.setPackageName(getPackageName(elementUtils, element));
g.setTarget(getSimpleName(element));
+ g.setTargetNew(getNameForNew(element));
TypeElement superclass = AptUtil.getSuperClassElement(element);
if (superclass.getAnnotation(JsonModel.class) != null) {
@@ -126,13 +129,13 @@ public void addElement(Element element) {
public void write() throws IOException {
{
Filer filer = processingEnv.getFiler();
- String generateClassName = classElement.asType().toString() + postfix;
+ String generateClassName = g.getPackageName() + "." + g.getTarget() + postfix;
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);
}
@@ -325,6 +328,13 @@ JsonElement genJsonElement(TypeMirror t, Element el, Kind kind) {
jsonElement.setOut(key.out());
jsonElement.setGetter(getter);
jsonElement.setModelName(t.toString());
+ if (kind == Kind.MODEL) {
+ Elements elementUtils = processingEnv.getElementUtils();
+ String packageName = getPackageName(elementUtils, el);
+ jsonElement.setGenName(packageName + "." + getSimpleName(el.asType()));
+ } else {
+ jsonElement.setGenName(t.toString());
+ }
jsonElement.setKind(kind);
jsonElement.setConverter(converterClassName);
@@ -454,6 +464,9 @@ public JsonElement visitList(DeclaredType t, Element el) {
jsonElement.setOut(key.out());
jsonElement.setGetter(getter);
jsonElement.setModelName(tm.toString());
+ Elements elementUtils = processingEnv.getElementUtils();
+ String packageName = getPackageName(elementUtils, el);
+ jsonElement.setGenName(packageName + "." + getSimpleName(type.asType()));
jsonElement.setKind(Kind.LIST);
}
View
18 ...pullparser-apt/src/main/java/net/vvakame/util/jsonpullparser/factory/GeneratingModel.java
@@ -37,6 +37,8 @@
String target = "Invalid";
+ String targetNew = "Invalid";
+
List<JsonElement> elements = new ArrayList<JsonElement>();
boolean treatUnknownKeyAsError;
@@ -139,6 +141,22 @@ public void setTarget(String target) {
}
/**
+ * @return the targetNew
+ * @category accessor
+ */
+ public String getTargetNew() {
+ return targetNew;
+ }
+
+ /**
+ * @param targetNew the targetNew to set
+ * @category accessor
+ */
+ public void setTargetNew(String targetNew) {
+ this.targetNew = targetNew;
+ }
+
+ /**
* @return the elements
* @category accessor
*/
View
18 jsonpullparser-apt/src/main/java/net/vvakame/util/jsonpullparser/factory/JsonElement.java
@@ -47,6 +47,8 @@
String modelName;
+ String genName;
+
String converter;
Kind subKind = Kind.UNKNOWN;
@@ -239,6 +241,22 @@ public void setModelName(String modelName) {
}
/**
+ * @return the genName
+ * @category accessor
+ */
+ public String getGenName() {
+ return genName;
+ }
+
+ /**
+ * @param genName the genName to set
+ * @category accessor
+ */
+ public void setGenName(String genName) {
+ this.genName = genName;
+ }
+
+ /**
* @return the converter
* @category accessor
*/
View
2  ...rser-apt/src/main/java/net/vvakame/util/jsonpullparser/factory/template/MvelTemplate.java
@@ -90,6 +90,7 @@ public static void writeJsonMeta(JavaFileObject fileObject, GeneratingModel mode
map.put("existsBase", model.isExistsBase());
map.put("targetBase", model.getTargetBase());
map.put("target", model.getTarget());
+ map.put("targetNew", model.getTargetNew());
{
List<Map<String, String>> jsonElements = new ArrayList<Map<String, String>>();
for (JsonElement jsonElement : model.getElements()) {
@@ -134,6 +135,7 @@ public static void writeJsonMeta(JavaFileObject fileObject, GeneratingModel mode
map.put("key", el.getKey());
map.put("originalName", el.getOriginalName());
map.put("modelName", el.getModelName());
+ map.put("genName", el.getGenName());
map.put("setter", el.getSetter());
map.put("getter", el.getGetter());
map.put("kind", el.getKind().name());
View
146 jsonpullparser-apt/src/main/resources/JsonModelGen.java.mvel
@@ -18,90 +18,90 @@ import net.vvakame.util.jsonpullparser.util.JsonSliceUtil;
@end{}
/**
- * Parses JSON-formatted data into {@link @{target}} with {@link JsonPullParser}.<br>
+ * Parses JSON-formatted data into {@link @{targetNew}} with {@link JsonPullParser}.<br>
* NB: This class is auto-generated by APT; please do not edit by hand.
*/
public class @{target}@{postfix} {
/**
- * Attempts to parse the given data as {@link List} of {@link @{target}}.
+ * Attempts to parse the given data as {@link List} of {@link @{targetNew}}.
* @param json JSON-formatted data
- * @return {@link List} of {@link @{target}}
+ * @return {@link List} of {@link @{targetNew}}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
- public static List<@{target}> getList(String json) throws IOException, JsonFormatException {
+ public static List<@{targetNew}> getList(String json) throws IOException, JsonFormatException {
JsonPullParser parser = JsonPullParser.newParser(json);
return getList(parser, null);
}
/**
- * Attempts to parse the given data as {@link List} of {@link @{target}}.<br>
+ * Attempts to parse the given data as {@link List} of {@link @{targetNew}}.<br>
* Accepts {@link OnJsonObjectAddListener}; allows you to peek various intermittent instances as parsing goes.
*
* @param json JSON-formatted data
* @param listener {@link OnJsonObjectAddListener} to notify
- * @return {@link List} of {@link @{target}}
+ * @return {@link List} of {@link @{targetNew}}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
- public static List<@{target}> getList(String json, OnJsonObjectAddListener listener) throws IOException, JsonFormatException {
+ public static List<@{targetNew}> getList(String json, OnJsonObjectAddListener listener) throws IOException, JsonFormatException {
JsonPullParser parser = JsonPullParser.newParser(json);
return getList(parser, listener);
}
/**
- * Attempts to parse the given data as {@link List} of {@link @{target}}.
+ * Attempts to parse the given data as {@link List} of {@link @{targetNew}}.
*
* @param stream JSON-formatted data
- * @return {@link List} of {@link @{target}}
+ * @return {@link List} of {@link @{targetNew}}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
- public static List<@{target}> getList(InputStream stream) throws IOException, JsonFormatException {
+ public static List<@{targetNew}> getList(InputStream stream) throws IOException, JsonFormatException {
JsonPullParser parser = JsonPullParser.newParser(stream);
return getList(parser, null);
}
/**
- * Attempts to parse the given data as {@link List} of {@link @{target}}.<br>
+ * Attempts to parse the given data as {@link List} of {@link @{targetNew}}.<br>
* Accepts {@link OnJsonObjectAddListener}; allows you to peek various intermittent instances as parsing goes.
*
* @param stream JSON-formatted data
* @param listener {@link OnJsonObjectAddListener} to notify
- * @return {@link List} of {@link @{target}}
+ * @return {@link List} of {@link @{targetNew}}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
- public static List<@{target}> getList(InputStream stream, OnJsonObjectAddListener listener) throws IOException, JsonFormatException {
+ public static List<@{targetNew}> getList(InputStream stream, OnJsonObjectAddListener listener) throws IOException, JsonFormatException {
JsonPullParser parser = JsonPullParser.newParser(stream);
return getList(parser, listener);
}
/**
- * Attempts to parse the given data as {@link List} of {@link @{target}}.
+ * Attempts to parse the given data as {@link List} of {@link @{targetNew}}.
*
* @param parser {@link JsonPullParser} with some JSON-formatted data
- * @return {@link List} of {@link @{target}}
+ * @return {@link List} of {@link @{targetNew}}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
- public static List<@{target}> getList(JsonPullParser parser) throws IOException, JsonFormatException {
+ public static List<@{targetNew}> getList(JsonPullParser parser) throws IOException, JsonFormatException {
return getList(parser, null);
}
/**
- * Attempts to parse the given data as {@link List} of {@link @{target}}.<br>
+ * Attempts to parse the given data as {@link List} of {@link @{targetNew}}.<br>
* Accepts {@link OnJsonObjectAddListener}; allows you to peek various intermittent instances as parsing goes.
*
* @param parser {@link JsonPullParser} with some JSON-formatted data
* @param listener {@link OnJsonObjectAddListener} to notify
- * @return {@link List} of {@link @{target}}
+ * @return {@link List} of {@link @{targetNew}}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
- public static List<@{target}> getList(JsonPullParser parser, OnJsonObjectAddListener listener) throws IOException, JsonFormatException {
- List<@{target}> list = new ArrayList<@{target}>();
+ public static List<@{targetNew}> getList(JsonPullParser parser, OnJsonObjectAddListener listener) throws IOException, JsonFormatException {
+ List<@{targetNew}> list = new ArrayList<@{targetNew}>();
State eventType = parser.getEventType();
if (eventType == State.VALUE_NULL) {
if (listener != null) {
@@ -117,7 +117,7 @@ public class @{target}@{postfix} {
}
}
while (parser.lookAhead() != State.END_ARRAY) {
- @{target} tmp = get(parser, listener);
+ @{targetNew} tmp = get(parser, listener);
list.add(tmp);
}
parser.getEventType();
@@ -125,85 +125,85 @@ public class @{target}@{postfix} {
}
/**
- * Attempts to parse the given data as {@link @{target}}.
+ * Attempts to parse the given data as {@link @{targetNew}}.
*
* @param json JSON-formatted data
- * @return {{@link @{target}}
+ * @return {{@link @{targetNew}}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
- public static @{target} get(String json) throws IOException, JsonFormatException {
+ public static @{targetNew} get(String json) throws IOException, JsonFormatException {
JsonPullParser parser = JsonPullParser.newParser(json);
return get(parser, null);
}
/**
- * Attempts to parse the given data as {@link @{target}}.<br>
+ * Attempts to parse the given data as {@link @{targetNew}}.<br>
* Accepts {@link OnJsonObjectAddListener}; allows you to peek various intermittent instances as parsing goes.
*
* @param json JSON-formatted data
* @param listener {@link OnJsonObjectAddListener} to notify
- * @return {@link @{target}}
+ * @return {@link @{targetNew}}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
- public static @{target} get(String json, OnJsonObjectAddListener listener) throws IOException, JsonFormatException {
+ public static @{targetNew} get(String json, OnJsonObjectAddListener listener) throws IOException, JsonFormatException {
JsonPullParser parser = JsonPullParser.newParser(json);
return get(parser, listener);
}
/**
- * Attempts to parse the given data as {@link @{target}}.
+ * Attempts to parse the given data as {@link @{targetNew}}.
*
* @param stream JSON-formatted data
- * @return {{@link @{target}}
+ * @return {{@link @{targetNew}}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
- public static @{target} get(InputStream stream) throws IOException, JsonFormatException {
+ public static @{targetNew} get(InputStream stream) throws IOException, JsonFormatException {
JsonPullParser parser = JsonPullParser.newParser(stream);
return get(parser, null);
}
/**
- * Attempts to parse the given data as {@link @{target}}.<br>
+ * Attempts to parse the given data as {@link @{targetNew}}.<br>
* Accepts {@link OnJsonObjectAddListener}; allows you to peek various intermittent instances as parsing goes.
*
* @param stream JSON-formatted data
* @param listener {@link OnJsonObjectAddListener} to notify
- * @return {@link @{target}}
+ * @return {@link @{targetNew}}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
- public static @{target} get(InputStream stream, OnJsonObjectAddListener listener) throws IOException, JsonFormatException {
+ public static @{targetNew} get(InputStream stream, OnJsonObjectAddListener listener) throws IOException, JsonFormatException {
JsonPullParser parser = JsonPullParser.newParser(stream);
return get(parser, listener);
}
/**
- * Attempts to parse the given data as {@link @{target}}.
+ * Attempts to parse the given data as {@link @{targetNew}}.
*
* @param parser {@link JsonPullParser} with some JSON-formatted data
- * @return {{@link @{target}}
+ * @return {{@link @{targetNew}}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
- public static @{target} get(JsonPullParser parser) throws IOException, JsonFormatException {
+ public static @{targetNew} get(JsonPullParser parser) throws IOException, JsonFormatException {
return get(parser, null);
}
/**
- * Attempts to parse the given data as {@link @{target}}.<br>
+ * Attempts to parse the given data as {@link @{targetNew}}.<br>
* Accepts {@link OnJsonObjectAddListener}; allows you to peek various intermittent instances as parsing goes.
*
* @param parser {@link JsonPullParser} with some JSON-formatted data
* @param listener {@link OnJsonObjectAddListener} to notify
- * @return {@link @{target}}
+ * @return {@link @{targetNew}}
* @throws IOException if an I/O error occurs.
* @throws IllegalStateException @SaveOrigin is enabled while {@link JsonPullParser#setLogEnable()} is not called yet.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
- public static @{target} get(JsonPullParser parser, OnJsonObjectAddListener listener) throws IOException, IllegalStateException, JsonFormatException {
+ public static @{targetNew} get(JsonPullParser parser, OnJsonObjectAddListener listener) throws IOException, IllegalStateException, JsonFormatException {
@if{storeJsonElement.storeJson == true}
int start = -1;
@if{storeJsonElement.treatLogDisabledAsError == true}
@@ -218,7 +218,7 @@ public class @{target}@{postfix} {
}
@end{}
@end{}
- @{target} obj = new @{target}();
+ @{targetNew} obj = new @{targetNew}();
State eventType = parser.getEventType();
if (eventType == State.VALUE_NULL) {
if (listener != null) {
@@ -274,13 +274,13 @@ public class @{target}@{postfix} {
* @param parser {@link JsonPullParser} in action
* @param listener {@link OnJsonObjectAddListener} to listen new instance creations
* @param key JSON key being parsed
- * @param obj {@link @{target}} (and like) being rebuilt
+ * @param obj {@link @{targetNew}} (and like) being rebuilt
* @return True if the given key is parsed successfully, false otherwise
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException Data is malformed, or its type is unexpected
* @author vvakame
*/
- public static boolean parseValue(JsonPullParser parser, OnJsonObjectAddListener listener, String key, @{target} obj) throws IOException, JsonFormatException {
+ public static boolean parseValue(JsonPullParser parser, OnJsonObjectAddListener listener, String key, @{targetNew} obj) throws IOException, JsonFormatException {
@foreach{obj : inElements}
if ("@{obj.key}".equals(key)) {
@if{obj.kind == "BOOLEAN"}
@@ -334,7 +334,7 @@ public class @{target}@{postfix} {
@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, listener));
+ obj.@{obj.setter}(@{obj.genName}@{postfix}.getList(parser, listener));
@end{}
@else{obj.kind == "BOOLEAN_WRAPPER"}
obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserBoolean(parser));
@@ -369,7 +369,7 @@ public class @{target}@{postfix} {
@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, listener);
+ @{obj.modelName} tmp = @{obj.genName}@{postfix}.get(parser, listener);
obj.@{obj.setter}(tmp);
@else{obj.kind == "CONVERTER"}
@{obj.converter} converter = @{obj.converter}.getInstance();
@@ -388,39 +388,39 @@ public class @{target}@{postfix} {
}
/**
- * Encodes the given {@link List} of {@link @{target}} into the JSON format, and appends it into the given stream using {@link JsonPullParser#DEFAULT_CHARSET}.<br>
+ * Encodes the given {@link List} of {@link @{targetNew}} into the JSON format, and appends it into the given stream using {@link JsonPullParser#DEFAULT_CHARSET}.<br>
* This method is an alias of {@link #encodeListNullToBlank(Writer, List)}.
*
* @param out {@link OutputStream} to be written
- * @param list {@link List} of {@link @{target}} to be encoded
+ * @param list {@link List} of {@link @{targetNew}} to be encoded
* @throws IOException if an I/O error occurs.
*/
- public static void encodeList(OutputStream out, List<? extends @{target}> list) throws IOException {
+ public static void encodeList(OutputStream out, List<? extends @{targetNew}> list) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(out, JsonPullParser.DEFAULT_CHARSET);
encodeListNullToBlank(writer, list);
}
/**
- * Encodes the given {@link List} of {@link @{target}} into the JSON format, and writes it using the given writer.<br>
+ * Encodes the given {@link List} of {@link @{targetNew}} into the JSON format, and writes it using the given writer.<br>
* This method is an alias of {@link #encodeListNullToBlank(Writer, List)}.
*
* @param writer {@link Writer} to be used for writing value
- * @param list {@link List} of {@link @{target}} to be encoded
+ * @param list {@link List} of {@link @{targetNew}} to be encoded
* @throws IOException if an I/O error occurs.
*/
- public static void encodeList(Writer writer, List<? extends @{target}> list) throws IOException {
+ public static void encodeList(Writer writer, List<? extends @{targetNew}> list) throws IOException {
encodeListNullToBlank(writer, list);
}
/**
- * Encodes the given {@link List} of {@link @{target}} into the JSON format, and writes it using the given writer.<br>
+ * Encodes the given {@link List} of {@link @{targetNew}} into the JSON format, and writes it using the given writer.<br>
* Writes "[]" if null is given.
*
* @param writer {@link Writer} to be used for writing value
- * @param list {@link List} of {@link @{target}} to be encoded
+ * @param list {@link List} of {@link @{targetNew}} to be encoded
* @throws IOException if an I/O error occurs.
*/
- public static void encodeListNullToBlank(Writer writer, List<? extends @{target}> list) throws IOException {
+ public static void encodeListNullToBlank(Writer writer, List<? extends @{targetNew}> list) throws IOException {
if (list == null) {
writer.write("[]");
writer.flush();
@@ -431,14 +431,14 @@ public class @{target}@{postfix} {
}
/**
- * Encodes the given {@link List} of {@link @{target}} into the JSON format, and writes it using the given writer.<br>
+ * Encodes the given {@link List} of {@link @{targetNew}} into the JSON format, and writes it using the given writer.<br>
* Writes "null" if null is given.
*
* @param writer {@link Writer} to be used for writing value
- * @param list {@link List} of {@link @{target}} to be encoded
+ * @param list {@link List} of {@link @{targetNew}} to be encoded
* @throws IOException if an I/O error occurs.
*/
- public static void encodeListNullToNull(Writer writer, List<? extends @{target}> list) throws IOException {
+ public static void encodeListNullToNull(Writer writer, List<? extends @{targetNew}> list) throws IOException {
if (list == null) {
writer.write("null");
writer.flush();
@@ -462,39 +462,39 @@ public class @{target}@{postfix} {
}
/**
- * Encodes the given {@link @{target}} into the JSON format, and appends it into the given stream using {@link JsonPullParser#DEFAULT_CHARSET}.<br>
- * This method is an alias of {@link #encodeNullToBlank(Writer, @{target})}.
+ * Encodes the given {@link @{targetNew}} into the JSON format, and appends it into the given stream using {@link JsonPullParser#DEFAULT_CHARSET}.<br>
+ * This method is an alias of {@link #encodeNullToBlank(Writer, @{targetNew})}.
*
* @param out {@link OutputStream} to be written
- * @param obj {@link @{target}} to be encoded
+ * @param obj {@link @{targetNew}} to be encoded
* @throws IOException if an I/O error occurs.
*/
- public static void encode(OutputStream out, @{target} obj) throws IOException {
+ public static void encode(OutputStream out, @{targetNew} obj) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(out, JsonPullParser.DEFAULT_CHARSET);
encodeNullToBlank(writer, obj);
}
/**
- * Encodes the given {@link @{target}} into the JSON format, and writes it using the given writer.<br>
- * This method is an alias of {@link #encodeNullToBlank(Writer, @{target})}.
+ * Encodes the given {@link @{targetNew}} into the JSON format, and writes it using the given writer.<br>
+ * This method is an alias of {@link #encodeNullToBlank(Writer, @{targetNew})}.
*
* @param writer {@link Writer} to be used for writing value
- * @param obj {@link @{target}} to be encoded
+ * @param obj {@link @{targetNew}} to be encoded
* @throws IOException if an I/O error occurs.
*/
- public static void encode(Writer writer, @{target} obj) throws IOException {
+ public static void encode(Writer writer, @{targetNew} obj) throws IOException {
encodeNullToBlank(writer, obj);
}
/**
- * Encodes the given {@link @{target}} into the JSON format, and writes it using the given writer.<br>
+ * Encodes the given {@link @{targetNew}} into the JSON format, and writes it using the given writer.<br>
* Writes "{}" if null is given.
*
* @param writer {@link Writer} to be used for writing value
- * @param obj {@link @{target}} to be encoded
+ * @param obj {@link @{targetNew}} to be encoded
* @throws IOException if an I/O error occurs.
*/
- public static void encodeNullToBlank(Writer writer, @{target} obj) throws IOException {
+ public static void encodeNullToBlank(Writer writer, @{targetNew} obj) throws IOException {
if (obj == null) {
writer.write("{}");
writer.flush();
@@ -505,14 +505,14 @@ public class @{target}@{postfix} {
}
/**
- * Encodes the given {@link @{target}} into the JSON format, and writes it using the given writer.<br>
+ * Encodes the given {@link @{targetNew}} into the JSON format, and writes it using the given writer.<br>
* Writes "null" if null is given.
*
* @param writer {@link Writer} to be used for writing value
- * @param obj {@link @{target}} to be encoded
+ * @param obj {@link @{targetNew}} to be encoded
* @throws IOException if an I/O error occurs.
*/
- public static void encodeNullToNull(Writer writer, @{target} obj) throws IOException {
+ public static void encodeNullToNull(Writer writer, @{targetNew} obj) throws IOException {
if (obj == null) {
writer.write("null");
return;
@@ -537,7 +537,7 @@ public class @{target}@{postfix} {
* @throws IOException if an I/O error occurs.
* @author vvakame
*/
- public static boolean encodeValue(Writer writer, @{target} obj) throws IOException {
+ public static boolean encodeValue(Writer writer, @{targetNew} obj) throws IOException {
@if{existsBase == true}
@if{outElements.size() == 0}
boolean existParentValue = false;
@@ -587,7 +587,7 @@ public class @{target}@{postfix} {
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"}
@@ -613,7 +613,7 @@ public class @{target}@{postfix} {
@else{obj.kind == "JSON_HASH"}
obj.@{obj.getter}().toJson(writer);
@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{}
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,18 +41,18 @@ 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{}
@@ -60,15 +60,15 @@ public class @{target}JsonMeta {
}
@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
141 jsonpullparser-apt/src/test/java/net/vvakame/sample/ForInnerClass.java
@@ -0,0 +1,141 @@
+package net.vvakame.sample;
+
+import java.util.List;
+
+import net.vvakame.sample.ForInnerClass.InnerClass1.InnerClass2;
+import net.vvakame.util.jsonpullparser.annotation.JsonKey;
+import net.vvakame.util.jsonpullparser.annotation.JsonModel;
+
+/**
+ * Test for @JsonModel at inner class.
+ * @author vvakame
+ */
+@JsonModel(builder = true)
+public class ForInnerClass {
+
+ @JsonKey
+ int a = 0;
+
+
+ /**
+ * @return the a
+ * @category accessor
+ */
+ public int getA() {
+ return a;
+ }
+
+ /**
+ * @param a the a to set
+ * @category accessor
+ */
+ public void setA(int a) {
+ this.a = a;
+ }
+
+
+ /**
+ * Test for @JsonModel at inner class. (child)
+ * @author vvakame
+ */
+ // @JsonModel
+ // is this bug? can't find up Element on apt round.
+ public static class InnerClass1 {
+
+ @JsonKey
+ int b = 0;
+
+
+ /**
+ * @return the b
+ * @category accessor
+ */
+ public int getB() {
+ return b;
+ }
+
+ /**
+ * @param b the b to set
+ * @category accessor
+ */
+ public void setB(int b) {
+ this.b = b;
+ }
+
+
+ /**
+ * Test for @JsonModel at inner class. (grandchild)
+ * @author vvakame
+ */
+ @JsonModel(builder = true)
+ public static class InnerClass2 {
+
+ @JsonKey
+ int c = 0;
+
+
+ /**
+ * @return the c
+ * @category accessor
+ */
+ public int getC() {
+ return c;
+ }
+
+ /**
+ * @param c the c to set
+ * @category accessor
+ */
+ public void setC(int c) {
+ this.c = c;
+ }
+ }
+ }
+
+ /**
+ * Test for @JsonModel at inner class. (field has inner class)
+ * @author vvakame
+ */
+ @JsonModel(builder = true)
+ public static class InnerClass3 {
+
+ @JsonKey
+ InnerClass2 a;
+
+ @JsonKey
+ List<InnerClass2> b;
+
+
+ /**
+ * @return the a
+ * @category accessor
+ */
+ public InnerClass2 getA() {
+ return a;
+ }
+
+ /**
+ * @param a the a to set
+ * @category accessor
+ */
+ public void setA(InnerClass2 a) {
+ this.a = a;
+ }
+
+ /**
+ * @return the b
+ * @category accessor
+ */
+ public List<InnerClass2> getB() {
+ return b;
+ }
+
+ /**
+ * @param b the b to set
+ * @category accessor
+ */
+ public void setB(List<InnerClass2> b) {
+ this.b = b;
+ }
+ }
+}
View
18 ...pt/src/test/java/net/vvakame/util/jsonpullparser/factory/JsonAnnotationProcessorTest.java
@@ -21,6 +21,7 @@
import net.vvakame.sample.ComplexData;
import net.vvakame.sample.ConverterData;
import net.vvakame.sample.ExtendsData;
+import net.vvakame.sample.ForInnerClass;
import net.vvakame.sample.PrimitiveTypeData;
import net.vvakame.sample.PrimitiveWrapperData;
import net.vvakame.sample.PrimitiveWrapperListData;
@@ -174,6 +175,23 @@ public void testForAbstractClass() throws Exception {
assertThat(getCompiledResult(), is(false));
}
+ /**
+ * Tests for error raised to abstract class.
+ * @throws Exception
+ * @author vvakame
+ */
+ @Test
+ public void testForInnerClass() throws Exception {
+ JsonAnnotationProcessor processor = new JsonAnnotationProcessor();
+ addProcessor(processor);
+
+ addCompilationUnit(ForInnerClass.class);
+
+ compile();
+
+ assertThat(getCompiledResult(), is(true));
+ }
+
@Override
protected void setUp() throws Exception {
super.setUp();
View
140 jsonpullparser-usage/src/main/java/net/vvakame/sample/ForInnerClassData.java
@@ -0,0 +1,140 @@
+package net.vvakame.sample;
+
+import java.util.List;
+
+import net.vvakame.sample.ForInnerClassData.InnerClassA.InnerClassAB;
+import net.vvakame.util.jsonpullparser.annotation.JsonKey;
+import net.vvakame.util.jsonpullparser.annotation.JsonModel;
+
+/**
+ * Generate class for inner class.
+ * @author vvakame
+ */
+@JsonModel(builder = true)
+public class ForInnerClassData {
+
+ @JsonKey
+ int a;
+
+
+ /**
+ * @return the a
+ * @category accessor
+ */
+ public int getA() {
+ return a;
+ }
+
+ /**
+ * @param a the a to set
+ * @category accessor
+ */
+ public void setA(int a) {
+ this.a = a;
+ }
+
+
+ /**
+ * Generate class for inner class.
+ * @author vvakame
+ */
+ // @JsonModel
+ public static class InnerClassA {
+
+ @JsonKey
+ int b;
+
+
+ /**
+ * @return the b
+ * @category accessor
+ */
+ public int getB() {
+ return b;
+ }
+
+ /**
+ * @param b the b to set
+ * @category accessor
+ */
+ public void setB(int b) {
+ this.b = b;
+ }
+
+
+ /**
+ * Generate class for inner class.
+ * @author vvakame
+ */
+ @JsonModel(builder = true)
+ public static class InnerClassAB {
+
+ @JsonKey
+ int c;
+
+
+ /**
+ * @return the c
+ * @category accessor
+ */
+ public int getC() {
+ return c;
+ }
+
+ /**
+ * @param c the c to set
+ * @category accessor
+ */
+ public void setC(int c) {
+ this.c = c;
+ }
+ }
+ }
+
+ /**
+ * Generate class for inner class.
+ * @author vvakame
+ */
+ @JsonModel(builder = true)
+ public static class InnerClassB {
+
+ @JsonKey
+ InnerClassAB d;
+
+ @JsonKey
+ List<InnerClassAB> e;
+
+
+ /**
+ * @return the d
+ * @category accessor
+ */
+ public InnerClassAB getD() {
+ return d;
+ }
+
+ /**
+ * @param d the d to set
+ * @category accessor
+ */
+ public void setD(InnerClassAB d) {
+ this.d = d;
+ }
+
+ /**
+ * @return the e
+ * @category accessor
+ */
+ public List<InnerClassAB> getE() {
+ return e;
+ }
+
+ /**
+ * @param e the e to set
+ * @category accessor
+ */
+ public void setE(List<InnerClassAB> e) {
+ this.e = e;
+ }
+ }
+}
View
102 ...ge/src/test/java/net/vvakame/util/jsonpullparser/factory/JsonAnnotationProcessorTest.java
@@ -39,6 +39,15 @@
import net.vvakame.sample.ExtendsData1Generated;
import net.vvakame.sample.ExtendsData2;
import net.vvakame.sample.ExtendsData2Generated;
+import net.vvakame.sample.ForInnerClassData;
+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;
@@ -52,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;
@@ -458,6 +468,98 @@ public void primitiveWrapperList() throws IOException, IllegalStateException,
}
/**
+ * TODO テストを適当な所に移す.<br>
+ * Tests for the inner class by Gen.
+ * @author vvakame
+ * @throws IOException
+ * @throws JsonFormatException
+ * @throws IllegalStateException
+ */
+ @Test
+ public void innerClassGen() throws IOException, JsonFormatException {
+ { // normal
+ ForInnerClassData data = new ForInnerClassData();
+ data.setA(1);
+ StringWriter writer = new StringWriter();
+ ForInnerClassDataGenerated.encode(writer, data);
+ assertThat("{\"a\":1}", is(writer.toString()));
+ }
+ { // grandchild
+ InnerClassAB data = new InnerClassAB();
+ data.setC(2);
+ StringWriter writer = new StringWriter();
+ InnerClassABGenerated.encode(writer, data);
+ assertThat("{\"c\":2}", is(writer.toString()));
+
+ data = InnerClassABGenerated.get(writer.toString());
+ assertThat(data.getC(), is(2));
+ }
+ { // child
+ 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();
+ InnerClassBGenerated.encode(writer, data);
+ System.out.println(writer.toString());
+ assertThat("{\"d\":{\"c\":0},\"e\":[{\"c\":0}]}", is(writer.toString()));
+
+ data = InnerClassBGenerated.get(writer.toString());
+ assertThat(data.getD(), notNullValue());
+ assertThat(data.getE().size(), is(1));
+ }
+ }
+
+ /**
+ * 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
Please sign in to comment.
Something went wrong with that request. Please try again.