<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4532237817668141511</id><updated>2012-02-16T18:05:41.325-08:00</updated><title type='text'>reflection-dsl</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://reflection-dsl.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4532237817668141511/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://reflection-dsl.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Leonardo</name><uri>http://www.blogger.com/profile/15259184983197132286</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='http://4.bp.blogspot.com/_p3ugCEdfXbo/SxkgZb2_wwI/AAAAAAAAAEE/nAEu26Yx9ho/S220/leo.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4532237817668141511.post-7254361438797724837</id><published>2009-12-04T14:15:00.000-08:00</published><updated>2009-12-08T16:39:35.146-08:00</updated><title type='text'>Easy Easy Reflection with Reflection-dsl</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_p3ugCEdfXbo/SxuRJo51MII/AAAAAAAAAE4/1BGIWDNPb4M/s1600-h/pieceOfCake.jpg"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; cursor: pointer; width: 335px; height: 297px;" src="http://1.bp.blogspot.com/_p3ugCEdfXbo/SxuRJo51MII/AAAAAAAAAE4/1BGIWDNPb4M/s400/pieceOfCake.jpg" alt="Piece of Cake - Image taken from http://www.pascalebakehouse.com/" id="BLOGGER_PHOTO_ID_5412078972000612482" title="Piece of Cake - Image taken from http://www.pascalebakehouse.com/" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a href="http://code.google.com/p/reflection-dsl/"&gt;Reflection-dsl&lt;/a&gt;: Piece of Cake&lt;br /&gt;&lt;br /&gt;If you have being programming with Java for some time now, it is almost certain you have already done something with Reflection, and I'm pretty sure you thought:&lt;br /&gt;&lt;br /&gt;-There must be a way to this without all this pain!!!&lt;br /&gt;&lt;br /&gt;It you came across this blog, it is probably because your prayers were answered: there is an easy way.&lt;br /&gt;&lt;br /&gt;Reflection-dsl  is really a handy tool for working with meta-programming in Java. The first way to work with it is using its internal Domains Specific Language (DSL), a very fluent way to write reflection, but let's talk about that a bit later.&lt;br /&gt;&lt;br /&gt;The other two ways to work with &lt;a href="http://code.google.com/p/reflection-dsl/"&gt;Reflection-dsl&lt;/a&gt; are really an innovative aproach to the problem, they abstract a Class as a database table.&lt;br /&gt;&lt;br /&gt;Imagine a table like this, named PERSON:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_p3ugCEdfXbo/SxuLVtiCaII/AAAAAAAAAEo/MV4GFzTiLeo/s1600-h/classTable.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 96px;" src="http://2.bp.blogspot.com/_p3ugCEdfXbo/SxuLVtiCaII/AAAAAAAAAEo/MV4GFzTiLeo/s400/classTable.jpg" alt="" id="BLOGGER_PHOTO_ID_5412072582331656322" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;If you had a table like that, couldn't you query it like this:&lt;br /&gt;¨SELECT * FROM person WHERE name = 'setName'¨ to find a row in wich the name is 'setName'?&lt;br /&gt;&lt;br /&gt;or use Hibernate's APIs, HQL or Criteria to have the same result.&lt;br /&gt;&lt;br /&gt;Now, imagine a class like this:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_p3ugCEdfXbo/SxuMSUWUa3I/AAAAAAAAAEw/n8vibqjuYAE/s1600-h/classPerson.jpg"&gt;&lt;img style="cursor: pointer; width: 356px; height: 190px;" src="http://3.bp.blogspot.com/_p3ugCEdfXbo/SxuMSUWUa3I/AAAAAAAAAEw/n8vibqjuYAE/s400/classPerson.jpg" alt="" id="BLOGGER_PHOTO_ID_5412073623543638898" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;With reflection-dsl you can think of a class as a table, take a look on the above class and above table, aren't they, after all, pretty much the same thing? So, with Reflection-DSL you can query your class like this:&lt;br /&gt;Introspector.createQuery("FROM br.com..bit.ideias.reflection.Person WHERE name = 'setName'").list();&lt;br /&gt;&lt;br /&gt;or just&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;/span&gt;Introspector&lt;span style="font-style: italic;"&gt;.&lt;/span&gt;forClass(Person.class).query("name = 'setName'")&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;At this time you must be thinking, what about "AND", "OR" etc, no problem, use them at will... it will work just fine&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;or use Criterion API:&lt;br /&gt;Introspector.createCriterion(Person.class)&lt;br /&gt; .add(Restriction.eq("setName")).list();&lt;br /&gt;&lt;br /&gt;Well, if it looks interesting, take a look at the wiki pages for further examples.&lt;br /&gt;&lt;a href="http://code.google.com/p/reflection-dsl/"&gt;Reflection-dsl&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Almost forgetting, the DSL part:&lt;span&gt;&lt;span&gt;&lt;br /&gt;Boolean isLogged = Introspector.forClass(&lt;/span&gt;&lt;span class="string"&gt;"package.User"&lt;/span&gt;&lt;span&gt;).create(&lt;/span&gt;&lt;span class="string"&gt;"param1"&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class="number"&gt;2&lt;/span&gt;&lt;span&gt;).method(&lt;/span&gt;&lt;span class="string"&gt;"isLogged"&lt;/span&gt;&lt;span&gt;).invoke();  &lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;br /&gt;String formatedValue = Introspector.forClass(&lt;span class="string"&gt;"package.FormatUtil"&lt;/span&gt;&lt;span&gt;).create(Locale.US)&lt;br /&gt;.method(&lt;/span&gt;&lt;span class="string"&gt;"formatCurrency"&lt;/span&gt;&lt;span&gt;).invoke(&lt;/span&gt;&lt;span class="number"&gt;1560.99&lt;/span&gt;&lt;span&gt;,  &lt;/span&gt;&lt;span class="string"&gt;"R$ #,##"&lt;/span&gt;&lt;span&gt;);&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="postbody"&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Hope you like it and leave comments.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4532237817668141511-7254361438797724837?l=reflection-dsl.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reflection-dsl.blogspot.com/feeds/7254361438797724837/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://reflection-dsl.blogspot.com/2009/12/easy-easy-reflection-with-reflection.html#comment-form' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4532237817668141511/posts/default/7254361438797724837'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4532237817668141511/posts/default/7254361438797724837'/><link rel='alternate' type='text/html' href='http://reflection-dsl.blogspot.com/2009/12/easy-easy-reflection-with-reflection.html' title='Easy Easy Reflection with Reflection-dsl'/><author><name>Leonardo</name><uri>http://www.blogger.com/profile/15259184983197132286</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='http://4.bp.blogspot.com/_p3ugCEdfXbo/SxkgZb2_wwI/AAAAAAAAAEE/nAEu26Yx9ho/S220/leo.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_p3ugCEdfXbo/SxuRJo51MII/AAAAAAAAAE4/1BGIWDNPb4M/s72-c/pieceOfCake.jpg' height='72' width='72'/><thr:total>0</thr:total></entry></feed>
