Thursday, July 3, 2014

Get Class Type of Generic Parameter in Java

Here is the process to get the Class Type of GenericParameter that was passed in to the Java Class.

We need to follow small tweak to make it work because Java Generics uses Type Erasure at Runtime.
Trick is: While creating Object, we need to create anonymous class. (So that getGenericSuperclass() will return target class)

Here is the requirement:
   Implement a Generic Transformer, which will transform input Objects to dynamic Objects based on class Type parameter.


import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;

public class GenTransformer implements Transformer {

public Class returnedClass() {
        ParameterizedType parameterizedType;
try {
parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
} catch (Exception e) {
throw new IllegalArgumentException("Instantiate your Transformer as anonymous class. (Ex: new GenTransformer() {})");
}

        @SuppressWarnings("unchecked")
        Class ret = (Class) parameterizedType.getActualTypeArguments()[0];
        return ret;
    }

@Override
public Object transform(Object arg0) {
Class cls = returnedClass();
return arg0 + "-" + cls.getName();
}

public static void main(String[] args) {
List names = new ArrayList();
names.add("Name1");
names.add("Name2");
List res1 = (List) CollectionUtils.collect(names, new GenTransformer(){});
System.out.println(res1);
}
}

Thursday, May 27, 2010

Difference between redirect and forward

Difference between redirect and forward

Servlet redirect and servlet forward both are used to handle the request processing to some other URL/Servlet but there is a big difference in the way it works. The main difference is
  • Servlet redirect always sends a HTTP status code 303 to client along with the redirect URL. Client then sends a new request to the URL.Thus response.sendRedirect() takes one more round trip than forward where as servlet forward just forwards the request to another resource on the server and it does not take a full round trip that is why forward is some time referred as server side redirect.
  • The another difference is you can redirect the request to some other URL on some another site but you can not forward the request to some other URL on different site.
  • Servlet forward will forward the existing request to another JSP or Servlet, so all the request parameters and attributes will be available to destination servlet. However with redirect, browser sends new request to specified URL, so old request parameters and attributes will not be available to destination resource.
  • Browser is completely unaware of servlet forward and hence the URL in browser address bar will remain unchanged. Where as the URL in browser address bar will change to new URL when servlet redirect is used. 
Reference: http://www.jsptube.com/examples/response-sendredirect-servlet.html

Friday, January 15, 2010

GroupBox / FieldSet Legend Component / Spark Skin in Flex 4

I have written a new custom component/spark skin for Group Box. I tried with many of the GroupBox components that are developed in Flex 3. But somehow, I was not able to get it working as I expected. So, I ended up creating a Spark Skin and custom component for this Group Box.

Here is the sample look and feel of the component. It can be rendered with out label as well.



It has total of 3 steps to make use of the complete component.
  1. Create a Custom Component called GroupBox which has a Property called "label"
  2. Create a GroupBox Spark Skin which accepts arguments for Label. (Can add more arguments for background shadow)
  3. Make use of the new GroupBox component with the GroupBoxSkin in your component mxml
1. Create a Custom Component for GroupBox  
    Let us name it "GroupBox.as"

2. Create a Spark Skin for Group Box     
    Let us name it "GroupBoxLegendSkin.mxml"
]]> -->

3. Use it in your MXML Component

Let me know, if you see any issues while using it.
If you can add / improve the functionality you are most welcome for the suggestions.

Wednesday, December 9, 2009

Technical Links


  1. Clustered Scheduling with Spring and Quartz - http://toddkaufman.blogspot.com/2008/09/clustered-scheduling-with-spring-and.html
  • Flex
  1. Flex Multi Column Filtering - http://www.iwobanas.com/2009/06/datagrid-with-client-side-filtering-and-searching
  2. Flex Auto Complete Component - http://hillelcoren.com/flex-autocomplete/
  3. Advanced Data Grid footer: http://files.seanhess.net/demos/FooterDataGrid/Test.html
  4. ---------------
  5. Flex Group Box / Field Set / Legend - http://flexdevtips.blogspot.com/2009/04/titled-border-box.html
  6. Flex 4, Custom Layouts - http://www.adobe.com/devnet/flex/articles/spark_layouts.html
  7. Spark Group and SkinnableContainers - http://help.adobe.com/en_US/Flex/4.0/UsingSDK/WSF1E2E7DD-8FA6-408d-9652-4B5E57A85A5A.html
  8. Flex Dairy Blog: http://flexdiary.blogspot.com/2008/06/musings-on-advanceddatagrid-part-1.html
  9. Flex Single SignOn: http://www.adobe.com/products/livecycle/mosaic/
  10. Flex Testing: http://www.gorillalogic.com/flexmonkey
  11. Flex inherited mxml component for visual elements: http://cookbooks.adobe.com/post_Adding_multiple_sets_of_visual_children_to_custom-12927.html 
  12. Cairngorm Framework Reference: http://robsondesign.com/demo/cairngorm_diagram_explorer/
  13. Flex Page Flip: http://www.megazine3.de/?c=home&l=en
  14. Flex 4, GraniteDS Integration: http://java.dzone.com/articles/enterprise-ria-spring-3-flex-4 
  15. Flex and Maven integration with Flexmojo: http://www.adobe.com/devnet/flex/articles/flex-maven-flexmojos-pt1.html
  • Java / J2ee / Spring
  1. GWT 2 Spring 3 JPA 2 Hibernate 3.5 Tutorial : http://www.javacodegeeks.com/2010/05/gwt-2-spring-3-jpa-2-hibernate-35.html#ixzz0v6fQoPZO
  2. Java Singleton pattern for Multi threaded Application: http://avricot.com/blog/index.php?post/2010/05/11/Java-MultiThread-singleton-LocalThread-vs-Double-Check-Locked-(dcl)-vs-synchronized-vs-static
  3. Hibernate: Extra-Lazy Collection Fetching: https://sites.google.com/a/pintailconsultingllc.com/java/hibernate-extra-lazy-collection-fetching
  4. Mongo DB Video Presentation: http://vimeo.com/28760769
  5. Spring Profiles:  http://java.dzone.com/articles/using-spring-profiles-xml
  • Server Configuration
  1. Apache Server with Tomcat on SSL -  http://www.javacodegeeks.com/2012/06/apache-http-server-with-tomcat-on-ssl.html
  • Html / JavaScript
  1. IFrame parent URL - http://av5.com/docs/changing-parent-window-s-url-from-iframe-content.htm