Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to get all the statements of a particular SwitchCase?
How to get all the statements of a particular SwitchCase? [message #249311] Tue, 06 November 2007 06:21 Go to next message
Rahul Mishra is currently offline Rahul MishraFriend
Messages: 19
Registered: July 2009
Junior Member
Hi folks,
The JDT DOM model SwitchCase statement doesnt have any api to retrieve
all the statements under a SwitchCase. It supports only the following:
1. expression(getExpression())
2. says whether its default case or not(isDefault())

Any suggestions on how to collect all the statements of a paricular
SwitchCase would be helpful.

Scenario:

class a
{
void meth(int i)
{
switch(i)
{
case 1: i++;
break;
default: i--;
break;
case 2: i+=2;
break;
}

}
}

1.How to collect all the statements under the default case?
2.How to collect all the statements of the last SwitchCase of the switch
statements?
Re: How to get all the statements of a particular SwitchCase? [message #249323 is a reply to message #249311] Tue, 06 November 2007 10:27 Go to previous messageGo to next message
Frederic Fusier is currently offline Frederic FusierFriend
Messages: 147
Registered: July 2009
Senior Member
Rahul Mishra wrote:
> Hi folks, The JDT DOM model SwitchCase statement doesnt have any api to
> retrieve all the statements under a SwitchCase. It supports only the
> following: 1. expression(getExpression()) 2. says whether its default
> case or not(isDefault())
>
> Any suggestions on how to collect all the statements of a paricular
> SwitchCase would be helpful.
>
> Scenario:
>
> class a
> {
> void meth(int i)
> {
> switch(i)
> {
> case 1: i++;
> break;
> default: i--;
> break;
> case 2: i+=2;
> break;
> }
>
> }
> }
>
> 1.How to collect all the statements under the default case?
> 2.How to collect all the statements of the last SwitchCase of the switch
> statements?
>
I strongly advice to use the ASTView (see http://www.eclipse.org/jdt/ui/astview/index.php)
to have a more precise idea of what is the hierarchy of DOM/AST nodes in a compilation unit.

If you were using this really useful tool, you'd see that statements are not children of SwitchCase
but SwitchStatement instead... Here's the structure of your example's method body:
BODY
Block [44, 185]
STATEMENTS (1)
SwitchStatement [52, 171]
EXPRESSION
STATEMENTS (9)
SwitchCase [80, 7]
ExpressionStatement [88, 4]
BreakStatement [111, 6]
SwitchCase [128, 8]
ExpressionStatement [137, 4]
BreakStatement [160, 6]
SwitchCase [177, 7]
ExpressionStatement [185, 5]
BreakStatement [209, 6]


Here's a code snippet illustrating how you can browse it:
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(source.toCharArray());
final CompilationUnit unit = (CompilationUnit) parser.createAST(null);
MethodDeclaration method = (MethodDeclaration) ((AbstractTypeDeclaration)unit.types().get(0)).bodyDeclarati ons().get(0);
List switchStatements = ((SwitchStatement) method.getBody().statements().get(0)).statements();
int size = switchStatements.size(), i=0;
while (i<size) {
ASTNode node = (ASTNode) switchStatements.get(i++);
if (node.getNodeType() == ASTNode.SWITCH_CASE) {
SwitchCase switchCase = (SwitchCase) node;
Expression switchCaseExpression = switchCase.getExpression();
if (switchCaseExpression == null) { // default
int n=-1;
while (i<size && node.getNodeType() != ASTNode.BREAK_STATEMENT) {
node = (ASTNode) switchStatements.get(i++);
n++;
}
System.out.println("There is/are "+n+" statement(s) in the default case");
} else if (switchCaseExpression.getNodeType() == ASTNode.NUMBER_LITERAL) {
String token = ((NumberLiteral)switchCaseExpression).getToken();
if (token.equals("2")) {
int n=-1;
while (i<size && node.getNodeType() != ASTNode.BREAK_STATEMENT) {
node = (ASTNode) switchStatements.get(i++);
n++;
}
System.out.println("There is/are "+n+" statement(s) in the '2' case");
}
}
}
}

Running this snippet on your example, you get the following output:
There is/are 1 statement(s) in the default case
There is/are 1 statement(s) in the '2' case

HTH
Re: How to get all the statements of a particular SwitchCase? [message #249328 is a reply to message #249323] Tue, 06 November 2007 12:30 Go to previous messageGo to next message
Rahul Mishra is currently offline Rahul MishraFriend
Messages: 19
Registered: July 2009
Junior Member
Thanks Frederic.
So, there's no hierarchy in the switch statements as is evident from the
ASTView.

My above post was to stress on the design of the JDT .
Dont you think the switch statement hierarchy currently in JDT is a poor
design. It would have been better if there was a parent-child (contains)
relationship between the switch cases and the statements specific to that
case. The iterative/recursive traversal of the parent would have been
really be helpful in the static analysis of the source.
Your Views?

Regards,
Rahul Mishra.
Re: How to get all the statements of a particular SwitchCase? [message #249332 is a reply to message #249328] Tue, 06 November 2007 13:37 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

This is a multi-part message in MIME format.
--------------040203050809040603050002
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Rahul,

I think JDT's design simply reflects what's perhaps a poor design in
Java itself. Consider this bit of erroneous code

public static void main(String[] args)
{
int i = 0;
switch (i)
{
case 0:
int x = 10;
case 1:
int y = x;
}
}

The problem isn't that x isn't visible in "int y = x"---the statements
after "case 0" aren't actually in a nested scope---but rather the
problem is that x might not be initialized when you jump straight to
"case 1". So code that does static analysis better take into account
that the cases of a switch might flow from one to the next...


Rahul Mishra wrote:
> Thanks Frederic. So, there's no hierarchy in the switch statements as
> is evident from the ASTView.
>
> My above post was to stress on the design of the JDT .
> Dont you think the switch statement hierarchy currently in JDT is a
> poor design. It would have been better if there was a parent-child
> (contains) relationship between the switch cases and the statements
> specific to that case. The iterative/recursive traversal of the parent
> would have been really be helpful in the static analysis of the source.
> Your Views?
>
> Regards,
> Rahul Mishra.
>


--------------040203050809040603050002
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Rahul,<br>
<br>
I think JDT's design simply reflects what's perhaps a poor design in
Java itself.
Re: How to get all the statements of a particular SwitchCase? [message #249536 is a reply to message #249332] Tue, 13 November 2007 10:26 Go to previous messageGo to next message
Rahul Mishra is currently offline Rahul MishraFriend
Messages: 19
Registered: July 2009
Junior Member
Dear Ed Merks,

Sorry, for the delayed reply as I was devoid of internet.

> public static void main(String[] args)
> {
> int i = 0;
> switch (i)
> {
> case 0:
> int x = 10;
> case 1:
> int y = 20; //changed from int y=x;
> }
> }

Would it simply mean that since there isnt any break statement attached to
"case 0" therefore, statements of "case 1" is also attached to "case 0".
But, i am a bit confused after seeing the grammar from SUN .

If we go through the following JLS3 grammar

SwitchStatement =
'switch', '(', Expression, ')', SwitchBlock ;

SwitchBlock =
'{', [SwitchBlockStatementGroups], [SwitchLabels], '}' ;

SwitchBlockStatementGroups =
SwitchBlockStatementGroup | SwitchBlockStatementGroups,
SwitchBlockStatementGroup ;

/* There can be empty switch labels or even the statements with/without
any break , but each block statement is attached to a single case label as
can be seen from the grammar. If there is a block statement , then that
statement has to be attached to a single case label.
(see the grammar of SwitchBlockStatementGroup below */

SwitchBlockStatementGroup =
SwitchLabels, BlockStatements ;

SwitchLabels =
SwitchLabel | SwitchLabels, SwitchLabel ;

SwitchLabel =
'case', ConstantExpression, ':' | 'case', EnumConstantName, ':' |
'default', ':' ;

Please clarify .

Thanks in anticipation.

Regards,
Rahul Mishra
Re: How to get all the statements of a particular SwitchCase? [message #249541 is a reply to message #249332] Tue, 13 November 2007 10:48 Go to previous messageGo to next message
Rahul Mishra is currently offline Rahul MishraFriend
Messages: 19
Registered: July 2009
Junior Member
Dear Ed Merks,

The previous post was a bit scrappy due to inconsistent line feeds as I
copied and pasted it .
Here, have formatted it.

> public static void main(String[] args)
> {
> int i = 0;
> switch (i)
> {
> case 0:
> int x = 10;
> case 1:
> int y = 20; //changed from int y=x;
> }
> }

Would it simply mean that since there isnt any break statement attached to
"case 0" therefore, statements of "case 1" is also attached to "case 0".
But, i am a bit confused after seeing the grammar from SUN .

If we go through the following JLS3 grammar

/* URL:
http://java.sun.com/docs/books/jls/third_edition/html/statem ents.html#14.11
*/


SwitchStatement:
switch ( Expression ) SwitchBlock

SwitchBlock:
{ SwitchBlockStatementGroupsopt SwitchLabelsopt } /* (opt is a
subscript)*/

SwitchBlockStatementGroups:
SwitchBlockStatementGroup
SwitchBlockStatementGroups SwitchBlockStatementGroup

/* There can be empty switch labels or even the statements with/without
any break , but each block statement is attached to a single case label as
can be seen from the grammar. If there is a block statement , then that
statement has to be attached to a single case label.
(see the grammar of SwitchBlockStatementGroup below) */


SwitchBlockStatementGroup:
SwitchLabels BlockStatements

SwitchLabels:
SwitchLabel
SwitchLabels SwitchLabel

SwitchLabel:
case ConstantExpression :
case EnumConstantName :
default :

EnumConstantName:
Identifier

Please clarify .

Thanks in anticipation.

Regards,
Rahul Mishra
Re: How to get all the statements of a particular SwitchCase? [message #249546 is a reply to message #249541] Tue, 13 November 2007 11:24 Go to previous message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

This is a multi-part message in MIME format.
--------------000601090707060602050106
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Rahul,

There might even be a conditional break:

public static void main(String[] args)
{
int i = 0;
switch (i)
{
case 0:
int x = 10;
if (args.length > 2)
{
break;
}
case 1:
int y = 20; //changed from int y=x;
}
}

I really am no expert at the JDT APIs so perhaps I should refrain from
commenting further but it seems to me that a parent child relationship
would also imply a parent scope and a child scope in terms of visibility
for declarations, but in this case, there is only a single flat scope.
In any case, the chances that the JDT APIs will change to conform to
your expectations are probably zero since that would break existing
clients...


Rahul Mishra wrote:
> Dear Ed Merks,
>
> The previous post was a bit scrappy due to inconsistent line feeds as
> I copied and pasted it .
> Here, have formatted it.
>
>> public static void main(String[] args)
>> {
>> int i = 0;
>> switch (i)
>> {
>> case 0:
>> int x = 10;
>> case 1:
>> int y = 20; //changed from int y=x;
>> }
>> }
>
> Would it simply mean that since there isnt any break statement
> attached to "case 0" therefore, statements of "case 1" is also
> attached to "case 0". But, i am a bit confused after seeing the
> grammar from SUN .
>
> If we go through the following JLS3 grammar
> /* URL:
> http://java.sun.com/docs/books/jls/third_edition/html/statem ents.html#14.11
> */
>
>
> SwitchStatement:
> switch ( Expression ) SwitchBlock
>
> SwitchBlock:
> { SwitchBlockStatementGroupsopt SwitchLabelsopt } /* (opt is a
> subscript)*/
>
> SwitchBlockStatementGroups:
> SwitchBlockStatementGroup
> SwitchBlockStatementGroups SwitchBlockStatementGroup
>
> /* There can be empty switch labels or even the statements
> with/without any break , but each block statement is attached to a
> single case label as can be seen from the grammar. If there is a block
> statement , then that statement has to be attached to a single case
> label.
> (see the grammar of SwitchBlockStatementGroup below) */
>
>
> SwitchBlockStatementGroup:
> SwitchLabels BlockStatements
>
> SwitchLabels:
> SwitchLabel
> SwitchLabels SwitchLabel
>
> SwitchLabel:
> case ConstantExpression :
> case EnumConstantName :
> default :
>
> EnumConstantName:
> Identifier
>
> Please clarify .
>
> Thanks in anticipation.
>
> Regards,
> Rahul Mishra
>


--------------000601090707060602050106
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Rahul,<br>
<br>
There might even be a conditional break:<br>
<br>
<small>
Previous Topic:EclipseCon 2008 talk submission deadline approaching
Next Topic:Changes to AST and class file but not source
Goto Forum:
  


Current Time: Tue Dec 23 01:03:44 GMT 2014

Powered by FUDForum. Page generated in 0.02483 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software