Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Class for faking Using in Java

This is a class for faking the 'using' syntax in Java. The idea is that you subclass it to provide your resource initialisation and finalisation code, providing protected final methods for resource access you don't want to be publically available. Users then create an anonymous inner class that overrides the body() method and has access to these resources only within the method. Sensitive resources should throw an exception if invoked when isLive returns false.

Now updated to include significantly less evil. (It no longer needs the _return method...)


   1  
   2  public abstract class Use<T>
   3  {
   4          private boolean live = false;
   5  
   6          protected final void isLive()
   7          {
   8              return live;
   9          } 
  10  
  11  	/**
  12  	 * Override this to provide initialisation code for this Use class. This will always be called before
  13  	 * the body is executed.
  14  	 */
  15  	protected void start()
  16  	{
  17  	}
  18  
  19  	/**
  20  	 * Override this to provide finalisation code for this Use class. This will always be called at the end
  21  	 * of use() execution.
  22  	 */
  23  	protected void finish()
  24  	{
  25  	}
  26  
  27  	/**
  28  	 * The body of the code to execute. Returning a value from this should execute _return(T value).
  29           */
  30  	protected abstract T body();
  31  
  32  	/**
  33  	 * Returns body(), or null if this was never invoked. This will call
  34  	 * all neccessary initialisation and finalisation code.
  35   	 */
  36  	public final T use()
  37  	{
  38  		try
  39  		{
  40  			start();
  41                          live = true;
  42  
  43  			return body();
  44  		}
  45  		finally
  46  		{
  47                          live = false;
  48  			finish();
  49  		}
  50  	}
  51  
  52  }

2 + 2 = 5

Here is some very exciting Java code for printing 5.

   1  
   2  import java.security.*;
   3  import java.lang.reflect.*;
   4  import java.util.*;
   5  
   6  public class Test
   7  {
   8      public static void main(String[] args) throws Exception
   9      {   
  10          AccessController.doPrivileged( new PrivilegedAction<Object>(){ public Object run(){try {
  11          Field field = Class.forName("java.lang.Integer$IntegerCache").getDeclaredFields()[0];
  12          field.setAccessible(true);
  13  
  14          Integer[] cache = (Integer[])field.get(null);
  15              
  16          cache[130] = 3;  
  17              
  18          Integer foo = 2;
  19   
  20          System.out.println(foo + 2); 
  21              
  22          return null;} catch(Exception e) { throw new RuntimeException(e); } } }); 
  23      }
  24  
  25  
  26  }
  27  
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS