• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

CODE/IDE LEVEL VARIABLE, aka Variable before compilation

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic