编写自定义Lombok Annotation处理程序 [英] Writing custom Lombok Annotation handlers

查看:197
本文介绍了编写自定义Lombok Annotation处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写自定义Lombok Annotation处理程序。我知道 http://notatube.blogspot.de/2010/12/project-lombok-创建-custom.html 。但是当前的lombok jar文件不包含许多.class文件,而是包含名为.SCL.lombok的文件。

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 contain many .class files, but files named .SCL.lombok instead.

我发现,.SCL.lombok文件是.class文件,Lombok的构建脚本在生成jar文件时重命名它们,并且ShadowClassLoader 能够加载这些类 - 并且首字母缩略词SCL似乎来了由此。似乎这样做的原因只是避免使用基于SCL的jar污染任何项目的命名空间。IDE中的自动完成程序不会提出除实际公共API之外的任何内容。

I found, the .SCL.lombok files are the .class files, the build script of Lombok does rename them while generating the jar file, and the ShadowClassLoader is capable of loading these classes -- and the acronym SCL seems to come from this. It seems the reason for this is just to "Avoid contaminating the namespace of any project using an SCL-based jar. Autocompleters in IDEs will NOT suggest anything other than actual public API."

我只能通过


  • 解压缩lombok.jar的内容

  • 将.SCL.lombok文件重命名为.class

  • 将生成的目录添加到编译类路径

另外,为了能够使用我的自定义处理程序,我需要创建一个包含lombok类和我的自定义处理程序的新fat jar。自定义lombok类加载器本质上阻止在其他多个jar中添加自定义处理程序。

In addition, to be able to use my custom handler, I needed to create a new fat jar containing both the lombok classes and my custom handler. The custom lombok class loader essentially prevents adding custom handlers in other multiple jars.

这是扩展Lombok的唯一方法吗?或者我错过了什么?

Is this the only way to extend Lombok? Or am I missing something?

我正在使用以下版本脚本

I am using the following buildscript

apply plugin: 'java'

repositories {
    jcenter()
}

configurations {
    lombok
    compileOnly
}

def unpackedAndRenamedLombokDir = file("$buildDir/lombok")

task unpackAndRenameLombok {
    inputs.files configurations.lombok
    outputs.dir unpackedAndRenamedLombokDir
    doFirst {
        mkdir unpackedAndRenamedLombokDir
        delete unpackedAndRenamedLombokDir.listFiles()
    }
    doLast {
        copy {
            from zipTree(configurations.lombok.singleFile)
            into unpackedAndRenamedLombokDir
            rename "(.*)[.]SCL[.]lombok", '$1.class'
        }
    }
}

sourceSets {
    main {
        compileClasspath += configurations.compileOnly
        output.dir(unpackedAndRenamedLombokDir, builtBy: unpackAndRenameLombok)
    }
}

tasks.compileJava {
    dependsOn unpackAndRenameLombok
}

dependencies {
    compile files("${System.properties['java.home']}/../lib/tools.jar")
    compile "org.eclipse.jdt:org.eclipse.jdt.core:3.10.0"
    compile 'javax.inject:javax.inject:1'
    lombok 'org.projectlombok:lombok:1.16.6'

    compileOnly files(unpackedAndRenamedLombokDir)
}


推荐答案

在此期间,Reinier Zwitserloot创建了一个新的git-branch sclExpansionUpdate ,其中包含 ShadowClassLoader

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


ShadowClassLoader是现在更友好地试图扩展lombok。

ShadowClassLoader is now friendlier to trying to extend lombok.

你的(单独的)jar / dir应该有一个名为
META-INF / ShadowClassLoader的文件。该文件应包含字符串
'lombok'。如果你有,那个jar / dir中的任何类都将在与lombok类相同的空间中加载
。您还可以将类
文件重命名为.SCL.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.

我猜这个还没有进入主分支,因为它肯定没有经过那么多的测试 - 我只是为自己尝试了它,它包含一个小错误,阻止从扩展加载所需的META-INF /服务。要修复它,你应该用 inOwnBase 替换两个方法调用 partOfShadow

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<URL> sec = super.getResources(name);
while (sec.hasMoreElements()) {
    URL item = sec.nextElement();
    if (!inOwnBase(item, name)) vector.add(item); // <<-- HERE
}

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

I用上面的修复程序对它进行了测试,看起来效果很好(虽然测试不多)。

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

旁注:这个新的扩展机制现在终于也是可以在与lombok不同的命名空间中使用扩展注释处理程序和注释 - 很好!

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!

这篇关于编写自定义Lombok Annotation处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆