Skip to content

Commit

Permalink
add skipping of generics types
Browse files Browse the repository at this point in the history
  • Loading branch information
MBoegers committed May 24, 2023
1 parent 208fc71 commit f1b73f8
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 211 deletions.
16 changes: 11 additions & 5 deletions src/main/java/org/openrewrite/java/migrate/lang/UseVarKeyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,17 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m

Expression initializer = vd.getVariables().get(0).getInitializer();
boolean isDeclarationOnly = isNull(initializer);
if (isDeclarationOnly) return vd;

initializer = initializer.unwrap();
boolean isNullAssigment = initializer instanceof J.Literal && isNull(((J.Literal) initializer).getValue());
boolean alreadyUseVar = typeExpression instanceof J.Identifier && "var".equals(((J.Identifier) typeExpression).getSimpleName());
boolean isGeneric = typeExpression instanceof J.ParameterizedType; // todo implement generics!
boolean isGenericDefinition = typeExpression instanceof J.ParameterizedType ;
boolean isGenericInitializer = initializer instanceof J.NewClass && ((J.NewClass) initializer).getClazz() instanceof J.ParameterizedType;
boolean useTernary = initializer instanceof J.Ternary;
if (alreadyUseVar || isDeclarationOnly || isNullAssigment || isGeneric || useTernary) return vd;
if (alreadyUseVar || isNullAssigment|| isGenericDefinition || isGenericInitializer|| useTernary) return vd;

J.VariableDeclarations result = transformToVar(vd);
return result;
return transformToVar(vd);
}

private boolean determineIfOutsideInitializer(Cursor cursor, boolean childWasBlock) {
Expand Down Expand Up @@ -149,8 +152,11 @@ private J.VariableDeclarations transformToVar(@NotNull J.VariableDeclarations vd

if (initializer instanceof J.Literal) {
initializer = expandWithPrimitivTypeHint(vd, initializer);
}
} else if(initializer instanceof J.MethodInvocation && nonNull(((J.MethodInvocation) initializer).getTypeParameters())) {
initializer = initializer;
} else if(initializer instanceof J.NewClass && ((J.NewClass) initializer).getClazz() instanceof J.ParameterizedType) {

}
return vd.withTemplate(template, vd.getCoordinates().replace(), simpleName, initializer);
}

Expand Down
Loading

0 comments on commit f1b73f8

Please sign in to comment.