projectlombok / lombok

Very spicy additions to the Java programming language.
https://projectlombok.org/
Other
12.78k stars 2.37k forks source link

Tutorial request: "How to extend Lombok" #1068

Open dyorgio opened 8 years ago

dyorgio commented 8 years ago

Hello,

I already did a lombok extension with a new annotation and Handler. But it is just compatible with 1.14.x, because on 1.16.x There is no lombok.javac.JavacAnnotationHandler.class... just *.lombok files.

How can I extend lombok 1.16.x in another project?

Lombok extension is a good feature to support I think. Many developers want to make specific instrumentations and use official lombok distro too.

dyorgio commented 8 years ago

I made a ASTHelper class, using '$' as prefix to allow reserved words as method names... The final result appears to be very good, on my point of view :P:

From:

@RefGetter
private Ref<Concorrent> concorrent;

Code:

private JCBlock body(JavacNode fieldNode) {
    return $if($field(fieldNode).$neq().$null())
                .$then(
                    $field(fieldNode).$set($invokeStatic("com.googlecode.objectify.Ref", "create", $invoke(fieldNode.getName(), "get"))),
                    $return($invoke(fieldNode.getName(), "get"))
                ).$else()
                /*   */.$return().$null().toBlock();
}

Result:

public Concorrent getConcorrent() {
    if (concorrent != null){
        concorrent = Ref.create(concorrent.get());
        return concorrent.get();
    } else {
        return null;
    }
}
jiazhai commented 7 years ago

second @dyorgio . "Lombok extension is a good feature to support I think. Many developers want to make specific instrumentations and use official lombok distro too." Is there a plan to do this? :)

cloorc commented 7 years ago

I'm curious about this feature too. It's hard to find out how to fix this.

javatlacati commented 7 years ago

No error here, if you are not reusing lombok coer to "extend" lombok by creating your very own custom annotations leave them as they are otherwise do a custom build of the project to change the extension to .class to enable the classloader to load them.

weisebrazil commented 7 years ago

I used lombok in my projects, but I have other annotations processors and it will be better if I could merge all in one.

avrum commented 5 years ago

So any news regarding this one? Can i create my own annotation using the 1.16 version? I'm getting: Cannot resolve symbol 'HandleUtilityClass' While trying import lombok.javac.handlers.HandleUtilityClass;

Stephcraft commented 2 years ago

I made a ASTHelper class, using '$' as prefix to allow reserved words as method names... The final result appears to be very good, on my point of view :P:

From:

@RefGetter
private Ref<Concorrent> concorrent;

Code:

private JCBlock body(JavacNode fieldNode) {
    return $if($field(fieldNode).$neq().$null())
                .$then(
                    $field(fieldNode).$set($invokeStatic("com.googlecode.objectify.Ref", "create", $invoke(fieldNode.getName(), "get"))),
                    $return($invoke(fieldNode.getName(), "get"))
                ).$else()
                /*   */.$return().$null().toBlock();
}

Result:

public Concorrent getConcorrent() {
    if (concorrent != null){
        concorrent = Ref.create(concorrent.get());
        return concorrent.get();
    } else {
        return null;
    }
}

Where can I find your ASTHelper class?

dyorgio commented 2 years ago

Hello @Stephcraft here is all Handler classes: handler.zip

You need to replace Ref and @RefGetter (used for this example) with your cenario/classes/annotations.