Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

20
LINES

< > BotCompany Repo | #1017345 // cloneWithoutFields (shallow clone)

JavaX fragment (include)

1  
static <A> A cloneWithoutFields(A o, Set<S> fieldsToSkip) ctex {
2  
  if (o == null) null;
3  
  final A clone = cast nuObject(o.getClass());
4  
  Class c = o.getClass();
5  
  while (c != Object.class) {
6  
    Field[] fields = c.getDeclaredFields();
7  
    for (final Field field : fields) {
8  
      if (contains(fieldsToSkip, field.getName())) continue;
9  
      if ((field.getModifiers() & Modifier.STATIC) != 0) continue;
10  
      field.setAccessible(true);
11  
      field.set(clone, field.get(o));
12  
    }
13  
    c = c.getSuperclass();
14  
  }
15  
  return clone;
16  
}
17  
18  
static <A> A cloneWithoutFields(A o, S... fieldsToSkip) {
19  
  ret cloneWithoutFields(o, asHashSet(fieldsToSkip));
20  
}

Author comment

Began life as a copy of #1015334

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1017345
Snippet name: cloneWithoutFields (shallow clone)
Eternal ID of this version: #1017345/4
Text MD5: 360958d82754d226158d17a3e5cc6132
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-07-17 21:22:47
Source code size: 661 bytes / 20 lines
Pitched / IR pitched: No / No
Views / Downloads: 450 / 513
Version history: 3 change(s)
Referenced in: [show references]