- my webspace

- my webspace

Latest Comment

Why I must believe in GOD
Testing time is almost passed..there are many colours in lif...
27/06/12 01:43 More...
By Tarun Shekhawat

Allama Iqbal - Selective verse...
Yahoouj
Really good work about this website was done. Keep trying mo...
07/03/10 15:04 More...
By Roderick

Allama Iqbal - Selective verse...
Great Job
You have dont a great job of collecting these... Even I had ...
25/08/09 01:01 More...
By Sikandar

O ye who don't believe !
It's like Lehman Brothers :grin
11/10/08 10:31 More...
By anurag Chaturvedi

I Protest
@Sikku
Thanks Sikku for the feedback. I never intend to blame, a...
29/07/08 11:06 More...
By Aminur Rashid

Login






Lost Password?
Home arrow Java arrow Resurrecting the dead ones.
Resurrecting the dead ones. PDF Print E-mail
User Rating: / 0
PoorBest 
Written by Aminur Rashid   
Friday, 27 April 2012

In one of the puzzle at his blog site, Wouter came up with this java puzzle : Who came first Chicken or Egg. Here is the puzzle from his website:

The egg:
package chicken;
 
public class Egg {
    final Object first;
 
    public Egg(Chicken mom) {
        first = mom.first;
    }
}

The Chicken:
package chicken;
 
public class Chicken {
    final Object first;
 
    public Chicken(Egg egg) {
        first = egg.first;
    }
 
    public void ask() {
        // The goal is to reach this line
        System.out.println("First there was the " + first);
    }
}

The creator:
package creator;
 
import chicken.Chicken;
 
public class Creator {
    public static void main(String[] args) {
        new Chicken(null).ask();
    }
}

Task is to modify Creator class in a way, that ask method is called. And rule remains that reflection should not be used. [you must run with the security manager enabled (-Djava.security.manager). Your solution must be in the creator package].

Try out before looking for the solution.




Interesting stuff, and below was my solution for the same. Check the use of finalize method for resurrecting the dead object. This is what secured coding standards called "finalizer" attack. Check out here and make sure you write a compliant code.
package creator;
 
import chicken.Chicken;
import chicken.Egg;
 
public class Creator extends Egg {
    private static Creator badEgg = null;
 
    Creator(Chicken e) {
        super(e);
    }
 
    public static Creator get() {
        try {
            new Creator(null);
        } catch (Exception ex) {
            //Who cares. It is expected.
        }
        try {
            synchronized (Creator.class) {
                while (badEgg == null) {
                    System.gc();
                    Creator.class.wait(10);
                }
            }
        } catch (InterruptedException ex) {
            return null;
        }
        return badEgg;
    }
 
    public void finalize() {
        synchronized (Creator.class) {
            badEgg = this;
            Creator.class.notify();
        }
    }
 
    public static void main(String[] args) {
        Creator creator = Creator.get();
        Chicken goodChicken = new Chicken(creator);
        goodChicken.ask();
    }
}


StumbleUponDigg This!Bookmark on Delicious

Add as favourites (434) | Quote this article on your site | Views: 1814 | E-mail

Be first to comment this article
RSS comments

Only registered users can write comments.
Please login or register.

Last Updated ( Friday, 27 April 2012 )
 
< Prev   Next >
Aminur Rashid