<?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>Elliott&#039;s Development Blog &#187; Java</title>
	<atom:link href="http://www.elliottsprehn.com/blog/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.elliottsprehn.com/blog</link>
	<description>Exploring Life Through Math, Algorithms and Code</description>
	<lastBuildDate>Mon, 30 Aug 2010 17:25:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Getting the Expected Results for GetCurrentTemplatePath() in a Custom Tag.</title>
		<link>http://www.elliottsprehn.com/blog/2007/07/17/getting-the-expected-results-for-getcurrenttemplatepath-in-a-custom-tag/</link>
		<comments>http://www.elliottsprehn.com/blog/2007/07/17/getting-the-expected-results-for-getcurrenttemplatepath-in-a-custom-tag/#comments</comments>
		<pubDate>Tue, 17 Jul 2007 21:43:39 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Adobe]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.elliottsprehn.com/blog/2007/07/17/getting-the-expected-results-for-getcurrenttemplatepath-in-a-custom-tag/</guid>
		<description><![CDATA[While working on the template system used for the conference websites I ran across a problem where I needed the path to the template that called a custom tag. The first thing I tried was getCurrentTemplatePath() thinking that it might return that since the documentation makes no mention of custom tags. Instead, however, the function [...]]]></description>
			<content:encoded><![CDATA[<p>While working on the template system used for the <a href="http://cfunited.com">conference websites</a> I ran across a problem where I needed the path to the template that called a custom tag. The first thing I tried was getCurrentTemplatePath() thinking that it might return that since the documentation makes no mention of custom tags. Instead, however, the function returns the path to the custom tag itself.</p>
<p><a href="http://www.bennadel.com/">Ben Nadel</a> noticed some of this <a href="http://www.bennadel.com/blog/828-Dynamic-And-Unexpected-ColdFusion-GetCurrentTemplatePath-Behavior.htm">odd behavior</a> as well.</p>
<p>I spent a long time trying to figure out how to get the caller template path, including what Ben did which was to add a special function to the caller scope.</p>
<pre class="brush: coldfusion">&lt;cfscript&gt;
function getCallerTemplatePath() {
    return getCurrentTemplatePath();
}
caller.getCallerTemplatePath = getCallerTemplatePath;
path = caller.getCallerTemplatePath();
&lt;/cfscript&gt;</pre>
<p>This doesn&#8217;t work though. Instead I still got the template path of the custom tag!</p>
<p>I dug around in the PageContext (which is returned from getPageContext() if you&#8217;re not familiar) with no luck and finally gave up resorting to this&#8230;</p>
<pre class="brush: js">/**
    Monumental hack, but the only way I could figure out how to do
    a getCurrentTemplatePath() like call that resolves to the page
    that called this custom tag.
*/
function getCallerTemplatePath() {
    try {
       error;
    } catch( any cfcatch ) {
        return cfcatch.tagContext[3].template;
    }
}</pre>
<p>Which worked but really felt like a hack since it means throwing an exception on every request. So I kept an eye out as I dug around in the internals of the CF engine for various other things, and today I was rewarded with an awesome solution.</p>
<pre class="brush: js">/** Gets the path to the page that called this custom tag. */
function getCallerTemplatePath() {
    var field = getMetaData(caller).getDeclaredField("pageContext");
    field.setAccessible(true);
    return field.get(caller).getPage().getCurrentTemplatePath();
}</pre>
<p>Now to get at why and how this kind of thing works&#8230;</p>
<p>Inside the ColdFusion runtime the foundation unit for all scripts, components and tags is the <code>coldfusion.runtime.CFPage</code> object, and the getCurrentTemplatePath() function is really identical to&#8230;</p>
<pre class="brush: js">function getCurrentTemplatePath() {
    return getPageContext().getPage().getCurrentTemplatePath();
}</pre>
<p>After realizing this it dawned on me that the custom tags, cfcs, and pages all have their own PageContext and Page objects, and as such the template path is going to be different, or rather bound, to the page in which the function is called from, not where it&#8217;s defined.</p>
<p>Knowing this I was able to grab the page context out of the caller scope, which is the page context of the caller, and not the current page, and use that to get the current template path of the page for which that page context operates.</p>
<p>Also, for those who aren&#8217;t familiar, the getMetaData() function can be used to return the java.lang.Class instance for most objects you wouldn&#8217;t normally be able to call getClass() on in ColdFusion. For instance you can call <code>getMetaData(variables).getName()</code> and you&#8217;ll get <code>coldfusion.runtime.VariableScope</code>.</p>
<p>Doing this really made my code feel less icky, so I hope this is useful to someone else.</p>
<p>(PS, Tested and works on CF6+ and CF7+, anyone have CF8?)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elliottsprehn.com/blog/2007/07/17/getting-the-expected-results-for-getcurrenttemplatepath-in-a-custom-tag/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>I Object!</title>
		<link>http://www.elliottsprehn.com/blog/2007/02/26/i-object/</link>
		<comments>http://www.elliottsprehn.com/blog/2007/02/26/i-object/#comments</comments>
		<pubDate>Mon, 26 Feb 2007 08:15:02 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.elliottsprehn.com/blog/?p=3</guid>
		<description><![CDATA[While doing some casual web surfing I came across a rather interesting blog entry about Ruby&#8217;s types and looping. I started typing a reply, and then I realized it was really long, so I&#8217;m putting it here: One reason I think methods like this are great is that Ruby is intended to be read! Which [...]]]></description>
			<content:encoded><![CDATA[<p>While doing some casual web surfing I came across a rather interesting blog entry about <a href="http://www.ditchnet.org/wp/2007/02/07/3times-erh-links/">Ruby&#8217;s types and looping</a>. I started typing a reply, and then I realized it was really long, so I&#8217;m putting it here:</p>
<p>One reason I think methods like this are great is that Ruby is intended to be <strong>read</strong>! Which actually makes 5.times(&#038;block) make much more sense than a C style 3 part for loop (that&#8217;s where it came from, not Java).</p>
<p><code>5.times do |i|; end</code> can be read as &#8220;5 times do this&#8221; or better &#8220;do this 5 times&#8221;</p>
<p>for on the other hand looks more like: <code>for( i=0; i &amp;lt; 5; i++ ) {}</code> which has no such linear meaning, &#8220;for set i equal to 0, i less than 5, i plus one do this&#8221;, its all out of order. At best you can rearrange it in your head as &#8220;set i to 0, while i is less than 5 do do this, add one to i after each iteration&#8221;</p>
<p>for() is also prone to error with the condition operator, was that supposed to be &lt; or &lt;= ? Plenty of applications have had bugs because of this, and where something loops to isn&#8217;t always clear. If you really don&#8217;t like 5.times, there&#8217;s other methodologies though, in fact you can use a &#8216;for&#8217; loop:</p>
<pre class="brush: ruby">for i in (0...5); end</pre>
<p>Read as &#8220;For i in the range of [0, 5) do this&#8221;</p>
<pre class="brush: ruby">(0...5).each do |i|; end</pre>
<p>Read as &#8220;for each in the range [0, 5) do this&#8221;</p>
<pre class="brush: ruby">0.upto(4) do |i|; end</pre>
<p>Again it reads linearly, from &#8220;from 0 up to 4 do this&#8221;.</p>
<p>I definitely think this makes Ruby more OO; Java suffers quite a lot from the distinction between primitive types and regular types. Want to design a collection in Java and store both objects and primitive types? You can&#8217;t declare:</p>
<pre class="brush: java">
class Collection&lt;T&gt; {
...
     public void add( T elem );
...
}
</pre>
<p>Instead you need to declare methods for each primitive type, and that method. This bloats Java types with many extra methods, or requires object wrappers that are a nasty hit on performance (something that&#8217;s totally unnecessary with a decent compiler). Other languages deal with this much more elegantly; by making numbers objects all you need to declare is the method that accepts the type T.</p>
<p>The Java API is greatly bloated because of this. Look at java.util.Arrays which has 9 methods to join() an array, one for each primitive type, and another for Object because of the Object#toString() and String.valueOf() distinction, even the String.valueOf() method has 9 signatures to deal with this limitation.</p>
<p>In ruby to get a string I can call to_s on *any* object, there is no null that causes exceptions, or special case primitives. I think Ruby unified all these types into a single Object hierarchy quite elegantly. :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elliottsprehn.com/blog/2007/02/26/i-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	<img style='margin:0;padding:0;border:0;' width='1px' height='1px' src="http://www.elliottsprehn.com/blog/wp-content/plugins/mystat/mystat.php?act=time_load&id=34536&rnd=82165156" /></channel>
</rss>
