Skip to content

Commit

Permalink
Fix for issue #73
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Nov 25, 2019
1 parent 9374d10 commit 5b5535c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
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.73
Bundle-Version: 5.0.74
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 @@ -2300,6 +2300,50 @@ else if(switchCaseExpression instanceof FieldAccess) {
}
}
}
else if(statement instanceof VariableDeclarationStatement) {
VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement)statement;
List<VariableDeclarationFragment> fragments = variableDeclarationStatement.fragments();
for(VariableDeclarationFragment fragment : fragments) {
Expression intializerExpression = fragment.getInitializer();
SimpleName accessedVariable = null;
if(intializerExpression instanceof SimpleName) {
accessedVariable = (SimpleName)intializerExpression;
}
else if(intializerExpression instanceof FieldAccess) {
FieldAccess fieldAccess = (FieldAccess)intializerExpression;
accessedVariable = fieldAccess.getName();
}
if(accessedVariable != null) {
IBinding initializerExpressionBinding = accessedVariable.resolveBinding();
if(initializerExpressionBinding != null && initializerExpressionBinding.getKind() == IBinding.VARIABLE) {
IVariableBinding accessedVariableBinding = (IVariableBinding)initializerExpressionBinding;
if(accessedVariableBinding.isField() && typeCheckElimination.getTypeField().resolveBinding().isEqualTo(accessedVariableBinding)) {
if(modify && !nodeExistsInsideTypeCheckCodeFragment(intializerExpression)) {
ASTRewrite sourceRewriter = ASTRewrite.create(sourceTypeDeclaration.getAST());
AST contextAST = sourceTypeDeclaration.getAST();
MethodInvocation getterMethodInvocation = contextAST.newMethodInvocation();
if(typeCheckElimination.getTypeFieldGetterMethod() != null) {
sourceRewriter.set(getterMethodInvocation, MethodInvocation.NAME_PROPERTY, typeCheckElimination.getTypeFieldGetterMethod().getName(), null);
}
else {
sourceRewriter.set(getterMethodInvocation, MethodInvocation.NAME_PROPERTY, contextAST.newSimpleName("get" + abstractClassName), null);
}
sourceRewriter.replace(intializerExpression, getterMethodInvocation, null);
try {
TextEdit sourceEdit = sourceRewriter.rewriteAST();
ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement();
CompilationUnitChange change = compilationUnitChanges.get(sourceICompilationUnit);
change.getEdit().addChild(sourceEdit);
change.addTextEditGroup(new TextEditGroup("Replace field access with invocation of getter method", new TextEdit[] {sourceEdit}));
} catch (JavaModelException e) {
e.printStackTrace();
}
}
}
}
}
}
}
List<Expression> methodInvocations = expressionExtractor.getMethodInvocations(statement);
for(Expression expression : methodInvocations) {
if(expression instanceof MethodInvocation) {
Expand Down

0 comments on commit 5b5535c

Please sign in to comment.