0

Rewriting this for greater clarity.

R.java is a great resource in Android. It's basically cheating, by dynamically changing at compile time to provide access to all of your resources.

It would be awesome to have something like this available for applications running core Java. However, I understand that R.java does some crazy hackery to make this work.

So, here are my goals. If anyone has any ideas to point me in the right direction, that would be great.

1) Public static fields that link to resources defined by the developer.

2) The fields are created dynamically, ie they are not hard-coded into existence.

3) The dynamic creation occurs at compile time, so the following statement would be accepted by the compiler: MyRJava.resourceName.

Is this even remotely possible? Where would I start looking to see if it can be done?

craigmiller160
  • 5,751
  • 9
  • 41
  • 75

2 Answers2

1

Java doesn't support this, but Groovy does. As a Java developer, you should have no problem picking up Groovy for something like this. One of the nice things about Groovy is that it coexists very well with Java code. Where I work, we frequently go back and forth between the two languages.

Community
  • 1
  • 1
Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
0

Remotely possible. We were successfully using APT and Maven to set up a compile time goal which generates additional Java classes. This however not a real dynamic class generation, as we're doing everything in compile time.

The advantage is that after saving (and the code generator has run), you can use all the genarated fields normally (i.e. in autocomplete, and so on).

It is also possible to create something similar in runtime, i.e. you can create a class file, compile it, and then get access to its fields and methods via reflection. In this case however accessing to the genrated fields are more problematic - and finally you'd better go with a simple Map to hold "resource ids" like R.java.

Gee Bee
  • 1,794
  • 15
  • 17