Skip to content

Commit

Permalink
comment and reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysdh540 committed May 17, 2024
1 parent 1a3dd64 commit 5ea26b0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/dev/nolij/zson/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.nolij.zson;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Comment {
String value();
}
53 changes: 53 additions & 0 deletions src/main/java/dev/nolij/zson/Zson.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.nolij.zson;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.Map.Entry;

Expand Down Expand Up @@ -127,6 +129,57 @@ public static String escape(String string, char escapeQuotes) {
return result.toString();
}

public static Map<String, ZsonValue> fromObject(Object object) {
Map<String, ZsonValue> map = Zson.object();
for (Field field : object.getClass().getDeclaredFields()) {
if(Modifier.isStatic(field.getModifiers())) continue;
Comment comment = field.getAnnotation(Comment.class);
String commentValue = comment == null ? null : comment.value();
try {
map.put(field.getName(), new ZsonValue(commentValue, field.get(object)));
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to get field " + field.getName(), e);
}
}
return map;
}

public static <T> T toObject(Map<String, ZsonValue> map, Class<T> type) {
try {
T object = type.getDeclaredConstructor().newInstance();
for (Field field : type.getDeclaredFields()) {
if(Modifier.isStatic(field.getModifiers())) continue;
setField(field, object, map.get(field.getName()).value);
}
return object;
} catch (Exception e) {
throw new RuntimeException("Failed to create object of type " + type.getSimpleName(), e);
}
}

private static <T> void setField(Field field, Object object, Object value) {
@SuppressWarnings("unchecked")
Class<T> type = (Class<T>) field.getType();
try {
if(type.isPrimitive()) {
switch (type.getName()) {
case "boolean" -> field.setBoolean(object, (boolean) value);
case "short" -> field.setShort(object, (short) (int) value);
case "int" -> field.setInt(object, (int) value);
case "float" -> field.setFloat(object, (float) (double) value);
case "double" -> field.setDouble(object, (double) value);
}
} else {
field.set(object, type.cast(value));
}
} catch (Exception e) {
throw new RuntimeException(
"Failed to set field " + field.getName() + " (type " + type.getSimpleName() + ") to " + value + " " +
"(type " + value.getClass().getSimpleName() + ")", e
);
}
}

private Zson() {
}
}

0 comments on commit 5ea26b0

Please sign in to comment.