Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] False positive for UseInstanceofToCompareClasses on local generic class #30

Open
oowekyala opened this issue Apr 3, 2019 · 1 comment

Comments

@oowekyala
Copy link
Member

UseInstanceofToCompareClasses reports on non-reifiable types, even though they're not acceptable as operand to instanceof. E.g.:

class Foo<T> {

public Foo<T> wrap() {
   // since the local class is not static, the T here refers to the T of the outer instance
   class MyWrapper extends Foo<T> {
      
   }
   this instanceof MyWrapper // -> error, MyWrapper is not reifiable, because its supertype Foo<T> isn't 

   this.getClass().equals(MyWrapper.class) // this is the only way to compare it
} 

}
@jsotuyod jsotuyod changed the title False positive for UseInstanceofToCompareClasses on local generic class [java] False positive for UseInstanceofToCompareClasses on local generic class Apr 8, 2019
@jsotuyod jsotuyod transferred this issue from pmd/pmd Apr 1, 2024
@jsotuyod
Copy link
Member

jsotuyod commented Apr 1, 2024

This is actually an internal rule defined by us in our dogfood ruleset, so I move the issue here instead

<rule name="UseInstanceofToCompareClasses"
language="java"
since="5.0"
message="replace o.getClass().equals(MyClass.class) with o instanceof MyClass"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<description>replace o.getClass().equals(MyClass.class) with o instanceof MyClass. Make sure MyClass doesn't have descendants</description>
<priority>1</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//MethodCall[pmd-java:matchesSig("_#equals(java.lang.Object)")]
[MethodCall[pmd-java:matchesSig("_#getClass()")]]
[ArgumentList/ClassLiteral]
]]>
</value>
</property>
</properties>
</rule>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants