BigDecimal符号eclipse插件或漂亮的外部工具 [英] BigDecimal notation eclipse plugin or nice external tool

查看:157
本文介绍了BigDecimal符号eclipse插件或漂亮的外部工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用BigDecimal进行很多操作,我发现不得不表达

  Double a = b  -  c * d; //自然方式

as

  BigDecimal a = b.subtract(c.multiply(d))// BigDecimal方式

不仅丑陋,而且是我和业务分析师之间的错误和沟通问题的根源。他们完全可以用双音节读取代码,但现在他们不能。



当然,一个完美的解决方案将是java支持操作符重载,但由于这不会要发生,我正在寻找一个eclipse插件,甚至一个外部工具,从自然的方式到bigdecimal自动转换。



我是尝试预处理源代码或动态翻译或任何复杂的东西,我只想要一些可以输入文本和获取文本的东西,并将自然的方式作为源代码中的注释。



PS:我发现这个令人难以置信的智能黑客,但我不想开始进行字节码操作。也许我可以使用它来创建一个Natural2BigDecimal翻译器,但如果有人已经完成了这样的工具,我不想重新发明。



我不想切换到Scala / Groovy / JavaScript,我也不能,公司规则禁止任何东西,但服务器端代码中的java。

解决方案

我不是要处理源代码...我只是想要一些我可以输入[bigDecimal算术表达式]文本。



解决问题的一半是承认问题是什么。您完全想要预处理您的BigDecimal表达式以产生合法的Java。



您只有两个基本选择:




  • 独立的域特定语言和DSL编译器,接受标准表达式,并将它们直接转换为Java代码。 (这是一种预处理器)。这会让您有保留所有表达式片段的问题,并以某种方式知道将其放在Java代码中的位置。


  • 读取Java源文本的工具,查找这些表达式,并将其转换为文本中的BigDecimal。我会提出一些让你在实际代码之外编写表达式并插入翻译的东西。 (b)其他答案可能(从另一个答案中偷走):




$ p> // BigDecimal a = b - c * d;
BigDecimal a = b.subtract(c.multiply(d));

意思是将注释中的大十进制表达式编译成其java等价物,并替换以下内容



要实现第二个想法,您需要一个程序转换系统,它可以应用源到源重写规则来转换(生成转换的特殊情况)代码,这只是一个预处理器,被组织为可根据您的需要自定义。



我们的 DMS软件重组工具包及其 Java Front End 可以执行此操作。您需要一个完整的Java解析器来完成该转换部分;您将需要名称和类型解析,以便您可以解析/检查提议的e为了理智而表现出来。



虽然我同意原来的Java符号是丑陋的,你的建议会让它更漂亮,但我个人的看法是不值得的努力。你最终依赖于一个复杂的工具(是的,DMS是复杂的:操纵代码并不容易)获得一个相当小的收益。



如果您和您的团队撰写了数千种这些公式,或者这些公式的作者是Java天真的,这可能是有道理的。在这种情况下,
我会更进一步,只是坚持你写你需要的标准表达式格式。您可以自定义Java前端以检测操作数类型为十进制类型,并为您重写。然后,您可以在每个Java编译步骤之前运行此预处理程序。


I need to make a lot of operations using BigDecimal, and I found having to express

Double a = b - c * d; //natural way

as

BigDecimal a = b.subtract(c.multiply(d))//BigDecimal way

is not only ugly, but a source of mistakes and communication problems between me and business analysts. They were perfectly able to read code with Doubles, but now they can't.

Of course a perfect solution will be java support for operator overloading, but since this not going to happen, I'm looking for an eclipse plugin or even an external tool that make an automatic conversion from "natural way" to "bigdecimal way".

I'm not trying to preprocess source code or dynamic translation or any complex thing, I just want something I can input text and get text, and keep the "natural way" as a comment in source code.

P.S.: I've found this incredible smart hack but I don't want to start doing bytecode manipulation. Maybe I can use that to create a Natural2BigDecimal translator, but I don't want to reinvent the wheel if someone has already done such a tool.

I don't want to switch to Scala/Groovy/JavaScript and I also can't, company rules forbid anything but java in server side code.

解决方案

"I'm not trying to preprocess source code ... I just want something I can input [bigDecimal arithmetic expression] text".

Half of solving a problem is recognizing the problem for what it is. You exactly want something to preprocess your BigDecimal expressions to produce legal Java.

You have only two basic choices:

  • A stand-alone "domain specific language" and DSL compiler that accepts "standard" expressions and converts them directly to Java code. (This is one kind of preprocessor). This leaves you with the problem of keeping all the expression fragments around, and somehow knowing where to put them in the Java code.

  • A tool that reads the Java source text, finds such expressions, and converts them to BigDecimal in the text. I'd suggest something that let you code the expressions outside the actual code and inserted the translation.

Perhaps (stolen from another answer):

 // BigDecimal a = b - c * d;
 BigDecimal a = b.subtract( c.multiply( d ) );

with the meaning "compile the big decimal expression in the comment into its java equivalent, and replace the following statement with that translation.

To implement the second idea, you need a program transformation system, which can apply source-to-source rewriting rules to transforms (generate as a special case of transform) the code. This is just a preprocessor that is organized to be customizable to your needs.

Our DMS Software Reengineering Toolkit with its Java Front End could do this. You need a full Java parser to do that transformation part; you'll want name and type resolution so that you can parse/check the proposed expression for sanity.

While I agree that the as-is Java notation is ugly, and your proposal would make it prettier, my personal opinion is this isn't worth the effort. You end up with a dependency on a complex tool (yes, DMS is complex: manipulating code isn't easy) for a rather marginal gain.

If you and your team wrote thousands of these formulas, or the writers of such formulas were Java-naive it might make sense. In that case, I'd go further, and simply insist you write the standard expression format where you need it. You could customize the Java Front End to detect when the operand types were of decimal type, and do the rewriting for you. Then you simply run this preprocessor before every Java compilation step.

这篇关于BigDecimal符号eclipse插件或漂亮的外部工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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