Skip to content

Commit

Permalink
Fix some uses of J.AnnotatedType
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Dec 14, 2023
1 parent 9d2d86e commit 41f907d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
}

private static boolean anyAnnotationApplied(J.VariableDeclarations mv) {
return !mv.getLeadingAnnotations().isEmpty()
|| mv.getTypeExpression() instanceof J.AnnotatedType;
return !mv.getAllAnnotations().isEmpty();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
J.VariableDeclarations varDecls = super.visitVariableDeclarations(multiVariable, ctx);
final TypedTree varDeclsTypeExpression = varDecls.getTypeExpression();
if (varDecls.getVariables().size() == 1 && varDecls.getVariables().get(0).getInitializer() != null
&& varDecls.getTypeExpression() instanceof J.ParameterizedType) {
if (varDeclsTypeExpression != null &&
varDecls.getVariables().size() == 1 &&
varDecls.getVariables().get(0).getInitializer() != null &&
varDecls.getTypeExpression() instanceof J.ParameterizedType) {
varDecls = varDecls.withVariables(ListUtils.map(varDecls.getVariables(), nv -> {
if (nv.getInitializer() instanceof J.NewClass) {
nv = nv.withInitializer(maybeRemoveParams(parameterizedTypes((J.ParameterizedType) varDeclsTypeExpression), (J.NewClass) nv.getInitializer()));
Expand Down Expand Up @@ -213,8 +215,7 @@ private J.NewClass maybeRemoveParams(@Nullable List<JavaType> paramTypes, J.NewC
if (paramTypes != null && (java9 || newClass.getBody() == null) && newClass.getClazz() instanceof J.ParameterizedType) {
J.ParameterizedType newClassType = (J.ParameterizedType) newClass.getClazz();
if (newClassType.getTypeParameters() != null) {
if (paramTypes.size() != newClassType.getTypeParameters().size() ||
newClassType.getTypeParameters().stream().anyMatch(p -> p instanceof J.AnnotatedType)) {
if (paramTypes.size() != newClassType.getTypeParameters().size() || hasAnnotations(newClassType)) {
return newClass;
} else {
for (int i = 0; i < paramTypes.size(); i++) {
Expand All @@ -232,6 +233,24 @@ private J.NewClass maybeRemoveParams(@Nullable List<JavaType> paramTypes, J.NewC
return newClass;
}

private static boolean hasAnnotations(J type) {
if (type instanceof J.ParameterizedType) {
J.ParameterizedType parameterizedType = (J.ParameterizedType) type;
if (hasAnnotations(parameterizedType.getClazz())) {
return true;
} else if (parameterizedType.getTypeParameters() != null) {
for (Expression typeParameter : parameterizedType.getTypeParameters()) {
if (hasAnnotations(typeParameter)) {
return true;
}
}
}
} else {
return type instanceof J.AnnotatedType;
}
return false;
}

private boolean isAParameter() {
return getCursor().dropParentUntil(p -> p instanceof J.MethodInvocation ||
p instanceof J.ClassDeclaration ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ void doNotChangeAnnotatedTypeParameters() {
class Test {
private void test(Object t) {
List<String> l = new ArrayList<@Nullable String>();
List<List<String>> l = new ArrayList<List<@Nullable String>>();
}
}
"""
Expand Down

0 comments on commit 41f907d

Please sign in to comment.