Skip to content

update of repository #9

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

Merged
merged 2 commits into from
May 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Driver.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import lang::java::refactoring::SwitchString;
import lang::java::refactoring::VarArgs;
import lang::java::refactoring::Diamond;
import lang::java::refactoring::AnonymousToLambda;
import lang::java::refactoring::FilterPatternToLambda;
import lang::java::refactoring::ExistPatternToLambda;
import lang::java::refactoring::FilterPattern;

import lang::java::util::ManageCompilationUnit;
import lang::java::m3::M3Util;
Expand Down Expand Up @@ -53,6 +54,7 @@ public void refactorProjects(loc input, bool verbose = true) {
case /VA/: executeTransformations(projectFiles, toInt(projectDescriptor[3]), verbose, refactorVarArgs, "varargs");
case /DI/: executeTransformations(projectFiles, toInt(projectDescriptor[3]), verbose, refactorDiamond, "diamond");
case /AC/: executeTransformations(projectFiles, toInt(projectDescriptor[3]), verbose, refactorAnonymousInnerClass, "aic");
case /EP/: executeTransformations(projectFiles, toInt(projectDescriptor[3]), verbose, refactorExistPattern, "exist pattern");
case /FP/: executeTransformations(projectFiles, toInt(projectDescriptor[3]), verbose, refactorFilterPattern, "filter pattern");
default: logMessage(" ... nothing to be done");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
module lang::java::refactoring::FilterPatternToLambda
module lang::java::refactoring::ExistPatternToLambda

import ParseTree;
import lang::java::\syntax::Java18;

public tuple[int, CompilationUnit] refactorFilterPattern(CompilationUnit cu) {
/**
* Refactor a compilation unit to replace
* foreach statements, according to the
* exist pattern, into a lambda expression.
*/
public tuple[int, CompilationUnit] refactorExistPattern(CompilationUnit cu) {
int total = 0;
CompilationUnit unit = visit(cu) {
case(BlockStatements)`for(<UnannType t> <Identifier var> : <Expression exp>) { if(<Expression e>) { return true; } } return false;` : {
total += 1;
insert (BlockStatements)`return <Identifier exp>.stream().anyMatches(<Identifier var> -\> <Expression e>);`;
insert (BlockStatements)`return <Identifier exp>.stream().anyMatch(<Identifier var> -\> <Expression e>);`;
}
};
return <total, unit>;
Expand Down
20 changes: 20 additions & 0 deletions src/lang/java/refactoring/FilterPattern.rsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module lang::java::refactoring::FilterPattern

import ParseTree;
import lang::java::\syntax::Java18;

/**
* Refactor a compilation unit to replace
* foreach statements, according to the
* exist pattern, into a lambda expression.
*/
public tuple[int, CompilationUnit] refactorFilterPattern(CompilationUnit cu) {
int total = 0;
CompilationUnit unit = visit(cu) {
case(BlockStatements)`for(<UnannType t> <Identifier var> : <Expression exp>) { if(<Expression e>) { return true; }}` : {
total += 1;
insert (BlockStatements)`c = exp.stream().filter(var -\> e).list();`;
}
};
return <total, unit>;
}
4 changes: 3 additions & 1 deletion testes/BasicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public static void whileWithIteratorStatement() {
}

for(int s : list) { if(s.length()) { return true; }
}
} return false;

//for(int s: list) { if(s.lenght()) { return true; } }
}

public static void ifElseStatement(String string, int number, boolean bool) {
Expand Down