From cmehta82 at gmail.com Wed Sep 1 10:32:12 2010 From: cmehta82 at gmail.com (Chirag Mehta) Date: Wed, 1 Sep 2010 23:02:12 +0530 Subject: [Mirror-dev] A Very Small Query Message-ID: Hi, I have seen this API today itself for the first time and I am not a very big expert, even I am just merely 5 years experience software engineer, please excuse me if I make a wrong statement. I have seen a piece of code on your site, which was written as below: Reflect a field by name (will return null if not found): Class clazz; Field f = new Mirror().on(clazz).reflect().field("fieldName"); Reflecting all fields of a class (will return an empty list if none found): Class clazz; List l = new Mirror().on(clazz).reflectAll().fields(); Reflecting all fields of a class that matches a Matcher (will return an empty list if none found): Class clazz; List l = new Mirror().on(clazz).reflectAll().fieldsMatching(new YourCustomMatcher()); Great efforts, I have used reflection in very dirty way and this is much simpler and cleaner way no doubt. The thing which annoyed me was ?*new Mirror()?*. A new object reference is required which may not be required further in future. Please explain me the decision which made this usage, like why you didn?t prefer it to make a Util... for example Reflect a field by name (will return null if not found): Class clazz; Field f = *MirrorUtil.onClassReflectField(clazz, "fieldName");* Reflecting all fields of a class (will return an empty list if none found): Class clazz; List l = *MirrorUtil.onClassReflectAllFields(clazz, "fieldName");* Reflecting all fields of a class that matches a Matcher (will return an empty list if none found): Class clazz; List l = *MirrorUtil.onClassReflectAllFieldsMatching(clazz, new YourCustomMatcher());* I am sure you did think about this type of option, but I am curious why didn?t it has been used. You know the Java world is too much dependent on OOPS and Garbage Collection is very heavy operation. Regards Chirag -------------- next part -------------- An HTML attachment was scrubbed... URL: From jonas at vidageek.net Thu Sep 2 10:03:36 2010 From: jonas at vidageek.net (Jonas Abreu) Date: Thu, 2 Sep 2010 14:03:36 -0300 Subject: [Mirror-dev] A Very Small Query In-Reply-To: References: Message-ID: Hi Chirag, Basically that was exactly what we where trying to get away from. A single class that have all the logic inside it and that is somewhat harder to test. While developing Mirror, we really tried to focus on readability. Even though MirrorUtil.onClassReflectAllFieldsMatching(clazz, new YourCustomMatcher()) is quite easy to read, we believe that having the parameters close to the dsl part that is referring to them makes the dsl easier to read. Another reason not to use the static approach was allowing for a ReflectionProvider, that can be used to change how reflection is done (for example, there is a reflection provider that uses internal sun classes to speed things up). Also, garbage collectors are quite smart. Unless you ask for a full gc (with System.gc) it will be really fast collecting short-lived objects (this is a good post on the subject http://java.dzone.com/articles/gc-goodstuff-collector). Hope I could clarify your doubts. Regards, Jonas Abreu VidaGeek.net http://vidageek.net Adaptworks - Coaching you to the change http://www.adaptworks.com.br On Wed, Sep 1, 2010 at 2:32 PM, Chirag Mehta wrote: > Hi, > > I have seen this API today itself for the first time and I am not a very big > expert, even I am just merely 5 years experience software engineer, please > excuse me if I make a wrong statement. > > I have seen a piece of code on your site, which was written as below: > > > > Reflect a field by name (will return null if not found): > > Class clazz; > > Field f = new Mirror().on(clazz).reflect().field("fieldName"); > > > > Reflecting all fields of a class (will return an empty list if none found): > > Class clazz; > > List l = new Mirror().on(clazz).reflectAll().fields(); > > > > Reflecting all fields of a class that matches a Matcher (will return > an empty list if none found): > > Class clazz; > > List l = new Mirror().on(clazz).reflectAll().fieldsMatching(new > YourCustomMatcher()); > > > > Great efforts, I have used reflection in very dirty way and this is much > simpler and cleaner way no doubt. The thing which annoyed me was ?new > Mirror()?. A new object reference is required which may not be required > further in future. > > > > Please explain me the decision which made this usage, like why you didn?t > prefer it to make a Util... for example > > > > Reflect a field by name (will return null if not found): > > Class clazz; > > Field f = MirrorUtil.onClassReflectField(clazz, "fieldName"); > > > > Reflecting all fields of a class (will return an empty list if none found): > > Class clazz; > > List l = MirrorUtil.onClassReflectAllFields(clazz, "fieldName"); > > Reflecting all fields of a class that matches a Matcher (will return > an empty list if none found): > > > > Class clazz; > > List l = MirrorUtil.onClassReflectAllFieldsMatching(clazz, new > YourCustomMatcher()); > > > > I am sure you did think about this type of option, but I am curious why > didn?t it has been used. You know the Java world is too much dependent on > OOPS and Garbage Collection is very heavy operation. > > > > Regards > > Chirag > > _______________________________________________ > Mirror-dev mailing list > Mirror-dev at lista.vidageek.net > http://lista.vidageek.net/listinfo.cgi/mirror-dev-vidageek.net > >