diamond with anonymous classes

JAX-RSGenericEntity<T>라는 클래스가 있다. 이유는 모르겠지만 두개의 생성자 모두, 접근제어자가 protected로 선언되어 있다.

public class GenericEntity<T> extends Object {
    protected GenericEntity(T entity)
    protected GenericEntity(T entity, Type genericType)

무턱대고 다음과 같이 했더니

final List<Stock> list = getStocks();
final GenericEntity<List<Stock>> entity = new GenericEntity<>(list) {};

에러가 덜컥…

error: cannot infer type arguments for GenericEntity<T>

“다이아몬드를 사다 놓았는데 왜 컴파일을 못하니? 왜 컴파일을 못하니…? 괴상하게도 오늘은 운수가 좋더니만…”

JSR-334: Small Enhancements to the JavaTM Programming Language (link)

Internally, a Java compiler operates over a richer set of types than those that can be written down explicitly in a Java program. The compiler-internal types which cannot be written in a Java program are called non-denotable types. Non-denotable types can occur as the result of the inference used by diamond. Therefore, using diamond with anonymous inner classes is not supported since doing so in general would require extensions to the class file signature attribute to represent non-denotable types, a de facto JVM change. It is feasible that future platform versions could allow use of diamond when creating an anonymous inner class as long as the inferred type was denotable.

음… 암튼 뭔가 깊은 뜻이 있는 듯 하다.

JEP 213: Milling Project Coin (link)

Allow diamond with anonymous classes if the argument type of the inferred type is denotable. Because the inferred type using diamond with an anonymous class constructor could be outside of the set of types supported by the signature attribute, using diamond with anonymous classes was disallowed in Java SE 7. As noted in the JSR 334 proposed final draft, it would be possible to ease this restriction if the inferred type was denotable.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s