<?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; getclass</title>
	<atom:link href="http://silveiraneto.net/tag/getclass/feed/" rel="self" type="application/rss+xml" />
	<link>http://silveiraneto.net</link>
	<description>the world is a pixel</description>
	<lastBuildDate>Sun, 08 Jan 2012 05:17:57 +0000</lastBuildDate>
	<language>pt-br</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<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[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 = new Life&#40;&#41;; try &#123; Field field = life.getClass&#40;&#41;.getDeclaredField&#40;&#34;meaning&#34;&#41;; field.setAccessible&#40;true&#41;; System.out.println&#40;field.get&#40;life&#41;&#41;; &#125; catch &#40;NoSuchFieldException e&#41; &#123; [...]]]></description>
			<content:encoded><![CDATA[<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 java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.Field</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: #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: #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>;
            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: #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: #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: #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: #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>
		<item>
		<title>JavaFX, getting resources of inside your JAR</title>
		<link>http://silveiraneto.net/2009/04/10/javafx-getting-resources-of-inside-your-jar/</link>
		<comments>http://silveiraneto.net/2009/04/10/javafx-getting-resources-of-inside-your-jar/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 19:51:54 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[fileinputstream]]></category>
		<category><![CDATA[getclass]]></category>
		<category><![CDATA[JAR]]></category>
		<category><![CDATA[jarfile]]></category>
		<category><![CDATA[JavaFX]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[picture]]></category>
		<category><![CDATA[resource]]></category>
		<category><![CDATA[sandwich]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=2471</guid>
		<description><![CDATA[Tradução: há uma versão em Português desse artigo. For some classes like javafx.scene.image.Image is easy load an image from a external resource like: ImageView &#123; image: Image &#123; url: &#34;http://example.com/myPicture.png&#34; &#125; &#125; or a resource inside your own Jar file with the __DIR__ constant: ImageView &#123; image: Image &#123; url: &#34;{__DIR__}/myPicture.png&#34; &#125; &#125; But for [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><img src="http://silveiraneto.net/wp-content/uploads/2008/10/br.png" alt="br flag" /><small><strong>Tradução:</strong> há uma <a href="http://silveiraneto.net/2009/04/10/javafx-acessando-recursos-de-dentro-do-jar/">versão em Português</a> desse artigo.</small></p></blockquote>
<p>For some classes like <a href="http://java.sun.com/javafx/1/docs/api/javafx.scene.image/javafx.scene.image.Image.html">javafx.scene.image.Image</a> is easy load an image from a external resource like:</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;">ImageView <span style="color: #009900;">&#123;</span>
    image<span style="color: #339933;">:</span> <span style="color: #003399;">Image</span> <span style="color: #009900;">&#123;</span>
        url<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;http://example.com/myPicture.png&quot;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>or a resource inside your own Jar file with the __DIR__ constant:</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;">ImageView <span style="color: #009900;">&#123;</span>
    image<span style="color: #339933;">:</span> <span style="color: #003399;">Image</span> <span style="color: #009900;">&#123;</span>
        url<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;{__DIR__}/myPicture.png&quot;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>But for other classes loading a internal resource (inside your own jarfile) is not so direct. For example, in the article <a title="JavaFX XML" href="http://silveiraneto.net/2008/12/25/parsing-xml-sandwich-with-javafx/">Parsing a XML Sandwich with JavaFX</a> I had to place the XML file in a temp directory. A more elegant way would be:</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">handlexml</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileInputStream</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.data.pull.*</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.ext.swing.*</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.Scene</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.stage.Stage</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Resource<span style="color: #009900;">&#123;</span>
    function getUrl<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">:</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    function getStream<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">:</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResourceAsStream</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
var list <span style="color: #339933;">=</span> SwingList <span style="color: #009900;">&#123;</span> width<span style="color: #339933;">:</span> <span style="color: #cc66cc;">600</span>, height<span style="color: #339933;">:</span> <span style="color: #cc66cc;">300</span><span style="color: #009900;">&#125;</span>
&nbsp;
var myparser <span style="color: #339933;">=</span> PullParser <span style="color: #009900;">&#123;</span>
    documentType<span style="color: #339933;">:</span> PullParser.<span style="color: #006633;">XML</span>;
    onEvent<span style="color: #339933;">:</span> function <span style="color: #009900;">&#40;</span>e<span style="color: #339933;">:</span> <span style="color: #003399;">Event</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        var item <span style="color: #339933;">=</span> SwingListItem <span style="color: #009900;">&#123;</span>text<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;event {e}&quot;</span><span style="color: #009900;">&#125;</span>;
        insert item into list.<span style="color: #006633;">items</span>;
    <span style="color: #009900;">&#125;</span>
    input<span style="color: #339933;">:</span> Resource<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>.<span style="color: #006633;">getStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my.xml&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
myparser.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
Stage <span style="color: #009900;">&#123;</span>
    title<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Map&quot;</span>
    scene<span style="color: #339933;">:</span> Scene <span style="color: #009900;">&#123;</span>
        content<span style="color: #339933;">:</span> list
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>With a simple XML file called <em>my.xml</em> inside your package.</p>
<div class="wp_syntax">
<div class="code">
<pre class="xml xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bread</span> <span style="color: #000066;">sesame</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;catchup</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hamburguer</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cheese</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;chedar&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;maionese</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lettuce</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bread<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>
</div>
</div>
<p><center><img src="http://silveiraneto.net/wp-content/uploads/2009/04/fileplace.png" alt="fileplace" title="fileplace" width="210" height="107" class="alignnone size-full wp-image-2474" /></center><br />
And we get the same result as before, but all files inside our Jars.<br />
<center><img alt="" src="http://silveiraneto.net/wp-content/uploads/2008/12/javafx_xml_sandwich.png" title="XML Sandwich" class="alignnone" width="625" height="354" /></center></p>
<p>References:</p>
<ul>
<ol>Hildeberto&#8217;s article <a href="http://www.hildeberto.com/2008/12/acessing-resources-inside-of-jar-files.html">Acessing Resources Inside of JAR Files</a></ol>
<ol><a href="http://java.sun.com/javafx/1/tutorials/core/">JavaFX Core Tutorial</a></ol>
<ol>JavaFX forum, <a href="http://forums.sun.com/thread.jspa?threadID=5367468&#038;tstart=2">this</a> and <a href="http://forums.sun.com/thread.jspa?threadID=5340352">this</a> thread.</ol>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/04/10/javafx-getting-resources-of-inside-your-jar/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

