Skip to content

Commit

Permalink
Merge branch 'main' into serializable-uid-context-free
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Aug 16, 2023
2 parents 4e70935 + b58a9f9 commit 164afc7
Show file tree
Hide file tree
Showing 57 changed files with 4,013 additions and 292 deletions.
20 changes: 0 additions & 20 deletions .github/workflows/dependency-check.yml

This file was deleted.

3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ dependencies {

implementation(platform("org.openrewrite:rewrite-bom:${rewriteVersion}"))
implementation("org.openrewrite:rewrite-java")
implementation("org.openrewrite:rewrite-groovy:${rewriteVersion}")
implementation("org.openrewrite:rewrite-kotlin:${rewriteVersion}")
implementation("org.openrewrite.meta:rewrite-analysis:${rewriteVersion}")
implementation("org.apache.commons:commons-text:latest.release")

testImplementation("org.openrewrite:rewrite-groovy")
testImplementation("org.junit-pioneer:junit-pioneer:2.0.0")
testImplementation("org.junit-pioneer:junit-pioneer:2.0.1")
testImplementation("junit:junit:4.13.2")

testRuntimeOnly("org.openrewrite:rewrite-java-17")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ private J.VariableDeclarations maybeFixVariableDeclarations(J.VariableDeclaratio
|| !J.Modifier.hasModifier(modifiers, J.Modifier.Type.Static)
|| !J.Modifier.hasModifier(modifiers, J.Modifier.Type.Final)) {
varDecls = varDecls.withModifiers(Arrays.asList(
new J.Modifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, J.Modifier.Type.Private, Collections.emptyList()),
new J.Modifier(Tree.randomId(), Space.SINGLE_SPACE, Markers.EMPTY, J.Modifier.Type.Static, Collections.emptyList()),
new J.Modifier(Tree.randomId(), Space.SINGLE_SPACE, Markers.EMPTY, J.Modifier.Type.Final, Collections.emptyList())
new J.Modifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, null, J.Modifier.Type.Private, Collections.emptyList()),
new J.Modifier(Tree.randomId(), Space.SINGLE_SPACE, Markers.EMPTY, null, J.Modifier.Type.Static, Collections.emptyList()),
new J.Modifier(Tree.randomId(), Space.SINGLE_SPACE, Markers.EMPTY, null, J.Modifier.Type.Final, Collections.emptyList())
));
}
if (TypeUtils.asPrimitive(varDecls.getType()) != JavaType.Primitive.Long) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, executionContext);
if (enumEquals.matches(m)) {
if (enumEquals.matches(m) && m.getSelect() != null) {
Cursor parent = getCursor().dropParentUntil(is -> is instanceof J.Unary || is instanceof J.Block);
boolean isNot = parent.getValue() instanceof J.Unary && ((J.Unary) parent.getValue()).getOperator() == J.Unary.Type.Not;
if (isNot) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.List;
import java.util.Set;

import static java.util.Collections.emptyList;

public class ExplicitLambdaArgumentTypes extends Recipe {
@Override
public String getDisplayName() {
Expand Down Expand Up @@ -138,6 +140,7 @@ private TypeTree buildTypeTree(@Nullable JavaType type, Space space) {
J.Identifier identifier = new J.Identifier(Tree.randomId(),
space,
Markers.EMPTY,
emptyList(),
fq.getClassName(),
type,
null
Expand Down Expand Up @@ -174,6 +177,7 @@ private TypeTree buildTypeTree(@Nullable JavaType type, Space space) {
return new J.Identifier(Tree.randomId(),
space,
Markers.EMPTY,
emptyList(),
genericType.getName(),
type,
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;

import static java.util.Collections.emptyList;
import static org.openrewrite.Tree.randomId;
Expand Down Expand Up @@ -112,7 +109,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
if (cd.getType() != null && typesToFinalize.remove(cd.getType().getFullyQualifiedName())) {
List<J.Modifier> modifiers = new ArrayList<>(cd.getModifiers());
modifiers.add(new J.Modifier(randomId(), Space.EMPTY, Markers.EMPTY, J.Modifier.Type.Final, emptyList()));
modifiers.add(new J.Modifier(randomId(), Space.EMPTY, Markers.EMPTY, null, J.Modifier.Type.Final, emptyList()));
modifiers = sortModifiers(modifiers);
cd = cd.withModifiers(modifiers);
if (cd.getType() instanceof JavaType.Class && !cd.getType().hasFlags(Flag.Final)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
})) {
mv = autoFormat(
mv.withModifiers(
ListUtils.concat(mv.getModifiers(), new J.Modifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, J.Modifier.Type.Final, Collections.emptyList()))
ListUtils.concat(mv.getModifiers(), new J.Modifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, null, J.Modifier.Type.Final, Collections.emptyList()))
), p);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ private static VariableDeclarations updateModifiers(final VariableDeclarations v
Modifier finalModifier = new Modifier(Tree.randomId(),
Space.EMPTY,
Markers.EMPTY,
null,
Type.Final,
emptyList());
if (leadingAnnotations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
return type != null ? v.withVariableType(type.withFlags(
Flag.bitMapToFlags(type.getFlagsBitMap() | Flag.Final.getBitMask()))) : null;
})).withModifiers(ListUtils.concat(mv.getModifiers(),
new J.Modifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, J.Modifier.Type.Final,
emptyList()))), ctx);
new J.Modifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, null,
J.Modifier.Type.Final, emptyList()))), ctx);
}

return mv;
Expand All @@ -121,7 +121,9 @@ private static List<J.VariableDeclarations.NamedVariable> collectPrivateFields(J
.stream()
.filter(statement -> statement instanceof J.VariableDeclarations)
.map(J.VariableDeclarations.class::cast)
.filter(mv -> mv.hasModifier(J.Modifier.Type.Private) && !mv.hasModifier(J.Modifier.Type.Final))
.filter(mv -> mv.hasModifier(J.Modifier.Type.Private)
&& !mv.hasModifier(J.Modifier.Type.Final)
&& !mv.hasModifier(J.Modifier.Type.Volatile))
.filter(mv -> !anyAnnotationApplied(mv))
.map(J.VariableDeclarations::getVariables)
.flatMap(Collection::stream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ public J.InstanceOf processInstanceOf(J.InstanceOf instanceOf, Cursor cursor) {
randomId(),
Space.build(" ", emptyList()),
Markers.EMPTY,
emptyList(),
name,
type,
null));
Expand Down Expand Up @@ -251,6 +252,7 @@ public J processTypeCast(J.TypeCast typeCast, Cursor cursor) {
randomId(),
typeCast.getPrefix(),
Markers.EMPTY,
emptyList(),
name,
typeCast.getType(),
fieldType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Collections;
import java.util.Scanner;

import static java.util.Collections.emptyList;
import static org.openrewrite.Tree.randomId;

final class JavaElementFactory {
Expand Down Expand Up @@ -51,11 +52,12 @@ static Expression className(JavaType type, boolean qualified) {
Space.EMPTY,
Markers.EMPTY,
name,
new JLeftPadded<>(Space.EMPTY, new J.Identifier(randomId(), Space.EMPTY, Markers.EMPTY, part, typeOfContaining, null), Markers.EMPTY),
new JLeftPadded<>(Space.EMPTY, new J.Identifier(randomId(), Space.EMPTY, Markers.EMPTY,
emptyList(), part, typeOfContaining, null), Markers.EMPTY),
typeOfContaining
);
} else {
name = new J.Identifier(randomId(), Space.EMPTY, Markers.EMPTY, part, type, null);
name = new J.Identifier(randomId(), Space.EMPTY, Markers.EMPTY, emptyList(), part, type, null);
}
}
assert name != null;
Expand All @@ -69,7 +71,7 @@ static J.MemberReference newInstanceMethodReference(JavaType.Method method, Expr
Markers.EMPTY,
new JRightPadded<>(containing, Space.EMPTY, Markers.EMPTY),
null,
new JLeftPadded<>(Space.EMPTY, new J.Identifier(randomId(), Space.EMPTY, Markers.EMPTY, method.getName(), null, null), Markers.EMPTY),
new JLeftPadded<>(Space.EMPTY, new J.Identifier(randomId(), Space.EMPTY, Markers.EMPTY, emptyList(), method.getName(), null, null), Markers.EMPTY),
type,
method,
null
Expand All @@ -91,7 +93,7 @@ static J.FieldAccess newClassLiteral(@Nullable JavaType type, boolean qualified)
className(type, qualified),
new JLeftPadded<>(
Space.EMPTY,
new J.Identifier(randomId(), Space.EMPTY, Markers.EMPTY, "class", parameterized, null),
new J.Identifier(randomId(), Space.EMPTY, Markers.EMPTY, emptyList(), "class", parameterized, null),
Markers.EMPTY
),
parameterized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.RecipeRunException;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.ShortenFullyQualifiedTypeReferences;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Marker;

Expand Down Expand Up @@ -61,7 +63,7 @@ public Duration getEstimatedEffortPerOccurrence() {
}

@Override
public JavaVisitor<ExecutionContext> getVisitor() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaVisitor<ExecutionContext>() {
final JavaTemplate ifElseIfPrimitive = JavaTemplate.builder("" +
"if(#{any()} == #{any()}) {\n" +
Expand Down Expand Up @@ -183,6 +185,7 @@ public J visitSwitch(J.Switch switch_, ExecutionContext ctx) {
} else {
generatedIf = ifElseIfEnum.apply(getCursor(), switch_.getCoordinates().replace(), tree, enumIdentToFieldAccessString(cases[0].getPattern()), tree, enumIdentToFieldAccessString(cases[1].getPattern()));
}
doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());
} else {
if (cases[1] == null) {
if (isDefault(cases[0])) {
Expand Down Expand Up @@ -249,7 +252,7 @@ private boolean switchesOnEnum(J.Switch switch_) {
}

private String enumIdentToFieldAccessString(Expression casePattern) {
String caseType = requireNonNull(TypeUtils.asFullyQualified(casePattern.getType())).getClassName();
String caseType = requireNonNull(TypeUtils.asFullyQualified(casePattern.getType())).getFullyQualifiedName();
if (casePattern instanceof J.FieldAccess) {
// may be a field access in Groovy
return caseType + "." + ((J.FieldAccess) casePattern).getSimpleName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,19 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
}

public static List<J.Modifier> sortModifiers(List<J.Modifier> modifiers) {
for (J.Modifier mod : modifiers) {
if (mod.getType() == J.Modifier.Type.LanguageExtension) {
// avoid harmful changes with modifiers not seen in Java
return modifiers;
}
}

List<J.Modifier.Type> sortedTypes = modifiers.stream()
.map(J.Modifier::getType)
.sorted(Comparator.comparingInt(J.Modifier.Type::ordinal))
.collect(toList());


return ListUtils.map(modifiers, (i, mod) -> mod.getType() == sortedTypes.get(i) ? mod : mod.withType(sortedTypes.get(i)));
}
}
3 changes: 1 addition & 2 deletions src/main/java/org/openrewrite/staticanalysis/NeedBraces.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,5 @@ public J.ForLoop visitForLoop(J.ForLoop forLoop, ExecutionContext ctx) {
}
return elem;
}

}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import java.time.Duration;
import java.util.*;

import static java.util.Collections.emptyList;

public class NoPrimitiveWrappersForToStringOrCompareTo extends Recipe {
private static final MethodMatcher NUMBER_TO_STRING_MATCHER = new MethodMatcher("java.lang.Number toString()", true);
private static final MethodMatcher BOOLEAN_TO_STRING_MATCHER = new MethodMatcher("java.lang.Boolean toString()", true);
Expand Down Expand Up @@ -95,7 +97,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}
if (arg != null && !TypeUtils.isString(arg.getType()) && mi.getSelect() != null) {
JavaType.FullyQualified fq = mi.getMethodType().getDeclaringType();
mi = mi.withSelect(new J.Identifier(UUID.randomUUID(), mi.getSelect().getPrefix(), Markers.EMPTY, fq.getClassName(), fq, null));
mi = mi.withSelect(new J.Identifier(UUID.randomUUID(), mi.getSelect().getPrefix(), Markers.EMPTY, emptyList(), fq.getClassName(), fq, null));
//noinspection ArraysAsListWithZeroOrOneArgument
mi = mi.withArguments(Arrays.asList(arg));
}
Expand All @@ -112,7 +114,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu

if (arg != null && !TypeUtils.isString(arg.getType()) && mi.getSelect() != null) {
JavaType.FullyQualified fq = mi.getMethodType().getDeclaringType();
mi = mi.withSelect(new J.Identifier(UUID.randomUUID(), mi.getSelect().getPrefix(), Markers.EMPTY, fq.getClassName(), fq, null));
mi = mi.withSelect(new J.Identifier(UUID.randomUUID(), mi.getSelect().getPrefix(), Markers.EMPTY, emptyList(), fq.getClassName(), fq, null));
mi = mi.withArguments(ListUtils.concat(arg, mi.getArguments()));
mi = maybeAutoFormat(mi, mi.withName(mi.getName().withSimpleName("compare")), executionContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Optional;
import java.util.Set;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;

public class ReferentialEqualityToObjectEquals extends Recipe {
Expand Down Expand Up @@ -70,7 +71,7 @@ private static J.MethodInvocation asEqualsMethodInvocation(J.Binary binary, @Nul
Markers.EMPTY,
new JRightPadded<>(binary.getLeft().withPrefix(Space.EMPTY), Space.EMPTY, Markers.EMPTY),
null,
new J.Identifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, "equals", JavaType.Primitive.Boolean, null),
new J.Identifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, emptyList(), "equals", JavaType.Primitive.Boolean, null),
JContainer.build(singletonList(new JRightPadded<>(binary.getRight().withPrefix(Space.EMPTY), Space.EMPTY, Markers.EMPTY))),
new JavaType.Method(
null,
Expand Down
Loading

0 comments on commit 164afc7

Please sign in to comment.