3

I want to create an auto-generated resource file similar to R from android. I know how to do the parsing and creation of the file and essentially creating a new class file. What I don't know how to do is start this auto-generation process.

So, using eclipse (although if there is a way to make this happen in an agnostic fashion, I would prefer it), how can I trigger an auto-generation session to read a properties file and create a .java file holding static variables and the "keys" from this parsed file, that I can then reference from my code?

ahodder
  • 11,353
  • 14
  • 71
  • 114
  • If you only want a way to trigger the generation after changing a resource (.properties file) you could use a ant-builder, which calls your generator. Right Mouse Click on Project -> Properties -> Builder -> New – Andreas Sep 11 '12 at 08:28

2 Answers2

2

A few examples of how to generate java source files have already been provided. It should be relatively easy to read the properties file and invoke one of these APIs.

To trigger the code generation process, you need to add a custom build step. With Ant, just add a custom task. And then hook it up to an Eclipse Builder: project Properties -> Builders -> New.

Subsequently, Eclipse should find and refresh this file on its own. If it doesn't, then check your configs: Preferences -> General -> Workspace -> find "Refresh using native hooks or polling" and similar ones and check them. (Note that I'm not 100% sure that this last part will work.)

The path of least resistance is to run this build step separately. If your properties file is not changing that often, then it shouldn't be that big a deal. This is similar to what you'd do if you use Protocol Buffers, JAXB, wsdl2java, etc. If you want everything to work magically like R.java does, then you probably have to do something a little more complicated:

 - Try to use Eclipse Builder options to control when the Ant task is executed - If you can't figure that out, then I'd check out how Eclipse hooks up to the above projects (that is, to Protocol Buffers, JAXB, wsdl2java, etc.) - Look at the ADT custom PreCompilerBuilder class - Check out the build-helper-plugin

Community
  • 1
  • 1
jtoberon
  • 8,706
  • 1
  • 35
  • 48
  • Thanks for this. Exactly what I needed. Gonna give the bounty a few more days to see if I can milk some more details for the future, but in all likely hood, it's yours. – ahodder Sep 11 '12 at 15:02
  • If ant works for you, then once you have the ant task add a builder to the eclipse project (project properties, builders, add the ant one). There are a lot of options for selecting the task and controlling when it is executed. In the past, I've been able to set this up such that a single directory was monitored for changes which then caused the ant task to run (was doing jsmin automagically). – philwb Sep 12 '12 at 14:37
  • You're right, I seem to have forgotten to include the info about Eclipse Builders. I added it above for completeness. – jtoberon Sep 12 '12 at 14:52
0

It is common to use a ResourceBundle to create an object that allows you to lookup properties by key. You can learn about the ResourceBundle on the Java Trail.

The basic idea is that you have a text file with a .properties extension. You point the ResourceBundle object to that file. You can then use the getString() or getObject() method passing in the key to the property you want. That is all there is to it. You just need to load the ResourceBundle when you start your program (or sometime before you need it).

If you create you own class that has a ResourceBundle as a member value, you can use a simple bit of code like this to have a simple get() method to get the property value:

    public String get(String aString)
    {        
    String sVal = null;

    try
    {
        sVal = (String)myProperties.getObject(aString);
    }
    catch (MissingResourceException e)
    {
        log.debug("Missing Property Value: "+aString);
    }

    return sVal;
    }

I hope that is useful.

Ruminator
  • 129
  • 3
  • 13
  • Thanks, but not what I'm looking for. I'm looking final non-instantiable class that will be auto-generated to contain the keys from a resource file that I may then auto-create a .java file. – ahodder Sep 07 '12 at 20:36
  • Sorry, I must have jumped the gun a bit when I saw that you wanted to read from a properties file. The best way to do that is with a `ResourceBundle`. – Ruminator Sep 09 '12 at 14:45