从生成的二进制文件中剥离ScalaSignature批注 [英] Strip ScalaSignature annotation from resulting binaries
问题描述
如何以编程方式删除java
或scala
注释?
How can you strip java
or scala
annotations programmatically?
当前项目的可交付成果之一是我的代码,以及我的代码所依赖的所有依赖项,它们都是uber-jar.我们正在构建一个SDK,因此需要重命名所有包含的依赖项,以免干扰SDK客户端的依赖项(这意味着如果我们使用的是apache commons版本X,而他们使用的是版本Y,则没有冲突).我们使用sbt-assembly
重命名了代码所依赖的包.例如org.apache.*-> com.mycompany.org.apache.*
One of the deliverables for my current project is my code, plus whatever dependencies my code relies on, as an uber-jar. We're building an SDK therefore any of the included dependencies need to be renamed as to not interfere with the SDK client's dependencies (meaning if we're using apache commons version X, and they're using version Y, there aren't any conflicts). We used sbt-assembly
to rename the packages our code relies on. For example org.apache.* -> com.mycompany.org.apache.*
我们的项目主要是用scala
编写的,并且正在使用sbt
进行构建.最近,我确定阴影会导致ScalaSignature
注释出现问题.当在客户的项目中使用uber-jar时,这又会导致构建问题.
Our project is largely written in scala
and we're using sbt
to build. Recently I determined that shading causes problems with the ScalaSignature
annotation. This in turn causes build problems when the uber-jar is used in the client's project.
我已确定删除ScalaSignature
可以解决此问题.我使用ProGuard
来执行此操作,但是该工具对于我们要实现的目标来说是过大了.我只是想知道是否有人找到了可以删除指定注释的工具.甚至更好的是,该工具将在scalac
满意的情况下保留ScalaSignature
的同时重命名程序包.
I have determined that removing ScalaSignature
resolves the problem. I used ProGuard
to do this, however that tool is overkill for what we are trying to achieve. I'm just curious if anyone out there has found a tool that can strip specified annotations. Or even better, a tool that will rename the packages while preserving ScalaSignature
in a way that scalac
is happy with.
推荐答案
You might want to check JavaAssist library, which clearly allows to get/add annotations dynamically.
接着,从理论上讲,应该可以使用
Following that, it should be theoretically possible to use AnnotationsAttribute
in the following way (haven't tried myself but let us know if the below has worked):
MethodInfo minfo = m.getMethodInfo();
AnnotationsAttribute attr = (AnnotationsAttribute)
minfo.getAttribute(AnnotationsAttribute.invisibleTag);
Annotation[] annotations = attr.getAnnotations();
// ...remove the unnecessary annotation from array...
attr.setAnnotations(annotations);
这篇关于从生成的二进制文件中剥离ScalaSignature批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!