Fog Creek Software
Discussion Board




java preprocessor?

hi tool folks,

I have an unusual requirement: c-style macros and #ifdefs in my java source.  I've already justified my need and thought hard about avoiding it with the usual if(SOMETHING) guards, but I really really want a traditional preprocessor for this particular project.  So are there any?  Anyone have any experience with any?

(Google found mostly articles explaining the _lack_ of macros in java... and the one or two freeware preprocessors I found were dead links...)

thanks for your help, and sorry if this is a tangent topic

i like i
Tuesday, August 12, 2003

I know very little about it, but would aspect-oriented programming work to do the equivalent?

Konrad
Tuesday, August 12, 2003

When I googled "Java Preprocessor" I found links in even the first page which ain't dead.

For simple tasks maybe you can just use Ant's Token filter?

S.C.
Tuesday, August 12, 2003

Perhaps code generation will server you purpose:

http://www.eclipse.org/articles/index.html
http://jakarta.apache.org/velocity/index.html

Ged Byrne
Tuesday, August 12, 2003

I concur that code generation might be helpful.  If the solutions above do not have the needed granularity, two more options are Javacc and Antlr.

Scot
Tuesday, August 12, 2003

I'm pretty sure you can use the C preprocessor for your java file. I haven't used it lately, but I remember some kind of '-I' option of the C compiler to just preprocess the file.

R Chevallier
Tuesday, August 12, 2003

If you really want a macro processor, go grab a copy of m4 - it's a generic macro engine. Run your Java code through this before handing it to the compiler.

m4 does not use the #ifdef syntax of the C preprocessor, so if you want exactly that you'll need to get a C preprocessor somewhere. m4 is actually more powerful than the C preprocessor though.

You can get a unix version from the GNU folks. There are win32 ports out there as well.

Chris Tavares
Tuesday, August 12, 2003

sorry, I can't think of a good reason to use macros, so can you elaborate on why you need them?  I suppose its *possible*, but when most people ask how to use macros in java, they're still in their C mentality. 

vince
Tuesday, August 12, 2003

It would be nice to be able to easily generate debug builds of a jar file rather than having to either a) check a variable at runtime to decide whether to run the debug blocks or b) change a constant at compile time to do each build.

Jeremy
Tuesday, August 12, 2003

Umm... how about the C preprocessor?

Use the option to pre-process only, add build step, you're done.

dude
Tuesday, August 12, 2003

with a pre proc you can do things like simulate generics??

I use sybase powerdesigner with the generation template language to do such like?

can you be more specific

Karel
Wednesday, August 13, 2003

Last time I tried preprocessing with a c++ compiler it complained about the non-c++ishness of the code, as if the preprocessing includes a syntax check too.

M4 seems worth a try.  Or if I go Ant maybe I can go VPP too.  Actually, at this point, I have virtually written my own preprocessor since I've had no fun with others.

Sideline defending my need for a preprocessor: some things, like J2ME games for multiple handsets, are best done in a 'c mentality' even when you have to use Java.

i like i
Wednesday, August 13, 2003

damn, no edit post nor preview :-/

I'd like to thank everyone for their input too, and didn't mean my prev post to sound otherwise ;-)

i like i
Wednesday, August 13, 2003

Have in mind that java statement like this: if (debugLevel == 3) { do something } will act as a preprocessor if the debugLEvel is declared final. So go ahead, declare final variables and write if statements for them.

Yassen Damyanov
Tuesday, August 26, 2003

*  Recent Topics

*  Fog Creek Home