简体   繁体   中英

Eclipse not recognizing passing an extended generic class

In my project I have an abstract custom object:

public abstract class CustomObject {
    //abstract class goodness
}

In another class I am using a few different classes that extend CustomObject and am writing a method that can take the specific class that extends it as a parameter, populates the common fields, and spits it back out as the result.

The method looks like this right now:

public <T> T setCommonProperties( Class<T extends CustomObject> clazz, Identifier ident ) {
    CustomObject populate = clazz.newInstance();
    //populate populate
}

This looks correct with what I have found while searching the internet, but eclipse marks the extends as incorrect. It gives me Syntax error on token "extends", , expected when I hover over the extends.

Does anyone have any idea why eclipse may be doing this?

This is a Java error; Eclipse is correctly marking it as an error. Your code has incorrect Java generics syntax. The extends upper bound belongs on the declaration at the beginning, not on the use of the type parameter. Try

public <T extends CustomObject> T setCommonProperties( Class<T> clazz, Identifier ident )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM