Skip to content

Commit

Permalink
refactor: Code cleanup (#165)
Browse files Browse the repository at this point in the history
* refactor: Code cleanup

Co-authored-by: Moderne <[email protected]>

* remove unnecessary cast

Co-authored-by: Moderne <[email protected]>
  • Loading branch information
pway99 and TeamModerne authored Jul 28, 2021
1 parent 3982f0e commit 1125324
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private static boolean isFloatingPointType(Expression expression) {
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(expression.getType());
if (fullyQualified != null) {
String typeName = fullyQualified.getFullyQualifiedName();
return (typeName.equals("java.lang.Double") || typeName.equals("java.lang.Float"));
return ("java.lang.Double".equals(typeName) || "java.lang.Float".equals(typeName));
}

JavaType.Primitive parameterType = TypeUtils.asPrimitive(expression.getType());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/openrewrite/java/testing/assertj/JUnitAssertEqualsToAssertThat.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private static boolean isFloatingPointType(Expression expression) {
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(expression.getType());
if (fullyQualified != null) {
String typeName = fullyQualified.getFullyQualifiedName();
return (typeName.equals("java.lang.Double") || typeName.equals("java.lang.Float"));
return ("java.lang.Double".equals(typeName) || "java.lang.Float".equals(typeName));
}

JavaType.Primitive parameterType = TypeUtils.asPrimitive(expression.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private static boolean isFloatingPointType(Expression expression) {
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(expression.getType());
if (fullyQualified != null) {
String typeName = fullyQualified.getFullyQualifiedName();
return (typeName.equals("java.lang.Double") || typeName.equals("java.lang.Float"));
return ("java.lang.Double".equals(typeName) || "java.lang.Float".equals(typeName));
}

JavaType.Primitive parameterType = TypeUtils.asPrimitive(expression.getType());
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/openrewrite/java/testing/junit5/AssertToAssertions.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
Expression firstArg = args.get(0);
// Suppress arg-switching for Assertions.assertEquals(String, String)
if (args.size() == 2) {
if (m.getSimpleName().equals("assertSame")
|| m.getSimpleName().equals("assertNotSame")
|| m.getSimpleName().equals("assertEquals")
|| m.getSimpleName().equals("assertNotEquals")) {
if ("assertSame".equals(m.getSimpleName())
|| "assertNotSame".equals(m.getSimpleName())
|| "assertEquals".equals(m.getSimpleName())
|| "assertNotEquals".equals(m.getSimpleName())) {
return m;
}
}
Expand Down Expand Up @@ -101,7 +101,7 @@ private boolean isStringArgument(Expression arg) {

private static boolean isJunitAssertMethod(J.MethodInvocation method) {
if (method.getType() != null && TypeUtils.isAssignableTo(ASSERTION_TYPE, method.getType().getDeclaringType())) {
return !method.getSimpleName().equals("assertThat");
return !"assertThat".equals(method.getSimpleName());
}
if (!(method.getSelect() instanceof J.Identifier)) {
return false;
Expand All @@ -111,7 +111,7 @@ private static boolean isJunitAssertMethod(J.MethodInvocation method) {
return false;
}
JavaType.FullyQualified receiverType = (JavaType.FullyQualified) receiver.getType();
return receiverType.getFullyQualifiedName().equals("org.junit.Assert");
return "org.junit.Assert".equals(receiverType.getFullyQualifiedName());
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/openrewrite/java/testing/junit5/ExpectedExceptionToAssertThrows.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl
Expression expectMethodArg = args.get(0);
isExpectArgAMatcher = isHamcrestMatcher(expectMethodArg);
JavaType.FullyQualified argType = TypeUtils.asFullyQualified(expectMethodArg.getType());
if (!isExpectArgAMatcher && (argType == null || !argType.getFullyQualifiedName().equals("java.lang.Class"))) {
if (!isExpectArgAMatcher && (argType == null || !"java.lang.Class".equals(argType.getFullyQualifiedName()))) {
return m;
}
}
Expand Down Expand Up @@ -235,7 +235,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (method.getType() != null && method.getType().getDeclaringType().getFullyQualifiedName().equals("org.junit.rules.ExpectedException")) {
if (method.getType() != null && "org.junit.rules.ExpectedException".equals(method.getType().getDeclaringType().getFullyQualifiedName())) {
switch (method.getSimpleName()) {
case "expect":
getCursor().putMessageOnFirstEnclosing(J.MethodDeclaration.class, "expectedExceptionMethodInvocation", method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
Cursor classDeclCursor = getCursor().dropParentUntil(J.ClassDeclaration.class::isInstance);
// methods having names starting with parametersFor... are init methods
if (m.getSimpleName().startsWith(PARAMETERS_FOR_PREFIX)) {
((Set<String>) classDeclCursor.computeMessageIfAbsent(INIT_METHOD_REFERENCES, v -> new HashSet<String>())).add(m.getSimpleName());
classDeclCursor.computeMessageIfAbsent(INIT_METHOD_REFERENCES, v -> new HashSet<>()).add(m.getSimpleName());
}
return m;
}
Expand All @@ -112,7 +112,7 @@ public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ex
String annotationArgumentValue = getAnnotationArgumentForInitMethod(anno, "method", "named");
if (annotationArgumentValue != null) {
for (String method : annotationArgumentValue.split(",")) {
((Set<String>) classDeclCursor.computeMessageIfAbsent(INIT_METHOD_REFERENCES, v -> new HashSet<String>())).add(method);
classDeclCursor.computeMessageIfAbsent(INIT_METHOD_REFERENCES, v -> new HashSet<>()).add(method);
}
} else if (anno.getArguments() != null && !anno.getArguments().isEmpty()) {
// This conversion is not supported add a comment to the annotation and the method name to the not supported list
Expand All @@ -130,15 +130,15 @@ public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ex
String namedInitMethod = getLiteralAnnotationArgumentValue(annotation);
if (namedInitMethod != null) {
J.MethodDeclaration m = getCursor().dropParentUntil(J.MethodDeclaration.class::isInstance).getValue();
((Set<String>) classDeclCursor.computeMessageIfAbsent(INIT_METHOD_REFERENCES, v -> new HashSet<String>())).add(m.getSimpleName());
classDeclCursor.computeMessageIfAbsent(INIT_METHODS_MAP, v -> new HashMap<String, String>()).put(namedInitMethod, m.getSimpleName());
classDeclCursor.computeMessageIfAbsent(INIT_METHOD_REFERENCES, v -> new HashSet<>()).add(m.getSimpleName());
classDeclCursor.computeMessageIfAbsent(INIT_METHODS_MAP, v -> new HashMap<>()).put(namedInitMethod, m.getSimpleName());
}
} else if (TEST_CASE_NAME_MATCHER.matches(anno)) {
// test name for ParameterizedTest argument
Object testNameArg = getLiteralAnnotationArgumentValue(anno);
String testName = testNameArg != null ? testNameArg.toString() : "{method}({params}) [{index}]";
J.MethodDeclaration md = getCursor().dropParentUntil(J.MethodDeclaration.class::isInstance).getValue();
classDeclCursor.computeMessageIfAbsent(INIT_METHODS_MAP, v -> new HashMap<String, String>()).put(md.getSimpleName(), testName);
classDeclCursor.computeMessageIfAbsent(INIT_METHODS_MAP, v -> new HashMap<>()).put(md.getSimpleName(), testName);
}
return anno;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
J.MethodDeclaration md = super.visitMethodDeclaration(method, executionContext);
if (md.getSimpleName().startsWith("test") && md.getLeadingAnnotations().stream().noneMatch(JUNIT_TEST_ANNOTATION_MATCHER::matches)) {
md = updateMethodDeclarationAnnotationAndModifier(md, "@Test", "org.junit.jupiter.api.Test");
} else if (md.getSimpleName().equals("setUp")) {
} else if ("setUp".equals(md.getSimpleName())) {
md = updateMethodDeclarationAnnotationAndModifier(md, "@BeforeEach", "org.junit.jupiter.api.BeforeEach");
} else if (md.getSimpleName().equals("tearDown")) {
} else if ("tearDown".equals(md.getSimpleName())) {
md = updateMethodDeclarationAnnotationAndModifier(md, "@AfterEach", "org.junit.jupiter.api.AfterEach");
}
return md;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
variableForInitMethod = variableForInitMethod.withTypeExpression(variableForInitMethod.getTypeExpression().withPrefix(Space.EMPTY).withComments(new ArrayList<>()));
}
//noinspection unchecked
((TreeMap<Integer, Statement>) params.computeIfAbsent(FIELD_INJECTION_ARGUMENTS, v -> new TreeMap<Integer, Statement>())).put(position, variableForInitMethod);
((TreeMap<Integer, Statement>) params.computeIfAbsent(FIELD_INJECTION_ARGUMENTS, v -> new TreeMap<>())).put(position, variableForInitMethod);

}
return variableDeclarations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
JavaType newFolderMethodDeclaration = methods
.filter(m -> {
List<Statement> params = m.getParameters();
return m.getSimpleName().equals("newFolder")
return "newFolder".equals(m.getSimpleName())
&& params.size() == 2
&& params.get(0).hasClassType(FILE_TYPE)
&& params.get(1).hasClassType(STRING_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
String assignParamName = ((J.Identifier) assign.getVariable()).getSimpleName();
Expression e = assign.getAssignment();

if (assignParamName.equals("expected")) {
if ("expected".equals(assignParamName)) {
assert e instanceof J.FieldAccess;

m = m.withTemplate(JavaTemplate.builder(this::getCursor, "Object o = () -> #{}").build(),
Expand All @@ -146,7 +146,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
e, lambda);

maybeAddImport("org.junit.jupiter.api.Assertions", "assertThrows");
} else if (assignParamName.equals("timeout")) {
} else if ("timeout".equals(assignParamName)) {
doAfterVisit(new AddTimeoutAnnotationStep(m, e));
}
}
Expand Down

0 comments on commit 1125324

Please sign in to comment.