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

Changing reference types(strings) inside methods

c# oop

I am passing a string variable to a method. I know strings are reference types but the value that I assign inside the method is lost.

Parsing HTML issues with Apache Tika

java parsing html-parsing apache-tika

I am crawling a webpage and after crawling it extract all the links from that webpage and then I am trying to parse all the url using Apache Tika and BoilerPipe by using below code so for some url ...

AmazonS3 putObject with InputStream length example

java google-app-engine amazon-s3 md5 inputstream

I am uploading a file to S3 using java -- this is what I got so far: