Skip to content

Commit

Permalink
feat: Introduced CommonTypes enum (#1456)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt committed Jan 13, 2024
1 parent 0cb3f9b commit d43f2aa
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package xyz.keksdose.spoon.code_solver.analyzer.spoon;

import spoon.Launcher;
import spoon.reflect.reference.CtTypeReference;

/**
* The CommonTypes enum represents common types in Java. It is used to provide a convenient way to
* reference common types and their corresponding Class objects. Each enum value represents a
* specific common type, such as STRING, INTEGER, or BOOLEAN. <br>
* Each enum value is associated with a Class object representing the corresponding common type. The
* CommonTypes enum should be used whenever you need to refer to a common type in Java.
*/
public enum CommonTypes {
STRING(String.class),
INTEGER(Integer.class),
LONG(Long.class),
DOUBLE(Double.class),
FLOAT(Float.class),
BOOLEAN(Boolean.class),
BYTE(Byte.class),
CHARACTER(Character.class),
SHORT(Short.class),
VOID(Void.class),
OBJECT(Object.class);

private final CtTypeReference<?> clazz;

CommonTypes(Class<?> clazz) {
this.clazz = new Launcher().getFactory().Type().createReference(clazz);
}

public CtTypeReference<?> getClazz() {
return clazz;
}

@Override
public String toString() {
return "CommonTypes{" + "clazz=" + clazz.getQualifiedName() + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import io.github.martinwitt.laughing_train.domain.entity.AnalyzerResult;
import io.github.martinwitt.laughing_train.spoonutils.InvocationMatcher;
import java.util.ArrayList;
import java.util.List;
import spoon.reflect.code.*;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtType;
Expand All @@ -17,6 +15,9 @@
import xyz.keksdose.spoon.code_solver.history.MarkdownString;
import xyz.keksdose.spoon.code_solver.transformations.BadSmell;

import java.util.ArrayList;
import java.util.List;

public class IndexOfReplaceableByContains extends SpoonRefactoring {

private final ThreadLocal<InvocationMatcher> matcher = new ThreadLocal<InvocationMatcher>();
Expand Down Expand Up @@ -111,7 +112,7 @@ private boolean isMinusOne(CtExpression<?> expression) {
return false;
}

record ResultRecord(CtExpression<?> indexOfCall, CtExpression<?> minusOne) {}
private record ResultRecord(CtExpression<?> indexOfCall, CtExpression<?> minusOne) {}

private List<ResultRecord> getIndexMinusOnePairs(CtElement clazz) {
List<ResultRecord> resultRecords = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package xyz.keksdose.spoon.code_solver.analyzer.spoon.rules;

import io.github.martinwitt.laughing_train.domain.entity.AnalyzerResult;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.declaration.CtType;
import spoon.reflect.visitor.Filter;
import xyz.keksdose.spoon.code_solver.analyzer.spoon.CommonTypes;
import xyz.keksdose.spoon.code_solver.analyzer.spoon.SpoonAnalyzerResult;
import xyz.keksdose.spoon.code_solver.analyzer.spoon.SpoonRefactoring;
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 java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

public class UnnecessaryToString extends SpoonRefactoring {

private static final BadSmell BAD_SMELL =
Expand Down Expand Up @@ -79,7 +81,7 @@ private static final class UnnecessaryToStringFilter implements Filter<CtInvocat
public boolean matches(CtInvocation element) {
return element.getTarget() != null
&& element.getTarget().getType() != null
&& element.getTarget().getType().getSimpleName().equals("String")
&& element.getTarget().getType().equals(CommonTypes.STRING.getClazz())
&& element.getExecutable().getSimpleName().equals("toString");
}
}
Expand Down

0 comments on commit d43f2aa

Please sign in to comment.