Re: Annotation processor
Antti wrote:
The annotation processor should modify Foo's source and insert/modify/
remove a constant field declaration containing the timestamp.
Clientcode would use reflection to find it out.
Modifications of inputs is not allowed during annotation processing.
Quote from the Filer documentation:
"In general, processors must not knowingly attempt to overwrite existing
files that were not generated by some processor. A Filer may reject
attempts to open a file corresponding to an existing type, like
java.lang.Object. Likewise, the invoker of the annotation processing
tool must not knowingly configure the tool such that the discovered
processors will attempt to overwrite existing files that were not generated.
Processors can indicate a source or class file is generated by including
an @Generated annotation."
So, maybe when you annotate your classes with @Generated it would work?
But, even if it would, it sounds to me like an annotations processing
rules abuse, so I discourage that.
Alternatively a runtime annotation, eg 'CompiledAt', tag could be
inserted/modified/removed.
RetentionPolicy.CLASS is enough, but post-processing of a generated
class files is required, because 'apt' cannot do that (compiler's output
is not an input for processor rounds, even when a processor is used with
'javac'). Post-processing of a compiled class files could be supported
with some bytecode manipulation framework, e.g. ObjectWeb ASM.
As a third alternative the annotation processor could completely
(re)generate a source containing a static Map<Class,Timestamp> and a
static getter to query for a Class' compilation timestamp.
Possible, but unnecessarily complicated IMHO. Easier version of that
approach would be (re)generation of a single meta-data resource (for
instance a compile-times.properties file with a simple
class-name=compile-time pairs), or separate meta-data resource for each
class.
Latter two solutions force client code dependent of another component,
however.
True, but that's the way (I think) annotation processing tools are
intended to operate -- by design the user input (a source code) is
read-only.
On the other hand I am not sure if messing with sources not
completely generated by apt process is a good idea either.
FWIW, I would do that simply transforming a class files (possibly only
these specially annotated) just after compilation. Consequently, all
Java standard tools for annotation processing seams to be useless in
your case to me.
HTH,
piotr