Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
major changes now. need to change lot of variable names and all
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric05 committed Mar 6, 2019
1 parent 85562b1 commit 2f16cd2
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 36 deletions.
87 changes: 59 additions & 28 deletions src/main/java/schema/GenericTranslator.java
Expand Up @@ -9,6 +9,7 @@
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.util.Set;

public class GenericTranslator<Source, Dest> {
Expand All @@ -29,84 +30,114 @@ public class GenericTranslator<Source, Dest> {
private static final String DESTINATION = "destination";

private static Gson gson = new Gson();
private HashMap<Method, Method> MethodMap;
private HashMap<SetterImpl, GetterImpl> MethodMap;
private Class<Source> source;
private Class<Dest> dest;

public GenericTranslator(String schema, Class<Source> source, Class<Dest> dest)
throws NoSuchMethodException {
throws NoSuchMethodException, SecurityException, NoSuchFieldException {
this.source = source;
this.dest = dest;
JsonObject schemaJson = gson.fromJson(schema, JsonObject.class);
MethodMap = new HashMap<Method, Method>();
MethodMap = new HashMap<SetterImpl, GetterImpl>();
for (Entry<String, JsonElement> e : schemaJson.entrySet()) {
String field = e.getKey();
JsonElement fieldValue = e.getValue();

Method sourceMethod = getSourceMethod(field, fieldValue);
GetterImpl sourceMethod = getSourceMethod(field, fieldValue);

Class<?> returnType = sourceMethod.getReturnType();

Method destMethod = getDestMethod(fieldValue, returnType);
Class<?> returnType = sourceMethod.ReturnType;

SetterImpl destMethod = getDestMethod(fieldValue, returnType);
MethodMap.put(destMethod, sourceMethod);
}
}

public void translate(Source source, Dest dest)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Set<Entry<Method, Method>> entrySet = MethodMap.entrySet();
for (Entry<Method, Method> entry : entrySet) {
Method destMethod = entry.getKey();
Method sourceMethod = entry.getValue();
destMethod.invoke(dest, sourceMethod.invoke(source));
public void translate(Source source, Dest destObj)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
SecurityException, NoSuchFieldException, InstantiationException {
Set<Entry<SetterImpl, GetterImpl>> entrySet = MethodMap.entrySet();
for (Entry<SetterImpl, GetterImpl> entry : entrySet) {
SetterImpl destMethod = entry.getKey();

GetterImpl sourceMethod = entry.getValue();
Object value = sourceMethod.getReturnValue(source);
destMethod.setValue(destObj, value);
// SetterImpl sim = new SetterImpl<>();
// sim.intialize(dest, "name");
// try {
// sim.setValue(destObj, value);
// } catch (NoSuchMethodException e) {
// e.printStackTrace();
// } catch (SecurityException e) {
// e.printStackTrace();
// } catch (IllegalAccessException e) {
// e.printStackTrace();
// } catch (IllegalArgumentException e) {
// e.printStackTrace();
// } catch (InvocationTargetException e) {
// e.printStackTrace();
// } catch (NoSuchFieldException e) {
// e.printStackTrace();
// } catch (InstantiationException e) {
// e.printStackTrace();
// }
// destMethod.invoke(destObj,value );
}
}

@SuppressWarnings("deprecation")
private Method getDestMethod(JsonElement destDescriptor, @SuppressWarnings("rawtypes") Class typeClass)
throws NoSuchMethodException, SecurityException {
private SetterImpl getDestMethod(JsonElement destDescriptor, @SuppressWarnings("rawtypes") Class typeClass)
throws NoSuchMethodException, SecurityException, NoSuchFieldException {
String destMethodName;

// if descriptor is string itself, then value will be field name
if (!destDescriptor.isJsonObject()) {
String valueString = destDescriptor.getAsString();
destMethodName = "set" + StringUtils.capitalise(valueString);
destMethodName = StringUtils.capitalise(valueString);
} else {
// in else case descriptor should contain destination or destinantion method
// name
JsonObject descriptor = destDescriptor.getAsJsonObject();
JsonElement destJsonElement = descriptor.get(DESTINATION);
if (destJsonElement != null) {
// destination is defined
destMethodName = "set" + StringUtils.capitalise(destJsonElement.getAsString());
destMethodName = StringUtils.capitalise(destJsonElement.getAsString());
} else {
// destiantion is not defined, going with destination method name
destMethodName = descriptor.get(DESTINATION_METHOD).getAsString();
}
}
Method destMethod = dest.getMethod(destMethodName, typeClass);
return destMethod;
SetterImpl setterImpl = new SetterImpl();
setterImpl.intialize(dest, destMethodName);
// Method destMethod = dest.getMethod(destMethodName, typeClass);
return setterImpl;
}

@SuppressWarnings("deprecation")
private Method getSourceMethod(String field, JsonElement value) throws NoSuchMethodException {
private GetterImpl getSourceMethod(String field, JsonElement value) throws NoSuchMethodException {
String sourceMethodName;
String Methodsuffix;
String methodSuffix;
if (value.isJsonObject()) {
JsonElement sourceJsonElement = value.getAsJsonObject().get(SOURCE);
{
if (sourceJsonElement == null) {
Methodsuffix = field;
methodSuffix = field;
} else {
Methodsuffix = sourceJsonElement.getAsString();
methodSuffix = sourceJsonElement.getAsString();
}
}
} else {
Methodsuffix = field;
methodSuffix = field;
}
sourceMethodName = "get" + StringUtils.capitalise(Methodsuffix);
Method sourceMethod = source.getMethod(sourceMethodName);
return sourceMethod;
GetterImpl getterImpl = new GetterImpl();
getterImpl.initialize(source, methodSuffix);
// sourceMethodName = "get" + StringUtils.capitalise(methodSuffix);
// Method sourceMethod = source.getMethod(sourceMethodName);
return getterImpl;
}
private static String getGetterName(String s){
return "get" + StringUtils.capitalise(s);

}
}
42 changes: 42 additions & 0 deletions src/main/java/schema/GetterImpl.java
@@ -0,0 +1,42 @@
package schema;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.regex.Pattern;
import static schema.SConstant.separator;

import org.apache.commons.lang.StringUtils;

public class GetterImpl<Source> {
public ArrayList<Method> methods;
public Class ReturnType;

@SuppressWarnings("rawtypes")
public void initialize(Class SourceClass, String name) throws NoSuchMethodException, SecurityException {
String[] names= name.split(separator);
ArrayList<Method> methodlist = new ArrayList<Method>();
for(String subName:names){
Method m = SourceClass.getMethod(getGetterName(subName));
SourceClass = m.getReturnType();
methodlist.add(m);
}
methods = methodlist;
ReturnType = methodlist.get(methodlist.size()-1).getReturnType();
return;

}

public Object getReturnValue(Source source) throws NoSuchMethodException, SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
Object obj = source;
for(Method subMethod:methods){
obj = subMethod.invoke(obj);
}
return obj;
}

public static String getGetterName(String name){
return "get"+ StringUtils.capitalise(name);
}
}
7 changes: 7 additions & 0 deletions src/main/java/schema/SConstant.java
@@ -0,0 +1,7 @@
package schema;

import java.util.regex.Pattern;

public class SConstant {
public final static String separator = Pattern.quote(".");
}
63 changes: 63 additions & 0 deletions src/main/java/schema/SetterImpl.java
@@ -0,0 +1,63 @@
package schema;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;

import org.apache.commons.lang.StringUtils;

import static schema.SConstant.separator;

public class SetterImpl<Dest> {
public ArrayList<Method> methods;
public String[] names;

public void intialize(Class DestClass, String name)
throws NoSuchFieldException, SecurityException, NoSuchMethodException {
names = name.split(separator);
Class finalClass = DestClass;
methods = new ArrayList<Method>();
for (String subName : names) {
String getterName = getGetterName(name);
Method getterMethod = finalClass.getMethod(getterName);
Method method = finalClass.getMethod(getSetterName(subName), getterMethod.getReturnType());
methods.add(method);
finalClass = getterMethod.getReturnType();
}
// Collections.reverse(methods);
}

public static String getSetterName(String name) {
return "set" + StringUtils.capitalise(name);
}

public static String getGetterName(String name) {
return "get" + StringUtils.capitalize(name);
}

public void setValue(Dest destObj, Object obj) throws NoSuchMethodException, SecurityException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException,
InstantiationException {
int count=0;
Object innerObject=destObj;
for(String name:names){
Method getterMethod = innerObject.getClass().getMethod(getGetterName(name));
Object innerObjectAttribute = getterMethod.invoke(innerObject);
if (innerObjectAttribute == null || count == names.length-1) {
Class type = innerObject.getClass().getMethod(getGetterName(name)).getReturnType();
if(count == names.length-1){
innerObjectAttribute = obj;
}else{
innerObjectAttribute = type.newInstance();
}
Method setterMethod = methods.get(count);
setterMethod.invoke(innerObject, innerObjectAttribute);
}
innerObject = innerObjectAttribute;
count++;
}
}

}
20 changes: 20 additions & 0 deletions src/test/java/schema/Complex/Bus.java
@@ -0,0 +1,20 @@
package schema.Complex;

public class Bus{
private Tyre tyre;

/**
* @return the tyre
*/
public Tyre getTyre() {
return tyre;
}

/**
* @param tyre the tyre to set
*/
public void setTyre(Tyre tyre) {
this.tyre = tyre;
}

}
19 changes: 19 additions & 0 deletions src/test/java/schema/Complex/Bus2.java
@@ -0,0 +1,19 @@
package schema.Complex;

public class Bus2{
private String name;

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}
19 changes: 19 additions & 0 deletions src/test/java/schema/Complex/Tyre.java
@@ -0,0 +1,19 @@
package schema.Complex;
public class Tyre{
private String name;

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

}

0 comments on commit 2f16cd2

Please sign in to comment.