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 + "}"; } } |
Change query string in JAVA
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 57 58 59 |
import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; public class MyURLClass { public String addOrChangeQuery(String mainUrl, String key, String newValue) throws MalformedURLException, UnsupportedEncodingException { int queryStart = mainUrl.indexOf('?'); String newUrl; if (queryStart == -1) { newUrl = mainUrl + "?" + getNewQuery(null, key, newValue); } else { newUrl = mainUrl.substring(0, queryStart) + "?" + getNewQuery(mainUrl.substring(queryStart + 1), key, newValue); } return newUrl; } private String getNewQuery(String query, String key, String newValue) throws UnsupportedEncodingException { Map<String, String> map = new HashMap<>(); if (query == null) { map.put(key, newValue); } else { String[] params = query.split("&"); Boolean isKey = false; for (String param : params) { String queryName = param.split("=")[0]; String queryValue = param.split("=")[1]; if (queryName.equalsIgnoreCase(key)) { isKey = true; queryValue = newValue; } map.put(queryName, queryValue); } if (!isKey) { map.put(key, newValue); } } StringBuilder sb = new StringBuilder(); for (HashMap.Entry<String, String> e : map.entrySet()) { if (sb.length() > 0) { sb.append('&'); } sb.append(URLEncoder.encode(e.getKey(), "UTF-8")).append('=').append(URLEncoder.encode(e.getValue(), "UTF-8")); } return sb.toString(); } private Map<String, String> getQueryMap(String query) { Map<String, String> map = new HashMap<>(); String[] params = query.split("&"); for (String param : params) { String queryName = param.split("=")[0]; String queryValue = param.split("=")[1]; map.put(queryName, queryValue); } return map; } } |