Skip to content
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

review: feat: EqualsVisitor can report which attribute is not equal #1998

Merged
merged 2 commits into from
May 22, 2018
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
118 changes: 48 additions & 70 deletions src/main/java/spoon/support/visitor/equals/EqualsChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@
import spoon.reflect.declaration.CtModifiable;
import spoon.reflect.declaration.CtNamedElement;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.path.CtRole;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.declaration.CtImport;
import spoon.reflect.reference.CtReference;
import spoon.reflect.visitor.CtInheritanceScanner;

public class EqualsChecker extends CtInheritanceScanner {
private CtElement other;
protected CtElement other;
private boolean isNotEqual;
private CtRole notEqualRole;

public void setOther(CtElement other) {
this.other = other;
Expand All @@ -48,16 +50,24 @@ public boolean isNotEqual() {
return isNotEqual;
}

private void setNotEqual() {
public CtRole getNotEqualRole() {
return notEqualRole;
}

/**
* @param role the role of the not equal attribute, or null if there is no such role
*/
protected void setNotEqual(CtRole role) {
notEqualRole = role;
isNotEqual = true;
throw NotEqualException.INSTANCE;
}

@Override
public void scanCtNamedElement(CtNamedElement e) {
final CtNamedElement peek = (CtNamedElement) this.other;
if (!e.getSimpleName().equals(peek.getSimpleName())) {
setNotEqual();
return;
setNotEqual(CtRole.NAME);
}
super.scanCtNamedElement(e);
}
Expand All @@ -66,8 +76,7 @@ public void scanCtNamedElement(CtNamedElement e) {
public void scanCtReference(CtReference reference) {
final CtReference peek = (CtReference) this.other;
if (!reference.getSimpleName().equals(peek.getSimpleName())) {
setNotEqual();
return;
setNotEqual(CtRole.NAME);
}
super.scanCtReference(reference);
}
Expand All @@ -82,8 +91,7 @@ public void scanCtStatement(CtStatement s) {
return;
}
if (leftLabel == null || !leftLabel.equals(rightLabel)) {
setNotEqual();
return;
setNotEqual(CtRole.LABEL);
}
super.scanCtStatement(s);
}
Expand All @@ -93,32 +101,26 @@ public void scanCtModifiable(CtModifiable m) {
final CtModifiable peek = (CtModifiable) this.other;
if (m.getVisibility() == null) {
if (peek.getVisibility() != null) {
setNotEqual();
return;
setNotEqual(CtRole.MODIFIER);
}
} else if (peek.getVisibility() == null) {
setNotEqual();
return;
setNotEqual(CtRole.MODIFIER);
} else if (!m.getVisibility().equals(peek.getVisibility())) {
setNotEqual();
return;
setNotEqual(CtRole.MODIFIER);
}
if (m.getModifiers().size() != peek.getModifiers().size()) {
setNotEqual();
return;
setNotEqual(CtRole.MODIFIER);
}
if (!m.getModifiers().containsAll(peek.getModifiers())) {
setNotEqual();
return;
setNotEqual(CtRole.MODIFIER);
}
super.scanCtModifiable(m);
}

@Override
public <T, A extends T> void visitCtAssignment(CtAssignment<T, A> assignment) {
if (!(assignment instanceof CtOperatorAssignment) && this.other instanceof CtOperatorAssignment) {
setNotEqual();
return;
setNotEqual(null);
}
super.visitCtAssignment(assignment);
}
Expand All @@ -128,15 +130,12 @@ public <T, A extends T> void visitCtOperatorAssignment(CtOperatorAssignment<T, A
final CtOperatorAssignment peek = (CtOperatorAssignment) this.other;
if (assignment.getKind() == null) {
if (peek.getKind() != null) {
setNotEqual();
return;
setNotEqual(CtRole.OPERATOR_KIND);
}
} else if (peek.getKind() == null) {
setNotEqual();
return;
setNotEqual(CtRole.OPERATOR_KIND);
} else if (!assignment.getKind().equals(peek.getKind())) {
setNotEqual();
return;
setNotEqual(CtRole.OPERATOR_KIND);
}
super.visitCtOperatorAssignment(assignment);
}
Expand All @@ -146,15 +145,12 @@ public <T> void visitCtBinaryOperator(CtBinaryOperator<T> e) {
final CtBinaryOperator peek = (CtBinaryOperator) this.other;
if (e.getKind() == null) {
if (peek.getKind() != null) {
setNotEqual();
return;
setNotEqual(CtRole.OPERATOR_KIND);
}
} else if (peek.getKind() == null) {
setNotEqual();
return;
setNotEqual(CtRole.OPERATOR_KIND);
} else if (!e.getKind().equals(peek.getKind())) {
setNotEqual();
return;
setNotEqual(CtRole.OPERATOR_KIND);
}
super.visitCtBinaryOperator(e);
}
Expand All @@ -164,15 +160,12 @@ public <T> void visitCtUnaryOperator(CtUnaryOperator<T> e) {
final CtUnaryOperator peek = (CtUnaryOperator) this.other;
if (e.getKind() == null) {
if (peek.getKind() != null) {
setNotEqual();
return;
setNotEqual(CtRole.OPERATOR_KIND);
}
} else if (peek.getKind() == null) {
setNotEqual();
return;
setNotEqual(CtRole.OPERATOR_KIND);
} else if (!e.getKind().equals(peek.getKind())) {
setNotEqual();
return;
setNotEqual(CtRole.OPERATOR_KIND);
}
super.visitCtUnaryOperator(e);
}
Expand All @@ -181,8 +174,7 @@ public <T> void visitCtUnaryOperator(CtUnaryOperator<T> e) {
public <T> void visitCtArrayTypeReference(CtArrayTypeReference<T> e) {
final CtArrayTypeReference peek = (CtArrayTypeReference) this.other;
if (e.getDimensionCount() != peek.getDimensionCount()) {
setNotEqual();
return;
setNotEqual(CtRole.TYPE);
}
super.visitCtArrayTypeReference(e);
}
Expand All @@ -192,15 +184,12 @@ public void visitCtBreak(CtBreak e) {
final CtBreak peek = (CtBreak) this.other;
if (e.getTargetLabel() == null) {
if (peek.getTargetLabel() != null) {
setNotEqual();
return;
setNotEqual(CtRole.TARGET_LABEL);
}
} else if (peek.getTargetLabel() == null) {
setNotEqual();
return;
setNotEqual(CtRole.TARGET_LABEL);
} else if (!e.getTargetLabel().equals(peek.getTargetLabel())) {
setNotEqual();
return;
setNotEqual(CtRole.TARGET_LABEL);
}
super.visitCtBreak(e);
}
Expand All @@ -210,15 +199,12 @@ public void visitCtContinue(CtContinue e) {
final CtContinue peek = (CtContinue) this.other;
if (e.getTargetLabel() == null) {
if (peek.getTargetLabel() != null) {
setNotEqual();
return;
setNotEqual(CtRole.TARGET_LABEL);
}
} else if (peek.getTargetLabel() == null) {
setNotEqual();
return;
setNotEqual(CtRole.TARGET_LABEL);
} else if (!e.getTargetLabel().equals(peek.getTargetLabel())) {
setNotEqual();
return;
setNotEqual(CtRole.TARGET_LABEL);
}
super.visitCtContinue(e);
}
Expand All @@ -227,8 +213,7 @@ public void visitCtContinue(CtContinue e) {
public <T> void visitCtExecutableReference(CtExecutableReference<T> e) {
final CtExecutableReference peek = (CtExecutableReference) this.other;
if (e.isConstructor() != peek.isConstructor()) {
setNotEqual();
return;
setNotEqual(null);
}
super.visitCtExecutableReference(e);
}
Expand All @@ -237,8 +222,7 @@ public <T> void visitCtExecutableReference(CtExecutableReference<T> e) {
public <T> void visitCtMethod(CtMethod<T> e) {
final CtMethod peek = (CtMethod) this.other;
if (e.isDefaultMethod() != peek.isDefaultMethod()) {
setNotEqual();
return;
setNotEqual(CtRole.MODIFIER);
}
super.visitCtMethod(e);
}
Expand All @@ -247,8 +231,7 @@ public <T> void visitCtMethod(CtMethod<T> e) {
public <T> void visitCtParameter(CtParameter<T> e) {
final CtParameter peek = (CtParameter) this.other;
if (e.isVarArgs() != peek.isVarArgs()) {
setNotEqual();
return;
setNotEqual(CtRole.MODIFIER);
}
super.visitCtParameter(e);
}
Expand All @@ -258,15 +241,12 @@ public <T> void visitCtLiteral(CtLiteral<T> e) {
final CtLiteral peek = (CtLiteral) this.other;
if (e.getValue() == null) {
if (peek.getValue() != null) {
setNotEqual();
return;
setNotEqual(CtRole.VALUE);
}
} else if (peek.getValue() == null) {
setNotEqual();
return;
setNotEqual(CtRole.VALUE);
} else if (!e.getValue().equals(peek.getValue())) {
setNotEqual();
return;
setNotEqual(CtRole.VALUE);
}
super.visitCtLiteral(e);
}
Expand All @@ -277,17 +257,15 @@ public void visitCtImport(CtImport ctImport) {

if (ctImport.getImportKind() == null) {
if (peek.getImportKind() != null) {
setNotEqual();
return;
setNotEqual(null);
}
} else if (peek.getImportKind() == null) {
setNotEqual();
return;
setNotEqual(null);
} else if (!ctImport.getImportKind().equals(peek.getImportKind())) {
setNotEqual();
return;
setNotEqual(null);
}

super.visitCtImport(ctImport);
}

}
Loading