2

I want to be able to process a java project pre-build so I can locate all annotations, such as @Getter, and replace them with an actual getter statement for the assigned field.

@Getter String s = "string";

I basically want to do what "Project Lombok" does, but I want to do it myself, to learn as well as simplify it for my needs.

The problem is, well, I don't know where to start. I have searched and have found nothing of any use, and have tried myself as well, with no luck. I would like it to run on eclipse build, so the jar is automatically correct. I also would like to create it so when I insert the annotation into a java file it doesn't show as an error, which I assume I can fix this with a dependency.

As far as experience goes, I know how to use and create annotations, but only for storing meta-data, and not being used as a placeholder, per-say.

If you have any tips or know of any resources to help me with this, I would greatly appreciate it.

P.S. I already looked through Project Lombok's source code, and it was too extensive to locate what needs to be done.

2
  • This Wikipedia article about Java annotations should be helpful: en.wikipedia.org/wiki/Java_annotation May 14, 2014 at 18:31
  • 1
    Puce's answer is right. Annotation processors are supposed to write a new file, not to modify an existing class. What Lombok does is pretty complicated and hard to understand, but only because there's no simple way.
    – maaartinus
    May 14, 2014 at 21:31

3 Answers 3

2

AFAIK Lombok does some AST manipulation in annotation processors (not 100% sure), which isn't officially supported by the specification. Thus it's more like a hack to overcome some shortcomings in the Java language.

1
  • That is 100% correct (for javac, Eclipse is a different story). Using annotation processors it is officially not possible to modify existing code. Full Disclosure: I am one of the Project Lombok core developers. May 15, 2014 at 7:42
0

You'll need to write a custom annotation processor.

Basically you'll be searching for your annotations and adding custom code to the resulting class file before the the final compilation step.

Your processor needs to implement javax.annotation.processing.Processor to handle your annotations. I've done this before for NetBeans and used a good article from a few years ago as a guide http://deors.wordpress.com/2011/10/08/annotation-processors/.

Good luck!

-1

It's very simple. First of all you need to attach lombok.jar to your project.
Right click your project & go to properties. Attach external jar/library. Now import the following:-

import lombok.Getter;
import lombok.Setter;
import lombok.AccessLevel;
// and so on....

Now you're able to use @Getter @Setter as follows:-

@Getter @Setter private Car car;

Now you're able to use getCar() & setCar(...)
Note:- You may need to enable annotation processing for NetBeans. Don't ask me how to do that. Just google it or visit lombok's site.

1
  • If you read my question you would see I was asking how to do this on my own, and used lombok as an example, rather than wanted to know how to use it...
    – user2426902
    May 14, 2014 at 19:24

Your Answer

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