Definations And Use :- Annotations
Annotation is Metadata. Metadata is data about data
1 2 3 4 5 6 7 8 9 10 11 12 |
public class AnnotationsSample { protected void execute() { Car car = new Car("Ford", "F150", "2018"); JsonSerializer serializer = new JsonSerializer(); try { serializer.serialize(car); } catch (JsonSerializeException e) { e.printStackTrace(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class Car { @JsonField("manufacturer") private final String make; @JsonField private final String model; @IgnoreMe private final String year; public Car(String make, String model, String year) { this.make = make; this.model = model; this.year = year; } public String getMake() { return make; } public String getModel() { return model; } public String getYear() { return year; } @Override public String toString() { return year + " " + make + " " + model; } } |
1 2 3 4 5 6 7 8 9 10 |
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface IgnoreMe { String value() default ""; } |
1 2 3 4 5 6 7 8 9 10 |
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface JsonField { String value() default ""; } |
1 2 3 4 5 6 7 |
public class JsonSerializeException extends Exception { private static final long serialVersionUID = -8845242379503538623L; public JsonSerializeException(String message) { super(message); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; public class JsonSerializer { private static String getSerializedKey(Field field) { String annotationValue = field.getAnnotation(JsonField.class).value(); if (annotationValue.isEmpty()) { return field.getName(); } else { return annotationValue; } } public String serialize(Object object) throws JsonSerializeException { try { Class<?> objectClass = requireNonNull(object).getClass(); Map<String, String> jsonElements = new HashMap<>(); for (Field field : objectClass.getDeclaredFields()) { field.setAccessible(true); if (field.isAnnotationPresent(JsonField.class)) { jsonElements.put(getSerializedKey(field), (String) field.get(object)); } else if (field.isAnnotationPresent(IgnoreMe.class)) { } else { jsonElements.put(field.getName(), (String) field.get(object)); } } System.out.println(toJsonString(jsonElements)); return toJsonString(jsonElements); } catch (IllegalAccessException e) { throw new JsonSerializeException(e.getMessage()); } } private Object requireNonNull(Object object) { if (object == null) { throw new NullPointerException(); } else { return object; } } private String toJsonString(Map<String, String> jsonMap) { StringBuilder elementsString = new StringBuilder(); for (Map.Entry<String, String> entry : jsonMap.entrySet()) elementsString.append("\"").append(entry.getKey()).append("\":\"").append(entry.getValue()).append("\""); return "{" + elementsString + "}"; } } |