Skip to content

Commit

Permalink
refactor: Operator wrapping on end of line
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek and TeamModerne committed Sep 21, 2024
1 parent 1185126 commit 9ff13ed
Show file tree
Hide file tree
Showing 27 changed files with 135 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex

private J.VariableDeclarations maybeFixVariableDeclarations(J.VariableDeclarations varDecls) {
List<J.Modifier> modifiers = varDecls.getModifiers();
if (!J.Modifier.hasModifier(modifiers, J.Modifier.Type.Private)
|| !J.Modifier.hasModifier(modifiers, J.Modifier.Type.Static)
|| !J.Modifier.hasModifier(modifiers, J.Modifier.Type.Final)) {
if (!J.Modifier.hasModifier(modifiers, J.Modifier.Type.Private) ||
!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, null, J.Modifier.Type.Private, Collections.emptyList()),
new J.Modifier(Tree.randomId(), Space.SINGLE_SPACE, Markers.EMPTY, null, J.Modifier.Type.Static, Collections.emptyList()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (mi.getSelect() != null && isAtomicEqualsType(mi.getSelect().getType()) && aiMethodMatcher.matches(mi)
&& TypeUtils.isOfType(mi.getSelect().getType(), mi.getArguments().get(0).getType())) {
if (mi.getSelect() != null && isAtomicEqualsType(mi.getSelect().getType()) && aiMethodMatcher.matches(mi) &&
TypeUtils.isOfType(mi.getSelect().getType(), mi.getArguments().get(0).getType())) {
JavaType.FullyQualified fqt = TypeUtils.asFullyQualified(mi.getSelect().getType());
if (fqt != null) {
String templateString = "#{any(" + fqt.getFullyQualifiedName() + ")}.get() == #{any(" + fqt.getFullyQualifiedName() + ")}.get()";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
boolean appendToString = false;
for (Expression exp : flattenExpressions) {
if (appendToString) {
if (exp instanceof J.Literal
&& (((J.Literal) exp).getType() == JavaType.Primitive.String)
if (exp instanceof J.Literal &&
(((J.Literal) exp).getType() == JavaType.Primitive.String)
) {
group.add(exp);
} else {
addToGroups(group, groups);
groups.add(exp);
}
} else {
if (exp instanceof J.Literal
&& (((J.Literal) exp).getType() == JavaType.Primitive.String)) {
if (exp instanceof J.Literal &&
(((J.Literal) exp).getType() == JavaType.Primitive.String)) {
addToGroups(group, groups);
appendToString = true;
} else if ((exp instanceof J.Identifier || exp instanceof J.MethodInvocation) && exp.getType() != null) {
Expand Down Expand Up @@ -182,8 +182,8 @@ public static boolean flatAdditiveExpressions(Expression expression, List<Expres
return false;
}

return flatAdditiveExpressions(b.getLeft(), expressionList)
&& flatAdditiveExpressions(b.getRight(), expressionList);
return flatAdditiveExpressions(b.getLeft(), expressionList) &&
flatAdditiveExpressions(b.getRight(), expressionList);
} else if (expression instanceof J.Literal ||
expression instanceof J.Identifier ||
expression instanceof J.MethodInvocation ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public J.Try visitTry(J.Try tryable, P p) {
isEmptyBlock(t.getBody()) &&
isEmptyResources(t.getResources())) {
doAfterVisit(new DeleteStatement<>(tryable));
} else if (Boolean.TRUE.equals(emptyBlockStyle.getLiteralFinally()) && t.getFinally() != null
&& !t.getCatches().isEmpty() && isEmptyBlock(t.getFinally())) {
} else if (Boolean.TRUE.equals(emptyBlockStyle.getLiteralFinally()) && t.getFinally() != null &&
!t.getCatches().isEmpty() && isEmptyBlock(t.getFinally())) {
t = t.withFinally(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
if (service(AnnotationService.class).matches(variableDeclsCursor, LOMBOK_BUILDER_DEFAULT)) {
return v;
}
J.Literal literalInit = variable.getInitializer() instanceof J.Literal
? (J.Literal) variable.getInitializer()
: null;
J.Literal literalInit = variable.getInitializer() instanceof J.Literal ?
(J.Literal) variable.getInitializer() :
null;
if (literalInit != null && !variableDecls.hasModifier(J.Modifier.Type.Final)) {
if (TypeUtils.asFullyQualified(variable.getType()) != null &&
JavaType.Primitive.Null.equals(literalInit.getType())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
Statement statement = statements.get(i);
if (statement instanceof J.VariableDeclarations) {
J.VariableDeclarations varDecls = (J.VariableDeclarations) statement;
if (J.Modifier.hasModifier(varDecls.getModifiers(), J.Modifier.Type.Final)
&& varDecls.getVariables().stream().anyMatch(v -> v.getInitializer() == null)) {
if (J.Modifier.hasModifier(varDecls.getModifiers(), J.Modifier.Type.Final) &&
varDecls.getVariables().stream().anyMatch(v -> v.getInitializer() == null)) {
hasFinalUninitializedFieldVar = true;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public class FinalClassVisitor extends JavaIsoVisitor<ExecutionContext> {
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclaration, ExecutionContext ctx) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDeclaration, ctx);

if (cd.getKind() != J.ClassDeclaration.Kind.Type.Class || cd.hasModifier(J.Modifier.Type.Abstract)
|| cd.hasModifier(J.Modifier.Type.Final) || cd.getType() == null) {
if (cd.getKind() != J.ClassDeclaration.Kind.Type.Class || cd.hasModifier(J.Modifier.Type.Abstract) ||
cd.hasModifier(J.Modifier.Type.Final) || cd.getType() == null) {
return cd;
}

Expand Down Expand Up @@ -90,8 +90,8 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclarat
}

private void excludeSupertypes(JavaType.FullyQualified type) {
if (type.getSupertype() != null && type.getOwningClass() != null
&& typesToNotFinalize.add(type.getSupertype().getFullyQualifiedName())) {
if (type.getSupertype() != null && type.getOwningClass() != null &&
typesToNotFinalize.add(type.getSupertype().getFullyQualifiedName())) {
excludeSupertypes(type.getSupertype());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ private boolean hasFinalModifiers(final List<Statement> parameters) {
return parameters.stream().allMatch(p -> {
if (p instanceof J.VariableDeclarations) {
final List<J.Modifier> modifiers = ((J.VariableDeclarations) p).getModifiers();
return !modifiers.isEmpty()
&& modifiers.stream()
return !modifiers.isEmpty() &&
modifiers.stream()
.allMatch(m -> m.getType().equals(J.Modifier.Type.Final));
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
}

private boolean anyAnnotationApplied(Cursor variableCursor) {
return !service(AnnotationService.class).getAllAnnotations(variableCursor).isEmpty()
|| variableCursor.<J.VariableDeclarations>getValue().getTypeExpression() instanceof J.AnnotatedType;
return !service(AnnotationService.class).getAllAnnotations(variableCursor).isEmpty() ||
variableCursor.<J.VariableDeclarations>getValue().getTypeExpression() instanceof J.AnnotatedType;
}

/**
Expand All @@ -134,10 +134,10 @@ private List<J.VariableDeclarations.NamedVariable> collectPrivateFields(Cursor c
.stream()
.filter(J.VariableDeclarations.class::isInstance)
.map(J.VariableDeclarations.class::cast)
.filter(mv -> mv.hasModifier(J.Modifier.Type.Private)
&& !mv.hasModifier(J.Modifier.Type.Final)
&& (!(topLevel instanceof Cs) || mv.getModifiers().stream().noneMatch(m -> "readonly".equals(m.getKeyword()) || "const".equals(m.getKeyword())))
&& !mv.hasModifier(J.Modifier.Type.Volatile))
.filter(mv -> mv.hasModifier(J.Modifier.Type.Private) &&
!mv.hasModifier(J.Modifier.Type.Final) &&
(!(topLevel instanceof Cs) || mv.getModifiers().stream().noneMatch(m -> "readonly".equals(m.getKeyword()) || "const".equals(m.getKeyword()))) &&
!mv.hasModifier(J.Modifier.Type.Volatile))
.filter(mv -> !anyAnnotationApplied(new Cursor(bodyCursor, mv)))
.map(J.VariableDeclarations::getVariables)
.flatMap(Collection::stream)
Expand Down Expand Up @@ -249,9 +249,9 @@ private static boolean isInLoop(Cursor cursor) {
* @return true if the cursor is in a constructor or an initializer block (both static or non-static)
*/
private static boolean isInitializedByClass(Cursor cursor, boolean privateFieldIsStatic) {
Object parent = cursor.dropParentWhile(p -> (p instanceof J.Block && !((J.Block) p).isStatic())
|| p instanceof JRightPadded
|| p instanceof JLeftPadded)
Object parent = cursor.dropParentWhile(p -> (p instanceof J.Block && !((J.Block) p).isStatic()) ||
p instanceof JRightPadded ||
p instanceof JLeftPadded)
.getValue();
if (parent instanceof J.Block) {
return privateFieldIsStatic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, P
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, P p) {
J.MethodDeclaration md = super.visitMethodDeclaration(method, p);
if (md.getMethodType() == null || !md.isConstructor()
|| (md.hasModifier(J.Modifier.Type.Private) || md.hasModifier(J.Modifier.Type.Protected) || md.getMethodType().getDeclaringType().getKind().equals(JavaType.Class.Kind.Enum))) {
if (md.getMethodType() == null || !md.isConstructor() ||
(md.hasModifier(J.Modifier.Type.Private) || md.hasModifier(J.Modifier.Type.Protected) || md.getMethodType().getDeclaringType().getKind().equals(JavaType.Class.Kind.Enum))) {
return md;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public String getDisplayName() {

@Override
public String getDescription() {
return "Adds pattern variables to `instanceof` expressions wherever the same (side effect free) expression is referenced in a corresponding type cast expression within the flow scope of the `instanceof`."
+ " Currently, this recipe supports `if` statements and ternary operator expressions.";
return "Adds pattern variables to `instanceof` expressions wherever the same (side effect free) expression is referenced in a corresponding type cast expression within the flow scope of the `instanceof`." +
" Currently, this recipe supports `if` statements and ternary operator expressions.";
}

@Override
Expand Down Expand Up @@ -167,8 +167,8 @@ public void registerInstanceOf(J.InstanceOf instanceOf, Set<J> contexts) {
JavaType type = ((TypedTree) instanceOf.getClazz()).getType();

Optional<ExpressionAndType> existing = instanceOfs.keySet().stream()
.filter(k -> TypeUtils.isAssignableTo(type, k.getType())
&& SemanticallyEqual.areEqual(k.getExpression(), expression))
.filter(k -> TypeUtils.isAssignableTo(type, k.getType()) &&
SemanticallyEqual.areEqual(k.getExpression(), expression))
.findAny();
if (!existing.isPresent()) {
instanceOfs.put(new ExpressionAndType(expression, type), instanceOf);
Expand All @@ -182,8 +182,8 @@ public void registerTypeCast(J.TypeCast typeCast, Cursor cursor) {
JavaType type = typeCast.getClazz().getTree().getType();

Optional<ExpressionAndType> match = instanceOfs.keySet().stream()
.filter(k -> TypeUtils.isAssignableTo(type, k.getType())
&& SemanticallyEqual.areEqual(k.getExpression(), expression))
.filter(k -> TypeUtils.isAssignableTo(type, k.getType()) &&
SemanticallyEqual.areEqual(k.getExpression(), expression))
.findAny();
if (match.isPresent()) {
Cursor parent = cursor.getParentTreeCursor();
Expand All @@ -192,8 +192,8 @@ public void registerTypeCast(J.TypeCast typeCast, Cursor cursor) {
for (Iterator<?> it = cursor.getPath(); it.hasNext(); ) {
Object next = it.next();
if (validContexts.contains(next)) {
if (parent.getValue() instanceof J.VariableDeclarations.NamedVariable
&& !variablesToDelete.containsKey(instanceOf)) {
if (parent.getValue() instanceof J.VariableDeclarations.NamedVariable &&
!variablesToDelete.containsKey(instanceOf)) {
variablesToDelete.put(instanceOf, parent.getValue());
} else {
replacements.put(typeCast, instanceOf);
Expand Down Expand Up @@ -258,9 +258,9 @@ private String patternVariableName(J.InstanceOf instanceOf, Cursor cursor) {
VariableNameStrategy strategy;
if (root instanceof J.If) {
J.VariableDeclarations.NamedVariable variable = variablesToDelete.get(instanceOf);
strategy = variable != null
? VariableNameStrategy.exact(variable.getSimpleName())
: VariableNameStrategy.normal(contextScopes.get(instanceOf));
strategy = variable != null ?
VariableNameStrategy.exact(variable.getSimpleName()) :
VariableNameStrategy.normal(contextScopes.get(instanceOf));
} else {
strategy = VariableNameStrategy.short_();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J visitBinary(J.Binary binary, ExecutionContext ctx) {
if (isZero(binary.getLeft()) || isZero(binary.getRight())) {
boolean zeroRight = isZero(binary.getRight());
if (binary.getOperator() == J.Binary.Type.Equal || binary.getOperator() == J.Binary.Type.NotEqual
|| zeroRight && binary.getOperator() == J.Binary.Type.GreaterThan
|| !zeroRight && binary.getOperator() == J.Binary.Type.LessThan) {
if (binary.getOperator() == J.Binary.Type.Equal || binary.getOperator() == J.Binary.Type.NotEqual ||
zeroRight && binary.getOperator() == J.Binary.Type.GreaterThan ||
!zeroRight && binary.getOperator() == J.Binary.Type.LessThan) {
J maybeSizeCall = zeroRight ? binary.getLeft() : binary.getRight();
if (maybeSizeCall instanceof J.MethodInvocation) {
J.MethodInvocation maybeSizeCallMethod = (J.MethodInvocation) maybeSizeCall;
Expand All @@ -78,10 +78,10 @@ public J visitBinary(J.Binary binary, ExecutionContext ctx) {
}
} else if (isOne(binary.getLeft()) || isOne(binary.getRight())) {
boolean oneRight = isOne(binary.getRight());
if ((oneRight && binary.getOperator() == J.Binary.Type.LessThan)
|| (!oneRight && binary.getOperator() == J.Binary.Type.GreaterThan)
|| (oneRight && binary.getOperator() == J.Binary.Type.GreaterThanOrEqual)
|| (!oneRight && binary.getOperator() == J.Binary.Type.LessThanOrEqual)) {
if ((oneRight && binary.getOperator() == J.Binary.Type.LessThan) ||
(!oneRight && binary.getOperator() == J.Binary.Type.GreaterThan) ||
(oneRight && binary.getOperator() == J.Binary.Type.GreaterThanOrEqual) ||
(!oneRight && binary.getOperator() == J.Binary.Type.LessThanOrEqual)) {
J maybeSizeCall = oneRight ? binary.getLeft() : binary.getRight();
if (maybeSizeCall instanceof J.MethodInvocation) {
J.MethodInvocation maybeSizeCallMethod = (J.MethodInvocation) maybeSizeCall;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ private boolean isDefault(J.Case case_) {

private boolean switchesOnEnum(J.Switch switch_) {
JavaType selectorType = switch_.getSelector().getTree().getType();
return selectorType instanceof JavaType.Class
&& ((JavaType.Class) selectorType).getKind() == JavaType.Class.Kind.Enum;
return selectorType instanceof JavaType.Class &&
((JavaType.Class) selectorType).getKind() == JavaType.Class.Kind.Enum;
}

};
Expand Down
Loading

0 comments on commit 9ff13ed

Please sign in to comment.