[java] java.lang.reflect and Code Generation

Started by
5 comments, last by Son of Cain 18 years, 9 months ago
Hi folks, My current project must generate Java code, and so I have to design a class that represents a legal Java method. I've searched google extensively, but all I got was tutorials about how to retrive information about methods of an existing class - which is not what I need. So, do you guys know of a good book or tutorial that deals with reflect? I've seen the Type and TypeVariable interfaces, as well as GenericDeclaration, but I couldn't figure out a way of working with them in order to generate code. Can you guys brainstorm for a while? ;) Son Of Cain
a.k.a javabeats at yahoo.ca
Advertisement
Maybe this can be of any help: Extensible Code Generation with Java, Part 1.

Or this: cglib.
"We confess our little faults to persuade people that we have no large ones." -Francois de La Rochefoucauld (1613 - 1680). | My blog
Thanks rohde, great links!

BTW, if you guys know of any "code beautifier" library, please mention it here ;)

Son Of Cain
a.k.a javabeats at yahoo.ca
I've heard some good things about Checkstyle

I presume this is what you mean by "code beautifier."
Exactly that, thank you =D

BTW, I sticked with Velocity and its templates to generate the code I need. Fast and easily customizable!

Son Of Cain
a.k.a javabeats at yahoo.ca
Here is some ant stuff for a beautifier call to JIndent (for copy paste purposes), this is just a ant target:

<pre>  <!-- ================================================================== -->  <!-- Format Source Code recursively throughout the source tree          -->  <!-- ================================================================== -->  <target name="format" depends="compile" description="JIndent Format" >        <java   jar="lib/Jindent.jar"                fork="true"                classpathref="classpath" >            <arg value="-r" />            <arg value="${src}" />        </java>  </target></pre>


You'll also need the JIndent pure java downloads from JIndent.com, and to put the jars from that download in the /lib dir of your project. This is not the JIndent-ant.jar but nearly all the other jars that come with the d/l.

It's nice, and you can edit this to accept a file with your custome format preferences, just like checkstyle, that is incase you find checkstyle not up to par. I don't remember if checkstyle accepts java1.5 -- I knodw the jalopy documentation says only to 1.4.1 (or so).

L-

[Edited by - Lucidquiet on August 5, 2005 4:22:55 PM]
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
Thanks guys, you helped me a lot!

Son Of Cain
a.k.a javabeats at yahoo.ca

This topic is closed to new replies.

Advertisement