Skip to content

RemoveRedundantTypeCast should not remove required downcast #495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -56,10 +56,10 @@ void parametersDoNotMatch() {
java(
"""
import java.util.Collection;

class Test {
Class<? extends Collection<String>> test = (Class<? extends Collection<String>>) get();

Class<?> get() {
return null;
}
Expand All @@ -76,7 +76,7 @@ void primitiveCast() {
java(
"""
import java.io.DataOutputStream;

class Test {
void m(DataOutputStream out) {
out.writeByte((byte) 0xff);
Expand All @@ -96,31 +96,31 @@ void genericTypeVariableCast() {
java(
"""
import java.util.Iterator;

class GenericNumberIterable<T extends Number> implements Iterable<T> {

private final Iterable<Number> wrappedIterable;

GenericNumberIterable(Iterable<Number> wrap) {
this.wrappedIterable = wrap;
}

@Override
public Iterator<T> iterator() {
final Iterator<Number> iter = wrappedIterable.iterator();

return new Iterator<T>() {
@Override
public boolean hasNext() {
return iter.hasNext();
}

@Override
@SuppressWarnings("unchecked")
public T next() {
return (T) iter.next();
}

@Override
public void remove() {
throw new UnsupportedOperationException();
Expand All @@ -141,7 +141,7 @@ void changeTypeCastInReturn() {
java(
"""
import java.util.*;

class Test {
public <T extends Collection<String>> T test() {
return (T) get();
Expand All @@ -162,7 +162,7 @@ void nonSamParameter() {
java(
"""
import java.util.*;

class Test {
public boolean foo() {
return Objects.equals("x", (Comparable<String>) (s) -> 1);
Expand Down Expand Up @@ -210,7 +210,7 @@ void wildcardGenericsInTargetType() {
java(
"""
import java.util.List;

class Test {
Object o = null;
List<?> l = (List<?>) o;
Expand Down Expand Up @@ -309,7 +309,7 @@ void downCastParameterizedTypes() {
java(
"""
import java.util.List;

class Test {
Object o = (List<String>) method();
Object o2 = (List<? extends String>) method();
Expand All @@ -322,7 +322,7 @@ List<String> method() {
""",
"""
import java.util.List;

class Test {
Object o = method();
Object o2 = method();
Expand Down Expand Up @@ -402,7 +402,7 @@ void lambdaWithComplexTypeInference() {
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;

class Test {
void method() {
Object o2 = new MapDropdownChoice<String, Integer>(
Expand All @@ -414,7 +414,7 @@ void method() {
});
}
}

class MapDropdownChoice<K, V> {
public MapDropdownChoice(Supplier<? extends Map<K, ? extends V>> choiceMap) {
}
Expand Down Expand Up @@ -448,7 +448,7 @@ void castWildcard() {
"""
import java.util.ArrayList;
import java.util.List;

class Test {
void method() {
List<? extends Number> list = new ArrayList<>();
Expand All @@ -468,7 +468,7 @@ void removeImport() {
"""
import java.util.ArrayList;
import java.util.List;

class Test {
List method(List list) {
return (ArrayList) list;
Expand All @@ -477,7 +477,7 @@ List method(List list) {
""",
"""
import java.util.List;

class Test {
List method(List list) {
return list;
Expand All @@ -498,7 +498,7 @@ void retainCastInMarshaller() {
package org.glassfish.jaxb.core.marshaller;
import java.io.IOException;
import java.io.Writer;

public interface CharacterEscapeHandler {
void escape( char[] ch, int start, int length, boolean isAttVal, Writer out ) throws IOException;\s
}
Expand All @@ -517,7 +517,7 @@ public interface Marshaller {
"""
import javax.xml.bind.Marshaller;
import org.glassfish.jaxb.core.marshaller.CharacterEscapeHandler;

class Foo {
void bar(Marshaller marshaller) {
marshaller.setProperty("org.glassfish.jaxb.characterEscapeHandler", (CharacterEscapeHandler) (ch, start, length, isAttVal, out) -> {
Expand All @@ -528,4 +528,36 @@ void bar(Marshaller marshaller) {
)
);
}

@Test
void dontRemoveNecessaryDowncast() {
rewriteRun(
spec -> spec.recipe(new RemoveRedundantTypeCast()),
// language=java
java(
"""
package com.helloworld;

import java.util.Optional;

public class Foo {
public interface Bar {}

public static class BarImpl implements Bar {}

private Bar getBar() {
return new BarImpl();
}

private BarImpl getBarImpl() {
return new BarImpl();
}

public Bar baz() {
return Optional.of((Bar) getBarImpl()).orElse(getBar());
}
}
"""));
}
}

Loading