-
-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(filter): adds LineFilter to reason about statements in blocks
- Loading branch information
Showing
4 changed files
with
162 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/spoon/reflect/visitor/filter/LineFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* Copyright (C) 2006-2015 INRIA and contributors | ||
* Spoon - http://spoon.gforge.inria.fr/ | ||
* | ||
* This software is governed by the CeCILL-C License under French law and | ||
* abiding by the rules of distribution of free software. You can use, modify | ||
* and/or redistribute the software under the terms of the CeCILL-C license as | ||
* circulated by CEA, CNRS and INRIA at http://www.cecill.info. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details. | ||
* | ||
* The fact that you are presently reading this means that you have had | ||
* knowledge of the CeCILL-C license and that you accept its terms. | ||
*/ | ||
package spoon.reflect.visitor.filter; | ||
|
||
import spoon.reflect.code.CtBlock; | ||
import spoon.reflect.code.CtIf; | ||
import spoon.reflect.code.CtLoop; | ||
import spoon.reflect.code.CtStatement; | ||
import spoon.reflect.code.CtStatementList; | ||
import spoon.reflect.declaration.CtElement; | ||
import spoon.reflect.declaration.ParentNotInitializedException; | ||
|
||
/** | ||
* This filter matches all elements that can be considered as line of code (e.g. directly contained in a block, or a then statement). This discards CtStatement that are not used as statement (such as a method call used as RHS). | ||
* <pre> | ||
* // lines of a method | ||
* List<CtStatement> lines = method.getElements(new LineFilter()); | ||
* // find the parent that is used as a statement (in a block or in a branch) | ||
* CtStatement parentStatement = element.getParent(new LineFilter()); | ||
* </pre> | ||
*/ | ||
public class LineFilter extends TypeFilter<CtStatement> { | ||
|
||
/** | ||
* Creates the filter. | ||
*/ | ||
public LineFilter() { | ||
super(CtStatement.class); | ||
} | ||
|
||
@Override | ||
public boolean matches(CtStatement element) { | ||
if (!super.matches(element)) { | ||
return false; | ||
} | ||
if (element instanceof CtBlock) { | ||
return false; | ||
} | ||
CtElement parent; | ||
try { | ||
parent = element.getParent(); | ||
} catch (ParentNotInitializedException e) { | ||
return false; | ||
} | ||
if (parent instanceof CtStatementList) { | ||
return true; | ||
} | ||
if (parent instanceof CtIf) { | ||
CtIf anIf = (CtIf) parent; | ||
return element.equals(anIf.getThenStatement()) || element.equals(anIf.getElseStatement()); | ||
} | ||
if (parent instanceof CtLoop) { | ||
CtLoop loop = (CtLoop) parent; | ||
return loop.getBody().equals(element); | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package spoon.test.filters; | ||
|
||
class FooLine { | ||
void simple() { | ||
int x = 3; | ||
int z = 0; | ||
System.out.println(z); | ||
} | ||
|
||
void loopBlock() { | ||
for(int i = 0; i < 10; i++) { | ||
System.out.println(i); | ||
} | ||
} | ||
|
||
void loopNoBlock() { | ||
for(int i = 0; i < 10; i++) | ||
System.out.println(i); | ||
} | ||
|
||
void ifBlock() { | ||
if (3 < 4) { | ||
System.out.println("if"); | ||
} | ||
} | ||
|
||
void ifNoBlock() { | ||
if (3 < 4) | ||
System.out.println("if"); | ||
} | ||
|
||
void switchBlock() { | ||
switch ("test") { | ||
case "test": | ||
break; | ||
default: | ||
System.out.println("switch"); | ||
} | ||
} | ||
} | ||
|