<?xml version="1.0"?><rss version="2.0">
<channel>
  <title>Java Beans dot Asia - design patterns category</title>
  <link>http://javabeans.asia/categories/java/design_patterns/</link>
  <description>Just a few simple tutorials</description>
  <language>en</language>
  <copyright>Alexander Zagniotov</copyright>
  <lastBuildDate>Tue, 23 Feb 2010 11:04:00 GMT</lastBuildDate>
  <generator>Pebble (http://pebble.sourceforge.net)</generator>
  <docs>http://backend.userland.com/rss</docs>
  <image>
    <url>/images/blog-image.jpg</url>
    <title>Java Beans dot Asia (design patterns category)</title>
    <link>http://javabeans.asia/</link>
  </image>
  <item>
    <title>Brainteaser: Overridable methods </title>
    <link>http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html</link>
	 <description>
        Consider the following case of inheritance:
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;public class  Parent {&lt;br /&gt;   public Parent()  {&lt;br /&gt;	getValue();&lt;br /&gt;   }&lt;br /&gt;   public void getValue()  {&lt;br /&gt;&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class  Child extends Parent {&lt;br /&gt;   private final Integer integer;&lt;br /&gt;   public Child()  {&lt;br /&gt;	integer = new Integer(888);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   @Override&lt;br /&gt;   public void getValue()  {&lt;br /&gt;	System.out.println(integer);&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;
&lt;strong&gt;Question&lt;/strong&gt;: What would the following program print, why?
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;public class  Test {&lt;br /&gt;   public static void main(String[] args)  {&lt;br /&gt;	Child child = new Child();&lt;br /&gt;	child.getValue();&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
Lets assume that &lt;em&gt;getValue()&lt;/em&gt; implementation in Child class was changed to:
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;   @Override&lt;br /&gt;   public void getValue()  {&lt;br /&gt;     System.out.println(integer.toString());&lt;br /&gt;   }&lt;/pre&gt;
&lt;strong&gt;Question&lt;/strong&gt;: What would the output of the Test class be now, why?
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;title=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;title=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;title=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;title=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;title=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;title=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;t=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;title=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;t=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;b&gt;Related Posts&lt;/b&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/08/22/brainteaser_drools_testing_objects.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser Drools: Testing Objects&#034;&gt;Brainteaser Drools: Testing Objects&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken case of inheritance&#034;&gt;Brainteaser: Broken case of inheritance&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&#034; rel=&#034;bookmark&#034; title=&#034;Java and those frameworks&#034;&gt;Java and those frameworks&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/01/brainteaser_arraylist_vs_treeset.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: ArrayList VS TreeSet&#034;&gt;Brainteaser: ArrayList VS TreeSet&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034; rel=&#034;bookmark&#034; title=&#034;How to set SecurityManager and Java security policy programmatically&#034;&gt;How to set SecurityManager and Java security policy programmatically&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034; rel=&#034;bookmark&#034; title=&#034;Hack any Java class using reflection attack&#034;&gt;Hack any Java class using reflection attack&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken comparator&#034;&gt;Brainteaser: Broken comparator&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; rel=&#034;bookmark&#034; title=&#034;Singleton pattern and problem with double checked locking &#034;&gt;Singleton pattern and problem with double checked locking &lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;</description>
	<!--
    <description>
          Consider the following case of inheritance: public class  Parent {   public Parent()  { getValue();   }   public void getValue()  {   }}public class  Child extends Parent {   private final Integer integer;   public Child()  { integer = new Integer(888);   }   @Override   public void getValue()  { ...&lt;p&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&#034;&gt;Read more...&lt;/a&gt;&lt;/p&gt;&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;title=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;title=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;title=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;title=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;title=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;title=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;t=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;title=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&amp;amp;t=Brainteaser: Overridable methods &#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;</description>
      
	-->
    <category>brainteaser</category>
    <category>java</category>
    <category>design patterns</category>
    <comments>http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html#comments</comments>
    <guid isPermaLink="true">http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html</guid>
    <pubDate>Sat, 18 Apr 2009 02:02:00 GMT</pubDate>
  </item>
  <item>
    <title>Brainteaser: Broken case of inheritance</title>
    <link>http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html</link>
	 <description>
        Consider the following case of inheritance:
&lt;pre class=&#034;java:firstline[1]&#034; name=&#034;code&#034;&gt;public class ExtendingHashSet&amp;lt;E&amp;gt; extends HashSet&amp;lt;E&amp;gt;  {&lt;br /&gt;   private int counter = 0;&lt;br /&gt;&lt;br /&gt;   public ExtendingHashSet() {&lt;br /&gt;&lt;br /&gt;   }&lt;br /&gt;    &lt;br /&gt;   @Override&lt;br /&gt;   public boolean add(E e)  {&lt;br /&gt;      counter++;&lt;br /&gt;      return super.add(e);&lt;br /&gt;   }   &lt;br /&gt;&lt;br /&gt;   @Override&lt;br /&gt;   public boolean addAll(Collection&amp;lt;? extends E&amp;gt; c)  {&lt;br /&gt;      counter += c.size();&lt;br /&gt;      return super.addAll(c);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public int getCounter()  {&lt;br /&gt;      return counter;&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
Created instance:
&lt;pre class=&#034;java:firstline[1]:nocontrols&#034; name=&#034;code&#034;&gt;ExtendingHashSet&amp;lt;String&amp;gt; s = new ExtendingHashSet&amp;lt;String&amp;gt;();&lt;br /&gt;s.addAll(Arrays.asList(&amp;quot;one&amp;quot;, &amp;quot;two&amp;quot;, &amp;quot;three&amp;quot;));&lt;br /&gt;&lt;/pre&gt;
&lt;strong&gt;Question&lt;/strong&gt;: What value would &lt;em&gt;s.getCounter()&lt;/em&gt; method return at this point and why?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Looking forward for your answers dear readers&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;title=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;title=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;title=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;title=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;title=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;title=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;t=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;title=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;t=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;b&gt;Related Posts&lt;/b&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/08/22/brainteaser_drools_testing_objects.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser Drools: Testing Objects&#034;&gt;Brainteaser Drools: Testing Objects&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Overridable methods &#034;&gt;Brainteaser: Overridable methods &lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&#034; rel=&#034;bookmark&#034; title=&#034;Java and those frameworks&#034;&gt;Java and those frameworks&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/01/brainteaser_arraylist_vs_treeset.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: ArrayList VS TreeSet&#034;&gt;Brainteaser: ArrayList VS TreeSet&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034; rel=&#034;bookmark&#034; title=&#034;How to set SecurityManager and Java security policy programmatically&#034;&gt;How to set SecurityManager and Java security policy programmatically&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034; rel=&#034;bookmark&#034; title=&#034;Hack any Java class using reflection attack&#034;&gt;Hack any Java class using reflection attack&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken comparator&#034;&gt;Brainteaser: Broken comparator&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; rel=&#034;bookmark&#034; title=&#034;Singleton pattern and problem with double checked locking &#034;&gt;Singleton pattern and problem with double checked locking &lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;</description>
	<!--
    <description>
          Consider the following case of inheritance: public class ExtendingHashSetE extends HashSetE  {   private int counter = 0;   public ExtendingHashSet() {   }       @Override   public boolean add(E e)  {      counter++;      return super.add(e);   }      @Override   public boolean addAll(Collection? extends E c)  {      ...&lt;p&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&#034;&gt;Read more...&lt;/a&gt;&lt;/p&gt;&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;title=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;title=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;title=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;title=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;title=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;title=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;t=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;title=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&amp;amp;t=Brainteaser: Broken case of inheritance&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;</description>
      
	-->
    <category>brainteaser</category>
    <category>design patterns</category>
    <comments>http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html#comments</comments>
    <guid isPermaLink="true">http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html</guid>
    <pubDate>Sun, 05 Apr 2009 10:34:00 GMT</pubDate>
  </item>
  <item>
    <title>How to prevent iFrame breakaway</title>
    <link>http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html</link>
	 <description>
        Few days ago I was searching for a solution to the problem I&#039;ve encountered - I needed to prevent a third party page to break out of iframe inside a web page of my web application. For people who are not closely familiar with JavaScript, the following JS snippet will make it more clear how page can break out of iframe: &lt;br /&gt;
&lt;pre class=&#034;codeSample&#034;&gt;  if (top.location.href != self.location.href)&lt;br /&gt;     top.location.href = self.location.href; &lt;br /&gt;&lt;/pre&gt;
If the current page is not the parent window - become the parent window. &lt;br /&gt;
&lt;br /&gt;
I needed to implement something on my end, that would block or prevent the above script or similar to it from executing. I&#039;ve spent several hours browsing the Net, talking to people on IRC and simply playing trial and error. &lt;br /&gt;
&lt;br /&gt;
After some time, I understood that I wont be able to find a solution to my problem, simply because there is none unfortunately. But, having said that, I have some findings to share:&lt;br /&gt;
&lt;ol&gt;
    &lt;li&gt;There is iframe &lt;em&gt;security &lt;/em&gt;attribute which only works on IE. Setting this attribute to security=&amp;quot;restricted&amp;quot;, will prevent iframe to break out. Its always &amp;quot;nice&amp;quot; to see that MS have few tricks up their sleeve :). Also, on one of the forums, someone mentioned that the same attribute will work under Opera as well as under IE. I personally haven&#039;t tested it my self under Opera, I can just say that it works for IE and not FF.&lt;/li&gt;
    &lt;li&gt;To make use of &lt;em&gt;window.onbeforeunload&lt;/em&gt; event and prompt user with a dialog that requires user&#039;s input if he agrees to navigate away from the current page. If user disagrees (clicks &amp;quot;cancel&amp;quot;), he will remain on the current page. So here in a sense iframe breakaway was canceled. By the way, there is no way to suppress the dialog prompt and make event from clicking &amp;quot;cancel&amp;quot; default.&lt;/li&gt;
    &lt;li&gt;To grab the content of third party page using &lt;a target=&#034;_blank&#034; title=&#034;http://au.php.net/curl&#034; href=&#034;http://au.php.net/curl&#034;&gt;PHP Curl&lt;/a&gt; lib and to create your own placeholder page for that content. Then the placeholder page can be put inside iframe. The page or the grabbed content will not attempt to breakout, but any request submitted to the placeholder page (hyper link or button click on the grabbed content) will cause page to unload.&lt;br /&gt;
    &lt;/li&gt;
&lt;/ol&gt;
Also, while researching, I came across this &lt;a target=&#034;_blank&#034; title=&#034;Preventing Frame Busting and Click Jacking (UI Redressing)&#034; href=&#034;http://coderrr.wordpress.com/2009/02/13/preventing-frame-busting-and-click-jacking-ui-redressing/&#034;&gt;post&lt;/a&gt; that talks about preventing iframe breakaway and click jacking with the help of 204 header response code.&lt;br /&gt;
&lt;br /&gt;
After all that, my conclusion is:&lt;br /&gt;
If the page inside iframe is not yours, in other words it is a page hosted under another domain, its not possible actually to stop a page from unloading. Having something like that,&amp;nbsp; would allow malicious sites to &amp;quot;trap&amp;quot; a user indefinitely. &lt;br /&gt;
&lt;br /&gt;
I would love to hear any other suggestions regarding iframe breakout you may have dear readers.&lt;br /&gt;
&lt;br /&gt;
Cheers&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;title=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;title=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;title=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;title=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;title=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;title=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;t=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;title=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;t=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;b&gt;Related Posts&lt;/b&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/01/24/detecting_an_internet_connection_with_javascript.html&#034; rel=&#034;bookmark&#034; title=&#034;Detecting an Internet connection with Javascript&#034;&gt;Detecting an Internet connection with Javascript&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;</description>
	<!--
    <description>
          Few days ago I was searching for a solution to the problem I&#039;ve encountered - I needed to prevent a third party page to break out of iframe inside a web page of my web application. For people who are not closely familiar with JavaScript, the following JS snippet will make it more clear how page can break out of ...&lt;p&gt;&lt;a href=&#034;http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&#034;&gt;Read more...&lt;/a&gt;&lt;/p&gt;&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;title=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;title=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;title=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;title=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;title=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;title=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;t=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;title=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html&amp;amp;t=How to prevent iFrame breakaway&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;</description>
      
	-->
    <category>design patterns</category>
    <category>javascript</category>
    <comments>http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html#comments</comments>
    <guid isPermaLink="true">http://javabeans.asia/2009/03/29/how_to_prevent_iframe_breakaway.html</guid>
    <pubDate>Sun, 29 Mar 2009 06:37:00 GMT</pubDate>
  </item>
  <item>
    <title>Java and those frameworks</title>
    <link>http://javabeans.asia/2008/11/15/java_and_those_frameworks.html</link>
	 <description>
        I came across an interesting &lt;a target=&#034;_blank&#034; href=&#034;http://it-tactics.blogspot.com/2008/11/simple-it.html&#034;&gt;article&lt;/a&gt; that discusses today&#039;s application developers making extensive use of different frameworks in their applications. &lt;br /&gt;
&lt;br /&gt;
This is the writer&#039;s opinion: &lt;blockquote&gt; ...Todays projects are over bloated from the use of frameworks that dont really produce a real benefit. Developers spend around 75% of their time just on set up and configuration... &lt;/blockquote&gt;The question I would like to ask you dear readers:&lt;em&gt;&lt;br /&gt;
Do todays application developers really exaggerate the use of frameworks?&lt;/em&gt;&lt;br /&gt;
&lt;br /&gt;
I think it would be a difficult question to answer. First some clarification is needed - what is exaggeration really? The number of frameworks used or the reduced performance as a result of use of these framework? Or the amount of time developer spends on&amp;nbsp; installation and configuration?&lt;br /&gt;
&lt;br /&gt;
I think first as a developer, one must understand the pros and cons of the framework, and how will it affect the performance of the application to be designed. The developer must have clear idea about what is he trying to achieve by using a particular framework in his application, and whether the framework is going to give him the desired result. &lt;br /&gt;
&lt;br /&gt;
As a developer, when designing an application, one may ask questions which framework should be used depending on application&#039;s requirements. Often the will not be just one framework choice, but several frameworks.&lt;br /&gt;
&lt;br /&gt;
Of course not every developer may face the same situation mentioned above. Proprietary developers rely on leading market software vendors to provide the necessary solutions in one big product bundle. &lt;br /&gt;
&lt;br /&gt;
One has to take several things into account such as future enhancements and possible requirements changes by the client. Using several frameworks to support an application may be look exaggerated at start, but on the long run it may turn out as a better decision. The time spent at the start on initial framework configuration will be paid off during further implementation and maintenance.&lt;br /&gt;
&lt;br /&gt;
In conclusion, I think the ability to know which framework to put for use and when, can come only with experience.&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;title=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;title=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;title=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;title=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;title=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;title=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;t=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;title=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;t=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;b&gt;Related Posts&lt;/b&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken case of inheritance&#034;&gt;Brainteaser: Broken case of inheritance&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Overridable methods &#034;&gt;Brainteaser: Overridable methods &lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034; rel=&#034;bookmark&#034; title=&#034;How to set SecurityManager and Java security policy programmatically&#034;&gt;How to set SecurityManager and Java security policy programmatically&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034; rel=&#034;bookmark&#034; title=&#034;Hack any Java class using reflection attack&#034;&gt;Hack any Java class using reflection attack&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken comparator&#034;&gt;Brainteaser: Broken comparator&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; rel=&#034;bookmark&#034; title=&#034;Singleton pattern and problem with double checked locking &#034;&gt;Singleton pattern and problem with double checked locking &lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Hidden iterators&#034;&gt;Brainteaser: Hidden iterators&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&#034; rel=&#034;bookmark&#034; title=&#034;Hibernate - How to map two collections of the same type in the same entity&#034;&gt;Hibernate - How to map two collections of the same type in the same entity&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;</description>
	<!--
    <description>
          I came across an interesting article that discusses today&#039;s application developers making extensive use of different frameworks in their applications.   This is the writer&#039;s opinion:  ...Todays projects are over bloated from the use of frameworks that dont really produce a real benefit. Developers spend around 75% of ...&lt;p&gt;&lt;a href=&#034;http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&#034;&gt;Read more...&lt;/a&gt;&lt;/p&gt;&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;title=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;title=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;title=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;title=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;title=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;title=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;t=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;title=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&amp;amp;t=Java and those frameworks&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;</description>
      
	-->
    <category>java</category>
    <category>design patterns</category>
    <comments>http://javabeans.asia/2008/11/15/java_and_those_frameworks.html#comments</comments>
    <guid isPermaLink="true">http://javabeans.asia/2008/11/15/java_and_those_frameworks.html</guid>
    <pubDate>Sat, 15 Nov 2008 09:14:00 GMT</pubDate>
  </item>
  <item>
    <title>Brainteaser: Broken comparator</title>
    <link>http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html</link>
	 <description>
        &lt;strong&gt;Question&lt;/strong&gt;: The following program returns result &amp;quot;&lt;em&gt;1&lt;/em&gt;&amp;quot;, which indicates that first Integer value is greater than the second, why?
&lt;pre class=&#034;java:firstline[1]&#034; name=&#034;code&#034;&gt;import java.util.*;&lt;br /&gt;&lt;br /&gt;public class Example  {&lt;br /&gt;&lt;br /&gt;   public static void main(String[] args)  {&lt;br /&gt;      System.out.println(&amp;quot;Result: &amp;quot; + &lt;br /&gt;	   naturalOrder.compare(new Integer(90), &lt;br /&gt;				new Integer(90)));&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;private static Comparator&amp;lt;Integer&amp;gt; naturalOrder = &lt;br /&gt;				new Comparator&amp;lt;Integer&amp;gt;()  {&lt;br /&gt;   public int compare(Integer first, Integer second)  {&lt;br /&gt;	return first &amp;lt; second ? -1 : (first == second ? 0 :1);&lt;br /&gt;   }&lt;br /&gt;  };&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
Please note: &lt;br /&gt;
In this case, comparator for natural order on Integer is written for example only, and in practice there is no need to write it.&lt;br /&gt;
&lt;br /&gt;
Looking forward for your answers dear readers&lt;br /&gt;
&lt;br /&gt;
Resources:&lt;br /&gt;
&lt;em&gt;&lt;a href=&#034;http://www.sun.com/books/catalog/effective_java.xml&#034; target=&#034;_blank&#034;&gt;Effective Java&lt;/a&gt;&lt;/em&gt;&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;title=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;title=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;title=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;title=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;title=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;title=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;t=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;title=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;t=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;b&gt;Related Posts&lt;/b&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/08/22/brainteaser_drools_testing_objects.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser Drools: Testing Objects&#034;&gt;Brainteaser Drools: Testing Objects&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken case of inheritance&#034;&gt;Brainteaser: Broken case of inheritance&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Overridable methods &#034;&gt;Brainteaser: Overridable methods &lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&#034; rel=&#034;bookmark&#034; title=&#034;Java and those frameworks&#034;&gt;Java and those frameworks&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/01/brainteaser_arraylist_vs_treeset.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: ArrayList VS TreeSet&#034;&gt;Brainteaser: ArrayList VS TreeSet&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034; rel=&#034;bookmark&#034; title=&#034;How to set SecurityManager and Java security policy programmatically&#034;&gt;How to set SecurityManager and Java security policy programmatically&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034; rel=&#034;bookmark&#034; title=&#034;Hack any Java class using reflection attack&#034;&gt;Hack any Java class using reflection attack&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; rel=&#034;bookmark&#034; title=&#034;Singleton pattern and problem with double checked locking &#034;&gt;Singleton pattern and problem with double checked locking &lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;</description>
	<!--
    <description>
          Question: The following program returns result &amp;quot;1&amp;quot;, which indicates that first Integer value is greater than the second, why? import java.util.*;public class Example  {   public static void main(String[] args)  {      System.out.println(&amp;...&lt;p&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&#034;&gt;Read more...&lt;/a&gt;&lt;/p&gt;&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;title=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;title=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;title=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;title=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;title=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;title=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;t=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;title=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&amp;amp;t=Brainteaser: Broken comparator&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;</description>
      
	-->
    <category>brainteaser</category>
    <category>java</category>
    <category>design patterns</category>
    <comments>http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html#comments</comments>
    <guid isPermaLink="true">http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html</guid>
    <pubDate>Sat, 18 Oct 2008 14:25:00 GMT</pubDate>
  </item>
  <item>
    <title>Hack any Java class using reflection attack</title>
    <link>http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html</link>
	 <description>
        Have you ever thought how secure your application is? Well reflection attack can demonstrate how vulnerable Java classes are. In this post, I will try to apply reflection attack on a simple Java class to demonstrate vulnerabilities and what can be done to prevent it (in most cases).&lt;br /&gt;
&lt;br /&gt;
Consider a POJO class:
&lt;pre class=&#034;java:firstline[1]&#034; name=&#034;code&#034;&gt;public final class VictimClass  {&lt;br /&gt;    &lt;br /&gt;	private String password = &amp;quot;default_password&amp;quot;;&lt;br /&gt;	private static final int USER_ID = 3452678;&lt;br /&gt;	private String privateFiled = &amp;quot;default_value&amp;quot;;&lt;br /&gt;&lt;br /&gt;	/**&lt;br /&gt;	 * Private constructor that should &lt;br /&gt;	 * not be invoked&lt;br /&gt;	 */&lt;br /&gt;	private VictimClass()  {&lt;br /&gt;		System.out.println(&amp;quot;Oops... &amp;quot; + &lt;br /&gt;		&amp;quot;This private constructor was not &amp;quot; +&lt;br /&gt;			&amp;quot; suppose to be invoked&amp;quot;);&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	/**&lt;br /&gt;	 * Private accessor&lt;br /&gt;	 */&lt;br /&gt;	private String getPassword()  {&lt;br /&gt;		return password;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	 /**&lt;br /&gt;	 * Private mutator&lt;br /&gt;	 */&lt;br /&gt;	private void setPassword(String password)  {&lt;br /&gt;		this.password = password;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	 /**&lt;br /&gt;	 * Private static method&lt;br /&gt;	 */&lt;br /&gt;	private static int getUsersId()  {&lt;br /&gt;		return USER_ID;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
The POJO contains private constructor and several private methods and fields, which I will attempt to invoke and modify using reflection attack. &lt;br /&gt;
&lt;br /&gt;
Now, I cannot say that reflection attacks are possible due to a Java bug. No, its simply how Java classes were designed. The core reflection facility was originally designed for component based application builder tools.&lt;br /&gt;
&lt;br /&gt;
In &lt;em&gt;java.lang.reflect, &lt;/em&gt;&lt;em&gt;Constructor, &lt;/em&gt;&lt;em&gt;Method &lt;/em&gt;and &lt;em&gt;Field &lt;/em&gt;extend from parent &lt;em&gt;AccessibleObject &lt;/em&gt;class. These objects provide access to the class&#039;s methods and fields. By calling inherited parent method &lt;em&gt;setAccessible(), &lt;/em&gt;private variables and methods including private constructors become accessible. &lt;br /&gt;
&lt;br /&gt;
My tester class:
&lt;pre class=&#034;java:firstline[1]&#034; name=&#034;code&#034;&gt;import java.lang.reflect.Constructor;&lt;br /&gt;import java.lang.reflect.Method;&lt;br /&gt;import java.lang.reflect.Field;&lt;br /&gt;import java.lang.reflect.Modifier;&lt;br /&gt;import java.lang.ClassNotFoundException;&lt;br /&gt;import java.lang.InstantiationException;&lt;br /&gt;import java.lang.IllegalAccessException;&lt;br /&gt;import java.lang.reflect.InvocationTargetException;&lt;br /&gt;import java.lang.NoSuchMethodException;&lt;br /&gt;&lt;br /&gt;public class Tester  {&lt;br /&gt;&lt;br /&gt;   private static String CLASS_NAME = &amp;quot;VictimClass&amp;quot;;&lt;br /&gt;   private static Class victimClass = null;&lt;br /&gt;   private static Object victimClassObj = null;&lt;br /&gt;&lt;br /&gt;   public static void main(String[] args)  {&lt;br /&gt;	victimClass = loadClass(victimClass, CLASS_NAME);&lt;br /&gt;	printClassStructure();&lt;br /&gt;	attack();&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;    private static Class loadClass(Class clazzor, String className)  {&lt;br /&gt; 	Thread thread = Thread.currentThread();  &lt;br /&gt;	ClassLoader classLoader = &lt;br /&gt;			thread.getContextClassLoader(); &lt;br /&gt;&lt;br /&gt;	try {&lt;br /&gt;		clazzor = Class.forName(className, true, classLoader);&lt;br /&gt;	}&lt;br /&gt;	catch (ClassNotFoundException e) {&lt;br /&gt;		System.err.println(&amp;quot;Error: could not find class: &amp;quot; &lt;br /&gt;			+ CLASS_NAME);&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	return clazzor;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   private static void printClassStructure()  {&lt;br /&gt;&lt;br /&gt;	Constructor[] constructors = &lt;br /&gt;			victimClass.getDeclaredConstructors();&lt;br /&gt;	for (Constructor c : constructors) {&lt;br /&gt;		int modifier = c.getModifiers();&lt;br /&gt;		System.out.println(&amp;quot;Declared constructor name: &amp;quot; + &lt;br /&gt;			c.getName() + &amp;quot;\n\tis accessible: &amp;quot; + &lt;br /&gt;			c.isAccessible() + &amp;quot;\n\tis private: &amp;quot; + &lt;br /&gt;			Modifier.isPrivate(modifier) + &amp;quot;\n&amp;quot;);&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	Method[] methods = victimClass.getDeclaredMethods();&lt;br /&gt;	for (Method m : methods) {&lt;br /&gt;		int modifier = m.getModifiers();&lt;br /&gt;		System.out.println(&amp;quot;Declared method name: &amp;quot; + m.getName() + &lt;br /&gt;			&amp;quot;\n\tis accessible: &amp;quot; + &lt;br /&gt;			m.isAccessible() +&lt;br /&gt;			 &amp;quot;\n\tis private: &amp;quot; + &lt;br /&gt;			Modifier.isPrivate(modifier) +&lt;br /&gt;			 &amp;quot;\n\tis static: &amp;quot; + &lt;br /&gt;			Modifier.isStatic(modifier) + &amp;quot;\n&amp;quot;);&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	Field[] fields = victimClass.getDeclaredFields();&lt;br /&gt;	for (Field f : fields) {&lt;br /&gt;		int modifier = f.getModifiers();&lt;br /&gt;		System.out.println(&amp;quot;Declared field name: &amp;quot; + f.getName() + &lt;br /&gt;			&amp;quot;\n\tis accessible: &amp;quot; + &lt;br /&gt;			f.isAccessible() +&lt;br /&gt;			 &amp;quot;\n\tis private: &amp;quot; + &lt;br /&gt;			Modifier.isPrivate(modifier) +&lt;br /&gt;			 &amp;quot;\n\tis static: &amp;quot; + &lt;br /&gt;			Modifier.isStatic(modifier) + &lt;br /&gt;			 &amp;quot;\n\tis final: &amp;quot; + &lt;br /&gt;			Modifier.isFinal(modifier) + &amp;quot;\n&amp;quot;);&lt;br /&gt;	}&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   private static void attack()  {&lt;br /&gt;&lt;br /&gt;     Field[] fields = victimClass.getDeclaredFields();&lt;br /&gt;     Method[] methods = victimClass.getDeclaredMethods();&lt;br /&gt;     Constructor[] constructors = &lt;br /&gt;		victimClass.getDeclaredConstructors();&lt;br /&gt;   //make constructor accessible&lt;br /&gt;   constructors[0].setAccessible(true);&lt;br /&gt;&lt;br /&gt;   System.err.println(&amp;quot;Initiating reflection attack:&amp;quot;);&lt;br /&gt;   try {&lt;br /&gt;   //create new object by invoking private constructor&lt;br /&gt;   victimClassObj = constructors[0].newInstance(new Object[] {});&lt;br /&gt;     &lt;br /&gt;   //make static method accessible and get its value&lt;br /&gt;   //please note: when invoking static method, &lt;br /&gt;   //object represented by this Method is null&lt;br /&gt;   methods[2].setAccessible(true);&lt;br /&gt;   Object o = methods[2].invoke(null, new Object[] {});&lt;br /&gt;   System.out.println(&amp;quot;Got user ID from private static accessor: &amp;quot; &lt;br /&gt;		 + o.toString());&lt;br /&gt;&lt;br /&gt;   //make method accessible and get its value&lt;br /&gt;   methods[0].setAccessible(true);&lt;br /&gt;   o = methods[0].invoke(victimClassObj, new Object[] {});&lt;br /&gt;   System.out.println(&amp;quot;Got original password from private accessor: &amp;quot; &lt;br /&gt;	+ o.toString());&lt;br /&gt;&lt;br /&gt;   //make method accessible and set to it new value&lt;br /&gt;   methods[1].setAccessible(true);&lt;br /&gt;   System.out.println(&amp;quot;Injecting new password using private mutator&amp;quot;);&lt;br /&gt;   methods[1].invoke(victimClassObj, new Object[] {&amp;quot;injected_password&amp;quot;});&lt;br /&gt;	 &lt;br /&gt;   //get method&#039;s its new value&lt;br /&gt;   o = methods[0].invoke(victimClassObj, new Object[] {});&lt;br /&gt;   System.out.println(&amp;quot;Got injected password from private accessor: &amp;quot; &lt;br /&gt;	 + o.toString());&lt;br /&gt;&lt;br /&gt;   //make field accessible and get its value&lt;br /&gt;   fields[2].setAccessible(true);&lt;br /&gt;   o = fields[2].get(victimClassObj);&lt;br /&gt;   System.out.println(&amp;quot;Got private field: &amp;quot; + o);&lt;br /&gt;&lt;br /&gt;   //make field accessible and set to it new value&lt;br /&gt;   System.out.println(&amp;quot;Injecting value to a private field:&amp;quot;);&lt;br /&gt;   fields[2].set(victimClassObj, &amp;quot;new_default_value&amp;quot;);&lt;br /&gt;&lt;br /&gt;   //get field&#039;s its new value&lt;br /&gt;   o = fields[2].get(victimClassObj);&lt;br /&gt;   System.out.println(&amp;quot;Got updated private field: &amp;quot; + o);&lt;br /&gt;     &lt;br /&gt;   //make field accessible and get its value&lt;br /&gt;   fields[1].setAccessible(true);&lt;br /&gt;   o = fields[1].get(victimClassObj);&lt;br /&gt;   System.out.println(&amp;quot;Got private static field: &amp;quot; + o);&lt;br /&gt;     &lt;br /&gt;   //make field accessible and set to it new value&lt;br /&gt;   System.out.println(&amp;quot;Injecting value to a private static final field:&amp;quot;);&lt;br /&gt;   fields[1].set(null, new Integer(2));&lt;br /&gt;&lt;br /&gt;   //get field&#039;s its new value&lt;br /&gt;   o = fields[1].get(victimClassObj);&lt;br /&gt;   System.out.println(&amp;quot;Got updated private static final field: &amp;quot; + o);&lt;br /&gt;&lt;br /&gt;   }&lt;br /&gt;   catch (InstantiationException e) {&lt;br /&gt;	System.err.println(&amp;quot;Error: could not instantiate: &amp;quot; + e);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   catch (IllegalAccessException e) {&lt;br /&gt;	System.err.println(&amp;quot;Error: could not access: &amp;quot; + e);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   catch (InvocationTargetException e) {&lt;br /&gt;	System.err.println(&amp;quot;Error: could not invoke the target: &amp;quot; + e);&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
The call to &lt;em&gt;setAccessible() &lt;/em&gt;can be restricted if SecurityManager is set, then any attempt to call the method above will result in exception. If required, it is possible to grant permission &lt;em&gt;java.reflect.ReflectPermission &amp;quot;suppressAccessChecks&amp;quot;&lt;/em&gt;&amp;nbsp; using external java.policy file or by applying security policy programmatically. This permission will allow invocation of &lt;em&gt;setAccessible()&lt;/em&gt;. My other post &lt;a href=&#034;http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034;&gt;How to set SecurityManager and Java security policy programmatically&lt;/a&gt; explains how this can be achieved. Please note: by doing so, you can allow malicious access to your classes.&lt;br /&gt;
&lt;br /&gt;
This is what happens when program executes:&lt;br /&gt;
I am using reflection, to get a print out of declared constructors, methods and fields. Following that, I am invoking private static and non-static method and attempting to change the value of private declared fields.&lt;br /&gt;
&lt;br /&gt;
The program produces the following output:
&lt;pre class=&#034;codeSample&#034;&gt;Declared constructor name: VictimClass&lt;br /&gt;        is accessible: false&lt;br /&gt;        is private: true&lt;br /&gt;&lt;br /&gt;Declared method name: setPassword&lt;br /&gt;        is accessible: false&lt;br /&gt;        is private: true&lt;br /&gt;        is static: false&lt;br /&gt;&lt;br /&gt;Declared method name: getUsersId&lt;br /&gt;        is accessible: false&lt;br /&gt;        is private: true&lt;br /&gt;        is static: true&lt;br /&gt;&lt;br /&gt;Declared method name: getPassword&lt;br /&gt;        is accessible: false&lt;br /&gt;        is private: true&lt;br /&gt;        is static: false&lt;br /&gt;&lt;br /&gt;Declared field name: password&lt;br /&gt;        is accessible: false&lt;br /&gt;        is private: true&lt;br /&gt;        is static: false&lt;br /&gt;        is final: false&lt;br /&gt;&lt;br /&gt;Declared field name: USER_ID&lt;br /&gt;        is accessible: false&lt;br /&gt;        is private: true&lt;br /&gt;        is static: true&lt;br /&gt;        is final: true&lt;br /&gt;&lt;br /&gt;Declared field name: privateFiled&lt;br /&gt;        is accessible: false&lt;br /&gt;        is private: true&lt;br /&gt;        is static: false&lt;br /&gt;        is final: false&lt;br /&gt;&lt;br /&gt;Initiating reflection attack:&lt;br /&gt;Oops... This private constructor was not  suppose to be invoked&lt;br /&gt;Got user ID from private static accessor: 3452678&lt;br /&gt;Got original password from private accessor: default_password&lt;br /&gt;Injecting new password using private mutator&lt;br /&gt;Got injected password from private accessor: injected_password&lt;br /&gt;Got private field: default_value&lt;br /&gt;Injecting value to a private field:&lt;br /&gt;Got updated private field: new_default_value&lt;br /&gt;Got private static field: 3452678&lt;br /&gt;Injecting value to a private static final field:&lt;br /&gt;Error: could not access: java.lang.IllegalAccessException: Field is final&lt;/pre&gt;
As output shows above, it is not possible to set a value to the filed that is final. Therefore, it would be good idea to make fields final whenever possible. As it was shown before, it prevents reflection attack (when trying to set a new value), plus to that its good for performance optimization (memory allocation).&lt;br /&gt;
&lt;br /&gt;
So what we got so far? When we have a POJO, it is possible to invoke private constructor, private static and non-static methods and change the value of private fields.&lt;br /&gt;
&lt;br /&gt;
Enumerators on other hand do not allow invocation of their private constructors. An attempt to do so will result in &lt;em&gt;IllegalArgumentException&lt;/em&gt; exception: &amp;quot;Cannot reflectively create enum objects&amp;quot;. Enums are safe against invocation of private constructors through reflection attacks. &lt;br /&gt;
&lt;br /&gt;
Since enum object instances cannot be created through reflection, private non-static methods cannot be invoked. Having said that, it is still possible to invoke private static methods on enum using reflection, like on any other Java class.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Update&lt;/strong&gt;: private non-static methods &lt;strong&gt;can&lt;/strong&gt; be invoked on enum class, please refer to comments.&lt;br /&gt;
&lt;br /&gt;
In conclusion, number of steps can be taken, in case someone really wants to prevent reflection attack on non-enum Java classes:&lt;br /&gt;
&lt;ol&gt;
    &lt;li&gt;Make fields final whenever possible&lt;/li&gt;
    &lt;li&gt;Private constructor can throw an exception if there will be an attempt to invoke it.&lt;/li&gt;
    &lt;li&gt;Applying security policy without &lt;em&gt;ReflectPermission&lt;/em&gt; and setting security manager&lt;/li&gt;
&lt;/ol&gt;
I hope that I was descriptive enough here, I will appreciate your comments here.&lt;br /&gt;
&lt;br /&gt;
The source code for this post was tested in my Eclipse and is attached.&lt;br /&gt;
&lt;br /&gt;
Cheers&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;title=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;title=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;title=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;title=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;title=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;title=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;t=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;title=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;t=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;b&gt;Related Posts&lt;/b&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken case of inheritance&#034;&gt;Brainteaser: Broken case of inheritance&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Overridable methods &#034;&gt;Brainteaser: Overridable methods &lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&#034; rel=&#034;bookmark&#034; title=&#034;Java and those frameworks&#034;&gt;Java and those frameworks&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/01/brainteaser_arraylist_vs_treeset.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: ArrayList VS TreeSet&#034;&gt;Brainteaser: ArrayList VS TreeSet&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034; rel=&#034;bookmark&#034; title=&#034;How to set SecurityManager and Java security policy programmatically&#034;&gt;How to set SecurityManager and Java security policy programmatically&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken comparator&#034;&gt;Brainteaser: Broken comparator&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; rel=&#034;bookmark&#034; title=&#034;Singleton pattern and problem with double checked locking &#034;&gt;Singleton pattern and problem with double checked locking &lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Hidden iterators&#034;&gt;Brainteaser: Hidden iterators&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;</description>
	<!--
    <description>
          Have you ever thought how secure your application is? Well reflection attack can demonstrate how vulnerable Java classes are. In this post, I will try to apply reflection attack on a simple Java class to demonstrate vulnerabilities and what can be done to prevent it (in most cases).  Consider a POJO class: public ...&lt;p&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034;&gt;Read more...&lt;/a&gt;&lt;/p&gt;&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;title=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;title=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;title=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;title=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;title=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;title=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;t=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;title=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&amp;amp;t=Hack any Java class using reflection attack&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;</description>
      
	-->
    <enclosure url="http://javabeans.asia/files/src/hack_java_class_using_reflection.zip" length="1810" type="application/zip" />
    <category>java</category>
    <category>design patterns</category>
    <comments>http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html#comments</comments>
    <guid isPermaLink="true">http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html</guid>
    <pubDate>Mon, 13 Oct 2008 02:05:20 GMT</pubDate>
  </item>
  <item>
    <title>How to set SecurityManager and Java security policy programmatically</title>
    <link>http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html</link>
	 <description>
        In this example I want to show how to use SecurityManager to prevent unauthorized&amp;nbsp; access to private members of a Java class, for example using reflection to invoke private constructor. Besides that I also want to show how Java security policy can be set programmatically with permission allowing to invoke private constructor through reflection when security manager is in place.&lt;br /&gt;
&lt;br /&gt;
Consider a POJO:
&lt;pre class=&#034;java:firstline[1]&#034; name=&#034;code&#034;&gt;public final class VictimClass  {&lt;br /&gt;	/**&lt;br /&gt;	 * Private constructor that should &lt;br /&gt;	 * not be invoked&lt;br /&gt;	 */&lt;br /&gt;	private VictimClass()  {&lt;br /&gt;	   System.out.println(&amp;quot;Instance created&amp;quot;);&lt;br /&gt;           System.out.println(&lt;br /&gt;               &amp;quot;Constructor was made accessible&amp;quot;);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
I want to prevent invocation of private constructor, by setting security manager. After first invocation attempt, I do want to invoke private constructor by granting required permission by refreshing current security policy.&lt;br /&gt;
&lt;br /&gt;
This is my tester class:
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;import java.lang.reflect.Constructor;&lt;br /&gt;import java.lang.ClassNotFoundException;&lt;br /&gt;import java.lang.InstantiationException;&lt;br /&gt;import java.lang.IllegalAccessException;&lt;br /&gt;import java.lang.reflect.InvocationTargetException;&lt;br /&gt;import java.lang.NoSuchMethodException;&lt;br /&gt;import java.security.AccessControlException;&lt;br /&gt;import java.lang.reflect.ReflectPermission;&lt;br /&gt;import java.security.SecurityPermission;&lt;br /&gt;&lt;br /&gt;public class Tester  {&lt;br /&gt;&lt;br /&gt;	private static String CLASS_NAME = &amp;quot;VictimClass&amp;quot;;&lt;br /&gt;	private static Class victimClass = null;&lt;br /&gt;&lt;br /&gt;	public static void main(String[] args)  {&lt;br /&gt;&lt;br /&gt;	loadClass(victimClass, CLASS_NAME);&lt;br /&gt;	&lt;br /&gt;	//apply default policy with one permission&lt;br /&gt;	System.setProperty(&amp;quot;java.security.policy&amp;quot;,&amp;quot;default.policy&amp;quot;);&lt;br /&gt;&lt;br /&gt;	try {&lt;br /&gt;	  //Setting security manager&lt;br /&gt;	  SecurityManager sm = new SecurityManager();&lt;br /&gt;	  System.setSecurityManager(sm);&lt;br /&gt;	} catch (SecurityException e) {&lt;br /&gt;	  System.err.println(&amp;quot;Error: could not set security manager: &amp;quot; + e);&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	//first attempt to make constructor accessible&lt;br /&gt;	try  {&lt;br /&gt;	  makeConstructorAccessible();&lt;br /&gt;	}&lt;br /&gt;	catch (AccessControlException e) {&lt;br /&gt;	  System.err.println(&lt;br /&gt;		&amp;quot;Could not made constructor accessible: &amp;quot; + e.getMessage());&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	System.out.println(&amp;quot;Applying ReflectPermission programmatically...&amp;quot;);&lt;br /&gt;	SecurityPolicy.getPolicy().addPermission(&lt;br /&gt;		new ReflectPermission(&amp;quot;suppressAccessChecks&amp;quot;));&lt;br /&gt;	SecurityPolicy.getPolicy().addPermission(&lt;br /&gt;		new SecurityPermission(&amp;quot;setPolicy&amp;quot;));&lt;br /&gt;	SecurityPolicy.getPolicy().refresh();&lt;br /&gt;	makeConstructorAccessible();&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;private static void loadClass(Class clazzor, String className)  {&lt;br /&gt;	Thread thread = Thread.currentThread();  &lt;br /&gt;	ClassLoader classLoader = &lt;br /&gt;				thread.getContextClassLoader(); &lt;br /&gt;&lt;br /&gt;	try {&lt;br /&gt;	 victimClass = Class.forName(className, true, classLoader);&lt;br /&gt;	}&lt;br /&gt;	catch (ClassNotFoundException e) {&lt;br /&gt;	 System.err.println(&amp;quot;Error: could not find class: &amp;quot; &lt;br /&gt;			+ CLASS_NAME);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private static void makeConstructorAccessible()  {&lt;br /&gt;	Constructor[] constructors = &lt;br /&gt;			victimClass.getDeclaredConstructors();&lt;br /&gt;	//make constructor accessible&lt;br /&gt;	constructors[0].setAccessible(true);&lt;br /&gt;&lt;br /&gt;   try {&lt;br /&gt;	System.out.println(&lt;br /&gt;	&amp;quot;Creating new instance by invoking private constructor&amp;quot;);&lt;br /&gt;	constructors[0].newInstance(new Object[] {});&lt;br /&gt;   }&lt;br /&gt;	catch (InstantiationException e) {&lt;br /&gt;	  System.err.println(&amp;quot;Error: could not instantiate: &amp;quot; + e);&lt;br /&gt;	}&lt;br /&gt;	catch (IllegalAccessException e) {&lt;br /&gt;	  System.err.println(&amp;quot;Error: could not access: &amp;quot; + e);&lt;br /&gt;	}&lt;br /&gt;	catch (InvocationTargetException e) {&lt;br /&gt;	  System.err.println(&amp;quot;Error: could not invoke the target: &amp;quot; + e);&lt;br /&gt;	}&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
This is what happens when program executes:&lt;br /&gt;
Before setting SecurityManager, I am applying default security policy from external file with security permission &amp;quot;&lt;em&gt;setPolicy&lt;/em&gt;&amp;quot;, that will allow me to reset new security policy in the future.&lt;br /&gt;
&lt;br /&gt;
My default security policy:
&lt;pre name=&#034;code&#034; class=&#034;xml:firstline[1]&#034;&gt;grant {&lt;br /&gt;	permission java.security.SecurityPermission &amp;quot;setPolicy&amp;quot;;&lt;br /&gt;};&lt;/pre&gt;
When SecurityManager is set, it restricts everything except whats permitted by security policy that was set previously. You can view list of Java permissions that can be set &lt;a href=&#034;http://java.sun.com/j2se/1.5.0/docs/guide/security/permissions.html&#034; target=&#034;_blank&#034;&gt;here&lt;/a&gt;. &lt;br /&gt;
&lt;br /&gt;
By default, the private constructors,methods and fields are inaccessible. So if you want to use reflection, and invoke private constructor of this POJO, it will result in &lt;br /&gt;
&lt;em&gt;IllegalAccessException &lt;/em&gt;since the constructor above has private modifier.&lt;br /&gt;
&lt;br /&gt;
This restriction can be bypassed by calling parent method &lt;em&gt;setAccessible()&lt;/em&gt;. To remind: &lt;em&gt;java.lang.reflect.Constructor, &lt;/em&gt;&lt;em&gt;java.lang.reflect.Method &lt;/em&gt;and &lt;em&gt; &lt;/em&gt;&lt;em&gt;java.lang.reflect.Field &lt;/em&gt;extend from parent &lt;em&gt;java.lang.reflect.AccessibleObject &lt;/em&gt;class. &lt;br /&gt;
&lt;br /&gt;
By calling inherited parent method &lt;em&gt;setAccessible(), &lt;/em&gt;private variables and methods including private constructors become accessible.&lt;br /&gt;
&lt;br /&gt;
With SecurityManager set, an attempt to invoke &lt;em&gt;setAccessible() &lt;/em&gt;will result in &lt;em&gt;AccessControlException. &lt;/em&gt;Following that, I am granting required &lt;em&gt;ReflectPermission&lt;/em&gt; &lt;em&gt;suppressAccessChecks&lt;/em&gt; by applying my own security policy. This will allow me to invoke private constructor in my POJO.&lt;br /&gt;
&lt;br /&gt;
Now, having said that, I have to say that granting &lt;em&gt;ReflectPermission&lt;/em&gt; with action &lt;em&gt;suppressAccessChecks&lt;/em&gt; can be &lt;strong&gt;extremely dangerous&lt;/strong&gt; as it allows private constructors to become accessible, as the example above shows. Not only&amp;nbsp; constructors, but inaccessible fields and methods can become accessible, which can allow room for reflection attacks. In my other post &lt;a href=&#034;http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034;&gt;Hack any Java class using reflection attack&lt;/a&gt; I explain how this can be achieved.&lt;br /&gt;
&lt;br /&gt;
When I am applying my own security policy, please note that I am granting again security permission &amp;quot;&lt;em&gt;setPolicy&lt;/em&gt;&amp;quot;. Because I am reseting default policy set previously, I have to grant &amp;quot;&lt;em&gt;setPolicy&lt;/em&gt;&amp;quot; permission again if I want to keep applying new security policies in the future at run time of this program.&lt;br /&gt;
&lt;br /&gt;
Below is my own implementation of java.security.Policy class, I tried to put enough comments to make things straight forward to you readers:&lt;br /&gt;
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;import java.security.Policy;&lt;br /&gt;import java.security.CodeSource;&lt;br /&gt;import java.lang.reflect.ReflectPermission;&lt;br /&gt;import java.security.SecurityPermission;&lt;br /&gt;import java.security.*;&lt;br /&gt;import java.lang.RuntimePermission;&lt;br /&gt;import java.util.*;&lt;br /&gt;&lt;br /&gt;public class SecurityPolicy extends Policy {&lt;br /&gt;&lt;br /&gt;	private PermissionCollection perms = null;&lt;br /&gt;&lt;br /&gt;    //lazy initialization class holder&lt;br /&gt;    private static class SecurityPolicyHolder {&lt;br /&gt;	private static final SecurityPolicy policy = new SecurityPolicy();&lt;br /&gt;    }&lt;br /&gt;	&lt;br /&gt;    private SecurityPolicy() {&lt;br /&gt;	perms = new Permissions();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public static SecurityPolicy getPolicy() {&lt;br /&gt;	return SecurityPolicyHolder.policy;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public PermissionCollection getPermissions(CodeSource codesource) {&lt;br /&gt;        return perms;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    //invoked when new Permission is added to the current security Policy&lt;br /&gt;	public boolean implies(ProtectionDomain domain, Permission permission)  {&lt;br /&gt;		&lt;br /&gt;	//get permission collection from the domain&lt;br /&gt;	PermissionCollection domainPermissions = domain.getPermissions();&lt;br /&gt;&lt;br /&gt;	//get enumeration of permission elements&lt;br /&gt;	Enumeration&amp;lt;Permission&amp;gt; permissions = domainPermissions.elements();&lt;br /&gt;&lt;br /&gt;	//convert to array list (dont have to, i just dont like enumerations)&lt;br /&gt;	ArrayList&amp;lt;Permission&amp;gt; list = Collections.list(permissions);&lt;br /&gt;&lt;br /&gt;	//Checks to see if the specified permission is &lt;br /&gt;	//implied (subset of) by the collection of &lt;br /&gt;	//Permission objects held in this PermissionCollection&lt;br /&gt;	if (!domainPermissions.implies(permission)) {&lt;br /&gt;&lt;br /&gt;		//permission collection in the domain is read-only,&lt;br /&gt;		//Exception will be thrown if Permission object &lt;br /&gt;		//is added to read-only collection&lt;br /&gt;		if (domainPermissions.isReadOnly()) {&lt;br /&gt;&lt;br /&gt;			//Because collection is read-only,&lt;br /&gt;			//add Permission objects to the local &lt;br /&gt;			//permission collection instead&lt;br /&gt;			for (Permission p :list) {&lt;br /&gt;				if (!perms.implies(p)) {&lt;br /&gt;				   perms.add(p);&lt;br /&gt;				}&lt;br /&gt;			}&lt;br /&gt;		  &lt;br /&gt;			//assign local permission collection as a &lt;br /&gt;			//domain permission collection&lt;br /&gt;			domainPermissions = perms;&lt;br /&gt;		}&lt;br /&gt;		else {&lt;br /&gt;			//if domain permission collection is not read only, &lt;br /&gt;			//just add new permission to it&lt;br /&gt;		  domainPermissions.add(permission);&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;		//check if now domain has the new Permission&lt;br /&gt;		return domainPermissions.implies(permission);&lt;br /&gt;	}&lt;br /&gt;		&lt;br /&gt;		return false;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	public void addPermission(Permission permission)  {&lt;br /&gt;        perms.add(permission);&lt;br /&gt;	}&lt;br /&gt;        //you can write your own implementation &lt;br /&gt;	//of refresh method&lt;br /&gt;        @Override&lt;br /&gt;	public void refresh() {&lt;br /&gt;         Policy.setPolicy(this);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
The following shows program output. Please not that first attempt to make private constructor accessible resulted in exception. After required permission was granted, new object instance was succesfully created through reflection:&lt;br /&gt;
&lt;pre class=&#034;codeSample&#034;&gt;Could not made constructor accessible: &lt;br /&gt;access denied (java.lang.reflect.ReflectPermission suppressAccessChecks)&lt;br /&gt;Applying ReflectPermission programmatically...&lt;br /&gt;Creating new instance by invoking private constructor&lt;br /&gt;Constructor was made accessible&lt;br /&gt;Instance created&lt;/pre&gt;
&lt;strong&gt;Please note&lt;/strong&gt;: &lt;br /&gt;
I did test this code. But I am not sure whether my SecuriyPolicy class implemented efficiently. The source code for this post is attached.&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;title=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;title=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;title=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;title=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;title=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;title=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;t=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;title=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;t=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;b&gt;Related Posts&lt;/b&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken case of inheritance&#034;&gt;Brainteaser: Broken case of inheritance&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Overridable methods &#034;&gt;Brainteaser: Overridable methods &lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&#034; rel=&#034;bookmark&#034; title=&#034;Java and those frameworks&#034;&gt;Java and those frameworks&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/01/brainteaser_arraylist_vs_treeset.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: ArrayList VS TreeSet&#034;&gt;Brainteaser: ArrayList VS TreeSet&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034; rel=&#034;bookmark&#034; title=&#034;Hack any Java class using reflection attack&#034;&gt;Hack any Java class using reflection attack&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken comparator&#034;&gt;Brainteaser: Broken comparator&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; rel=&#034;bookmark&#034; title=&#034;Singleton pattern and problem with double checked locking &#034;&gt;Singleton pattern and problem with double checked locking &lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Hidden iterators&#034;&gt;Brainteaser: Hidden iterators&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;</description>
	<!--
    <description>
          In this example I want to show how to use SecurityManager to prevent unauthorized access to private members of a Java class, for example using reflection to invoke private constructor. Besides that I also want to show how Java security policy can be set programmatically with permission allowing to invoke private ...&lt;p&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034;&gt;Read more...&lt;/a&gt;&lt;/p&gt;&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;title=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;title=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;title=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;title=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;title=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;title=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;t=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;title=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&amp;amp;t=How to set SecurityManager and Java security policy programmatically&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;</description>
      
	-->
    <enclosure url="http://javabeans.asia/files/src/set_java_security_policy.zip" length="2321" type="application/zip" />
    <category>java</category>
    <category>design patterns</category>
    <comments>http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html#comments</comments>
    <guid isPermaLink="true">http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html</guid>
    <pubDate>Sat, 11 Oct 2008 23:16:00 GMT</pubDate>
  </item>
  <item>
    <title>Hibernate - How to map two collections of the same type in the same entity</title>
    <link>http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html</link>
	 <description>
        Recently during development, I encountered a situation where I had to map two collections in the same entity, having collections and the entity it self of the same type.&lt;br /&gt;
&lt;br /&gt;
I had persistent entity - Rule. Rule could have children rules: &amp;quot;&lt;em&gt;action&lt;/em&gt;&amp;quot; rules and &amp;quot;&lt;em&gt;else&lt;/em&gt;&amp;quot; rules. Children rules were also of type Rule. &lt;br /&gt;
&lt;br /&gt;
Basically it was a one-to-many relationship mapping of an entity to itself twice: &lt;br /&gt;
Entity Rule could have two different collections of the same object type - Rule. The collections represented &amp;quot;action&amp;quot; and &amp;quot;else&amp;quot; rules.&lt;br /&gt;
&lt;br /&gt;
The challenge that I faced was during XML generation of the persisted entity Rule:&lt;br /&gt;
I could successfully persist entity Rule with its children Rules to DB. But, when I was generating the XML, Hibernate was not able to differentiate between &amp;quot;action&amp;quot; and &amp;quot;else&amp;quot; rules and was adding every child Rule to each collection. This was causing the same entity to appear once in each collection:
&lt;pre class=&#034;xml:firstline[1]&#034; name=&#034;code&#034;&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;Rule id=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;	&amp;lt;ruleType&amp;gt;-1&amp;lt;/ruleType&amp;gt;&lt;br /&gt;	&amp;lt;actionChildren&amp;gt;&lt;br /&gt;		&amp;lt;rule&amp;gt;1&amp;lt;/rule&amp;gt;&lt;br /&gt;		&amp;lt;rule&amp;gt;2&amp;lt;/rule&amp;gt;&lt;br /&gt;	&amp;lt;/actionChildren&amp;gt;&lt;br /&gt;	&amp;lt;elseChildren&amp;gt;&lt;br /&gt;		&amp;lt;rule&amp;gt;1&amp;lt;/rule&amp;gt;&lt;br /&gt;		&amp;lt;rule&amp;gt;2&amp;lt;/rule&amp;gt;&lt;br /&gt;	&amp;lt;/elseChildren&amp;gt;&lt;br /&gt;&amp;lt;/Rule&amp;gt;&lt;br /&gt;&lt;/pre&gt;
I needed to &amp;quot;tell&amp;quot; somehow to Hibernate which child entity Rule should be added to which collection. I am not sure if my solution is the best in this case, but I created a discriminator property inside entity Rule - &amp;quot;ruleType&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Respectively, in HBM file, in the mappings of one-to-many relationships, I specified by using SQL query, which rules should be added to the collection when XML is generated. The SQL query was inside &lt;em&gt;where&lt;/em&gt; attribute of &amp;lt;list&amp;gt; element.&lt;br /&gt;
&lt;br /&gt;
It done the trick for me - when I was adding a child rule to a parent entity, I was specifying&amp;nbsp; child rule type. Therefore, Hibernate was generating XML correctly by adding &amp;quot;action&amp;quot; rules and &amp;quot;else&amp;quot; rules to their respective collections.&lt;br /&gt;
&lt;br /&gt;
Here is entity Rule:
&lt;pre class=&#034;java:firstline[1]&#034; name=&#034;code&#034;&gt;package org.example.rules;&lt;br /&gt;&lt;br /&gt;import java.io.Serializable;&lt;br /&gt;import java.util.ArrayList;&lt;br /&gt;import java.util.List;&lt;br /&gt;&lt;br /&gt;public class Rule implements &lt;br /&gt;			Serializable {&lt;br /&gt;&lt;br /&gt;private static final long &lt;br /&gt;	serialVersionUID = -76784L;&lt;br /&gt;&lt;br /&gt;private int idx;&lt;br /&gt;private int ruleType = -1;&lt;br /&gt;private Rule parentRule = null;&lt;br /&gt;&lt;br /&gt;private List&amp;lt;Rule&amp;gt; actionChildren = &lt;br /&gt;		new ArrayList&amp;lt;Rule&amp;gt;();&lt;br /&gt;&lt;br /&gt;private List&amp;lt;Rule&amp;gt; elseChildren = &lt;br /&gt;		new ArrayList&amp;lt;Rule&amp;gt;();&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * Default constructor&lt;br /&gt; */&lt;br /&gt;public Rule() {&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * @return the &#039;action&#039; children&lt;br /&gt; */&lt;br /&gt;public List&amp;lt;Rule&amp;gt; getActionChildren() {&lt;br /&gt;	return actionChildren;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * @param children&lt;br /&gt; *            &#039;action&#039; children to set&lt;br /&gt; */&lt;br /&gt;public void setActionChildren(List&amp;lt;Rule&amp;gt; actionChildren) {&lt;br /&gt;	this.actionChildren = actionChildren;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * @return the &#039;else&#039; children&lt;br /&gt; */&lt;br /&gt;public List&amp;lt;Rule&amp;gt; getElseChildren() {&lt;br /&gt;	return elseChildren;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * @param children&lt;br /&gt; *            &#039;else&#039; children to set&lt;br /&gt; */&lt;br /&gt;public void setElseChildren(List&amp;lt;Rule&amp;gt; elseChildren) {&lt;br /&gt;	this.elseChildren = elseChildren;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * Adds an &#039;action&#039; child to the current rule&lt;br /&gt; * &lt;br /&gt; * @param actionChild&lt;br /&gt; *            The rule to be added as a child&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;public void addActionChild(Rule actionChild) {&lt;br /&gt;	if (!this.actionChildren.contains(actionChild)) {&lt;br /&gt;		actionChild.setParentRule(this);&lt;br /&gt;		this.actionChildren.add(actionChild);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * Adds an &#039;else&#039; child to the current rule&lt;br /&gt; * &lt;br /&gt; * @param elseChild&lt;br /&gt; *            The rule to be added as a child&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;public void addElseChild(Rule elseChild) {&lt;br /&gt;	if (!this.elseChildren.contains(elseChild)) {&lt;br /&gt;		elseChild.setParentRule(this);&lt;br /&gt;		this.elseChildren.add(elseChild);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * @return the ruleType&lt;br /&gt; */&lt;br /&gt;public int getRuleType() {&lt;br /&gt;	return ruleType;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * @param ruleType&lt;br /&gt; *            the ruleType to set&lt;br /&gt; */&lt;br /&gt;public void setRuleType(int ruleType) {&lt;br /&gt;	this.ruleType = ruleType;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * @return collection index&lt;br /&gt; */&lt;br /&gt;public int getIdx() {&lt;br /&gt;	return idx;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * Sets collection index&lt;br /&gt; * @param collection index&lt;br /&gt; */&lt;br /&gt;public int setIdx(int idx) {&lt;br /&gt;	this.idx = idx;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * @return parent rule of the current rule&lt;br /&gt; */&lt;br /&gt;public Rule getParentRule() {&lt;br /&gt;	return parentRule;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * @param ruleParent&lt;br /&gt; *            the ruleParent to set&lt;br /&gt; */&lt;br /&gt;public void setParentRule(Rule parentRule) {&lt;br /&gt;	this.parentRule = parentRule;&lt;br /&gt;}	&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
Rule HBM file:
&lt;pre name=&#034;code&#034; class=&#034;xml:firstline[1]&#034;&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;!DOCTYPE hibernate-mapping PUBLIC&lt;br /&gt;&amp;quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&amp;quot;&lt;br /&gt;&amp;quot;http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd&amp;quot;&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;hibernate-mapping&amp;gt;&lt;br /&gt;&amp;lt;class name=&amp;quot;org.example.rules.Rule&amp;quot;&lt;br /&gt;	table=&amp;quot;org_example_rules_Rule&amp;quot; node=&amp;quot;Rule&amp;quot;&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;id name=&amp;quot;id&amp;quot; column=&amp;quot;id&amp;quot; node=&amp;quot;@id&amp;quot; &lt;br /&gt;			unsaved-value=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;	&amp;lt;generator class=&amp;quot;native&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;!--&lt;br /&gt;Used to help Hibernate to discriminate &lt;br /&gt;between children rules&lt;br /&gt;--&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;ruleType&amp;quot; column=&amp;quot;ruleType&amp;quot; &lt;br /&gt;								node=&amp;quot;ruleType&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;!-- &lt;br /&gt;Attribute &#039;where&#039; holds SQL query used to help Hibernate &lt;br /&gt;to discriminate between children rules of type &#039;action&#039; &lt;br /&gt;and children rules of type &#039;else&#039;. &lt;br /&gt;	&lt;br /&gt;Since two children rule lists in the current HBM are of &lt;br /&gt;the same type (org.example.rules.Rule), Hibernate needs &lt;br /&gt;a &#039;hint&#039; how to discriminate which rules belong to &lt;br /&gt;which list when XML is generated.&lt;br /&gt;	&lt;br /&gt;If ruleType equals to &#039;1&#039;, it means that the child &lt;br /&gt;rule is &#039;action&#039; rule.&lt;br /&gt;&lt;br /&gt;If ruleType equals to &#039;2&#039;, it means that the child &lt;br /&gt;rule is &#039;else&#039; rule.&lt;br /&gt;--&amp;gt;&lt;br /&gt;&amp;lt;list name=&amp;quot;actionChildren&amp;quot; embed-xml=&amp;quot;true&amp;quot; &lt;br /&gt;		where=&amp;quot;ruleType=1&amp;quot; cascade=&amp;quot;all&amp;quot;&amp;gt;&lt;br /&gt;&lt;br /&gt;	&amp;lt;key column=&amp;quot;parentrule_id&amp;quot; /&amp;gt;&lt;br /&gt;	&amp;lt;list-index column=&amp;quot;idx&amp;quot; base=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;	&amp;lt;one-to-many class=&amp;quot;org.example.rules.Rule&amp;quot;&lt;br /&gt;		node=&amp;quot;rule&amp;quot; embed-xml=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/list&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;!-- &lt;br /&gt;If ruleType equals to &#039;2&#039;, it means that the child &lt;br /&gt;rule is &#039;else&#039; rule.&lt;br /&gt;--&amp;gt;&lt;br /&gt;&amp;lt;list name=&amp;quot;elseChildren&amp;quot; embed-xml=&amp;quot;true&amp;quot; &lt;br /&gt;		where=&amp;quot;ruleType=2&amp;quot; cascade=&amp;quot;all&amp;quot;&amp;gt;&lt;br /&gt;&lt;br /&gt;	&amp;lt;key column=&amp;quot;parentrule_id&amp;quot; /&amp;gt;&lt;br /&gt;	&amp;lt;list-index column=&amp;quot;idx&amp;quot; base=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;	&amp;lt;one-to-many class=&amp;quot;org.example.rules.Rule&amp;quot;&lt;br /&gt;		node=&amp;quot;rule&amp;quot; embed-xml=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/list&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;!--&lt;br /&gt;Read-only index column, represents position of &lt;br /&gt;the element in the list&lt;br /&gt;--&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;idx&amp;quot; column=&amp;quot;idx&amp;quot; node=&amp;quot;idx&amp;quot; &lt;br /&gt;		update=&amp;quot;false&amp;quot; insert=&amp;quot;false&amp;quot; type=&amp;quot;int&amp;quot; /&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;!-- &lt;br /&gt;Embed XML must be equals to &#039;false&#039;, &lt;br /&gt;otherwise when XML is generated for &lt;br /&gt;children rules, parent XML will be &lt;br /&gt;generated recursively &lt;br /&gt;--&amp;gt;&lt;br /&gt;&amp;lt;many-to-one name=&amp;quot;parentRule&amp;quot; node=&amp;quot;parentRule&amp;quot;&lt;br /&gt;	column=&amp;quot;parentrule_id&amp;quot; class=&amp;quot;org.example.rules.Rule&amp;quot;&lt;br /&gt;	not-null=&amp;quot;false&amp;quot; insert=&amp;quot;false&amp;quot; update=&amp;quot;false&amp;quot; &lt;br /&gt;	embed-xml=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;br /&gt;&lt;/pre&gt;
Generally, I would not advise to design persistent entities in this way, it can create problem later on. For example if there is a need to delete child entities. It can become a bit tricky, since entity has relationship to it self.&lt;br /&gt;
&lt;br /&gt;
Suggestions? Flames?&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;title=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;title=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;title=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;title=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;title=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;title=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;t=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;title=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;t=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;b&gt;Related Posts&lt;/b&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken case of inheritance&#034;&gt;Brainteaser: Broken case of inheritance&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Overridable methods &#034;&gt;Brainteaser: Overridable methods &lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/03/08/hibernate_event_interceptor.html&#034; rel=&#034;bookmark&#034; title=&#034;Hibernate Event Interceptor&#034;&gt;Hibernate Event Interceptor&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&#034; rel=&#034;bookmark&#034; title=&#034;Java and those frameworks&#034;&gt;Java and those frameworks&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034; rel=&#034;bookmark&#034; title=&#034;How to set SecurityManager and Java security policy programmatically&#034;&gt;How to set SecurityManager and Java security policy programmatically&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034; rel=&#034;bookmark&#034; title=&#034;Hack any Java class using reflection attack&#034;&gt;Hack any Java class using reflection attack&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken comparator&#034;&gt;Brainteaser: Broken comparator&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; rel=&#034;bookmark&#034; title=&#034;Singleton pattern and problem with double checked locking &#034;&gt;Singleton pattern and problem with double checked locking &lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;</description>
	<!--
    <description>
          Recently during development, I encountered a situation where I had to map two collections in the same entity, having collections and the entity it self of the same type.  I had persistent entity - Rule. Rule could have children rules: &amp;quot;action&amp;quot; rules and &amp;quot;else&amp;quot; rules. Children rules were also of ...&lt;p&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&#034;&gt;Read more...&lt;/a&gt;&lt;/p&gt;&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;title=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;title=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;title=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;title=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;title=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;title=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;t=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;title=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html&amp;amp;t=Hibernate - How to map two collections of the same type in the same entity&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;</description>
      
	-->
    <category>design patterns</category>
    <category>hibernate</category>
    <comments>http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html#comments</comments>
    <guid isPermaLink="true">http://javabeans.asia/2008/09/14/hibernate_how_to_map_two_collections_of_the_same_type_in_the_same_entity.html</guid>
    <pubDate>Sun, 14 Sep 2008 12:00:00 GMT</pubDate>
  </item>
  <item>
    <title>Brainteaser: Hidden iterators</title>
    <link>http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html</link>
	 <description>
        &lt;blockquote&gt;... While locking can prevent iterators from throwing &lt;em&gt;ConcurrentMofdificationException&lt;/em&gt;, You have to remember to use locking everywhere a shared collection might be iterated. This is trickier than it sounds ...&lt;br /&gt;
&lt;/blockquote&gt;&lt;a href=&#034;http://jcip.net/&#034; target=&#034;_blank&#034;&gt;Brian Goetz&lt;/a&gt; p.83-84 &lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Question&lt;/strong&gt;: The following code could throw &lt;em&gt;ConcurrentMofdificationException&lt;/em&gt;, even though &lt;em&gt;add()&lt;/em&gt; is synchronized, why?&lt;br /&gt;
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;public class HiddenIterator  {&lt;br /&gt;	private final Set&amp;lt;Integer&amp;gt; set &lt;br /&gt;			= new HashSet&amp;lt;Integer&amp;gt;();&lt;br /&gt;&lt;br /&gt;   public synchronized void add(Integer i) {&lt;br /&gt;	set.add(i);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public synchronized void remove(Integer i) {&lt;br /&gt;	set.remove(i);&lt;br /&gt;   }&lt;br /&gt;	&lt;br /&gt;   public void addTenThings() {&lt;br /&gt;	Random r = new Random();&lt;br /&gt;	for (int index = 0; index &amp;lt; 10;  index++;) {&lt;br /&gt;		add(r.nextInt());&lt;br /&gt;	}&lt;br /&gt;   System.out.println(&amp;quot;Added ten elements to set: &amp;quot; + set);&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
Looking forward for your answers dear readers&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;t=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;t=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;b&gt;Related Posts&lt;/b&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/08/22/brainteaser_drools_testing_objects.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser Drools: Testing Objects&#034;&gt;Brainteaser Drools: Testing Objects&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken case of inheritance&#034;&gt;Brainteaser: Broken case of inheritance&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Overridable methods &#034;&gt;Brainteaser: Overridable methods &lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&#034; rel=&#034;bookmark&#034; title=&#034;Java and those frameworks&#034;&gt;Java and those frameworks&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/01/brainteaser_arraylist_vs_treeset.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: ArrayList VS TreeSet&#034;&gt;Brainteaser: ArrayList VS TreeSet&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034; rel=&#034;bookmark&#034; title=&#034;How to set SecurityManager and Java security policy programmatically&#034;&gt;How to set SecurityManager and Java security policy programmatically&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034; rel=&#034;bookmark&#034; title=&#034;Hack any Java class using reflection attack&#034;&gt;Hack any Java class using reflection attack&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken comparator&#034;&gt;Brainteaser: Broken comparator&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;</description>
	<!--
    <description>
          ... While locking can prevent iterators from throwing ConcurrentMofdificat...&lt;p&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034;&gt;Read more...&lt;/a&gt;&lt;/p&gt;&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;t=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;t=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;</description>
      
	-->
    <category>brainteaser</category>
    <category>design patterns</category>
    <category>threads</category>
    <comments>http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html#comments</comments>
    <guid isPermaLink="true">http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html</guid>
    <pubDate>Sun, 07 Sep 2008 08:05:00 GMT</pubDate>
  </item>
  <item>
    <title>Singleton pattern and problem with double checked locking </title>
    <link>http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html</link>
	 <description>
        There are several ways to initialize singleton object. Some are thread safe and some are not. Until the last two days I thought that double-checked locking idiom offers the best solution for singleton initialization in multi threaded environment. Well its not.&lt;br /&gt;
&lt;br /&gt;
To begin, I want to describe briefly several ways to initialize singleton:&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;Lazy initialization&lt;br /&gt;
    &lt;/li&gt;
    &lt;li&gt;Eager initialization&lt;/li&gt;
    &lt;li&gt;DCL&lt;/li&gt;
    &lt;li&gt;Class holder lazy initialization&lt;/li&gt;
    &lt;li&gt;Enum singleton&lt;/li&gt;
&lt;/ul&gt;
&lt;strong&gt;Lazy initialization&lt;/strong&gt;&lt;br /&gt;
The goal of lazy initialization was to defer initialization of an object until it is actually needed, while making sure that initialization is done only once. Lazy initialization example illustrated below:
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;public class Singleton&lt;br /&gt;{&lt;br /&gt;  private static Singleton instance = null;&lt;br /&gt;  &lt;br /&gt;  private Singleton() {&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public static Singleton getInstance() {&lt;br /&gt;    if (instance == null)  {&lt;br /&gt;      instance = new Singleton();&lt;br /&gt;     }&lt;br /&gt;    return instance;        &lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
This will work fine in single-threaded environment. Unfortunately since &lt;em&gt;getInstance&lt;/em&gt;() is not synchronized, two different instances of the object can be returned if two threads will access &lt;em&gt;getInstance&lt;/em&gt;() method concurrently. Which makes this lazy initialization unsafe in multi threaded environment.&lt;br /&gt;
&lt;br /&gt;
Unsafe lazy initialization can be fixed by making &lt;em&gt;getInstance&lt;/em&gt;() synchronized:
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;public class Singleton&lt;br /&gt;{&lt;br /&gt;  private static Singleton instance = null;&lt;br /&gt;  &lt;br /&gt;  private Singleton() {&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public static synchronized Singleton getInstance() {&lt;br /&gt;    if (instance == null)  {&lt;br /&gt;      instance = new Singleton();&lt;br /&gt;     }&lt;br /&gt;    return instance;        &lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
However, in this case there is performance hit for every invocation of &lt;em&gt;getInstance&lt;/em&gt;() method. Synchronized methods runs very slow compared to unsynchronized methods. I haven&#039;t tested this issue my self, so i cannot tell by how much slower.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Eager initialization&lt;/strong&gt;&lt;br /&gt;
By using eager initialization, the synchronization cost incurred for each method call is eliminated:
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;public class Singleton&lt;br /&gt;{&lt;br /&gt;  private static Singleton instance = new Singleton();&lt;br /&gt;  &lt;br /&gt;  private Singleton() {&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public static Singleton getInstance() {&lt;br /&gt;    return instance;        &lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
The problem with eager initialization that &lt;em&gt;Singleton &lt;/em&gt;object is instantiated regardless whether it is going to be used or not.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;DCL&lt;/strong&gt;&lt;br /&gt;
Double-checked locking (DCL) idiom was created to allow lazy initialization of a singleton object, without performance reducing costs as a result of synchronization. Unfortunately it does not work. Lets have a look why:
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;public class Singleton {&lt;br /&gt;  &lt;br /&gt;  private static Singleton instance = null;&lt;br /&gt;  &lt;br /&gt;  private Singleton() {&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public static Singleton getInstance() {&lt;br /&gt;    if (instance == null)  {&lt;br /&gt;        synchronized(Singleton.class)  {&lt;br /&gt;           if (instance == null)  {&lt;br /&gt;      		instance = new Singleton();&lt;br /&gt;     	  }           &lt;br /&gt;        } &lt;br /&gt;      }&lt;br /&gt;    return instance;&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
First there is a check whether initialization is needed with out synchronization, and if the &lt;em&gt;instance&lt;/em&gt; is not null - then use it.&lt;br /&gt;
Otherwise, synchronize and check again if &lt;em&gt;instance&lt;/em&gt; is null by making sure that only one thread will initialize the shared &lt;em&gt;Singleton&lt;/em&gt;.&lt;br /&gt;
&lt;br /&gt;
The problem with DCL is that code of fetching reference to constructed &lt;em&gt;Singleton &lt;/em&gt;object&lt;em&gt; &lt;/em&gt;does not require synchronization. &lt;br /&gt;
Lets say that thread &#039;a&#039; has entered synchronized block, made the &lt;em&gt;instance&lt;/em&gt; not null&amp;nbsp; but hasn&#039;t executed the constructor yet. At this stage thread &#039;b&#039; preempts thread &#039;a&#039;. Thread &#039;b&#039; checks if the &lt;em&gt;instance&lt;/em&gt; is not null, and since its not, it returns reference to a partially constructed &lt;em&gt;Singleton &lt;/em&gt;object. &lt;br /&gt;
&lt;br /&gt;
In other words - thread &#039;b&#039; sees a current value of the &lt;em&gt;Singleton &lt;/em&gt;object reference instead of its stale value, which causes the object to be seen in invalid or incorrect state.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Class holder lazy initialization&lt;/strong&gt;&lt;br /&gt;
Instead of DCL, lazy initialization class holder idiom offers better solution for singleton initialization in multi threaded environment, with the same benefits as DCL:
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;public class SingletonFactory {&lt;br /&gt;  &lt;br /&gt;  private static class SingletonHolder {&lt;br /&gt;     public static Singleton instance = new Singleton();&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;  public static Singleton getInstance()  {&lt;br /&gt;     return SingletonHolder.instance;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
The &lt;em&gt;SingletonHolder &lt;/em&gt;class initialization is deferred until its actually used, and because &lt;em&gt;Singleton &lt;/em&gt;is initialized with static initializer, no additional synchronization is needed. &lt;br /&gt;
The first invocation of&amp;nbsp; &lt;em&gt;getInstance&lt;/em&gt;() by any thread causes &lt;em&gt;SingletonHolder &lt;/em&gt;to be loaded and initialized during which time the &lt;em&gt;Singleton &lt;/em&gt;is initialized through static initializer.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Enum singleton&lt;/strong&gt;&lt;br /&gt;
Joshua Bloch in his book &amp;quot;&lt;a href=&#034;http://www.sun.com/books/catalog/effective_java.xml&#034; target=&#034;_blank&#034;&gt;Effective Java&lt;/a&gt;&amp;quot; offers a new interesting solution to initialize a Singleton by using Enum with a single type:
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;public class SingletonEnum {&lt;br /&gt;  INSTANCE;&lt;br /&gt;  &lt;br /&gt;  public void someMethod() {&lt;br /&gt;     ...&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;  public void anotherMethod() {&lt;br /&gt;     ...&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
Joshua claims: &lt;blockquote&gt; &amp;quot;...a single-element enum type is the best way to implement a singleton...&amp;quot; &lt;/blockquote&gt; Some people may argue against this approach by saying that this is not a class, but an enum - it enumarates plus enums cannot be subclassed. Well in case of a singleton there is no point in subclassing, since singletons have private constructors and singletons dont really ment to be subclassed. Lets have a look if Enum singleton is indeed the best solution so far:&lt;br /&gt;
&lt;br /&gt;
1. Enum singleton is final&lt;br /&gt;
2. It is serializable&lt;br /&gt;
3. It is a single instance in multithreaded environment &lt;br /&gt;
4. Does not allow invokation of private constructors through reflection attacks&lt;br /&gt;
&lt;br /&gt;
I think its quite original solution.&lt;br /&gt;
&lt;br /&gt;
I will appreciate your comments regarding this post. Cheers.&lt;br /&gt;
&lt;br /&gt;
Resources:&lt;br /&gt;
&lt;em&gt;&lt;a href=&#034;http://www.sun.com/books/catalog/effective_java.xml&#034; target=&#034;_blank&#034;&gt;Effective Java&lt;/a&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;a href=&#034;http://jcip.net/&#034; target=&#034;_blank&#034;&gt;Java Concurrency in Practice&lt;/a&gt;&lt;br /&gt;
&lt;a href=&#034;http://www.ibm.com/developerworks/java/library/j-dcl.html&#034; target=&#034;_blank&#034;&gt;Double-checked locking and the Singleton pattern&lt;/a&gt;&lt;/em&gt;&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;t=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;t=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;b&gt;Related Posts&lt;/b&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken case of inheritance&#034;&gt;Brainteaser: Broken case of inheritance&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Overridable methods &#034;&gt;Brainteaser: Overridable methods &lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&#034; rel=&#034;bookmark&#034; title=&#034;Java and those frameworks&#034;&gt;Java and those frameworks&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/01/brainteaser_arraylist_vs_treeset.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: ArrayList VS TreeSet&#034;&gt;Brainteaser: ArrayList VS TreeSet&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034; rel=&#034;bookmark&#034; title=&#034;How to set SecurityManager and Java security policy programmatically&#034;&gt;How to set SecurityManager and Java security policy programmatically&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034; rel=&#034;bookmark&#034; title=&#034;Hack any Java class using reflection attack&#034;&gt;Hack any Java class using reflection attack&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken comparator&#034;&gt;Brainteaser: Broken comparator&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Hidden iterators&#034;&gt;Brainteaser: Hidden iterators&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;</description>
	<!--
    <description>
          There are several ways to initialize singleton object. Some are thread safe and some are not. Until the last two days I thought that double-checked locking idiom offers the best solution for singleton initialization in multi threaded environment. Well its not.  To begin, I want to describe briefly several ways to ...&lt;p&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034;&gt;Read more...&lt;/a&gt;&lt;/p&gt;&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;t=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;t=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;</description>
      
	-->
    <category>java</category>
    <category>design patterns</category>
    <category>threads</category>
    <comments>http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html#comments</comments>
    <guid isPermaLink="true">http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html</guid>
    <pubDate>Sat, 06 Sep 2008 02:56:00 GMT</pubDate>
  </item>
  </channel>
</rss>
