2

I want to do some annotation processing (serialization):

What I want:

  • I dont want to subclass the classes in question
  • I want to insert a method into an existing class
  • I want to use ASM to add these methods
  • I dont want to generate them by hand, but automatically on compilation

What I have:

  • An annotation :)
  • The code to scan and modify a .class file with ASM

The problem:

  • I don't know when to process the .class files
  • As far as I know, the AbstractProcessor approach only allows creating new source files
  • With ASM, I modify .class files, but how can the compiler compile the .java files when the method still needs to be created by me?

Ideas:

  • Right now, the methods to be added are defined by an interface, but using a superclass, I could have a do-nothing implementation, which I can override in post. However, this robs a lot of flexibility, and still, I don't know how to compile it with javac in one step...

Thank you in advance for any tips, suggestions and solutions,
Till

4
  • Do the source files you have try to reference the nonexistent method? Then it's impossible to compile them without modifying the source or using a compiler plugin. Commented May 12, 2013 at 20:07
  • The problem is that the source can't be modified with standard apt annotation processing... Commented May 12, 2013 at 20:23
  • In Java 8 the collection classes get extended by lamda functionality and also for that purpose interfaces can have functions with default implementations. Commented May 12, 2013 at 20:56
  • Yeah, I love Java 8, it gets some nice features Scala had for a long time, but Android (Harmony) doesnt get them (I think), so we'll be stuck with plain old Java 6 :( Commented May 12, 2013 at 21:02

1 Answer 1

2

Project Lombok seems to modify binaries through an annotation processor. There is also a pretty good post explaining how it works here.

Project Lombok hooks itself into the compilation process as an annotation processor. But Lombok is not your normal annotation processor. Normally, annotation processors only generate new source files whereas Lombok modifies existing classes.

I don't think it is trivial to modify class files through a 'normal' annotation processor, but to quote the post:

The "hack" in Lombok is that, strictly speaking, the annotation processing spec doesn't allow you to modify existing classes. The annotation processing API doesn't provide a mechanism for changing the AST of a class. The clever people at Project Lombok got around this through some unpublished APIs of javac. Since Eclipse uses an internal compiler, Lombok also needs access to internal APIs of the Eclipse compiler.

The disadvantage of using these non-standard annotation processing APIs is that each annotation processor implementation (Javac, Eclipse, others?) needs to be dealt with differently, and they can break across versions because they are non-standard.

If you really want to do this as an annotation processor, then check out the Lombok source code and see how they do it. However, if it is possible to do it another way (post-compilation step, agent with a class file transformer) then one of these other approaches would be more portable.

Sign up to request clarification or add additional context in comments.

2 Comments

The problem is that with a post-compilation step, its not out of the box, like with standard apt annotation processing...
I guess a class file transformation won't work with the .dex format on android... Otherwise, I'd have accepted that runtime cost.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.