简体   繁体   English

Sun Codemodel的自定义格式化程序

[英]Custom formatter for Sun Codemodel

I'm using Java Sun Codemodel to generate java classes on the fly, and it works pretty well for me. 我正在使用Java Sun Codemodel即时生成Java类,它对我来说很好用。

Now I have an additional challenge: How do I control how the code is formatted? 现在,我还有一个挑战:如何控制代码的格式? Is there any way at all? 有什么办法吗? In particular, I'd like to have all curly brackets in a new line and would like to extend the line length to let's say 130 chars. 特别是,我想将所有花括号放在换行符中,并希望将行长扩展为130个字符。

Eg, we have a formatter that we use in Eclipse. 例如,我们有一个在Eclipse中使用的格式化程序。 There's no way I can just tell Codemodel to use that somehow, right? 我无法告诉Codemodel以某种方式使用它,对吗?

There's this JFormatter class that kind of looks like it could be useful for something, but it doesn't look like it can do more sophisticated stuff like that. 有一个JFormatter类,它看起来可能对某些东西很有用,但看起来却不能完成类似的工作。

JFormatter declared as final, so not, it isn't possible. JFormatter声明为final,所以不可能。

There's only two ways to solve the problem and continue using codemodel. 只有两种方法可以解决问题并继续使用代码模型。

  1. Process generated code with some another tool/library. 使用其他工具/库处理生成的代码。
  2. Redefine library classes. 重新定义库类。 Find which concrete classes does unwanted formatting, then create classes with same package/name in your project and write your own implementation. 查找哪些具体类进行了不必要的格式化,然后在项目中创建具有相同包/名称的类,并编写自己的实现。 There classes must appear in classpath earlier than codemodel jar. 那里的类​​必须早于代码模型jar出现在类路径中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM