Archive for the ‘ HowTo & Tutorial ’ Category

How to get Generic Metadata from a Class at Runtime

Like Annotations, Generics are also a runtime metadata that you can process at the run time.

Here you can see how to traverse the runtime information to get this information. You Can simply traverse the inheritance tree ofr the class and get the metadata of the generic definition. Somthing like

 public static Type[] getGenericDefinitons(Class classFrom, Class class1) ;
Type[] types=getGenericDefinitons(MockClass1.class,GenericInterface.class);//[String.class,Long.class,Integer.class] expected

First The Sample Cases :


public interface GenericInterface <T,K,L>{}

 public interface MockClass1 extends GenericInterface<String,Long,Integer>{}
 public interface MockClass11<T,K,L> extends GenericInterface<T,K,L>{}
 public interface MockClass12 extends MockClass11<String,Long,Integer>{}
 public interface MockClass2 extends MockClass1{}

 public class MockClass3 implements GenericInterface<String,Long,Integer>{}
 public class MockClass4 extends MockClass3{}

 public class GenericClass <T,K,L>{}
 public class AMockClass2 extends AMockClass1{}
 public class AMockClass1 extends GenericClass<String,Long,Integer>{}

And the java class metadata traverser to retrieve the Generic Information


public class Utils {

 /**Get the Generic definition from a class for given class with given index
 * @param clz Implementing class
 * @param class1 class with generic definition
 * @param index generic index
 *  @return null if not found
 * */

 public static Type getGenericDefiniton( Class clz, Class class1, int index) {
 Type[] t = getGenericDefinitons(clz, class1);
 if (t != null && t.length > index)
 return t[index];
 return null;

 }
 /**Get the Generic definitions from a class for given class
 * @param clz Implementing class
 * @param class1 class with generic definition
 * @return null if not found
 * */
 public static Type[] getGenericDefinitons(Class clz, Class class1) {
 Type[] t = null;
 while (clz != null) {

 t = getGenericDefinitonsThis(clz, class1);
 if (t != null)
 return t;
 Class[] interfaces = clz.getInterfaces();
 for (Class class2 : interfaces) {
 t = getGenericDefinitonsThis(class2, class1);
 if (t != null)
 return t;
 }
 clz = clz.getSuperclass();
 }
 return t;
 }
 /**Get the Generic definitions from a class for given class without looking super classes
 * @param clz Implementing class
 * @param class1 class with generic definition
 * @return null if not found
 * */
 public static Type[] getGenericDefinitonsThis(Class classFrom, Class class1) {

 Type[] genericInterfaces = classFrom.getGenericInterfaces();
 for (Type type : genericInterfaces) {
 if (type instanceof ParameterizedType) {
 ParameterizedType pt = (ParameterizedType) type;

 if (class1.isAssignableFrom((Class) pt.getRawType())) {
 Type[] actualTypeArguments = pt.getActualTypeArguments();
 return actualTypeArguments;
 }
 }

 }
 // check if it if available on generic super class
 Type genericSuperclass = classFrom.getGenericSuperclass();
 if (genericSuperclass instanceof ParameterizedType) {
 ParameterizedType pt = (ParameterizedType) genericSuperclass;

 if (class1.equals(pt.getRawType())) {
 Type[] actualTypeArguments = pt.getActualTypeArguments();
 return actualTypeArguments;
 }
 }
 return null;

 }

}

And the test case

PS: This class is not tested for Multi-dimensional Generic Definitions


public class UtilsTest {
 @Test
 public void simpleInterfaceExtend(){
 Type[] genericDefinitons = Utils.getGenericDefinitons(MockClass1.class, GenericInterface.class);
 compareResults(genericDefinitons);
 }

 @Test
 public void simpleInterfaceExtend2(){

 Type[] genericDefinitons = Utils.getGenericDefinitons(MockClass2.class, GenericInterface.class);
 compareResults(genericDefinitons);
 }
 @Test
 public void simpleInterfaceExtend3(){

 Type[] genericDefinitons = Utils.getGenericDefinitons(MockClass12.class, GenericInterface.class);
 compareResults(genericDefinitons);
 }
 @Test
 public void simpleInterfaceImplements(){
 Type[] genericDefinitons = Utils.getGenericDefinitons(MockClass3.class, GenericInterface.class);
 compareResults(genericDefinitons);
 }
 @Test
 public void simpleInterfaceImplements2(){

 Type[] genericDefinitons = Utils.getGenericDefinitons(MockClass4.class, GenericInterface.class);
 compareResults(genericDefinitons);
 }

 @Test
 public void simpleClassImplements(){
 Type[] genericDefinitons = Utils.getGenericDefinitons(AMockClass1.class, GenericClass.class);
 compareResults(genericDefinitons);
 }
 @Test
 public void simpleClassImplements2(){

 Type[] genericDefinitons = Utils.getGenericDefinitons(AMockClass2.class, GenericClass.class);
 compareResults(genericDefinitons);
 }

 private void compareResults(Type[] genericDefinitons) {
 assertEquals(String.class,genericDefinitons[0]);
 assertEquals(Long.class,genericDefinitons[1]);
 assertEquals(Integer.class,genericDefinitons[2]);
 }

}

Executors.newFixedThreadPool(n) – Suspended Threads

Only Exception is Real

Image by valentin.d via Flickr

Yesterday while I am trying to organize threads in Java I noticed something strange !!

Simply the default ExecutorService  could not manage to recover suspended threads.

After a small investigation, it looks like if any of the running thread throws any RuntimeException, ExecutorService may not recover and could not start any waiting runnable.

You can check the java code below.

SmartFix: Don’t throw any exception :P

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExecutorsTest {

	private static ExecutorService executorService;

	public static void main(String[] args) {
	    executorService = Executors.newFixedThreadPool(10);
	    for (int i = 0; i < 10000; i++) {
	        executorService.execute(new MyRunnable());
        }
    }

	public static class MyRunnable implements Runnable{

		@Override
        public void run() {
			while(true){
				long t=(long) (Math.random()*1000*10);
				try {
	                Thread.sleep(t);
                } catch (InterruptedException e) {}
                if(t<3000){
                	System.out.println(Thread.activeCount());
                	// COMMENT AND UNCOMMENT THE LINE BELOW TO SEE THE DIFFERENCE
                	throw new RuntimeException("s");

                }
			}
        }
	}
}
Enhanced by Zemanta

Focus on:Refactoring

My desk is a little untidy at the moment

Image by Neil Crosby via Flickr

Seperation of Concepts
Inversion Of Control
Desing patterns

They are all trying to tell you one common thing.

Please Make It Simple  and Organized

No matter what size is your project and how many teams/developers involved, the complexity if the code has tendency to be more complex. And after awhile it turn in to a YET another OLD piece of CRAP.

Refactoring is the only way to avoid this fate. Here is some tools and practices that would help you

Enhanced by Zemanta

Grails Productivity Tip: Depend on Your Plugin

Grails
Image via Wikipedia

Grails has amazing plugin infrastructure. In short it enables almost everything the core application can do and has really good  list of plugins: http://grails.org/Plugins

Here what I want to say is something a little bit different:

Do all your business in your application plugin

Reminder: Plugins are enabled in Grails

You can reuse all implementation in your plugins:

  • Model Object
  • Services
  • Controllers
  • Gsp pages

Plugins can define their plugin dependency, just like maven dependency.

Goal: Extreme Modularity

Model all your business in a single plugin.

You can consider grails an application container in this case which manages transactions and persistence, and ui and enable each plugin with all customization.  What you need to do is using config. groovy and ConfigurationHolder for configuration issues.

What grails will do is just starting up plugins and and the applications one by one and expose all services, domain class and controllers which will work fine.

It will increase your modularity and ease your integration issues. You can also run your plugins while your are developing them. This is the most amazing part of all the scenario. You do not need to enter pack-install cycle to test anything.

Tough Part: IDE

If you have already established something like this before with Java you would have setup your IDE for this kind of integration. But for grails there is not support as far as far as I know :( So be ready run some console commands:

cd mycore

grails package-plugin

cd ../myapp

grails install-plugin ../mycore/grails-mycore-plugin-0.1.zip

Reference Guide:

http://www.grails.org/The+Plug-in+Developers+Guide

Reblog this post [with Zemanta]

hibernate tip: smart id generator

A small tip to generate more meaningful Id. Instead of 32 for OrderID use O000000032.

Use smart and meaningful ids

Use smart and meaningful ids

Simple implementations:

  • SmartIdTableGenerator
  • SmartIdSequenceGenerator

Read more

Grails 101:GroovyBlogs.org

favicon

If you are trying to learn groovy and grails like me you should visit http://groovyblogs.org . Really nice aggregator for grails and groovy blogs and posts.

But the most important part this site is, it is open-source !!! Read more

tweetback plugin bug patch

I have been suffering from a known bug of tweetback plugin of wordpress for a week.

Tweetback Counter does not update its count if a tweetback is deleted in the admininterface or database

update wp_postmeta set meta_value='0' where meta_key='tweetcount';
update wp_postmeta set meta_value='a:0:{}' where meta_key='twittercomments';

I executed the above sql and fixed the post page errors at least
but left sidebar still shows wrong number of comments :(