A class is identified by comparing its name with a string literal
What it finds
shape.getClass().getName().equals("com.example.Circle") tests the class by its name: two classes of the same name loaded by different class loaders pass, a renamed or moved class silently fails, and subclasses never match. shape instanceof Circle or shape.getClass() == Circle.class compares the classes themselves.
Reported when the name of getClass() is compared with a string literal through equals.
From the rule's source, class_name_comparison.rs.
class Shapes { boolean circle(Object shape) { boolean a = shape.getClass().getName().equals("com.example.Circle");tx0 reports: the class is identified by its name boolean b = "Square".equals(shape.getClass().getSimpleName());tx0 reports: the class is identified by its name boolean c = shape.getClass() == String.class; return a || b || c; }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:class-name-comparison" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:class-name-comparison] reason="..."tx0 explain tx0/java:class-name-comparison shows the same in the terminal. Found a false positive? Report it.