File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes CODE/IDE LEVEL VARIABLE, aka Variable before compilation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "CODE/IDE LEVEL VARIABLE, aka Variable before compilation" Watch "CODE/IDE LEVEL VARIABLE, aka Variable before compilation" New topic

CODE/IDE LEVEL VARIABLE, aka Variable before compilation

Amaru Shakur
Ranch Hand

Joined: Jul 17, 2008
Posts: 50
ok so this might be really dumb but is there anyway i could have a variable in my texteditor before it compiles to represent a string. heres what i mean

say that i have a method that works with STrings and the methods it inherits from object

so like Class fooIntegerVersion()
Integer gerd= new Integer("1");


Class fooByteVersion()
Byte gerd= new Byte("1");

Class fooStringVersion()
String gerd= new String("1");


is there anyway i could just make a variable in the ide represneted by $ that would be intpreted as whatever i had in it.

lets say i said $=String;
Class fooStringVersion()
$ gerd= new $("1");

is there any way that the IDE would know to put String in where the $ sign is before it compiles?

(Note im not saying this is possible im asking if it is possible and how which idea what class, different idea, ect...)

ty all

Even though im marked for death I will spark till i loose my breath
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24166

Most IDEs have some kind of templating features that will help with this. The exact mechanism will depend on which IDE you're using. For an IDE-independent technique, you could use a macro preprocessor like m4 to generate your Java files.

If you want specific help, you'd need to narrow it down to a specific IDE or environment of interest.

[Jess in Action][AskingGoodQuestions]
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
Do it in two passes. Insert a string and then do a global replace on that string to the new string.
Amaru Shakur
Ranch Hand

Joined: Jul 17, 2008
Posts: 50
i am using the eclipse ide
Originally posted by Norm Radder:
Do it in two passes. Insert a string and then do a global replace on that string to the new string.

yeah thats what i have been doing but not as smart as a i should be.

Like i should have made the original with the $ sign. because on the global replace i acidently replace things that i shouldnt (like this isnt what is happening but an example say that i needed to replace int and i didnt use case sensitivity in the control f option , and i also used wrap and rather than just chang all the ints to longs, it also changed integers to longegers).
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
You can even do this at runtime using reflection.

(I haven't tried to compile this, and it's all from memory, so it might contain bugs. It should give you the general idea, though.)
[ July 24, 2008: Message edited by: Ilja Preuss ]

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rob Spoor

Joined: Oct 27, 2005
Posts: 19541

There is only one method for the primitive wrapper types that starts with "to": toString. The methods to get the primitive values are called booleanValue, byteValue, etc.

I'd change that into

Although this will still fail for Integer and Character.

A safe way is using the static TYPE field:

fieldClass will be int.class, char.class etc, so its name will be int, char, long etc.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: CODE/IDE LEVEL VARIABLE, aka Variable before compilation
Similar Threads
The One thing that confuses me in Java
examlab - 1 [reference variable casting]
confusing var-arg constructors
why I cann't assign FINAL INT to Byte
Sun Cirtification