<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Silveira Neto &#187; Reflection</title>
	<atom:link href="http://silveiraneto.net/tag/reflection/feed/" rel="self" type="application/rss+xml" />
	<link>http://silveiraneto.net</link>
	<description></description>
	<lastBuildDate>Fri, 09 Mar 2012 04:13:27 +0000</lastBuildDate>
	<language>pt-br</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Java: invoking a method by name</title>
		<link>http://silveiraneto.net/2010/04/29/java-invoking-a-method-by-name/</link>
		<comments>http://silveiraneto.net/2010/04/29/java-invoking-a-method-by-name/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 13:29:33 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[method]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3364</guid>
		<description><![CDATA[<a href="http://silveiraneto.net/2010/04/29/java-invoking-a-method-by-name/" title="Java: invoking a method by name"></a>import java.lang.reflect.*; &#160; public class Foo &#123; public void bar&#40;int param&#41;&#123; System.out.println&#40;param&#41;; &#125; &#160; public static void main&#40;String args&#91;&#93;&#41;&#123; Object f = new Foo&#40;&#41;; try &#123; Method m = f.getClass&#40;&#41;.getMethod&#40;&#34;bar&#34;, int.class&#41;; m.invoke&#40;f, 42&#41;; &#125; catch &#40;Exception e&#41;&#123; System.err.println&#40;e&#41;; &#125; &#125; &#8230;<p class="read-more"><a href="http://silveiraneto.net/2010/04/29/java-invoking-a-method-by-name/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://silveiraneto.net/2010/04/29/java-invoking-a-method-by-name/" title="Java: invoking a method by name"></a><p><center><br />
<a href="http://silveiraneto.net/wp-content/uploads/2010/04/Film_star_Helen_Twelvetrees_ca_1936_7_photograph_by_Sam_Hood.jpg"><img src="http://silveiraneto.net/wp-content/uploads/2010/04/Film_star_Helen_Twelvetrees_ca_1936_7_photograph_by_Sam_Hood.jpg" alt="" title="Film_star_Helen_Twelvetrees_ca_1936_7_photograph_by_Sam_Hood" width="388" height="500" class="alignnone size-full wp-image-3365" /></a></center></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> bar<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> param<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Object</span> f <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">Method</span> m <span style="color: #339933;">=</span> f.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bar&quot;</span>, <span style="color: #000066; font-weight: bold;">int</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			m.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>f, <span style="color: #cc66cc;">42</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<blockquote><p>$ java Foo<br />
42
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2010/04/29/java-invoking-a-method-by-name/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Java: Accessing Private Members</title>
		<link>http://silveiraneto.net/2009/11/14/java-accessing-private-members/</link>
		<comments>http://silveiraneto.net/2009/11/14/java-accessing-private-members/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 07:27:14 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Douglas Adams]]></category>
		<category><![CDATA[Field]]></category>
		<category><![CDATA[getclass]]></category>
		<category><![CDATA[getDeclaredField]]></category>
		<category><![CDATA[Hack]]></category>
		<category><![CDATA[Hacking]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Life]]></category>
		<category><![CDATA[Object]]></category>
		<category><![CDATA[Object-Oriented Programming]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=2942</guid>
		<description><![CDATA[<a href="http://silveiraneto.net/2009/11/14/java-accessing-private-members/" title="Java: Accessing Private Members"></a>Using reflection to change the accessibility of a private object field and access it at runtime. import java.lang.reflect.Field; &#160; class Life &#123; private int meaning = 42; &#125; &#160; class Hack &#123; public static void main&#40;String args&#91;&#93;&#41;&#123; Life life = &#8230;<p class="read-more"><a href="http://silveiraneto.net/2009/11/14/java-accessing-private-members/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://silveiraneto.net/2009/11/14/java-accessing-private-members/" title="Java: Accessing Private Members"></a><p>Using reflection to change the accessibility of a private object field and access it at runtime.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.Field</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Life <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> meaning <span style="color: #339933;">=</span> <span style="color: #cc66cc;">42</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Hack <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        Life life <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Life<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Field</span> field <span style="color: #339933;">=</span> life.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDeclaredField</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;meaning&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            field.<span style="color: #006633;">setAccessible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>field.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>life<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">NoSuchFieldException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IllegalAccessException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Output:</p>
<blockquote><p>42</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/11/14/java-accessing-private-members/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

