2

Consider the Java code:

ReadProperty.get("info")

And a my_stettings.properties file:

info=Lorem ipsum
server=computer01

I was wondering if it is possible to use a code analysis tool (Checkstyle, FindBugs, PMD...) to check if the String parameter of my get() method is available in the my_stettings.properties file.

ReadProperty.get("servers") //should produce a warning
ReadProperty.get("server") //is OK

Have you some inputs on how I can achieve this?

palacsint
  • 28,416
  • 10
  • 82
  • 109
Jmini
  • 9,189
  • 2
  • 55
  • 77

2 Answers2

2

I don't know any existing tool for doing this. However there are patterns used on the android platform which could be used for your problem, in this case the R resource class pattern which generates a class from property files. So the file my_stettings.properties:

info=Lorem ipsum
server=computer01

should lead to a class like

public final class R {
    public static final int info=1;
    public static final int server=1;
}

In this case you would use it with

ReadProperty.get(R.info);

which would not compile if the attribute is not available.

Community
  • 1
  • 1
ChrLipp
  • 15,526
  • 10
  • 75
  • 107
1

I don't think that this is possible or would worth it to develop this functionality.

ReadProperty.get() should return with null or throw an exception (an IllegalArgumentException, for example) if the key is not exist in the properties file and unit tests should check that clients of the ReadProperty works well (handles null return values properly, for example).

palacsint
  • 28,416
  • 10
  • 82
  • 109