Skip to content

Commit

Permalink
Fixed compilation errors when the variable to be returned
Browse files Browse the repository at this point in the history
is declared and return inside the type-checking code fragments.
In case of composite conditionals an else branch is added with a super method invocation to execute the default functionality.
  • Loading branch information
tsantalis committed Oct 5, 2019
1 parent ee341c0 commit 20efaaf
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 6 deletions.
2 changes: 1 addition & 1 deletion META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: JDeodorant Plug-in
Bundle-SymbolicName: gr.uom.java.jdeodorant; singleton:=true
Bundle-Version: 5.0.69
Bundle-Version: 5.0.70
Bundle-Activator: gr.uom.java.jdeodorant.refactoring.Activator
Bundle-Localization: plugin
Require-Bundle: org.eclipse.ui,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclaration;
Expand Down Expand Up @@ -136,7 +137,7 @@ else if(typeMethodInvocation != null)
else if(typeMethodInvocation != null)
sourceRewriter.set(abstractMethodInvocation, MethodInvocation.EXPRESSION_PROPERTY, typeMethodInvocation, null);
ListRewrite methodInvocationArgumentsRewrite = sourceRewriter.getListRewrite(abstractMethodInvocation, MethodInvocation.ARGUMENTS_PROPERTY);
if(returnedVariable != null) {
if(returnedVariable != null && !typeCheckElimination.returnedVariableDeclaredAndReturnedInBranches()) {
if(returnedVariable instanceof SingleVariableDeclaration) {
SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration)returnedVariable;
methodInvocationArgumentsRewrite.insertLast(singleVariableDeclaration.getName(), null);
Expand All @@ -160,7 +161,7 @@ else if(returnedVariable instanceof VariableDeclarationFragment) {
methodInvocationArgumentsRewrite.insertLast(clientAST.newThisExpression(), null);
setPublicModifierToSourceTypeDeclaration();
}
if(returnedVariable != null) {
if(returnedVariable != null && !typeCheckElimination.returnedVariableReturnedInBranches()) {
Assignment assignment = clientAST.newAssignment();
sourceRewriter.set(assignment, Assignment.OPERATOR_PROPERTY, Assignment.Operator.ASSIGN, null);
sourceRewriter.set(assignment, Assignment.LEFT_HAND_SIDE_PROPERTY, returnedVariable.getName(), null);
Expand Down Expand Up @@ -387,7 +388,7 @@ else if(variableDeclarationFragment.getParent() instanceof FieldDeclaration) {
abstractMethodModifiersRewrite.insertLast(abstractAST.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD), null);
abstractMethodModifiersRewrite.insertLast(abstractAST.newModifier(Modifier.ModifierKeyword.ABSTRACT_KEYWORD), null);
ListRewrite abstractMethodParametersRewrite = abstractRewriter.getListRewrite(abstractMethodDeclaration, MethodDeclaration.PARAMETERS_PROPERTY);
if(returnedVariable != null) {
if(returnedVariable != null && !typeCheckElimination.returnedVariableDeclaredAndReturnedInBranches()) {
if(returnedVariable instanceof SingleVariableDeclaration) {
SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration)returnedVariable;
abstractMethodParametersRewrite.insertLast(singleVariableDeclaration, null);
Expand Down Expand Up @@ -609,7 +610,7 @@ else if(variableDeclarationFragment.getParent() instanceof FieldDeclaration) {
ListRewrite concreteMethodModifiersRewrite = subclassRewriter.getListRewrite(concreteMethodDeclaration, MethodDeclaration.MODIFIERS2_PROPERTY);
concreteMethodModifiersRewrite.insertLast(subclassAST.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD), null);
ListRewrite concreteMethodParametersRewrite = subclassRewriter.getListRewrite(concreteMethodDeclaration, MethodDeclaration.PARAMETERS_PROPERTY);
if(returnedVariable != null) {
if(returnedVariable != null && !typeCheckElimination.returnedVariableDeclaredAndReturnedInBranches()) {
if(returnedVariable instanceof SingleVariableDeclaration) {
SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration)returnedVariable;
concreteMethodParametersRewrite.insertLast(singleVariableDeclaration, null);
Expand Down Expand Up @@ -704,6 +705,41 @@ else if(variableDeclarationFragment.getParent() instanceof FieldDeclaration) {
Block ifStatementBody = subclassAST.newBlock();
ifStatementBodyRewrite = subclassRewriter.getListRewrite(ifStatementBody, Block.STATEMENTS_PROPERTY);
subclassRewriter.set(enclosingIfStatement, IfStatement.THEN_STATEMENT_PROPERTY, ifStatementBody, null);
if(!typeCheckElimination.getDefaultCaseStatements().isEmpty()) {
Block elseStatementBody = subclassAST.newBlock();
ListRewrite elseStatementBodyRewrite = subclassRewriter.getListRewrite(elseStatementBody, Block.STATEMENTS_PROPERTY);
SuperMethodInvocation superMethodInvocation = subclassAST.newSuperMethodInvocation();
subclassRewriter.set(superMethodInvocation, SuperMethodInvocation.NAME_PROPERTY, subclassAST.newSimpleName(typeCheckElimination.getAbstractMethodName()), null);
ListRewrite superMethodInvocationArgumentRewrite = subclassRewriter.getListRewrite(superMethodInvocation, SuperMethodInvocation.ARGUMENTS_PROPERTY);
if(returnedVariable != null && !typeCheckElimination.returnedVariableDeclaredAndReturnedInBranches()) {
superMethodInvocationArgumentRewrite.insertLast(returnedVariable.getName(), null);
}
for(SingleVariableDeclaration abstractMethodParameter : typeCheckElimination.getAccessedParameters()) {
if(!abstractMethodParameter.equals(returnedVariable) && !abstractMethodParameter.equals(typeVariable)) {
superMethodInvocationArgumentRewrite.insertLast(abstractMethodParameter.getName(), null);
}
}
for(VariableDeclaration fragment : typeCheckElimination.getAccessedLocalVariables()) {
if(!fragment.equals(returnedVariable) && !fragment.equals(typeVariable)) {
superMethodInvocationArgumentRewrite.insertLast(fragment.getName(), null);
}
}
if(sourceTypeRequiredForExtraction()) {
String parameterName = sourceTypeDeclaration.getName().getIdentifier();
parameterName = parameterName.substring(0,1).toLowerCase() + parameterName.substring(1,parameterName.length());
superMethodInvocationArgumentRewrite.insertLast(subclassAST.newSimpleName(parameterName), null);
}
if(returnedVariable != null) {
ReturnStatement superMethodInvocationReturnStatement = subclassAST.newReturnStatement();
subclassRewriter.set(superMethodInvocationReturnStatement, ReturnStatement.EXPRESSION_PROPERTY, superMethodInvocation, null);
elseStatementBodyRewrite.insertLast(superMethodInvocationReturnStatement, null);
}
else {
ExpressionStatement superMethodInvocationExpressionStatement = subclassAST.newExpressionStatement(superMethodInvocation);
elseStatementBodyRewrite.insertLast(superMethodInvocationExpressionStatement, null);
}
subclassRewriter.set(enclosingIfStatement, IfStatement.ELSE_STATEMENT_PROPERTY, elseStatementBody, null);
}
concreteMethodBodyRewrite.insertLast(enclosingIfStatement, null);
}
SimpleName subclassCastInvoker = null;
Expand Down Expand Up @@ -787,7 +823,7 @@ else if(castExpression.getExpression() instanceof MethodInvocation) {
concreteMethodBodyRewrite.insertLast(newStatement, null);
}
}
if(returnedVariable != null) {
if(returnedVariable != null && !typeCheckElimination.returnedVariableReturnedInBranches()) {
ReturnStatement returnStatement = subclassAST.newReturnStatement();
subclassRewriter.set(returnStatement, ReturnStatement.EXPRESSION_PROPERTY, returnedVariable.getName(), null);
concreteMethodBodyRewrite.insertLast(returnStatement, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,103 @@ public List<SingleVariableDeclaration> getTypeCheckMethodParameters() {
return typeCheckMethod.parameters();
}

private Map<ReturnStatement, VariableDeclaration> getTypeCheckMethodReturnedVariableMap() {
Map<ReturnStatement, VariableDeclaration> map = new LinkedHashMap<ReturnStatement, VariableDeclaration>();
StatementExtractor statementExtractor = new StatementExtractor();
ExpressionExtractor expressionExtractor = new ExpressionExtractor();
List<Statement> typeCheckCodeFragmentReturnStatements = statementExtractor.getReturnStatements(typeCheckCodeFragment);
List<Statement> variableDeclarationStatements = statementExtractor.getVariableDeclarationStatements(typeCheckMethod.getBody());
List<Expression> variableDeclarationExpressions = expressionExtractor.getVariableDeclarationExpressions(typeCheckMethod.getBody());
for(Statement statement : typeCheckCodeFragmentReturnStatements) {
ReturnStatement returnStatement = (ReturnStatement)statement;
if(returnStatement.getExpression() instanceof SimpleName) {
SimpleName returnExpression = (SimpleName)returnStatement.getExpression();
List<SingleVariableDeclaration> parameters = typeCheckMethod.parameters();
for(SingleVariableDeclaration parameter : parameters) {
if(parameter.resolveBinding().isEqualTo(returnExpression.resolveBinding())) {
map.put(returnStatement, parameter);
}
}
for(Statement vdStatement : variableDeclarationStatements) {
VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement)vdStatement;
List<VariableDeclarationFragment> fragments = variableDeclarationStatement.fragments();
for(VariableDeclarationFragment fragment : fragments) {
if(fragment.resolveBinding().isEqualTo(returnExpression.resolveBinding())) {
map.put(returnStatement, fragment);
}
}
}
for(Expression expression : variableDeclarationExpressions) {
VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression)expression;
List<VariableDeclarationFragment> fragments = variableDeclarationExpression.fragments();
for(VariableDeclarationFragment fragment : fragments) {
if(fragment.resolveBinding().isEqualTo(returnExpression.resolveBinding())) {
map.put(returnStatement, fragment);
}
}
}
}
}
return map;
}

public boolean returnedVariableReturnedInBranches() {
Map<ReturnStatement, VariableDeclaration> map = getTypeCheckMethodReturnedVariableMap();
int returnedInBranchCounter = 0;
for(ReturnStatement key : map.keySet()) {
for(Expression expression : typeCheckMap.keySet()) {
ArrayList<Statement> branchStatements = typeCheckMap.get(expression);
if(branchStatements.contains(key)) {
returnedInBranchCounter++;
}
}
if(defaultCaseStatements.contains(key)) {
returnedInBranchCounter++;
}
}
return map.size() == returnedInBranchCounter;
}

public boolean returnedVariableDeclaredAndReturnedInBranches() {
Map<ReturnStatement, VariableDeclaration> map = getTypeCheckMethodReturnedVariableMap();
int returnedInBranchCounter = 0;
int declaredInBranchCounter = 0;
for(ReturnStatement key : map.keySet()) {
for(Expression expression : typeCheckMap.keySet()) {
ArrayList<Statement> branchStatements = typeCheckMap.get(expression);
if(branchStatements.contains(key)) {
returnedInBranchCounter++;
}
for(Statement statement : branchStatements) {
if(statement instanceof VariableDeclarationStatement) {
VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement)statement;
List<VariableDeclarationFragment> fragments = variableDeclarationStatement.fragments();
for(VariableDeclarationFragment fragment : fragments) {
if(fragment.equals(map.get(key))) {
declaredInBranchCounter++;
}
}
}
}
}
if(defaultCaseStatements.contains(key)) {
returnedInBranchCounter++;
}
for(Statement statement : defaultCaseStatements) {
if(statement instanceof VariableDeclarationStatement) {
VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement)statement;
List<VariableDeclarationFragment> fragments = variableDeclarationStatement.fragments();
for(VariableDeclarationFragment fragment : fragments) {
if(fragment.equals(map.get(key))) {
declaredInBranchCounter++;
}
}
}
}
}
return map.size() == returnedInBranchCounter && map.size() == declaredInBranchCounter;
}

public VariableDeclaration getTypeCheckMethodReturnedVariable() {
StatementExtractor statementExtractor = new StatementExtractor();
ExpressionExtractor expressionExtractor = new ExpressionExtractor();
Expand Down

0 comments on commit 20efaaf

Please sign in to comment.