fix: Ignore if field does not exist
This commit is contained in:
24
src/np/com/krrish/Mapper.java
Normal file
24
src/np/com/krrish/Mapper.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package np.com.krrish;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
|
||||
public class Mapper {
|
||||
public Object map(Object source, String destination) {
|
||||
Object object = new Object();
|
||||
try {
|
||||
Class<?> aClass = Class.forName(destination);
|
||||
object = aClass.getConstructor().newInstance();
|
||||
Field[] sourceFields = source.getClass().getDeclaredFields();
|
||||
for (Field sourceField : sourceFields) {
|
||||
sourceField.setAccessible(true);
|
||||
Field destinationField = object.getClass().getDeclaredField(sourceField.getName());
|
||||
destinationField.setAccessible(true);
|
||||
destinationField.set(object, sourceField.get(source));
|
||||
}
|
||||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchFieldException ignored) {
|
||||
}
|
||||
return object;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user