Java 제네릭: 캡처를 사용한 컴파일 실패 (Java generics: compilation failure using captures)


문제 설명

Java 제네릭: 캡처를 사용한 컴파일 실패 (Java generics: compilation failure using captures)

Why does the below code fail to compile? Can this example be simplified (to fewer classes) to demonstrate equivalent error?

The error message produced is:

func(capture of ? extends A) in ... cannot be aplied to B

    private static interface A {}
    private static class B implements A {}

    private static class C<T> {
        private final T t;
        private C(T t) {
            this.t = t;
        }
        private void func(T t) {}
    }

    @Test
    public void doTest() {
        B b = new B();
        C<? extends A> c = new C<B>(b);
        c.func(b); // fails here
    }

참조 솔루션

방법 1:

The problem is that C<? extends A> means "It's a C<T> for some type T, but I don't know which type it is ‑ only that it's something which extends A."

That means you can't assume that T = B, or that there's a conversion from B to T. But that's exactly what you do try to assume here:

c.func(b);

To demonstrate why it's invalid, suppose we make a change to your code:

private static class X implements A {}

B b = new B();
C<? extends A> c = new C<X>(new X());
c.func(b);

Now it's more obvious that it shouldn't compile ‑ if you've got a C<X>, you don't expect to be able to call func(b) where b is a B, do you? So given that I've only changed the right‑hand side of the assignment (not the declared type of c) it would be very odd for your example to compile but mine not to. Does that help?

As ever, see the Java Generics FAQ for more information, particularly the part about wildcards.

방법 2:

That line must fail, otherwise this obviously‑wrong code can compile:

private static interface A {}
private static class B implements A {}
private static class B2 implements A {}

@Test
public void doTest() {
    B b = new B();
    B2 b2 = new B2();
    C<? extends A> c = new C<B2>(b2); // B2 extends A, ok.
    c.func(b); // B extends A, but B is not B2, and it should fail.
}

(by LeonidJon Skeetkennytm)

참조 문서

  1. Java generics: compilation failure using captures (CC BY‑SA 3.0/4.0)

#capture #compiler-errors #generics #java






관련 질문

Java 제네릭: 캡처를 사용한 컴파일 실패 (Java generics: compilation failure using captures)

Python 함수 호출에서 stdout 출력을 캡처하는 방법은 무엇입니까? (How to capture stdout output from a Python function call?)

캡처한 이미지 캡처 및 공유를 구현하고 싶습니다. (I want to implement capture and sharing captured image)

Ambilight용 Python 화면 캡처(더 적은 리소스 집약적) (Python Screen Capture for Ambilight (less resource intensive))

Windows에서 "비디오 컨트롤러"에 대한 Java 액세스 (Java Access To "Video Controller" In Windows)

libpcap으로 PPP 패킷을 스니핑하는 방법은 무엇입니까? (How to sniff PPP packet with libpcap?)

DVB-T 스틱에서 AVI 파일로 캡처하는 방법 (How to capture from a DVB-T Stick to an AVI-File)

xlib를 사용하여 키 누름 이벤트 캡처 (capture key press event using xlib)

앱에서 HTTPS 트래픽을 캡처하고 싶지만 작동하지 않습니다. (I want to Capture HTTPS Traffic from app but it does not work)

Selenium-wire Python을 사용하여 네트워크 트래픽을 캡처하는 동안 다른 형식의 로그 보기 (Seeing logs in a different format while capturing network traffic using selenium-wire python)

Android 애플리케이션의 특정 프레임 내 자동 촬영 기능 (Auto picturing feature within specific frame in Android application)

안드로이드 스튜디오 캡처 이미지 더 빠르게 (android studio capture image faster)







코멘트