Tom Copeland's Recent Posts

RSS Feeds

« Finding opportunities for System.arraycopy | Main | Misplaced null checks »

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83451d3c069e200e55070ab958834

Listed below are links to weblogs that reference Using regular expressions in PMD rules:

Comments

XPath already has a function defined called "matches". You should already be able to do:

//ClassOrInterfaceDeclaration
[matches(@Image,'^(Foo|Bar)$')]

Without any extensions - if Jaxen doesn't actually implement that XPath function, you should probably call your new function "matches". Unless, of course, the ORO engine doesn't match the regex spec in the xpath-functions spec.

http://www.w3.org/TR/xpath-functions/#func-matches

Very interesting! Jaxen doesn't implement that function; I guess that's because it's a XPath 2.0 function. Hm. Now's the time to rename it, I reckon, before the 3.5 release... I'll propose that. Thank Daniel!

It's renamed 'matches' now in CVS... good times. Thanks Daniel!

What is the point of the Abstract Naming rule? In my humble opinion this has no added value. Please explain.

anonymous - it's for checking a naming convention - ensuring that abstract classes are prefixed with "Abstract"; in some shops that's considered a good practice.

Hi All,

I need to call a method on a java object if that kind of object is created in my class file.
Sample code can be like this:

public class TestClass {
public void test() {
MyObjectA objA;
MyObjectB objB = objA.createObjB();
String str = new String("Vipul");
str = null; //Just like that so that more BlockStatements are creating in between
objB.cleanup();
}
}

So the intention is to create a rule that can detect that if an object of type MyObjectB is created, we should make sure that we call cleanup() on this object in this class file.

I am wonderinf if this is possible using XPathRule?

I tried creating an XPathRule like this:

//Type/ReferenceType/ClassOrInterfaceType[
(@Image = 'FormContext')
and
(../../../descendant::Name[ends-with(@Image,'createFormContext')])
and
(
(not
(contains
(
(./ancestor::Block/descendant::StatementExpression/descendant::Name/attribute::Image),
concat(../../../VariableDeclarator/VariableDeclaratorId/attribute::Image,'.release')
)
)
)
)
]

But this does not works properly. If I introduce any statement before cleanup() is called on objB, this rule fails.
It stops at first occurence of (./ancestor::Block/descendant::StatementExpression/descendant::Name/attribute::Image and never reaches till the intended place that is 'objB.cleanup'.

The problem is that I am not able to figure out how can make sure my XPathRule does not stop on encountrring first occurrence of the path '(./ancestor::Block/descendant::StatementExpression/descendant::Name/attribute::Image' and keeps moving ahead with all combinations in which it ultimately also finds objB.cleanup no matter if it is inside if, while etc or if there are any other statements in between creation of objB and before cleanup() is called on it.

I would really appreciate if someone can provide a hint to acheive this or tell me if it feasible through XPath.


Thanks in Advance!!!

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been saved. Comments are moderated and will not appear until approved by the author. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Comments are moderated, and will not appear until the author has approved them.