RTC.CALL

当添加指向某些类的转换,同时又调用了在更为通用的类中所定义的方法时,将出现冗余类型转换 (RTC) 警告。以下是一个示例:

复制
public class Apple { 
  public String someAppleToString(Object o) { 
  return ((Apple) o).toString();
  }
}

在本示例中,指向 Apple 的类型转换是多余的,因为 Apple 类并未重新定义 toString() 方法,并且实际上使用的是 Object 的方法。当添加指向某些类的转换,同时又调用了在更为通用的类中所定义的方法时,会出现 RTC.CALL 警告。

示例 1

复制
      public class Instruction {
     }
     public class JmpInstruction extends Instruction {
         private final Number address;
         protected JmpInstruction(Number address) {
             this.address = address;
         }
         public Number getAddress() {
             return address;
         }
     }
     public class ShortJmpInstruction extends JmpInstruction {
         public ShortJmpInstruction(Byte address) {
             super(address);
         }
     }
     // ...
     public void visitInstruction(JmpInstruction i) {
         if (i instanceof ShortJmpInstruction) {
             // cast is not necessary here
             Number address =
                     ((ShortJmpInstruction) i).getAddress();
             print(address);
         }
         // then visit other instrucrions..
     }

针对第 30 行报告 RTC.CALL:从 com.klocwork.jdefects.checkers.ast.samples.RTC_CALL_Sample_1$JmpInstruction 到 com.klocwork.jdefects.checkers.ast.samples.RTC_CALL_Sample_1$ShortJmpInstruction 的类型转换是多余的,因为方法 getAddress 已在 com.klocwork.jdefects.checkers.ast.samples.RTC_CALL_Sample_1$JmpInstruction 中定义