Created
May 3, 2013 13:52
-
-
Save carlosspohr/5509221 to your computer and use it in GitHub Desktop.
Um clone com campos a excluir maroto.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package br.inf.carlos.base.util.cloning; | |
| import java.lang.reflect.Field; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| import net.vidageek.mirror.dsl.Mirror; | |
| import net.vidageek.mirror.list.dsl.Matcher; | |
| import net.vidageek.mirror.list.dsl.MirrorList; | |
| public class Clonefy { | |
| @SuppressWarnings("unchecked") | |
| public static <T> T clone(T object, String... excludes){ | |
| Mirror mirror = new Mirror(); | |
| final List<String> blacklist = Arrays.asList(excludes); | |
| MirrorList<Field> fields = mirror.on(object.getClass()).reflectAll().fields().matching(new Matcher<Field>() { | |
| @Override | |
| public boolean accepts(Field field) { | |
| return !blacklist.contains(field.getName()); | |
| } | |
| }); | |
| Object clone = mirror.on(object.getClass()).invoke().constructor().bypasser(); | |
| for (Field f : fields) { | |
| mirror.on(clone).set().field(f.getName()).withValue(mirror.on(object).get().field(f)); | |
| } | |
| return (T) clone; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment