Findbugs, PMD or Checkstyle rule to find access by a field

I have a class caches a set of values internally. These values can be updated periodically and the cached contents will be updated appropriately. As long as users of this class do something like:

...
public void anyMethod(anyParams) {
    AnyObject value = CacheClass.getValue(anyKey);
    ...
}

Then life will be fine as that "value" will not maintained locally. However if anyone does something like this:

public class MyClass {
    private AnyObject value = CacheClass.getValue(someKey);
   ...
}

Then if the cached value is updated the usage of it may not see the new value.

We use findbugs, checkstyle and PMD in the builds via Sonar. So I am wondering if there is a way to define a rule in any of these systems to detect and flag the second type of usage above. I have never written a rule in any these systems so would appreciate as much advice as possible (like, "well it can be done, but you really don't want to go there..." :)

Answers


Following XPath on your AST Node will catch Field declarations that calls your CacheClass method.

//FieldDeclaration[//PrimaryExpression/PrimaryPrefix/Name/@Image='CacheClass.getValue']

How to write PMD Custom Rule


Need Your Help

C#/WPF: LineGeometry with nested UI Elements

c# wpf visual-studio visual-studio-2010

I am having problems using LineGeometry for drawing a path between two simple shapes nested in canvas elements.

Trouble Creating and showing Users in Rails blog

ruby-on-rails ruby user blogs

I keep getting a variety of error while trying to create and show errors in a simple Rails blog I'm trying to create.Let me know if you see anything obvious or if you need me to post more code as I...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.