Java Generic wildcard misunderstanding [duplicate]

Having this:

public <T extends DataAgent> DataProcessor<? extends DataAgent> resolveProcessor(
            final String response, T dAgent) throws Exception {

        if (dAgent.getType() == RESULT_TYPE.INLINE) {
            return InlineParser.parse(response, (InlineDataAgent) dAgent);
        }
        return DefaultParser.parse(response, (DefaultDataAgent) dAgent);
    }

The code work as it should but i struggle with a warning that says ‘Generic wildcards should not be used in return types’.

Both Agent classes extending from DataAgent but they are specialized with specialized fields. Therefore i need the cast to work with the specialized subclass.

The DataProcessor is also a interface and the implementations are bounded to either DefaultDataAgent or InlineDataAgent.

How can i solve the wildcard problem?

0

Your question is about a linter rule. Linters are written by opinionated humans, and they aren’t generally peer reviewed. In contrast to a language spec which is written, reviewed, revised, agonized about, and effectively ‘forced onto the entire community’.

In other words, often ‘this linter tool tells me X is wrong’ is best answered by: “Well, then ditch the dumb tool, nobody is forcing you to use it!”.

Thus, one easy solution, if you’re convinced you’ve done it right, is just that. Stop using it / disable that rule.

What is the nature of wildcards in generics?

You have a DataProcessor<? extends DataAgent> as return type which the linter is complaining about.

To explain the intent behind that warning, let’s use a type everybody understands, and where all variances are much easier to explain: List.

Let’s say I have a list that can contain any number.

You might think that would be a List<? extends Number>. But that is incorrect. Question mark is best read as just that: Say “I do not know” as you read it out. a List<? extends Number> is pronounced as: “A list containing I-do-not-know… but I do know that whatever it is, it is a Number or a subtype of Number”.

In other words, it might be a List<Double>. It might be a List<Integer>. You don’t know. And because you don’t know, anybody that calls this method cannot add anything to this list. Not because it is immutable – but because there is no expression in java that is both a Double and an Integer at the same time, and yet, that’s the only thing you can safely add here as it might be either one. In fact, I lied – there is an expression that is both Double and Integer at the same time: the null literal. Indeed, list.add(null); works on a List<? extends Whatever>… and nothing else will when invoking add on such a list.

How useless.

In contrast, a List<Number> means, specifically, a ‘a list containing all sorts of numbers’. You can add doubles to this list, you can add integers to this list.

Here, your method should know what it is returning.

If your method is returning a ‘split’ type (where it returns either a List<Integer> or a List<Double> depending on the phase of the moon for example), your method just sucks. We don’t write methods that return 2 wildly different concepts – instead we write 2 methods then. I think we all agree this is a really bad method:

public Object returnUserNameOrId() {
  if (systemIsIdBased()) return 5;
  else return "username";
}

Returning either an Integer or a String is just not ‘java-like’. Java is not designed for this, the core libs don’t have an Either<Left, Right>, no core library function does this.

That’s what the linter tool is complaining about. Opinion: The linter rule is pretty good. You should indeed rewrite this method.

So, if in the PECS getup (Producer Extends, Consumer Super), if ‘producing’ is not a relevant aspect of it, just write DataProcessor<DataAgent>. If it is relevant, write 2 methods, or make a type var that the caller can somehow influence and return <A extends DataAgent> DataProcessor<A> instead.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật