Temelde başka bir sınıfın kopyası olan bir sınıfım var.
public class A {
int a;
String b;
}
public class CopyA {
int a;
String b;
}
Yaptığım şey, bir web servis çağrısı yoluyla göndermeden önce sınıftan değerleri Aiçine koymak . Şimdi bir yansıması-yöntemi oluşturmak istiyorum sınıftan temelde kopyalar (isim ve türe göre) aynıdır tüm alanları sınıfa .CopyACopyAACopyA
Bunu nasıl yapabilirim?
Şu ana kadar sahip olduğum şey bu, ama pek işe yaramıyor. Sanırım buradaki problem, döngüye girdiğim alanda bir alan oluşturmaya çalışıyorum.
private <T extends Object, Y extends Object> void copyFields(T from, Y too) {
Class<? extends Object> fromClass = from.getClass();
Field[] fromFields = fromClass.getDeclaredFields();
Class<? extends Object> tooClass = too.getClass();
Field[] tooFields = tooClass.getDeclaredFields();
if (fromFields != null && tooFields != null) {
for (Field tooF : tooFields) {
logger.debug("toofield name #0 and type #1", tooF.getName(), tooF.getType().toString());
try {
// Check if that fields exists in the other method
Field fromF = fromClass.getDeclaredField(tooF.getName());
if (fromF.getType().equals(tooF.getType())) {
tooF.set(tooF, fromF);
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Eminim bunu zaten bir şekilde yapmış biri olmalı