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

refactor: Operator wrapping on end of line #559

Merged
merged 1 commit into from
Sep 21, 2024
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 @@ -63,11 +63,11 @@ public J visitTypeCast(J.TypeCast typeCast, ExecutionContext ctx) {
elementType = ((JavaType.Array) elementType).getElemType();
}

boolean matches = (elementType instanceof JavaType.Class || elementType instanceof JavaType.Parameterized)
&& ((JavaType.FullyQualified) elementType).getOwningClass() == null // does not support inner class now
&& LIST_TO_ARRAY.matches(typeCast.getExpression())
&& typeCast.getExpression() instanceof J.MethodInvocation
&& ARRAYS_AS_LIST.matches(((J.MethodInvocation) typeCast.getExpression()).getSelect());
boolean matches = (elementType instanceof JavaType.Class || elementType instanceof JavaType.Parameterized) &&
((JavaType.FullyQualified) elementType).getOwningClass() == null // does not support inner class now
&& LIST_TO_ARRAY.matches(typeCast.getExpression()) &&
typeCast.getExpression() instanceof J.MethodInvocation &&
ARRAYS_AS_LIST.matches(((J.MethodInvocation) typeCast.getExpression()).getSelect());
if (!matches) {
return typeCast;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public String getDisplayName() {

@Override
public String getDescription() {
return "The recipe renames `getSuppressed()` and `addSuppressed(Throwable exception)` methods in classes "
+ "that extend `java.lang.Throwable` to `myGetSuppressed` and `myAddSuppressed(Throwable)`."
+ "These methods were added to Throwable in Java 7 and are marked final which cannot be overridden.";
return "The recipe renames `getSuppressed()` and `addSuppressed(Throwable exception)` methods in classes " +
"that extend `java.lang.Throwable` to `myGetSuppressed` and `myAddSuppressed(Throwable)`." +
"These methods were added to Throwable in Java 7 and are marked final which cannot be overridden.";
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/openrewrite/java/migrate/MXBeanRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclarat
}

List<Modifier> modifiers = new ArrayList<>(cd.getModifiers());
modifiers.removeIf(modifier -> modifier.getType() == Modifier.Type.Private
|| modifier.getType() == Modifier.Type.Protected
|| modifier.getType() == Modifier.Type.Abstract);
modifiers.removeIf(modifier -> modifier.getType() == Modifier.Type.Private ||
modifier.getType() == Modifier.Type.Protected ||
modifier.getType() == Modifier.Type.Abstract);
modifiers.add(new J.Modifier(randomId(), Space.EMPTY, Markers.EMPTY, Modifier.Type.Public, emptyList()));
return maybeAutoFormat(cd, cd.withModifiers(sortModifiers(modifiers)), ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public J visitTypeCast(J.TypeCast typeCast, ExecutionContext ctx) {
J j = super.visitTypeCast(typeCast, ctx);
if (Boolean.TRUE.equals(getCursor().pollNearestMessage(REFERENCE_CLONE_REPLACED))
&& j instanceof J.TypeCast) {
if (Boolean.TRUE.equals(getCursor().pollNearestMessage(REFERENCE_CLONE_REPLACED)) &&
j instanceof J.TypeCast) {
J.TypeCast tc = (J.TypeCast) j;
if (TypeUtils.isOfType(tc.getType(), tc.getExpression().getType())) {
return tc.getExpression();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public J visitMethodInvocation(J.MethodInvocation mi, ExecutionContext ctx) {
maybeAddImport("java.awt.GraphicsEnvironment", false);
maybeAddImport("java.awt.Window", false);
maybeAddImport("java.awt.GraphicsDevice.WindowTranslucency", false);
String templateString = "GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().isWindowTranslucencySupported(WindowTranslucency."
+ ((J.FieldAccess) mi.getArguments().get(0)).getSimpleName();
String templateString = "GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().isWindowTranslucencySupported(WindowTranslucency." +
((J.FieldAccess) mi.getArguments().get(0)).getSimpleName();
return JavaTemplate.builder(templateString).
imports("java.awt.GraphicsDevice",
"java.awt.GraphicsEnvironment",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
J.NewClass c = (J.NewClass) super.visitNewClass(newClass, ctx);
if (newBase64Encoder.matches(c)) {
// noinspection Convert2MethodRef
JavaTemplate.Builder encoderTemplate = useMimeCoder
? Semantics.expression(this, "getMimeEncoder", () -> Base64.getMimeEncoder())
: Semantics.expression(this, "getEncoder", () -> Base64.getEncoder());
JavaTemplate.Builder encoderTemplate = useMimeCoder ?
Semantics.expression(this, "getMimeEncoder", () -> Base64.getMimeEncoder()) :
Semantics.expression(this, "getEncoder", () -> Base64.getEncoder());
return encoderTemplate
.build()
.apply(updateCursor(c), c.getCoordinates().replace());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ private boolean isParentTypeDownCast(MethodCall immutableMethod) {

private boolean isParentTypeMatched(@Nullable JavaType type) {
JavaType.FullyQualified fq = TypeUtils.asFullyQualified(type);
return TypeUtils.isOfClassType(fq, javaType)
|| TypeUtils.isOfClassType(fq, "java.lang.Object");
return TypeUtils.isOfClassType(fq, javaType) ||
TypeUtils.isOfClassType(fq, "java.lang.Object");
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu

private boolean isIOExceptionOrException(JavaType.@Nullable FullyQualified fqCatch) {
return fqCatch != null &&
("java.io.IOException".matches(fqCatch.getFullyQualifiedName())
|| "java.lang.Exception".matches(fqCatch.getFullyQualifiedName()));
("java.io.IOException".matches(fqCatch.getFullyQualifiedName()) ||
"java.lang.Exception".matches(fqCatch.getFullyQualifiedName()));
}

private J.MethodInvocation toFilesCreateTempDir(J.MethodInvocation methodInvocation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
if (METHOD_MATCHER.matches(mi)) {
Expression select = mi.getSelect();
JavaType type = select != null ? select.getType() : getCursor().firstEnclosingOrThrow(J.ClassDeclaration.class).getType();
if (TypeUtils.isAssignableTo(JAVA_IO_FILE_INPUT_STREAM, type)
|| TypeUtils.isAssignableTo(JAVA_IO_FILE_OUTPUT_STREAM, type)) {
if (TypeUtils.isAssignableTo(JAVA_IO_FILE_INPUT_STREAM, type) ||
TypeUtils.isAssignableTo(JAVA_IO_FILE_OUTPUT_STREAM, type)) {
return mi.withName(mi.getName().withSimpleName("close"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
// Exit if var does not have @ElementCollection or has @Transient
if (FindAnnotations.find(multiVariable, "@javax.persistence.ElementCollection").isEmpty()
|| !FindAnnotations.find(multiVariable, "@javax.persistence.Transient").isEmpty()) {
if (FindAnnotations.find(multiVariable, "@javax.persistence.ElementCollection").isEmpty() ||
!FindAnnotations.find(multiVariable, "@javax.persistence.Transient").isEmpty()) {
return multiVariable;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
// Exit if class not annotated with either @Entity or @MappedSuperclass
if (FindAnnotations.find(classDecl, "javax.persistence.Entity").isEmpty()
&& FindAnnotations.find(classDecl, "javax.persistence.MappedSuperclass").isEmpty()) {
if (FindAnnotations.find(classDecl, "javax.persistence.Entity").isEmpty() &&
FindAnnotations.find(classDecl, "javax.persistence.MappedSuperclass").isEmpty()) {
return classDecl;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionCon

GradleProject gp = maybeGp.get();
GradleDependencyConfiguration rc = gp.getConfiguration("runtimeClasspath");
if (rc == null || rc.findResolvedDependency(JAKARTA_API_GROUP, JAKARTA_API_ARTIFACT) == null
|| rc.findResolvedDependency(JACKSON_GROUP, JACKSON_JAXB_ARTIFACT) != null) {
if (rc == null || rc.findResolvedDependency(JAKARTA_API_GROUP, JAKARTA_API_ARTIFACT) == null ||
rc.findResolvedDependency(JACKSON_GROUP, JACKSON_JAXB_ARTIFACT) != null) {
return g;
}

Expand Down Expand Up @@ -189,8 +189,8 @@ private Xml.Document maybeAddRuntimeDependency(Xml.Document d, ExecutionContext
return d;
}
MavenResolutionResult mavenModel = getResolutionResult();
if (!mavenModel.findDependencies(JACKSON_GROUP, JACKSON_JAXB_ARTIFACT, Scope.Runtime).isEmpty()
|| mavenModel.findDependencies(JAKARTA_API_GROUP, JAKARTA_API_ARTIFACT, Scope.Runtime).isEmpty()) {
if (!mavenModel.findDependencies(JACKSON_GROUP, JACKSON_JAXB_ARTIFACT, Scope.Runtime).isEmpty() ||
mavenModel.findDependencies(JAKARTA_API_GROUP, JAKARTA_API_ARTIFACT, Scope.Runtime).isEmpty()) {
return d;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration md, Execut
}

private boolean isPrivateAccessorMethodWithoutTransientAnnotation(J.MethodDeclaration method) {
return method.hasModifier(J.Modifier.Type.Private)
&& method.getParameters().get(0) instanceof J.Empty
&& method.getReturnTypeExpression().getType() != JavaType.Primitive.Void
&& FindAnnotations.find(method, "javax.persistence.Transient").isEmpty()
&& methodReturnsFieldFromClass(method);
return method.hasModifier(J.Modifier.Type.Private) &&
method.getParameters().get(0) instanceof J.Empty &&
method.getReturnTypeExpression().getType() != JavaType.Primitive.Void &&
FindAnnotations.find(method, "javax.persistence.Transient").isEmpty() &&
methodReturnsFieldFromClass(method);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public AnnotateTypesVisitor(String annotationToBeAdded) {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Set<String> injectedTypes) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, injectedTypes);
if (injectedTypes.contains(TypeUtils.asFullyQualified(cd.getType()).getFullyQualifiedName())
&& cd.getLeadingAnnotations().stream().noneMatch(annotationMatcher::matches)) {
if (injectedTypes.contains(TypeUtils.asFullyQualified(cd.getType()).getFullyQualifiedName()) &&
cd.getLeadingAnnotations().stream().noneMatch(annotationMatcher::matches)) {
maybeAddImport(annotationToBeAdded);
return template.apply(getCursor(), cd.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ public void addClass(JavaType type) {

public boolean isEmbeddableClass(@Nullable JavaType type) {
return definedEmbeddableClasses.stream()
.anyMatch(emb -> type.equals(emb)
|| type.isAssignableFrom(Pattern.compile(((JavaType.Class) emb).getFullyQualifiedName())));
.anyMatch(emb -> type.equals(emb) ||
type.isAssignableFrom(Pattern.compile(((JavaType.Class) emb).getFullyQualifiedName())));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
// Exit if not annotated with @Column and a relationship mapping annotation
if (FindAnnotations.find(multiVariable, COLUMN).isEmpty()
|| (FindAnnotations.find(multiVariable, "javax.persistence.OneToOne").isEmpty()
&& FindAnnotations.find(multiVariable, "javax.persistence.ManyToOne").isEmpty())) {
if (FindAnnotations.find(multiVariable, COLUMN).isEmpty() ||
(FindAnnotations.find(multiVariable, "javax.persistence.OneToOne").isEmpty() &&
FindAnnotations.find(multiVariable, "javax.persistence.ManyToOne").isEmpty())) {
return multiVariable;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
J.Try tri = getCursor().firstEnclosing(J.Try.class);
J.Try.Catch catch_ = getCursor().firstEnclosing(J.Try.Catch.class);
J.MethodDeclaration md = getCursor().firstEnclosing(J.MethodDeclaration.class);
if ((catch_ == null && tri != null && tri.getCatches().stream().anyMatch(c -> isExceptionType(c.getParameter().getType())))
|| (md != null && md.getThrows() != null && md.getThrows().stream().anyMatch(nt -> isExceptionType(nt.getType())))) {
if ((catch_ == null && tri != null && tri.getCatches().stream().anyMatch(c -> isExceptionType(c.getParameter().getType()))) ||
(md != null && md.getThrows() != null && md.getThrows().stream().anyMatch(nt -> isExceptionType(nt.getType())))) {
mi = (J.MethodInvocation) TO_DECLARED_CONS_NEW_INSTANCE.getVisitor().visitNonNull(mi, ctx);
}
}
return mi;
}

private boolean isExceptionType(@Nullable JavaType type) {
return TypeUtils.isOfType(type, exType)
|| TypeUtils.isOfType(type, thType);
return TypeUtils.isOfType(type, exType) ||
TypeUtils.isOfType(type, thType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ public J visitMethodInvocation(J.MethodInvocation methodInvocation, ExecutionCon
}

private static boolean wrapperNotNeeded(Expression expression) {
return expression instanceof J.Identifier
|| expression instanceof J.Literal
|| expression instanceof J.MethodInvocation
|| expression instanceof J.FieldAccess;
return expression instanceof J.Identifier ||
expression instanceof J.Literal ||
expression instanceof J.MethodInvocation ||
expression instanceof J.FieldAccess;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ private boolean isEndsWithSpecialCharacters(String content) {
}

private static boolean allLiterals(Expression exp) {
return isRegularStringLiteral(exp) || exp instanceof J.Binary
&& ((J.Binary) exp).getOperator() == J.Binary.Type.Addition
&& allLiterals(((J.Binary) exp).getLeft()) && allLiterals(((J.Binary) exp).getRight());
return isRegularStringLiteral(exp) || exp instanceof J.Binary &&
((J.Binary) exp).getOperator() == J.Binary.Type.Addition &&
allLiterals(((J.Binary) exp).getLeft()) && allLiterals(((J.Binary) exp).getRight());
}

private static boolean flatAdditiveStringLiterals(Expression expression,
Expand All @@ -203,8 +203,8 @@ private static boolean flatAdditiveStringLiterals(Expression expression,
}
concatenationSb.append(b.getPrefix().getWhitespace()).append("-");
concatenationSb.append(b.getPadding().getOperator().getBefore().getWhitespace()).append("-");
return flatAdditiveStringLiterals(b.getLeft(), stringLiterals, contentSb, concatenationSb)
&& flatAdditiveStringLiterals(b.getRight(), stringLiterals, contentSb, concatenationSb);
return flatAdditiveStringLiterals(b.getLeft(), stringLiterals, contentSb, concatenationSb) &&
flatAdditiveStringLiterals(b.getRight(), stringLiterals, contentSb, concatenationSb);
} else if (isRegularStringLiteral(expression)) {
J.Literal l = (J.Literal) expression;
stringLiterals.add(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public static boolean useGenerics(J.VariableDeclarations vd) {
}
initializer = initializer.unwrap();

return initializer instanceof J.NewClass
&& ((J.NewClass) initializer).getClazz() instanceof J.ParameterizedType;
return initializer instanceof J.NewClass &&
((J.NewClass) initializer).getClazz() instanceof J.ParameterizedType;
}

/**
Expand Down
Loading
Loading