Web Hosting Web Hosting, web hosting, JSP, Servlets, Tomcat, website hosting, web site hosting
Web Hosting, web hosting, JSP, Servlets, Tomcat, website hosting, web site hosting
Web Hosting, web hosting, JSP, Servlets, Tomcat, website hosting, web site hosting

Alden Hosting provides professional, efficient, and reliable business-class Web hosting services to small- and medium-sized businesses.

WWW.

Call Us Toll-Free
(877) 256-0328

Outside USA
1 - (201) 505-0430

Web Hosting Welcome Web Hosting Web Hosting Plans Overview , Fund Raising, Fundraising, web hosting, website hosting, web site hosting Web Hosting Fund Raising, Fundraising, web hosting Web Hosting Resellers, web Hosting Web Hosting Web Design, web Hosting Web Hosting Extra Services,  web Hosting Web Hosting Traffic Booster, web hosting Web Hosting Traffic Booster, web hosting Web Hosting Technical Support,  web Hosting Web Hosting webmaster tips,  web Hosting Web Hosting 30 Day Money Back, web hosting Web Hosting Legal Notices for Web Hosting Web Hosting Glossary Computer Terms for web Hosting Web Hosting Contact Information - web hosting

Site Map

  Web Hosting Web Hosting Sign-Up   Web Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting    Web Hosting Resellers web hosting, website hosting, web site hosting   Web Hosting EZ Site Control Panel for web hosting,website hosting, web site hosting
Class Literals as Runtime-Type Tokens (The Java™ Tutorials > Bonus > Generics)
Trail: Bonus
Lesson: Generics
Home Page > Bonus > Generics
Class Literals as Runtime-Type Tokens
One of the changes in JDK 5.0 is that the class java.lang.Class is generic. It's an interesting example of using genericity for something other than a container class.

Now that Class has a type parameter T, you might well ask, what does T stand for? It stands for the type that the Class object is representing.

For example, the type of String.class is Class<String>, and the type of Serializable.class is Class<Serializable>. This can be used to improve the type safety of your reflection code.

In particular, since the newInstance() method in Class now returns a T, you can get more precise types when creating objects reflectively.

For example, suppose you need to write a utility method that performs a database query, given as a string of SQL, and returns a collection of objects in the database that match that query.

One way is to pass in a factory object explicitly, writing code like:

interface Factory<T> { T make();} 

public <T> Collection<T> select(Factory<T> factory, String statement) { 
    Collection<T> result = new ArrayList<T>(); 
    /* Run sql query using jdbc */  
    for (/* Iterate over jdbc results. */) { 
        T item = factory.make();
        /* Use reflection and set all of item's fields from sql results. */ 
        result.add(item); 
    } 
    return result; 
}
You can call this either as
select(new Factory<EmpInfo>(){ public EmpInfo make() {
                               return new EmpInfo();
                               }}
      , "selection string");
or you can declare a class EmpInfoFactory to support the Factory interface
class EmpInfoFactory implements Factory<EmpInfo> {
    ...
    public EmpInfo make() { return new EmpInfo();}
}
and call it
select(getMyEmpInfoFactory(), "selection string");
The downside of this solution is that it requires either:
  • the use of verbose anonymous factory classes at the call site, or
  • declaring a factory class for every type used and passing a factory instance at the call site, which is somewhat unnatural.
It is natural to use the class literal as a factory object, which can then be used by reflection. Today (without generics) the code might be written:
Collection emps = sqlUtility.select(EmpInfo.class, "select * from emps");
...
public static Collection select(Class c, String sqlStatement) { 
    Collection result = new ArrayList();
    /* Run sql query using jdbc. */
    for (/* Iterate over jdbc results. */ ) { 
        Object item = c.newInstance(); 
        /* Use reflection and set all of item's fields from sql results. */  
        result.add(item); 
    } 
    return result; 
}
However, this would not give us a collection of the precise type we desire. Now that Class is generic, we can instead write the following:
Collection<EmpInfo> emps = 
                      sqlUtility.select(EmpInfo.class, "select * from emps");
...
public static <T> Collection<T> select(Class<T> c, String sqlStatement) { 
    Collection<T> result = new ArrayList<T>();
    /* Run sql query using jdbc. */
    for (/* Iterate over jdbc results. */ ) { 
        T item = c.newInstance(); 
        /* Use reflection and set all of item's fields from sql results. */  
        result.add(item);
    } 
    return result; 
} 
The above code gives us the precise type of collection in a type safe way.

This technique of using class literals as run time type tokens is a very useful trick to know. It's an idiom that's used extensively in the new APIs for manipulating annotations, for example.

Previous page: The Fine Print
Next page: More Fun with Wildcards
Web Hosting, web hosting, JSP, Servlets, Tomcat, website hosting, web site hosting
Add to My Yahoo!

XML icon

Add to Google

 

 

 

 

 

 

 

 

 

 

 

http://alden-servlet-Hosting.com
JSP at alden-servlet-Hosting.com
Servlets at alden-servlet-Hosting.com
Servlet at alden-servlet-Hosting.com
Tomcat at alden-servlet-Hosting.com
MySQL at alden-servlet-Hosting.com
Java at alden-servlet-Hosting.com
sFTP at alden-servlet-Hosting.com
http://alden-tomcat-Hosting.com
JSP at alden-tomcat-Hosting.com
Servlets at alden-tomcat-Hosting.com
Servlet at alden-tomcat-Hosting.com
Tomcat at alden-tomcat-Hosting.com
MySQL at alden-tomcat-Hosting.com
Java at alden-tomcat-Hosting.com
sFTP at alden-tomcat-Hosting.com
http://alden-sftp-Hosting.com
JSP at alden-sftp-Hosting.com
Servlets at alden-sftp-Hosting.com
Servlet at alden-sftp-Hosting.com
Tomcat at alden-sftp-Hosting.com
MySQL at alden-sftp-Hosting.com
Java at alden-sftp-Hosting.com
sFTP at alden-sftp-Hosting.com
http://alden-jsp-Hosting.com
JSP at alden-jsp-Hosting.com
Servlets at alden-jsp-Hosting.com
Servlet at alden-jsp-Hosting.com
Tomcat at alden-jsp-Hosting.com
MySQL at alden-jsp-Hosting.com
Java at alden-jsp-Hosting.com
sFTP at alden-jsp-Hosting.com
http://alden-java-Hosting.com
JSp at alden-java-Hosting.com
Servlets at alden-java-Hosting.com
Servlet at alden-java-Hosting.com
Tomcat at alden-java-Hosting.com
MySQL at alden-java-Hosting.com
Java at alden-java-Hosting.com
sFTP at alden-java-Hosting.com
JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP at JSP.aldenWEBhosting.com Servlets at servlets.aldenWEBhosting.com Tomcat at Tomcat.aldenWEBhosting.com mysql at mysql.aldenWEBhosting.com Java at Java.aldenWEBhosting.com Web Hosts Portal Web Links Web Links Web Hosting JSP Solutions Web Links JSP Solutions Web Hosting Servlets Solutions Web Links Servlets Solutions Web Hosting Web Links Web Links . .
.
.
. .
. . . . jsp hosting servlets hosting web hosting web sites designed cheap web hosting web site hosting myspace web hosting