Writing custom Lombok Annotation handlers

后端 未结 2 681
萌比男神i
萌比男神i 2020-12-13 18:57

I want to write custom Lombok Annotation handlers. I know http://notatube.blogspot.de/2010/12/project-lombok-creating-custom.html. But the current lombok jar file does not c

2条回答
  •  误落风尘
    2020-12-13 19:28

    In the meantime Reinier Zwitserloot created a new git-branch sclExpansionUpdate, that contains an updated version of the ShadowClassLoader:

    ShadowClassLoader is now friendlier to trying to extend lombok.

    Your (separate) jar/dir should have a file named META-INF/ShadowClassLoader. This file should contain the string 'lombok'. If you have that, any classes in that jar/dir will be loaded in the same space as lombok classes. You can also rename the class files to .SCL.lombok to avoid other loaders from finding them.

    I guess this did not yet make it into the main branch because it certainly has not been tested that much - I just tried it out for myself and it contains a little bug that prevents loading the required META-INF/services from extensions. To fix it you should replace two method calls to partOfShadow with inOwnBase:

    [... line 443]
    Enumeration sec = super.getResources(name);
    while (sec.hasMoreElements()) {
        URL item = sec.nextElement();
        if (!inOwnBase(item, name)) vector.add(item); // <<-- HERE
    }
    
    if (altName != null) {
        Enumeration tern = super.getResources(altName);
        while (tern.hasMoreElements()) {
            URL item = tern.nextElement();
            if (!inOwnBase(item, altName)) vector.add(item); // <<-- AND HERE
        }
    }
    

    I tested it with the above fix and it seems to work fine (not tested much though).

    On a side note: with this new extension mechanism, it is now finally also possible to have the extensions annotation handlers and annotations in a different namespace than "lombok" - nice!

提交回复
热议问题