Tuesday, September 28, 2010

Java Source Reflection - some manifestations

Java Reflection is a great tool, but a lot of cases like source code generation tools, there is a big problem that the code has to be compiled before it reflects on the same. Java Source Reflection comes to the rescue. This is a great way of analyzing the Java Source File without the need for compilation. A more detailed explanation can be found here

Tools like Eclipse make use of Source Reflection as it makes the tool much faster with its tooling. There is one interesting behavior though:

Say you have Class A in the package : com.company.product.Package

and I import the Class A like this in another class : import com.company.product.Package.A;

Now in the Eclipse Source Folder I create this directory structure as : com/company/product/package/A.java - Notice that the directory structure has a "p" there (May be developer mistyped the directory name) 


Your Ant Build will work fine, because when the class file is created, it will create the classes directory according to the package declared with the "P" and the import also would be recognized properly.

But eclipse will show you an error as the source path does not match the import or the Package declared. You might not see this issue with newly developed code using eclipse as you get the error instantly, but if the code is existing and imported to eclipse, this is a very good possibility


Interesting manifestation of Java Source Reflection Vs. Reflection (Or rather compilation)

No comments: