-
-
Notifications
You must be signed in to change notification settings - Fork 349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(filter): adds LineFilter that matches lines of code #678
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // find the parent that is used as a statement (in a block or in a branch) |
||
*/ | ||
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"); | ||
} | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GerardPaligot Why OverriddenMethodFilter is duplicated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oups