Permalink
Browse files

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

  • Loading branch information...
2 parents ecd44dc + 05ffd73 commit 48e31832910f79a698dd2d09f7e53db9553e284c @vvakame committed May 8, 2012
@@ -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());
+ }
+ }
}
/**
@@ -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);
}
@@ -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
*/
@@ -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
*/
@@ -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());
Oops, something went wrong.

0 comments on commit 48e3183

Please sign in to comment.