Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@
@EqualsAndHashCode(callSuper = true)
public class EmptyBlockVisitor<P> extends JavaIsoVisitor<P> {
EmptyBlockStyle emptyBlockStyle;
JavaTemplate throwException = JavaTemplate.builder("throw new #{}(#{any(String)});")
.imports("java.io.UncheckedIOException")
.build();
JavaTemplate continueStatement = JavaTemplate.builder("continue;").build();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,13 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
newType,
null
);
} else if (m.getReturnTypeExpression() instanceof J.AnnotatedType) {
J.AnnotatedType annotatedType = (J.AnnotatedType) m.getReturnTypeExpression();
J.ParameterizedType parameterizedType = (J.ParameterizedType) annotatedType.getTypeExpression();
typeExpression = annotatedType.withTypeExpression(removeFromParameterizedType(newType, parameterizedType));
} else {
J.ParameterizedType parameterizedType = (J.ParameterizedType) m.getReturnTypeExpression();
J.Identifier returnType = new J.Identifier(
randomId(),
Space.EMPTY,
Markers.EMPTY,
emptyList(),
newType.getClassName(),
newType,
null);
JavaType.Parameterized javaType = (JavaType.Parameterized) parameterizedType.getType();
typeExpression = parameterizedType.withClazz(returnType)
.withType(javaType != null ? javaType.withType(newType) :
new JavaType.Parameterized(null, newType, null)
);
typeExpression = removeFromParameterizedType(newType, parameterizedType);
}
m = m.withReturnTypeExpression(typeExpression);
}
Expand Down Expand Up @@ -183,22 +175,13 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
newType,
null
);
} else if (mv.getTypeExpression() instanceof J.AnnotatedType) {
J.AnnotatedType annotatedType = (J.AnnotatedType) mv.getTypeExpression();
J.ParameterizedType parameterizedType = (J.ParameterizedType) annotatedType.getTypeExpression();
typeExpression = annotatedType.withTypeExpression(removeFromParameterizedType(newType, parameterizedType));
} else {
J.ParameterizedType parameterizedType = (J.ParameterizedType) mv.getTypeExpression();
J.Identifier returnType = new J.Identifier(
randomId(),
Space.EMPTY,
Markers.EMPTY,
emptyList(),
newType.getClassName(),
newType,
null
);
JavaType.Parameterized javaType = (JavaType.Parameterized) parameterizedType.getType();
typeExpression = parameterizedType.withClazz(returnType)
.withType(javaType != null ? javaType.withType(newType) :
new JavaType.Parameterized(null, newType, null)
);
typeExpression = removeFromParameterizedType(newType, parameterizedType);
}

mv = mv.withTypeExpression(typeExpression);
Expand All @@ -213,6 +196,23 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
}
return mv;
}

private TypeTree removeFromParameterizedType(JavaType.FullyQualified newType,
J.ParameterizedType parameterizedType) {
J.Identifier returnType = new J.Identifier(
randomId(),
Space.EMPTY,
Markers.EMPTY,
emptyList(),
newType.getClassName(),
newType,
null
);
JavaType.Parameterized javaType = (JavaType.Parameterized) parameterizedType.getType();
return parameterizedType.withClazz(returnType)
.withType(javaType != null ? javaType.withType(newType) :
new JavaType.Parameterized(null, newType, null));
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.junitpioneer.jupiter.ExpectedToFail;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.marker.JavaVersion;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand All @@ -27,6 +28,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.javaVersion;

@SuppressWarnings("rawtypes")
class UseCollectionInterfacesTest implements RewriteTest {
Expand Down Expand Up @@ -130,6 +132,41 @@ public Set<Integer> method() {
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/223")
void annotatedReturnType() {
rewriteRun(
spec -> spec
.allSources(s -> s.markers(javaVersion(9)))
.parser(JavaParser.fromJavaVersion().classpath("annotations-24.1.0")),
//language=java
java(
"""
import java.util.HashSet;
import org.jetbrains.annotations.Nullable;

class Test {
public @Nullable HashSet<@Nullable Integer> method() {
return new HashSet<>();
}
}
""",
"""
import java.util.HashSet;
import java.util.Set;

import org.jetbrains.annotations.Nullable;

class Test {
public @Nullable Set<@Nullable Integer> method() {
return new HashSet<>();
}
}
"""
)
);
}

@Test
void preserveParameters() {
rewriteRun(
Expand Down Expand Up @@ -223,6 +260,37 @@ class Test {
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/223")
void annotatedFieldType() {
rewriteRun(
spec -> spec
.allSources(s -> s.markers(javaVersion(9)))
.parser(JavaParser.fromJavaVersion().classpath("annotations-24.1.0")),
//language=java
java(
"""
import java.util.HashSet;
import org.jetbrains.annotations.Nullable;

class Test {
public @Nullable HashSet<@Nullable Integer> values = new HashSet<>();
}
""",
"""
import java.util.HashSet;
import java.util.Set;

import org.jetbrains.annotations.Nullable;

class Test {
public @Nullable Set<@Nullable Integer> values = new HashSet<>();
}
"""
)
);
}

@Test
void arrayDeque() {
rewriteRun(
Expand Down