Skip to content

Commit

Permalink
Handle toString() and compareTo() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabrice Tiercelin committed May 2, 2021
1 parent 727a583 commit 2f35a94
Show file tree
Hide file tree
Showing 17 changed files with 404 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ && isNotNull(fragment.getInitializer())) {
varOccurrenceVisitor.traverseNodeInterruptibly(parentBlock);

if (varOccurrenceVisitor.isPrimitiveAllowed() && varOccurrenceVisitor.getAutoBoxingCount() < 2) {
refactorWrapper(node);
refactorWrapper(node, varOccurrenceVisitor.getToStringMethods(), varOccurrenceVisitor.getCompareToMethods());
return false;
}
}
Expand All @@ -191,11 +191,26 @@ && isNotNull(fragment.getInitializer())) {
return true;
}

private void refactorWrapper(final VariableDeclarationStatement node) {
private void refactorWrapper(final VariableDeclarationStatement node, final List<MethodInvocation> toStringMethods, final List<MethodInvocation> compareToMethods) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
ASTNodeFactory ast= cuRewrite.getASTBuilder();
TextEditGroup group= new TextEditGroup(""); //$NON-NLS-1$

for (MethodInvocation toStringMethod : toStringMethods) {
Type wrapperType= rewrite.createCopyTarget(node.getType());

rewrite.insertFirst(toStringMethod, MethodInvocation.ARGUMENTS_PROPERTY, ASTNodes.createMoveTarget(rewrite, toStringMethod.getExpression()), group);
rewrite.set(toStringMethod, MethodInvocation.EXPRESSION_PROPERTY, wrapperType, group);
}

for (MethodInvocation compareToMethod : compareToMethods) {
Type wrapperType= rewrite.createCopyTarget(node.getType());

rewrite.insertFirst(compareToMethod, MethodInvocation.ARGUMENTS_PROPERTY, ASTNodes.createMoveTarget(rewrite, compareToMethod.getExpression()), group);
rewrite.set(compareToMethod, MethodInvocation.EXPRESSION_PROPERTY, wrapperType, group);
rewrite.replace(compareToMethod.getName(), ast.newSimpleName("compare"), group); //$NON-NLS-1$
}

Type primitiveType= ast.type(getPrimitiveTypeName());

ASTNodes.replaceButKeepComment(rewrite, node.getType(), primitiveType, group);
Expand Down Expand Up @@ -255,6 +270,8 @@ private boolean isNotNull(final Expression expression) {

private class VarOccurrenceVisitor extends InterruptibleVisitor {
private final VariableDeclarationFragment varDecl;
private final List<MethodInvocation> toStringMethods = new ArrayList<>();
private final List<MethodInvocation> compareToMethods = new ArrayList<>();
private boolean isPrimitiveAllowed= true;
private boolean isVarReturned;
private int autoBoxingCount;
Expand All @@ -271,9 +288,18 @@ public int getAutoBoxingCount() {
return autoBoxingCount;
}

public List<MethodInvocation> getToStringMethods() {
return toStringMethods;
}

public List<MethodInvocation> getCompareToMethods() {
return compareToMethods;
}

@Override
public boolean visit(final SimpleName aVar) {
if (isPrimitiveAllowed && ASTNodes.isSameVariable(aVar, varDecl.getName())
if (isPrimitiveAllowed
&& ASTNodes.isSameVariable(aVar, varDecl.getName())
&& !aVar.getParent().equals(varDecl)) {
isPrimitiveAllowed= isPrimitiveAllowed(aVar);

Expand Down Expand Up @@ -361,9 +387,31 @@ private boolean isPrimitiveAllowed(final ASTNode node) {
case ASTNode.POSTFIX_EXPRESSION:
return getPostfixOutSafeOperators().contains(((PostfixExpression) parentNode).getOperator());

case ASTNode.METHOD_INVOCATION:
MethodInvocation methodInvocation= (MethodInvocation) parentNode;

if (node.getLocationInParent() == MethodInvocation.EXPRESSION_PROPERTY) {
if (ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "toString")) { //$NON-NLS-1$
toStringMethods.add(methodInvocation);
return true;
}

if (ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "compareTo", getWrapperFullyQualifiedName())) { //$NON-NLS-1$
if (ASTNodes.hasType((Expression) methodInvocation.arguments().get(0), getWrapperFullyQualifiedName())) {
autoBoxingCount++;
}

compareToMethods.add(methodInvocation);
return true;
}
}

break;

default:
return isSpecificPrimitiveAllowed(node);
}

return isSpecificPrimitiveAllowed(node);
}

private boolean isOfType(final ITypeBinding resolveTypeBinding) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,28 @@ public void replaceBitAssignedWrapper(Boolean aBoolean, Boolean anotherBoolean,
yetAnotherBoolean ^= assignedBoolean;
}

public String replaceWrapperAndToStringMethod(boolean b) {
// Keep this comment
Boolean alwaysInitializedVar = Boolean.TRUE;
if (alwaysInitializedVar) {
System.out.println("True!");
}

// Keep this comment too
return alwaysInitializedVar.toString();
}

public int replaceWrapperAndCompareToMethod(boolean b) {
// Keep this comment
Boolean alwaysInitializedVar = Boolean.TRUE;
if (alwaysInitializedVar) {
System.out.println("True!");
}

// Keep this comment too
return alwaysInitializedVar.compareTo(b);
}

public void doNotReplaceNullWrapper() {
Boolean reassignedBoolean = Boolean.TRUE;
reassignedBoolean = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,28 @@ public void replaceWrapperAssignedOnWrapperField() {
wrapperField = assignedByte;
}

public String replaceWrapperAndToStringMethod(byte b) {
// Keep this comment
Byte alwaysInitializedVar = Byte.MIN_VALUE;
if (alwaysInitializedVar > b) {
System.out.println("True!");
}

// Keep this comment too
return alwaysInitializedVar.toString();
}

public int replaceWrapperAndCompareToMethod(byte b) {
// Keep this comment
Byte alwaysInitializedVar = Byte.MIN_VALUE;
if (alwaysInitializedVar > b) {
System.out.println("True!");
}

// Keep this comment too
return alwaysInitializedVar.compareTo(b);
}

public Object doNotBreakAutoboxing() {
Byte returnedObject = Byte.MIN_VALUE;
return returnedObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,28 @@ public void replaceBitAssignedWrapper(int anInteger, int anotherInteger,
yetAnotherInteger ^= assignedCharacter;
}

public String replaceWrapperAndToStringMethod(char c) {
// Keep this comment
Character alwaysInitializedVar = Character.MIN_VALUE;
if (alwaysInitializedVar > c) {
System.out.println("True!");
}

// Keep this comment too
return alwaysInitializedVar.toString();
}

public int replaceWrapperAndCompareToMethod(char c) {
// Keep this comment
Character alwaysInitializedVar = Character.MIN_VALUE;
if (alwaysInitializedVar > c) {
System.out.println("True!");
}

// Keep this comment too
return alwaysInitializedVar.compareTo(c);
}

public Object doNotBreakAutoboxing() {
Character returnedObject = Character.MIN_VALUE;
return returnedObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,28 @@ public void replaceWrapperAssignedOnWrapperField() {
this.wrapperField = assignedDouble;
}

public String replaceWrapperAndToStringMethod(double d) {
// Keep this comment
Double alwaysInitializedVar = Double.MIN_VALUE;
if (alwaysInitializedVar > d) {
System.out.println("True!");
}

// Keep this comment too
return alwaysInitializedVar.toString();
}

public int replaceWrapperAndCompareToMethod(double d) {
// Keep this comment
Double alwaysInitializedVar = Double.MIN_VALUE;
if (alwaysInitializedVar > d) {
System.out.println("True!");
}

// Keep this comment too
return alwaysInitializedVar.compareTo(d);
}

public void doNotReplaceWrapperAssignedOnObjectField() {
Double assignedDouble = Double.MIN_VALUE;
objectField = assignedDouble;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,28 @@ public void replaceBitAssignedWrapper(Float aFloat, Float anotherFloat,
evenAnotherFloat /= assignedFloat;
}

public String replaceWrapperAndToStringMethod(float f) {
// Keep this comment
Float alwaysInitializedVar = Float.MIN_VALUE;
if (alwaysInitializedVar > f) {
System.out.println("True!");
}

// Keep this comment too
return alwaysInitializedVar.toString();
}

public int replaceWrapperAndCompareToMethod(float f) {
// Keep this comment
Float alwaysInitializedVar = Float.MIN_VALUE;
if (alwaysInitializedVar > f) {
System.out.println("True!");
}

// Keep this comment too
return alwaysInitializedVar.compareTo(f);
}

public Float doNotReplaceMultiAutoBoxedWrapper() {
Float assignedFloat = Float.MIN_VALUE;
Float anotherFloat = assignedFloat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,28 @@ public void replaceBitAssignedWrapper(Integer aInteger, Integer anotherInteger,
yetAnotherInteger ^= assignedInteger;
}

public String replaceWrapperAndToStringMethod(int i) {
// Keep this comment
Integer alwaysInitializedVar = Integer.MIN_VALUE;
if (alwaysInitializedVar > i) {
System.out.println("True!");
}

// Keep this comment too
return alwaysInitializedVar.toString();
}

public int replaceWrapperAndCompareToMethod(int i) {
// Keep this comment
Integer alwaysInitializedVar = Integer.MIN_VALUE;
if (alwaysInitializedVar > i) {
System.out.println("True!");
}

// Keep this comment too
return alwaysInitializedVar.compareTo(i);
}

public Integer doNotReplaceMultiAutoBoxedWrapper() {
Integer assignedInteger = Integer.MIN_VALUE;
Integer anotherInteger = assignedInteger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,28 @@ public void replaceBitAssignedWrapper(Long aLong, Long anotherLong,
yetAnotherLong ^= assignedLong;
}

public String replaceWrapperAndToStringMethod(long l) {
// Keep this comment
Long alwaysInitializedVar = Long.MIN_VALUE;
if (alwaysInitializedVar > l) {
System.out.println("True!");
}

// Keep this comment too
return alwaysInitializedVar.toString();
}

public int replaceWrapperAndCompareToMethod(long l) {
// Keep this comment
Long alwaysInitializedVar = Long.MIN_VALUE;
if (alwaysInitializedVar > l) {
System.out.println("True!");
}

// Keep this comment too
return alwaysInitializedVar.compareTo(l);
}

public Long doNotReplaceMultiAutoBoxedWrapper() {
Long assignedLong = Long.MIN_VALUE;
Long anotherLong = assignedLong;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,28 @@ public void replaceBitAssignedWrapper(Integer anInteger, Integer anotherInteger,
yetAnotherInteger ^= assignedShort;
}

public String replaceWrapperAndToStringMethod(short s) {
// Keep this comment
Short alwaysInitializedVar = Short.MIN_VALUE;
if (alwaysInitializedVar > s) {
System.out.println("True!");
}

// Keep this comment too
return alwaysInitializedVar.toString();
}

public int replaceWrapperAndCompareToMethod(short s) {
// Keep this comment
Short alwaysInitializedVar = Short.MIN_VALUE;
if (alwaysInitializedVar > s) {
System.out.println("True!");
}

// Keep this comment too
return alwaysInitializedVar.compareTo(s);
}

public Short doNotReplaceMultiAutoBoxedWrapper() {
Short assignedShort = Short.MIN_VALUE;
Short anotherShort = assignedShort;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,28 @@ public void replaceBitAssignedWrapper(Boolean aBoolean, Boolean anotherBoolean,
yetAnotherBoolean ^= assignedBoolean;
}

public String replaceWrapperAndToStringMethod(boolean b) {
// Keep this comment
boolean alwaysInitializedVar = Boolean.TRUE;
if (alwaysInitializedVar) {
System.out.println("True!");
}

// Keep this comment too
return Boolean.toString(alwaysInitializedVar);
}

public int replaceWrapperAndCompareToMethod(boolean b) {
// Keep this comment
boolean alwaysInitializedVar = Boolean.TRUE;
if (alwaysInitializedVar) {
System.out.println("True!");
}

// Keep this comment too
return Boolean.compare(alwaysInitializedVar, b);
}

public void doNotReplaceNullWrapper() {
Boolean reassignedBoolean = Boolean.TRUE;
reassignedBoolean = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,28 @@ public void replaceWrapperAssignedOnWrapperField() {
wrapperField = assignedByte;
}

public String replaceWrapperAndToStringMethod(byte b) {
// Keep this comment
byte alwaysInitializedVar = Byte.MIN_VALUE;
if (alwaysInitializedVar > b) {
System.out.println("True!");
}

// Keep this comment too
return Byte.toString(alwaysInitializedVar);
}

public int replaceWrapperAndCompareToMethod(byte b) {
// Keep this comment
byte alwaysInitializedVar = Byte.MIN_VALUE;
if (alwaysInitializedVar > b) {
System.out.println("True!");
}

// Keep this comment too
return Byte.compare(alwaysInitializedVar, b);
}

public Object doNotBreakAutoboxing() {
Byte returnedObject = Byte.MIN_VALUE;
return returnedObject;
Expand Down

0 comments on commit 2f35a94

Please sign in to comment.