background preloader

Dispatching

Facebook Twitter

Reflecting generics. Code by Any Other NameReflecting genericsby Ian RobertsonJune 23, 2007 Summary Type arguments to generic classes are not available for reflection at runtime - or are they? The type arguments for statically declared types can be discovered at runtime. A look at how to do this, and why you might want to. Probably the most common complaint about generics in Java is that they are not reified - there is not a way to know at runtime that a List<String> is any different from a List<Long>. I've gotten so used to this that I was quite surprised to run across Neil Gafter's work on Super Type Tokens. Super type tokens are a nice trick to distinguish between types which have the same raw type, but different type arguments. UserType is used to provide custom mappings between database values and Java objects. Unfortunately, because of type erasure, the "obvious" implementation of returnedClass() doesn't work.

While this works, it does so at the cost of forcing clients to repeat themselves: Cacm-sidebar.pdf (application/pdf Object) Untangle your servlet code with reflection - Java World. Despite the elegance of the Servlet API, real-world servlet classes tend to have pages of code in their doGet() and doPost() methods, often with complex conditional logic. Over time, more code must be added in response to evolving business needs. Long methods with complex branching result in a major headache for the unlucky developer who must maintain that code. Good programming style suggests that such branching should be avoided. This article presents a technique to modularize the functionality of a complex servlet using reflection, thereby simplifying the servlet structure.

Specifically, the CTLServlet base class parses the request URL to determine the name of an appropriate handler, finds a method by that name in the implementation class, and then invokes the method to handle the request. That makes it easy to break down servlet functionality into separate methods, and thus avoid very long method bodies within doGet() and doPost(). About reflection o.getClass(); Disciplining URLs. Dr. Dobb&#039;s | Java Reflection &amp; Smalltalk-Like Method Dispatching | July 1, 2004.