Skip to content

Commit 6beaa0f

Browse files
committed
support enums
1 parent 3791095 commit 6beaa0f

File tree

2 files changed

+59
-27
lines changed

2 files changed

+59
-27
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,20 @@ An example project can be found in the directory `examples/maven-example`.
8484
* `String`
8585
* `int`
8686
* `long`
87+
* `float`
88+
* `double`
89+
* `boolean`
90+
* Enums
8791
* Wrapper classes for supported primitive types
8892
* Objects of classes annotated with `@GenerateJSON`
8993
* `Collection`s if they are not part of other collections or arrays
9094
* Arrays
9195

9296
### Limitations
9397
* It is not possible to create an array/collection of collections
98+
* Objects annotated with `@GenerateJSON` need to have a no-args-constructor
99+
* Collections need to be initialized in the constructor
94100
* Generic objects are not supported (except generic collections)
95101
* Eclipse may not detect the annotation processor
96102
* Compile-time JSON-parser is not yet published to maven central so you will have to build it by yourself.
103+
* Configuration is not supported

src/main/java/io/github/danthe1st/json_compile/impl/JSONCreator.java

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ public class JSONCreator extends AbstractProcessor {
3030

3131
private static final String JSONOBJECT_PARAM_NAME = "data";
3232

33-
private static final Map <String, String> simpleAssignments = Map.of("java.lang.String", "String",
34-
"int", "Int","java.lang.Integer","Int",
35-
"long", "Long","java.lang.Long","Long");
33+
private static final Map <String, String> simpleAssignments = Map.ofEntries(Map.entry("java.lang.String", "String"),
34+
Map.entry("int", "Int"),Map.entry("java.lang.Integer","Int"),
35+
Map.entry("long", "Long"),Map.entry("java.lang.Long","Long"),
36+
Map.entry("boolean","Boolean"),Map.entry("java,lang.Boolean","Boolean"),
37+
Map.entry("float","Float"),Map.entry("java,lang.Float","Float"),
38+
Map.entry("double","Double"),Map.entry("java,lang.Double","Double"));
3639

3740
@Override
3841
public boolean process(Set <? extends TypeElement> annotations, RoundEnvironment roundEnv) {
@@ -43,7 +46,6 @@ public boolean process(Set <? extends TypeElement> annotations, RoundEnvironment
4346
processingEnv.getMessager().printMessage(Kind.ERROR,
4447
"An I/O error occured during processing element: " + e.getMessage(), element);
4548
}
46-
processingEnv.getMessager().printMessage(Kind.NOTE, "Element detected", element);
4749
}
4850
return false;
4951
}
@@ -52,6 +54,10 @@ private void generateJSON(Element element) throws IOException {
5254
if(element.getKind() == ElementKind.CLASS) {
5355
DeclaredType typeMirror = (DeclaredType) element.asType();
5456
String newClassName = typeMirror.toString() + "JSONLoader";
57+
if(element.getEnclosingElement()!=null&&element.getEnclosingElement().getKind()!=ElementKind.PACKAGE){
58+
processingEnv.getMessager().printMessage(Kind.ERROR,"Nested elements are not supported",element);
59+
return;
60+
}
5561

5662
JavaFileObject loaderSource = processingEnv.getFiler().createSourceFile(newClassName, element);
5763
try(ClassWriter writer = new ClassWriter(new BufferedWriter(loaderSource.openWriter()))) {
@@ -139,42 +145,41 @@ private void generateJSON(Element element, String fullyQualidiedClassName, Class
139145
writer.endClass();
140146
}
141147

142-
143-
144-
//TODO arrays/collections, more primitives
145148
private void addPropertyFromJSON(ClassWriter writer, JSONOperation jsonOperation,String jsonObjectName) throws IOException {
146149
TypeMirror type = jsonOperation.getType();
147150
String typeName = type.toString();
148151
String val = null;
152+
if(hasVisibilityProblems(type,true)){
153+
return;
154+
}
149155
if(type.getKind() == TypeKind.ARRAY) {
150-
//TODO test, add in addPropertyToJSON
151156
ArrayType arrayType = (ArrayType) type;
152157

153-
String jsonArrayName=addCreateJSONArrayCode(writer,jsonOperation,jsonObjectName);
158+
String jsonArrayName = addCreateJSONArrayCode(writer, jsonOperation, jsonObjectName);
154159

155-
String actualArrayName=jsonOperation.getAttributeName().replaceAll("\\[.*]","")+"DataArray";
156-
writer.addAssignment(arrayType.getComponentType().toString()+"[] "+actualArrayName,"null");
160+
String actualArrayName = jsonOperation.getAttributeName().replaceAll("\\[.*]", "") + "DataArray";
161+
writer.addAssignment(arrayType.getComponentType().toString() + "[] " + actualArrayName, "null");
157162

158-
writer.beginIf(jsonArrayName+"!=null");
163+
writer.beginIf(jsonArrayName + "!=null");
159164

160-
String componentName=arrayType.getComponentType().toString();
161-
int componentNameArrayStart=componentName.indexOf('[');
162-
String componentNameBefore=componentName;
163-
String componentNameAfter="";
164-
if(componentNameArrayStart!=-1){
165-
componentNameBefore=componentName.substring(0,componentNameArrayStart);
166-
componentNameAfter=componentName.substring(componentNameArrayStart);
165+
String componentName = arrayType.getComponentType().toString();
166+
int componentNameArrayStart = componentName.indexOf('[');
167+
String componentNameBefore = componentName;
168+
String componentNameAfter = "";
169+
if(componentNameArrayStart != -1) {
170+
componentNameBefore = componentName.substring(0, componentNameArrayStart);
171+
componentNameAfter = componentName.substring(componentNameArrayStart);
167172
}
168173

169-
writer.addAssignment(actualArrayName,"new "+componentNameBefore+"["+jsonArrayName+".length()]"+componentNameAfter+" ");
174+
writer.addAssignment(actualArrayName, "new " + componentNameBefore + "[" + jsonArrayName + ".length()]" + componentNameAfter + " ");
170175

171-
String counterVar=jsonArrayName+"Counter";
172-
writer.beginSimpleFor("int "+counterVar+"=0",counterVar+"<"+jsonArrayName+".length()",counterVar+"++");
176+
String counterVar = jsonArrayName + "Counter";
177+
writer.beginSimpleFor("int " + counterVar + "=0", counterVar + "<" + jsonArrayName + ".length()", counterVar + "++");
173178

174-
addPropertyFromJSON(writer,new ArrayElementOperation(actualArrayName+"["+counterVar+"]",arrayType.getComponentType()),jsonArrayName);
179+
addPropertyFromJSON(writer, new ArrayElementOperation(actualArrayName + "[" + counterVar + "]", arrayType.getComponentType()), jsonArrayName);
175180

176181
writer.endFor();
177-
val=actualArrayName;
182+
val = actualArrayName;
178183
writer.endIf();
179184
}else if(isCollection(type)){
180185
TypeMirror collectionType=getCollectionType(type);
@@ -197,7 +202,9 @@ private void addPropertyFromJSON(ClassWriter writer, JSONOperation jsonOperation
197202

198203
writer.endIf();
199204
writer.endFor();
200-
} else if(simpleAssignments.containsKey(typeName)) {
205+
} else if(type instanceof DeclaredType&&((DeclaredType) type).asElement().getKind()==ElementKind.ENUM){
206+
val = jsonObjectName + ".optEnum("+type+".class," + jsonOperation.getJSONAccessName() + ")";
207+
}else if(simpleAssignments.containsKey(typeName)) {
201208
String name=jsonOperation.getJSONAccessName();
202209
val = jsonObjectName + ".opt" + simpleAssignments.get(typeName) + "(" + name + ")";
203210
} else {
@@ -251,11 +258,24 @@ private TypeMirror getCollectionType(TypeMirror type){
251258
return null;
252259
}
253260

261+
private boolean hasVisibilityProblems(TypeMirror type,boolean displayError){
262+
if(type instanceof DeclaredType){
263+
DeclaredType declType= (DeclaredType) type;
264+
if(!declType.asElement().getModifiers().contains(Modifier.PUBLIC)){
265+
processingEnv.getMessager().printMessage(Kind.ERROR,"only public types are supported", declType.asElement());
266+
return true;
267+
}
268+
}
269+
return false;
270+
}
271+
254272
private void addPropertyToJSON(ClassWriter writer, JSONOperation jsonOperation,String jsonObjectName) throws IOException {
255273
TypeMirror type = jsonOperation.getType();
256274
String typeName = type.toString();
257275
String val = jsonOperation.getAccessor("obj");//TODO check if obj is correct here
258-
276+
if(hasVisibilityProblems(type,false)){
277+
return;
278+
}
259279
if(val == null) {
260280
processingEnv.getMessager().printMessage(Kind.ERROR, "type " + typeName + " is currenly not supported");
261281
}else if(type.getKind()==TypeKind.ARRAY){
@@ -309,7 +329,12 @@ private void addPropertyToJSON(ClassWriter writer, JSONOperation jsonOperation,S
309329
}else{
310330
writer.addMethodCall(jsonObjectName,"put","\""+jsonOperation.getAttributeName()+"\"",jsonArrayName);
311331
}
312-
332+
} else if(type instanceof DeclaredType&&((DeclaredType) type).asElement().getKind()==ElementKind.ENUM){
333+
if(jsonOperation.isChildType()){
334+
writer.addMethodCall(jsonObjectName,"put",val);
335+
}else{
336+
writer.addMethodCall(jsonObjectName,"put","\""+jsonOperation.getAttributeName()+"\"",val);
337+
}
313338
} else if(simpleAssignments.containsKey(typeName)) {
314339
if(jsonOperation.isChildType()){
315340
writer.addMethodCall(jsonObjectName, "put", val);

0 commit comments

Comments
 (0)