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

feat: convert primitives to String with String.valueOf #44

Merged
merged 1 commit into from
Jan 25, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import xyz.keksdose.spoon.code_solver.transformations.qodana.ArraysToString;
import xyz.keksdose.spoon.code_solver.transformations.qodana.EmptyStringCheck;
import xyz.keksdose.spoon.code_solver.transformations.qodana.InnerClassStatic;
import xyz.keksdose.spoon.code_solver.transformations.qodana.PrimitiveToString;
import xyz.keksdose.spoon.code_solver.transformations.qodana.StaticAccess;
import xyz.keksdose.spoon.code_solver.transformations.self.StringBuilderDirectUse;
import xyz.keksdose.spoon.code_solver.transformations.self.ThreadLocalWithInitial;
Expand All @@ -46,7 +47,8 @@ public TransformationEngine() {
this.processors = List.of(StringBuilderDirectUse::new, ThreadLocalWithInitial::new,
TempoaryFolderAsParameter::new, EmptyStringCheck::new, ArraysToString::new,
Junit4AnnotationsTransformation::new, TestAnnotation::new, AssertionsTransformation::new,
AssertThatTransformation::new, ExpectedExceptionRemoval::new, StaticAccess::new, InnerClassStatic::new);
AssertThatTransformation::new, ExpectedExceptionRemoval::new, StaticAccess::new, InnerClassStatic::new,
PrimitiveToString::new);
}

public TransformationEngine setPrinting(IPrinting printing) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

package xyz.keksdose.spoon.code_solver.transformations.qodana;

import spoon.reflect.code.BinaryOperatorKind;
import spoon.reflect.code.CtBinaryOperator;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.declaration.CtType;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtTypeReference;
import xyz.keksdose.spoon.code_solver.history.Change;
import xyz.keksdose.spoon.code_solver.history.ChangeListener;
import xyz.keksdose.spoon.code_solver.history.MarkdownString;
import xyz.keksdose.spoon.code_solver.transformations.BadSmell;
import xyz.keksdose.spoon.code_solver.transformations.TransformationProcessor;

public class PrimitiveToString extends TransformationProcessor<CtBinaryOperator<?>> {

private static final BadSmell STRING_VALUE_OF = new BadSmell() {
@Override
public MarkdownString getName() {
return MarkdownString.fromRaw("String-ValueOf-Primitive");
}

@Override
public MarkdownString getDescription() {
String raw = "Primitive types are converted to String using concationation with `\"\"`"
+ "String.valueOf(primitive) is the preferred way to convert a primitive to a String.";
String markdown = "Primitive types are converted to String using concationation with `\"\"`"
+ "`String.valueOf(primitive)` is the preferred way to convert a primitive to a String.";
return MarkdownString.fromMarkdown(raw, markdown);
}
};
public PrimitiveToString(ChangeListener listener) {
super(listener);
}

@Override
public void process(CtBinaryOperator<?> op) {
if (op.getKind().equals(BinaryOperatorKind.PLUS)) {
if (isEmptyString(op.getLeftHandOperand()) && isPrimitive(op.getRightHandOperand())) {
refactor(op, op.getRightHandOperand());
}
if (isPrimitive(op.getLeftHandOperand()) && isEmptyString(op.getRightHandOperand())) {
refactor(op, op.getLeftHandOperand());
}
}
}

private boolean isEmptyString(CtExpression<?> exp) {
return exp.toString().equals("\"\"");
}

private boolean isPrimitive(CtExpression<?> exp) {
return exp.getType() != null && exp.getType().isPrimitive();
}

private void refactor(CtBinaryOperator<?> op, CtExpression<?> primitive) {
CtTypeReference<Object> stringType = getFactory().Type().createSimplyQualifiedReference("java.lang.String");
CtExecutableReference<?> valueOf = getFactory().Executable()
.createReference(stringType, stringType, "valueOf", primitive.getType());
CtInvocation<?> valueOfInvocation = getFactory().Code()
.createInvocation(getFactory().createTypeAccess(stringType), valueOf, primitive);
notifyChangeListener(op, valueOfInvocation);
op.replace(valueOfInvocation);
}

private void notifyChangeListener(CtBinaryOperator<?> op, CtExpression<?> primitive) {
String raw = "Replaced " + op + " with String.valueOf(" + primitive + ")";
String markdown = "Replaced `" + op + "` with `String.valueOf(" + primitive + ")`";
CtType<?> parent = op.getParent(CtType.class).getTopLevelType();
setChanged(parent, new Change(STRING_VALUE_OF, MarkdownString.fromMarkdown(raw, markdown), parent));
}
}