Re: [pmd-devel] How to check type of variable with PMD?
A source code analyzer
Brought to you by:
adangel,
juansotuyo
|
From: Juan M. S. D. <jua...@gm...> - 2017-10-03 23:10:29
|
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/ > PrimaryPrefix/Name/@Image, > '.stop')] > [starts-with(Statement/StatementExpression/PrimaryExpression/ > PrimaryPrefix/Name/@Image, > ancestor::MethodDeclaration//BlockStatement/ > LocalVariableDeclaration/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 > |