正确使用以避免泛型类型不必要的转换(SuppressWarnings 未经检查的转换)

发布于 2024-11-19 00:30:56 字数 1652 浏览 2 评论 0 原文

那里有类似的问题,但我没有找到任何真正解决我的担忧或涵盖我的实际实施的问题。

使用以下示例代码(反映了我的实际情况),

public class MainTest {

    public static void main(String[] args) {
        WhateverDtoXmlParser parser = (new MainTest()).new WhateverDtoXmlParser();

        // I want to do this (having to do suppressWarnings)
        WhateverDto wd = parser.getDto();

        // Instead of (usage without the warning).
        // I want to avoid all of this!
        Dto d = parser.getDto();
        WhateverDto wd2 = null;
        if (d instanceof WhateverDto) { // All of this is stupid and unnecessary IMO.
            wd2 = (WhateverDto) d;
        }
    }

    abstract class AbstractDtoXmlParser {
        public abstract <T extends Dto> T getDto();
    }

    class WhateverDtoXmlParser extends AbstractDtoXmlParser {

        @SuppressWarnings("unchecked")
        @Override
        public WhateverDto getDto() { // instead of public Dto getDto() (to avoid instanceof + cast)
            return new WhateverDto();
        }
    }

    abstract class Dto {
        // ...
    }

    public class WhateverDto extends Dto {
        // ...
    }
}

即使我使用了抑制警告,您是否认为这是正确的用法? 我的意思是我知道WhateverDtoXmlParser 返回的类型将是 WhateverDto 而不仅仅是任何其他 Dto,因为我是这样编码的< /强>。为什么 Java 不能检查返回类型是否扩展 Dto ,因为我使用 > 显式指定它(加上它扩展一个抽象类...)并接受它?

要么我在那里执行此操作,要么我每次使用 getDto() 时都必须使用 instanceof 并进行强制转换..!在我看来,我当前的实现是“最好的”,但是为什么我会收到如此令人担忧的警告?

阅读其他线程后,似乎没有办法绕过此警告,但我应该继续当前的实现吗?

There are similar questions out there but I didn't find any that really answers my concerns or that covers my actual implementation.

With the following example code (which reflects my actual situation)

public class MainTest {

    public static void main(String[] args) {
        WhateverDtoXmlParser parser = (new MainTest()).new WhateverDtoXmlParser();

        // I want to do this (having to do suppressWarnings)
        WhateverDto wd = parser.getDto();

        // Instead of (usage without the warning).
        // I want to avoid all of this!
        Dto d = parser.getDto();
        WhateverDto wd2 = null;
        if (d instanceof WhateverDto) { // All of this is stupid and unnecessary IMO.
            wd2 = (WhateverDto) d;
        }
    }

    abstract class AbstractDtoXmlParser {
        public abstract <T extends Dto> T getDto();
    }

    class WhateverDtoXmlParser extends AbstractDtoXmlParser {

        @SuppressWarnings("unchecked")
        @Override
        public WhateverDto getDto() { // instead of public Dto getDto() (to avoid instanceof + cast)
            return new WhateverDto();
        }
    }

    abstract class Dto {
        // ...
    }

    public class WhateverDto extends Dto {
        // ...
    }
}

Would you consider this a correct usage even though I used a suppresswarnings?
I mean I KNOW the returned type from WhateverDtoXmlParser will be a WhateverDto and not just any other Dto because I coded it that way. Why can't Java check if the return type extends Dto as I explicitly specified it with <T extends Dto> (plus it extends an abstract class...) and accept it?

It's either I do this there, OR I have to use instanceofs and casts everytime I use getDto() .. ! It seems to me that my current implementation is the "best" but then why do I get such a concerning warning?

After reading the other threads it seems that there is no way to get around this warning, but should I go on with my current implementation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙与沫 2024-11-26 00:30:56

试试这个:

abstract class AbstractDtoXmlParser<T extends Dto> {
    public abstract T getDto();
}

class WhateverDtoXmlParser extends AbstractDtoXmlParser<WhateverDto> {

    @Override
    public WhateverDto getDto() {
        return new WhateverDto();
    }
}

Try this:

abstract class AbstractDtoXmlParser<T extends Dto> {
    public abstract T getDto();
}

class WhateverDtoXmlParser extends AbstractDtoXmlParser<WhateverDto> {

    @Override
    public WhateverDto getDto() {
        return new WhateverDto();
    }
}
余生共白头 2024-11-26 00:30:56

如果您确定返回的类型是您期望的类型,那么执行这样的不安全转换没有任何问题...

WhateverDto d = (WhateverDto) parser.getDto();

这仍然不是最干净的,但它不应该给您警告并且它会赢也不用写4行。

If you know for sure the type you are getting back is the type you are expecting, there is nothing wrong with doing an unsafe cast like this...

WhateverDto d = (WhateverDto) parser.getDto();

This still isn't the cleanest but it shouldn't give you warnings and it won't take 4 lines to write either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文