从生成的二进制文件中剥离ScalaSignature批注 [英] Strip ScalaSignature annotation from resulting binaries

查看:136
本文介绍了从生成的二进制文件中剥离ScalaSignature批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式删除javascala注释?

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.

推荐答案

您可能要检查JavaAssist库,该库

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屋!

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