Skip to content

Commit

Permalink
refactor: Fix missing braces
Browse files Browse the repository at this point in the history
  • Loading branch information
2 people authored and app committed Mar 12, 2024
1 parent 7622f66 commit 8f94063
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ private static boolean isSingleVariableDefinition(J.VariableDeclarations vd) {

boolean definesSingleVariable = vd.getVariables().size() == 1;
boolean isPureAssigment = JavaType.Primitive.Null.equals(vd.getType());
if (!definesSingleVariable || isPureAssigment) return false;
if (!definesSingleVariable || isPureAssigment) {
return false;
}

Expression initializer = vd.getVariables().get(0).getInitializer();
boolean isDeclarationOnly = initializer == null;
if (isDeclarationOnly) return false;
if (isDeclarationOnly) {
return false;
}

initializer = initializer.unwrap();
boolean isNullAssigment = initializer instanceof J.Literal && ((J.Literal) initializer).getValue() == null;
Expand Down Expand Up @@ -99,10 +103,14 @@ public static boolean declarationHasType(J.VariableDeclarations vd, JavaType typ
public static boolean useGenerics(J.VariableDeclarations vd) {
TypeTree typeExpression = vd.getTypeExpression();
boolean isGenericDefinition = typeExpression instanceof J.ParameterizedType;
if (isGenericDefinition) return true;
if (isGenericDefinition) {
return true;
}

Expression initializer = vd.getVariables().get(0).getInitializer();
if (initializer == null) return false;
if (initializer == null) {
return false;
}
initializer = initializer.unwrap();

return initializer instanceof J.NewClass
Expand Down Expand Up @@ -166,20 +174,27 @@ private static boolean isMethodParameter(J.VariableDeclarations vd, Cursor curso
* @return true iff the courser is inside an instance or static initializer block
*/
private static boolean isInsideInitializer(Cursor cursor, int nestedBlockLevel) {
if (Cursor.ROOT_VALUE.equals(cursor.getValue())) return false;
if (Cursor.ROOT_VALUE.equals( cursor.getValue() )) {
return false;
}

Object currentStatement = cursor.getValue();

// initializer blocks are blocks inside the class definition block, therefor a nesting of 2 is mandatory
boolean isClassDeclaration = currentStatement instanceof J.ClassDeclaration;
boolean followedByTwoBlock = nestedBlockLevel >= 2;
if (isClassDeclaration && followedByTwoBlock) return true;
if (isClassDeclaration && followedByTwoBlock) {
return true;
}

// count direct block nesting (block containing a block), but ignore paddings
boolean isBlock = currentStatement instanceof J.Block;
boolean isNoPadding = !(currentStatement instanceof JRightPadded);
if (isBlock) nestedBlockLevel += 1;
else if (isNoPadding) nestedBlockLevel = 0;
if (isBlock) {
nestedBlockLevel += 1;
} else if (isNoPadding) {
nestedBlockLevel = 0;
}

return isInsideInitializer(requireNonNull(cursor.getParent()), nestedBlockLevel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,36 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v
vd = super.visitVariableDeclarations(vd, ctx);

boolean isGeneralApplicable = DeclarationCheck.isVarApplicable(this.getCursor(), vd);
if (!isGeneralApplicable) return vd;
if (!isGeneralApplicable) {
return vd;
}

// recipe specific
boolean isPrimitive = DeclarationCheck.isPrimitive(vd);
boolean usesNoGenerics = !DeclarationCheck.useGenerics(vd);
boolean usesTernary = DeclarationCheck.initializedByTernary(vd);
if (isPrimitive || usesTernary || usesNoGenerics) return vd;
if (isPrimitive || usesTernary || usesNoGenerics) {
return vd;
}

//now we deal with generics, check for method invocations
Expression initializer = vd.getVariables().get(0).getInitializer();
boolean isMethodInvocation = initializer != null && initializer.unwrap() instanceof J.MethodInvocation;
if (!isMethodInvocation) return vd;
if (!isMethodInvocation) {
return vd;
}

//if no type paramters are present and no arguments we assume the type is hard to determine a needs manual action
boolean hasNoTypeParams = ((J.MethodInvocation) initializer).getTypeParameters() == null;
boolean argumentsEmpty = allArgumentsEmpty((J.MethodInvocation) initializer);
if (hasNoTypeParams && argumentsEmpty) return vd;
if (hasNoTypeParams && argumentsEmpty) {
return vd;
}

// mark imports for removal if unused
if (vd.getType() instanceof JavaType.FullyQualified) maybeRemoveImport((JavaType.FullyQualified) vd.getType());
if (vd.getType() instanceof JavaType.FullyQualified) {
maybeRemoveImport( (JavaType.FullyQualified) vd.getType() );
}

return transformToVar(vd, new ArrayList<>(), new ArrayList<>());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,50 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v
vd = super.visitVariableDeclarations(vd, ctx);

boolean isGeneralApplicable = DeclarationCheck.isVarApplicable(this.getCursor(), vd);
if (!isGeneralApplicable) return vd;
if (!isGeneralApplicable) {
return vd;
}

// recipe specific
boolean isPrimitive = DeclarationCheck.isPrimitive(vd);
boolean usesNoGenerics = !DeclarationCheck.useGenerics(vd);
boolean usesTernary = DeclarationCheck.initializedByTernary(vd);
if (isPrimitive || usesTernary || usesNoGenerics) return vd;
if (isPrimitive || usesTernary || usesNoGenerics) {
return vd;
}

//now we deal with generics
J.VariableDeclarations.NamedVariable variable = vd.getVariables().get(0);
List<JavaType> leftTypes = extractParameters(variable.getVariableType());
List<JavaType> rightTypes = extractParameters(variable.getInitializer());
if (rightTypes == null || (leftTypes.isEmpty() && rightTypes.isEmpty())) return vd;
if (rightTypes == null || (leftTypes.isEmpty() && rightTypes.isEmpty())) {
return vd;
}

// skip generics with type bounds, it's not yet implemented
for (JavaType type : leftTypes) {
if (hasBounds(type))
if (hasBounds( type )) {
return vd;
}
}
boolean genericHasBounds = anyTypeHasBounds(leftTypes);
if (genericHasBounds) return vd;
if (genericHasBounds) {
return vd;
}

// mark imports for removal if unused
if (vd.getType() instanceof JavaType.FullyQualified) maybeRemoveImport((JavaType.FullyQualified) vd.getType());
if (vd.getType() instanceof JavaType.FullyQualified) {
maybeRemoveImport( (JavaType.FullyQualified) vd.getType() );
}

return transformToVar(vd, leftTypes, rightTypes);
}

private static Boolean anyTypeHasBounds(List<JavaType> leftTypes) {
for (JavaType type : leftTypes) {
if (hasBounds(type))
if (hasBounds( type )) {
return true;
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,21 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v
vd = super.visitVariableDeclarations(vd, ctx);

boolean isGeneralApplicable = DeclarationCheck.isVarApplicable(getCursor(), vd);
if (!isGeneralApplicable) return vd;
if (!isGeneralApplicable) {
return vd;
}

boolean isPrimitive = DeclarationCheck.isPrimitive(vd);
boolean usesGenerics = DeclarationCheck.useGenerics(vd);
boolean usesTernary = DeclarationCheck.initializedByTernary(vd);
if (isPrimitive || usesGenerics || usesTernary) return vd;
if (isPrimitive || usesGenerics || usesTernary) {
return vd;
}

// mark imports for removal if unused
if (vd.getType() instanceof JavaType.FullyQualified) maybeRemoveImport((JavaType.FullyQualified) vd.getType());
if (vd.getType() instanceof JavaType.FullyQualified) {
maybeRemoveImport( (JavaType.FullyQualified) vd.getType() );
}

return transformToVar(vd);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v
vd = super.visitVariableDeclarations(vd, ctx);

boolean isGeneralApplicable = DeclarationCheck.isVarApplicable(this.getCursor(), vd);
if (!isGeneralApplicable) return vd;
if (!isGeneralApplicable) {
return vd;
}

// recipe specific
boolean isNoPrimitive = !DeclarationCheck.isPrimitive(vd);
boolean isByteVariable = DeclarationCheck.declarationHasType(vd, BYTE_TYPE);
boolean isShortVariable = DeclarationCheck.declarationHasType(vd, SHORT_TYPE);
if (isNoPrimitive || isByteVariable || isShortVariable) return vd;
if (isNoPrimitive || isByteVariable || isShortVariable) {
return vd;
}

// no need to remove imports, because primitives are never imported

Expand Down Expand Up @@ -109,7 +113,9 @@ private J.VariableDeclarations transformToVar(J.VariableDeclarations vd) {
private Expression expandWithPrimitivTypeHint(J.VariableDeclarations vd, Expression initializer) {
String valueSource = ((J.Literal) initializer).getValueSource();

if (valueSource == null) return initializer;
if (valueSource == null) {
return initializer;
}

boolean isLongLiteral = JavaType.Primitive.Long.equals(vd.getType());
boolean inferredAsLong = valueSource.endsWith("l") || valueSource.endsWith("L");
Expand Down

0 comments on commit 8f94063

Please sign in to comment.