Java


JAVA.ID.SHADOW : Shadowed Identifier (Java)

要旨

A class defines a field with the same name as another in a superclass.

Java classes can be extended and methods overridden. Sometimes, it is possible that methods are redefined in a way that seems an overriding but is actually the definition of another, distinct method. This is often cause of ambiguities and bugs. Moreover, fields in Java cannot be overridden and a subclass may add a synonym field of a superclass: the class ends up having two fields with the same name. This is often a source of ambiguities and might lead programmers to think that the field is actually overridden at all uses, while distinct synonym fields are used at different program points. This checker finds ambiguities and bugs in the extension of classes, method overriding and fields redefinition.

プロパティ

クラス名 Shadowed Identifier (Java)
日本語クラス名 Shadowed Identifier (Java)
クラス分類 信頼性 (reliability)
ニーモニック JAVA.ID.SHADOW
カテゴリー
CWE CWE:1076 Insufficient Adherence to Expected Conventions
対応言語 Java で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Shadowed Identifier (Java)"

The following example code uses two different classes named C (neither is shown):

// Super.java 
import sub.C;

public class Super implements Cloneable {
  public final int k = 13;

  public Super() {}

  public float myMethod(int a, float f) {
      return a + f + k;
  }

  public void anotherMethod(C par) {}
}
// Sub1.java 
public class Sub1 extends Super implements Cloneable {  // Redundant Implements Clause (Java) warning issued here
  public Sub1() {}

  public float myMethod(int a, int f) {
      return a + f;
  }
}
// Sub2.java 
public class Sub2 extends Super {
  public Sub2() {}

  public float mymethod(int a, float f) {               // Method Names Differ Only in Case (Java) warning issued here
      return a + f;
  }
}
// Sub3.java 
public class Sub3 extends Super {
  public final int k = 17;                              // Shadowed Identifier (Java) warning issued here
  public Sub3() {}

  public void anotherMethod(C par) {}                   // Non-overriding Method Signature (Java) warning issued here
}

In this example, the programmer would probably rewrite the classes as follows.

// Super.java after rewriting
import sub.C;

public class Super implements Cloneable {
  public final int k = 13;

  public Super() {}

  public float myMethod(int a, float f) {
      return a + f + k;
  }

  public void anotherMethod(C par) {}
}
// Sub1.java after rewriting
public class Sub1 extends Super {
  public Sub1() {}

  public float myMethod(int a, int f) {
      return a + f;
  }
}
// Sub2.java after rewriting
public class Sub2 extends Super {
  public Sub2() {}

  public float myMethod(int a, float f) {
      return a + f;
  }
}
// Sub3.java after rewriting
public class Sub3 extends Super {
  public Sub3() {}
  public void anotherMethod(sub.C par) {}
}

解決法

Check if a method redefinition should rather be an overriding. Check if a synonym field, already defined in a superclass, should be removed or renamed.

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。