Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CODE/IDE LEVEL VARIABLE, aka Variable before compilation

 
Amaru Shakur
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Norm Radder
Ranch Hand
Posts: 728
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic