File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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
Author

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");
System.out(gerd.toString());

}

Class fooByteVersion()
{
Byte gerd= new Byte("1");
System.out(gerd.toString());

}
Class fooStringVersion()
{
String gerd= new String("1");
System.out(gerd.toString());

}

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");
System.out(gerd.to$());

}
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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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: 690
    
    1
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
author
Sheriff

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
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Don't get me started about those stupid light bulbs.
 
subject: CODE/IDE LEVEL VARIABLE, aka Variable before compilation