Java


JAVA.ID.CASE.METHOD : Method Names Differ Only in Case (Java)

Summary

A method has a name identical to another in a superclass, up to capitalization.

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.

Properties

Class Name Method Names Differ Only in Case (Java)
Significance reliability
Mnemonic JAVA.ID.CASE.METHOD
Categories
CWE CWE:628 Function Call with Incorrectly Specified Arguments
Availability Available for Java only.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Method Names Differ Only in Case (Java)"

Example

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) {}
}

Resolution

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.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.