Skip to content
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

False Positive: BC_UNCONFIRMED_CAST_OF_RETURN_VALUE when using generic bounded type #1219

Open
pcimcioch opened this issue Jul 14, 2020 · 11 comments · Fixed by danielnorberg/auto-matter#184

Comments

@pcimcioch
Copy link

Following code is safe in terms of casting, but spotbugs reports BC_UNCONFIRMED_CAST_OF_RETURN_VALUE

package test;

class Parent {}
class Child extends Parent {}

class Factory<T extends Parent> {
    private final Class<T> type;

    Factory(Class<T> type) {
        this.type = type;
    }

    public T create() {
        try {
            return type.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            return null;
        }
    }
}

public class Test {
    public static void main(String[] args) {
        Factory<Child> cs = new Factory<>(Child.class);
        Child child = cs.create(); // BC_UNCONFIRMED_CAST_OF_RETURN_VALUE
        System.out.println(child);
    }
}
  • If Parent is interface instead of class, this bug is not reported
interface Parent {}
class Child implements Parent {}
  • If Factory doesn't bound type T to Parent, this bug is not reported
class Factory<T> {
  • If type is bounded on method-level, this bug is not reported
class Factory {
    public <T extends Parent> T create(Class<T> type) {
        try {
            return type.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            return null;
        }
    }
}

Spotbugs Version: 4.0.6
Report Level: Low

@welcome
Copy link

welcome bot commented Jul 14, 2020

Thanks for opening your first issue here! 😃
Please check our contributing guideline. Especially when you report a problem, make sure you share a Minimal, Complete, and Verifiable example to reproduce it in this issue.

@alzimmermsft
Copy link

alzimmermsft commented Feb 27, 2021

I'm also seeing a similar issue as this with a method such as:

public <T extends Throwable> T logThrowableAsError(T throwable) {
    // Logic to log the throwable.
    return throwable;
}

// Other code later
public void attemptSomething() throws IOException {
    try {
        trySomething();
    } catch (IOException ex) {
        throw logThrowableAsError(ex);
    }
}

Using Spotbugs version 4.2.0

@alzimmermsft
Copy link

We've upgraded to Spotbugs 4.2.2 and this continues to persist, is there any chance that this has been resolved by an even newer version?

@uhafner
Copy link

uhafner commented Oct 17, 2021

@danielnorberg
Copy link
Contributor

I'm also seeing this issue.

danielnorberg added a commit to danielnorberg/auto-matter that referenced this issue Oct 30, 2021
Fix spotbugs complaints in generated code. 

False positive of BC_UNCONFIRMED_CAST_OF_RETURN_VALUE remains in some cases when using generics due to spotbugs/spotbugs#1219.
@zman0900
Copy link

zman0900 commented Nov 7, 2021

Also seeing this with fairly basic use of Jackson. For example:

package crap

import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.json.JsonMapper;

public final class MyClass {
    private static final ObjectReader JSON_READER = JsonMapper.builder()
            .build()
            .readerFor(OtherClass.class);
}

[ERROR] Low: Unchecked/unconfirmed cast from com.fasterxml.jackson.databind.ObjectMapper to com.fasterxml.jackson.databind.json.JsonMapper of return value in crap.MyClass.() [crap.MyClass] At MyClass.java:[line 8] BC_UNCONFIRMED_CAST_OF_RETURN_VALUE

@CJ061626
Copy link

CJ061626 commented Aug 3, 2023

I'm seeing this issue with the latest release version of Kubernetes-client-6.8.0 when both ObjectMetaFluent class and SecretFluent class extend from the same base class BaseFluent.

Low: Unchecked/unconfirmed cast from io.fabric8.kubernetes.api.model.ObjectMetaFluent to io.fabric8.kubernetes.api.model.SecretFluent$MetadataNested of return value in ClassName.Class (package.ClassName) At ClassName.Java: BC_UNCONFIRMED_CAST_OF_RETURN_VALUE

import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.api.model.SecretBuilder;

public class ClassName {

final Secret secret =
            new SecretBuilder()
                .withNewMetadata()
                .withName(String)
                .withAnnotations(Map.of(String, String))
                .withLabels(Map.of(String, String))
                .endMetadata()
                .WithData(Map.of(String, String))
                .build()
}

@patchwork01
Copy link

I've reproduced this here: gchq/sleeper#1162

@salbracco24
Copy link

Any progress on this?

@robinjhector
Copy link
Contributor

robinjhector commented Dec 15, 2023

BC_UNCONFIRMED_CAST_OF_RETURN_VALUE is also apparent when you use the new pattern matching switch in java 21, (also together with generics) eg:

SomeInterface<?> myObject = ...

var result = switch(myObject) {
    case Impl1 i1 -> doStuff(i1); //i1 is cast to Impl1
}

I created a new issue: #2782

gtoison added a commit that referenced this issue Jan 13, 2024
hazendaz pushed a commit that referenced this issue Jan 13, 2024
…CAST_OF_RETURN_VALUE (#2811)

* test: reproducer for issue #1219

* fix: detect variable types in FindBadCast2

* doc: added changelog entry

* fix: removed unused code
@gtoison
Copy link
Contributor

gtoison commented Jan 14, 2024

This should be fixed by #2811 once released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.