Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static <T> T clone(T object, Class<T> cls)//works only for objects of class having 0-parameter constructor
- {
- T t = makeNewInstance(cls);
- try {
- Field[] f = object.getClass().getDeclaredFields();
- for (int i = 0; i < f.length; i++) {
- Field field = f[i];
- if(!isStatic(field.getModifiers()))// to jest po to żeby nie kopiować pól statycznych
- {
- field.setAccessible(true);
- field.set(t, field.get(object));
- }
- }
- return t;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement