Skip to content

Commit

Permalink
Enable the detection of Inline Variable with Pattern Instance expression
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Dec 24, 2024
1 parent 1fe5c30 commit acca712
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/java/gr/uom/java/xmi/LocationInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public enum CodeElementType {
TYPE_PARAMETER,
ANNOTATION_TYPE_MEMBER_DEFAULT_EXPRESSION,
//expressions
STRING_LITERAL, TEXT_BLOCK, CHAR_LITERAL, ARRAY_ACCESS, PREFIX_EXPRESSION, POSTFIX_EXPRESSION, INFIX_EXPRESSION, THIS_EXPRESSION, NUMBER_LITERAL, NULL_LITERAL, BOOLEAN_LITERAL, TYPE_LITERAL, FIELD_ACCESS, SIMPLE_NAME, EXPRESSION, QUALIFIED_NAME, CAST_EXPRESSION, PARENTHESIZED_EXPRESSION, ASSIGNMENT, INSTANCEOF_EXPRESSION;
STRING_LITERAL, TEXT_BLOCK, CHAR_LITERAL, ARRAY_ACCESS, PREFIX_EXPRESSION, POSTFIX_EXPRESSION, INFIX_EXPRESSION, THIS_EXPRESSION, NUMBER_LITERAL, NULL_LITERAL, BOOLEAN_LITERAL, TYPE_LITERAL, FIELD_ACCESS, SIMPLE_NAME, EXPRESSION, QUALIFIED_NAME, CAST_EXPRESSION, PARENTHESIZED_EXPRESSION, ASSIGNMENT, INSTANCEOF_EXPRESSION, PATTERN_INSTANCEOF_EXPRESSION;

private String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public void setIndex(int index) {
public abstract List<LeafExpression> getParenthesizedExpressions();
public abstract List<LeafExpression> getCastExpressions();
public abstract List<LeafExpression> getInstanceofExpressions();
public abstract List<LeafExpression> getPatternInstanceofExpressions();
public abstract List<TernaryOperatorExpression> getTernaryOperatorExpressions();
public abstract List<LambdaExpressionObject> getLambdas();
public abstract VariableDeclaration searchVariableDeclaration(String variableName);
Expand Down Expand Up @@ -188,6 +189,13 @@ public List<LeafExpression> findExpression(String s) {
locations.add(expression.getLocationInfo());
}
}
for(LeafExpression expression : getPatternInstanceofExpressions()) {
if(expression.getString().equals(s)) {
if(!locations.contains(expression.getLocationInfo()))
matchingExpressions.add(expression);
locations.add(expression.getLocationInfo());
}
}
for(LeafExpression expression : getParenthesizedExpressions()) {
if(expression.getString().equals(s)) {
if(!locations.contains(expression.getLocationInfo()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,23 @@ else if(stringConcatMatch(initializer, after)) {
return;
}
}
if(before.startsWith(JAVA.RETURN_SPACE) && before.endsWith(JAVA.STATEMENT_TERMINATION)) {
before = before.substring(JAVA.RETURN_SPACE.length(), before.length()-JAVA.STATEMENT_TERMINATION.length());
}
if(after.contains(before) && initializer != null && fragment2.getPatternInstanceofExpressions().size() > 0) {
for(LeafExpression expression2 : fragment2.getPatternInstanceofExpressions()) {
if(expression2.getString().endsWith(" " + variableName)) {
InlineVariableRefactoring ref = new InlineVariableRefactoring(declaration, operation1, operation2, insideExtractedOrInlinedMethod);
LeafMapping leafMapping = new LeafMapping(initializer, expression2, operation1, operation2);
ref.addSubExpressionMapping(leafMapping);
processInlineVariableRefactoring(ref, refactorings);
//if(identical()) {
identicalWithInlinedVariable = true;
//}
return;
}
}
}
}
if(classDiff != null && getFragment1().getVariableDeclarations().size() > 0 && initializer != null && getFragment1().getVariableDeclarations().toString().equals(getFragment2().getVariableDeclarations().toString())) {
VariableDeclaration variableDeclaration2 = getFragment2().getVariableDeclarations().get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class AbstractExpression extends AbstractCodeFragment {
private List<LeafExpression> parenthesizedExpressions;
private List<LeafExpression> castExpressions;
private List<LeafExpression> instanceofExpressions;
private List<LeafExpression> patternInstanceofExpressions;
private List<TernaryOperatorExpression> ternaryOperatorExpressions;
private List<LambdaExpressionObject> lambdas;

Expand Down Expand Up @@ -77,6 +78,7 @@ public AbstractExpression(CompilationUnit cu, String sourceFolder, String filePa
this.parenthesizedExpressions = visitor.getParenthesizedExpressions();
this.castExpressions = visitor.getCastExpressions();
this.instanceofExpressions = visitor.getInstanceofExpressions();
this.patternInstanceofExpressions = visitor.getPatternInstanceofExpressions();
this.ternaryOperatorExpressions = visitor.getTernaryOperatorExpressions();
this.lambdas = visitor.getLambdas();
this.expression = stringify(expression);
Expand Down Expand Up @@ -251,6 +253,11 @@ public List<LeafExpression> getInstanceofExpressions() {
return instanceofExpressions;
}

@Override
public List<LeafExpression> getPatternInstanceofExpressions() {
return patternInstanceofExpressions;
}

@Override
public List<TernaryOperatorExpression> getTernaryOperatorExpressions() {
return ternaryOperatorExpressions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class AnonymousClassDeclarationObject implements LocationInfoProvider {
private List<LeafExpression> parenthesizedExpressions = new ArrayList<>();
private List<LeafExpression> castExpressions = new ArrayList<>();
private List<LeafExpression> instanceofExpressions = new ArrayList<>();
private List<LeafExpression> patternInstanceofExpressions = new ArrayList<>();
private List<TernaryOperatorExpression> ternaryOperatorExpressions = new ArrayList<TernaryOperatorExpression>();
private List<LambdaExpressionObject> lambdas = new ArrayList<LambdaExpressionObject>();

Expand Down Expand Up @@ -167,6 +168,10 @@ public List<LeafExpression> getInstanceofExpressions() {
return instanceofExpressions;
}

public List<LeafExpression> getPatternInstanceofExpressions() {
return patternInstanceofExpressions;
}

public List<TernaryOperatorExpression> getTernaryOperatorExpressions() {
return ternaryOperatorExpressions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,15 @@ public List<LeafExpression> getInstanceofExpressions() {
return instanceofExpressions;
}

@Override
public List<LeafExpression> getPatternInstanceofExpressions() {
List<LeafExpression> instanceofExpressions = new ArrayList<>();
for(AbstractExpression expression : expressionList) {
instanceofExpressions.addAll(expression.getPatternInstanceofExpressions());
}
return instanceofExpressions;
}

@Override
public List<TernaryOperatorExpression> getTernaryOperatorExpressions() {
List<TernaryOperatorExpression> ternaryOperatorExpressions = new ArrayList<TernaryOperatorExpression>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ public List<LeafExpression> getInstanceofExpressions() {
return Collections.emptyList();
}

@Override
public List<LeafExpression> getPatternInstanceofExpressions() {
return Collections.emptyList();
}

@Override
public List<TernaryOperatorExpression> getTernaryOperatorExpressions() {
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class StatementObject extends AbstractStatement {
private List<LeafExpression> parenthesizedExpressions;
private List<LeafExpression> castExpressions;
private List<LeafExpression> instanceofExpressions;
private List<LeafExpression> patternInstanceofExpressions;
private List<TernaryOperatorExpression> ternaryOperatorExpressions;
private List<LambdaExpressionObject> lambdas;

Expand Down Expand Up @@ -82,6 +83,7 @@ public StatementObject(CompilationUnit cu, String sourceFolder, String filePath,
this.parenthesizedExpressions = visitor.getParenthesizedExpressions();
this.castExpressions = visitor.getCastExpressions();
this.instanceofExpressions = visitor.getInstanceofExpressions();
this.patternInstanceofExpressions = visitor.getPatternInstanceofExpressions();
this.ternaryOperatorExpressions = visitor.getTernaryOperatorExpressions();
this.lambdas = visitor.getLambdas();
int start = statement.getStartPosition();
Expand Down Expand Up @@ -291,6 +293,11 @@ public List<LeafExpression> getInstanceofExpressions() {
return instanceofExpressions;
}

@Override
public List<LeafExpression> getPatternInstanceofExpressions() {
return patternInstanceofExpressions;
}

@Override
public List<TernaryOperatorExpression> getTernaryOperatorExpressions() {
return ternaryOperatorExpressions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7506,14 +7506,24 @@ private boolean checkForSplitVariableDeclaration(AbstractCodeFragment leaf1, Lis
}
else if(leaf1.getVariableDeclarations().size() == 0 && mapping.getFragment1().getVariableDeclarations().size() == 0 && mapping.getFragment2().getVariableDeclarations().size() > 0) {
VariableDeclaration declaration2 = mapping.getFragment2().getVariableDeclarations().get(0);
List<LeafExpression> patternInstanceExpressions = mapping.getFragment2().getPatternInstanceofExpressions();
Set<AbstractCodeFragment> matchingVariableDeclarations1 = new LinkedHashSet<>();
for(AbstractCodeFragment l1 : leaves1) {
if(!alreadyMatched1(l1)) {
if(l1.getVariableDeclarations().size() > 0) {
VariableDeclaration declaration1 = l1.getVariableDeclarations().get(0);
boolean equalName = declaration1.getVariableName().equals(declaration2.getVariableName()) && !mapping.getFragment1().getString().startsWith(declaration2.getVariableName() + JAVA.ASSIGNMENT);
if(equalName && declaration1.getType() != null && declaration1.getType().equals(declaration2.getType())) {
matchingVariableDeclarations1.add(l1);
boolean foundInPatternInstance = false;
for(LeafExpression patternInstanceExpression : patternInstanceExpressions) {
if(patternInstanceExpression.getString().endsWith(" " + declaration1.getVariableName())) {
foundInPatternInstance = true;
break;
}
}
if(!foundInPatternInstance) {
matchingVariableDeclarations1.add(l1);
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/gr/uom/java/xmi/decomposition/Visitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.eclipse.jdt.core.dom.NumberLiteral;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
import org.eclipse.jdt.core.dom.PatternInstanceofExpression;
import org.eclipse.jdt.core.dom.PostfixExpression;
import org.eclipse.jdt.core.dom.PrefixExpression;
import org.eclipse.jdt.core.dom.PrimitiveType;
Expand Down Expand Up @@ -96,6 +97,7 @@ public class Visitor extends ASTVisitor {
private List<LeafExpression> parenthesizedExpressions = new ArrayList<>();
private List<LeafExpression> castExpressions = new ArrayList<>();
private List<LeafExpression> instanceofExpressions = new ArrayList<>();
private List<LeafExpression> patternInstanceofExpressions = new ArrayList<>();
private List<TernaryOperatorExpression> ternaryOperatorExpressions = new ArrayList<TernaryOperatorExpression>();
private List<LambdaExpressionObject> lambdas = new ArrayList<LambdaExpressionObject>();
private DefaultMutableTreeNode root = new DefaultMutableTreeNode();
Expand Down Expand Up @@ -270,6 +272,7 @@ private void removeAnonymousData() {
removeLast(this.parenthesizedExpressions, anonymous.getParenthesizedExpressions());
removeLast(this.castExpressions, anonymous.getCastExpressions());
removeLast(this.instanceofExpressions, anonymous.getInstanceofExpressions());
removeLast(this.patternInstanceofExpressions, anonymous.getPatternInstanceofExpressions());
removeLast(this.arguments, anonymous.getArguments());
this.ternaryOperatorExpressions.removeAll(anonymous.getTernaryOperatorExpressions());
this.anonymousClassDeclarations.removeAll(anonymous.getAnonymousClassDeclarations());
Expand Down Expand Up @@ -434,6 +437,16 @@ public boolean visit(InstanceofExpression node) {
return super.visit(node);
}

public boolean visit(PatternInstanceofExpression node) {
LeafExpression expression = new LeafExpression(cu, sourceFolder, filePath, node, CodeElementType.PATTERN_INSTANCEOF_EXPRESSION, container);
patternInstanceofExpressions.add(expression);
if(current.getUserObject() != null) {
AnonymousClassDeclarationObject anonymous = (AnonymousClassDeclarationObject)current.getUserObject();
anonymous.getPatternInstanceofExpressions().add(expression);
}
return super.visit(node);
}

public boolean visit(ThisExpression node) {
if(!(node.getParent() instanceof FieldAccess)) {
LeafExpression expression = new LeafExpression(cu, sourceFolder, filePath, node, CodeElementType.THIS_EXPRESSION, container);
Expand Down Expand Up @@ -907,6 +920,10 @@ public List<LeafExpression> getInstanceofExpressions() {
return instanceofExpressions;
}

public List<LeafExpression> getPatternInstanceofExpressions() {
return patternInstanceofExpressions;
}

public List<TernaryOperatorExpression> getTernaryOperatorExpressions() {
return ternaryOperatorExpressions;
}
Expand Down

0 comments on commit acca712

Please sign in to comment.