简体   繁体   English

禁止由打包库中的父类引起的Java编译器警告

[英]Suppress java compiler warnings caused by parent class in packaged library

I'm extending a class in a packaged library and the class I'm extending causes an "unchecked" compiler warning. 我正在扩展打包库中的类,而我正在扩展的类会导致“未经检查”的编译器警告。 Please note it is not my class that is causing the warning and that is why this is different from other questions I found here in SO. 请注意,不是由我的班级引起此警告,这就是为什么这与我在SO中发现的其他问题不同的原因。

To comply with code standards in my company, my code must compile without warnings but I can't get rid of this one. 为了遵守我公司的代码标准,我的代码必须在没有警告的情况下进行编译,但是我不能摆脱这一警告。

Initially, I wasn't even overriding the offending method. 最初,我什至没有覆盖违规方法。 I have overridden it only to add the @SuppressWarnings("unchecked") annotation but that didn't solve the problem. 我已经覆盖了它,只是添加了@SuppressWarnings(“ unchecked”)注释,但这并不能解决问题。

Within the library, an abstract class ClassA has this method: 在库中,抽象类ClassA具有此方法:

public abstract class ClassA {

    protected abstract <T> T methodA(
        String lockName, 
        TransactionCallback<T> txCallback);
}

Also within the library, another class ClassB extends ClassA causing the warning: 同样在库中,另一个类ClassB扩展了ClassA,从而引起警告:

public class ClassB extends ClassA {

    @Override
    protected Object methodA(
            String lockName, 
            TransactionCallback txCallback) {
        return someOtherMethod(lockName, txCallback, null);
    }

}

This uses an unchecked operation so, when I try to compile my class: 这使用了未经检查的操作,因此,当我尝试编译我的类时:

public class ClassC extends ClassB {

  @SuppressWarnings("unchecked")
  @Override
  protected Object methodA(String lockName, @SuppressWarnings("rawtypes") TransactionCallback txCallback) {
    return super.methodA(lockName, txCallback);
  }

}

I get this warning: 我收到此警告:

    [javac]   protected Object methodA(String lockName, @SuppressWarnings("rawtypes") TransactionCallback txCallback)
    [javac]                    ^
    [javac]   return type requires unchecked conversion from Object to T
    [javac]   where T is a type-variable:
    [javac]     T extends Object declared in method <T>methodA(String,TransactionCallback<T>)

Is there a way to actually suppress the warning even though I can't recompile the offending class but only its descendant? 即使我不能重新编译有问题的类,而只能重新编译其后代,有没有一种方法可以真正抑制警告?

I managed to reproduce. 我设法复制了。 @SuppressWarnings at the class-level seems to work, for whatever reason. 无论出于何种原因,类级别的@SuppressWarnings似乎都可以工作。

@SuppressWarnings("unchecked")
public class ClassC extends ClassB
{
    protected Object methodA(String lockName, TransactionCallback txCallback) {
        return super.methodA(lockName, txCallback);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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