Re: [pmd-devel] How to check type of variable with PMD?
A source code analyzer
Brought to you by:
adangel,
juansotuyo
|
From: Valeriy M. <v.m...@gm...> - 2017-10-04 04:45:55
|
Thank you! This is what I need. What version of PMD first introduced this functionality? On Oct 4, 2017 2:10 AM, "Juan Martín Sotuyo Dodero" <jua...@gm...> wrote: > Valeriy, it seems you are looking for type resolution code. You have the > `typeof` function available to do just that, and let it handle everything > on it's own (imports, fully-qualified access, star imports, same package > visibility, etc.). > > You can find more info on how to use this function from XPath on our docs: > https://pmd.github.io/pmd-5.8.1/customizing/howtowritearule.html#I_need_ > some_kind_of_Type_Resolution_for_my_rule > > Hope that helps! > > On Mon, Oct 2, 2017 at 11:01 AM, Valeriy Manenkov <v.m...@gm...> > wrote: > >> Hello, >> >> I will begin immediately with an example: >> >> void okMethod3() { >> // ... >> MyType con = connectionFactory.createConnection(); >> // ... >> publisher.close(); >> session.close(); >> con.stop(); // !!! does not free jms resources >> } >> >> void okMethod3() { >> // ... >> Connection con = connectionFactory.createConnection(); >> // ... >> publisher.close(); >> session.close(); >> con.stop(); // !!! does not free jms resources >> other.close(); >> } >> >> I need to add violation to line `con.stop()` when `con` have >> `Connection` type. How to check type of variable with PMD using XPath? >> >> I tried this, xpath expression: >> >> //BlockStatement >> [ends-with(Statement/StatementExpression/PrimaryExpression/P >> rimaryPrefix/Name/@Image, >> '.stop')] >> [starts-with(Statement/StatementExpression/PrimaryExpression >> /PrimaryPrefix/Name/@Image, >> ancestor::MethodDeclaration//BlockStatement/LocalVariableDec >> laration/VariableDeclarator/VariableDeclaratorId >> [../Type/ReferenceType/ClassOrInterfaceType[@Image='Connection']] >> [//ImportDeclaration/Name[@Image='javax.jms.Connection']] >> )] >> >> But it is not works >> >> -- >> Regards, >> Valeriy Manenkov >> >> ------------------------------------------------------------ >> ------------------ >> Check out the vibrant tech community on one of the world's most >> engaging tech sites, Slashdot.org! http://sdm.link/slashdot >> _______________________________________________ >> Pmd-devel mailing list >> Pmd...@li... >> https://lists.sourceforge.net/lists/listinfo/pmd-devel >> > > |