is this a bug: variables() gives duplicate variables
When I try to get a list of declared variables in a method, using variables() from class Method, I sometimes get two variables with the same name, although there is one declared. Is this a known bug?
Example:
the following method gives *two* local variables, both named mx.
int computeMaximum(int a, int b) {
int mx;
if (a >= b){
mx = a;
}else{
mx = b;
}
return mx;
}
Thanks for your thoughts,
Keeds
[492 byte] By [
keesa] at [2007-11-26 20:32:13]

# 1
I'm guessing this is probably not a bug. The method variables() will return multiple instances of the same variable if it is declared in multiple scopes within the function. I'm not sure exactly why 'mx' is counted in two scopes in the given code, but I assume it has to do with how java compiles the if-statement.
From the java documentation:
List<LocalVariable> variables() throws AbsentInformationException
Returns a list containing each LocalVariable declared in this method.
The list includes any variable declared in any scope within the method.
It may contain multiple variables of the same name declared within
disjoint scopes. Arguments are considered local variables and will be
present in the returned list.