is there a good mehtod to handle mutil version as #define #if #else?

now I had to use #if #define #else in my java project. It is not good. But what is other good method? For example, there is game project can build for mobile nokia 7650 and nokia QD, there is only little difference between. So I define final varibale MOBILE as :

public static final int NOK_QD = 10;
public static final int NOK7650 = 11;
public static int MOBILE =
//NOK7650;
NOK_QD;

Before build, I comment the line of NOK7650 or NOK_QD and uncomment another.
I can do this by perl script called by ant build file. But I want to know if there is more easy method to do it in IDEA.



5 comments
Comment actions Permalink

We use ant filter tokens for this. To be able to still compile and run with Idea,
we access the property with a getter that uses a default value:

private static String MOBILE_TEXT = "@mobile@";
public static int getMobileId() {
try {
int result = Integer.parseInt(MOBILE_TEXT);
return result;
}
catch(NumberFormatException e) {
// probably log a warning here, should not happen in production build
return NOK7650;
}
}
}

See ant doc at
http://ant.apache.org/manual/CoreTypes/filterset.html

Maybe in resource restricted mobile environment, you would just do
public static int MOBILE = @mobile@;
and then exclude that one file from compilation in Idea.

I am not really satisfied with this approach anyway. Anyone has a better suggestion?

Evans Zhang wrote:

now I had to use #if #define #else in my java project. It is not good. But what is other good method? For example, there is game project can build for mobile nokia 7650 and nokia QD, there is only little difference between. So I define final varibale MOBILE as :

public static final int NOK_QD = 10;
public static final int NOK7650 = 11;
public static int MOBILE =
//NOK7650;
NOK_QD;

Before build, I comment the line of NOK7650 or NOK_QD and uncomment another.
I can do this by perl script called by ant build file. But I want to know if there is more easy method to do it in IDEA.



0
Comment actions Permalink

This is what inheritance in object-oriented programming was designed for. I
think you should create subclasses like NokiaQDThing which implement method
getWhateverNumber() { return 10; }

Evans Zhang wrote:

now I had to use #if #define #else in my java project. It is not good. But what is other good method? For example, there is game project can build for mobile nokia 7650 and nokia QD, there is only little difference between. So I define final varibale MOBILE as :

public static final int NOK_QD = 10;
public static final int NOK7650 = 11;
public static int MOBILE =
//NOK7650;
NOK_QD;

Before build, I comment the line of NOK7650 or NOK_QD and uncomment another.
I can do this by perl script called by ant build file. But I want to know if there is more easy method to do it in IDEA.



0
Comment actions Permalink

Keith, I think you are just avoiding the problem:
Who decide which subclass to instantiate?
Usually you'll at least have some configuration file, but for mobiles
you do NOT want to install the code base for five different models.

Keith Lea wrote:

This is what inheritance in object-oriented programming was designed
for. I think you should create subclasses like NokiaQDThing which
implement method getWhateverNumber() { return 10; }

Evans Zhang wrote:

>> now I had to use #if #define #else in my java project. It is not good.
>> But what is other good method? For example, there is game project can
>> build for mobile nokia 7650 and nokia QD, there is only little
>> difference between. So I define final varibale MOBILE as :
>>
>> public static final int NOK_QD = 10;
>> public static final int NOK7650 = 11;
>> public static int MOBILE =
>> //NOK7650;
>> NOK_QD;
>>
>> Before build, I comment the line of NOK7650 or NOK_QD and uncomment
>> another.
>> I can do this by perl script called by ant build file. But I want to
>> know if there is more easy method to do it in IDEA.
>>
>>
>>
>>

0
Comment actions Permalink

I think it's much better to conditionally copy files than to conditionally
include code fragments. It allows better tool/editor support and is, to me, and
I think to many other developers, clearer, because you can look at a file and
see what it does instead of considering all the ifs and things like that.

Stephen Kelvin wrote:

Keith, I think you are just avoiding the problem:
Who decide which subclass to instantiate?
Usually you'll at least have some configuration file, but for mobiles
you do NOT want to install the code base for five different models.

Keith Lea wrote:

>> This is what inheritance in object-oriented programming was designed
>> for. I think you should create subclasses like NokiaQDThing which
>> implement method getWhateverNumber() { return 10; }
>>
>> Evans Zhang wrote:
>>
>>> now I had to use #if #define #else in my java project. It is not
>>> good. But what is other good method? For example, there is game
>>> project can build for mobile nokia 7650 and nokia QD, there is only
>>> little difference between. So I define final varibale MOBILE as :
>>>
>>> public static final int NOK_QD = 10;
>>> public static final int NOK7650 = 11;
>>> public static int MOBILE =
>>> //NOK7650;
>>> NOK_QD;
>>>
>>> Before build, I comment the line of NOK7650 or NOK_QD and uncomment
>>> another.
>>> I can do this by perl script called by ant build file. But I want to
>>> know if there is more easy method to do it in IDEA.
>>>
>>>
>>>
>>>

0
Comment actions Permalink

the IDEA parse the source code and create a abstract syntax tree and do type analyse, right? that is why IDEA is intelligent to edit and refactor. What I want to do is another edit and refactor in some viewpoint. So I hope there is a script language to descript what I want to do, and the IDEA can run the script. For example, I want to change the fiels MOBILE to init value "NOKIA7210" and comment the line "NOKIA_QD", I write a script as :
Pacman.fieldp[MOBILE].init.comment() //comment the init part
Pacman.field[MOBILE].init = "NOKIA7210" //set the init of membre MOBILE
you must understand what I mens...

0

Please sign in to leave a comment.