我正在寻找一种不使用 Groovy 的解决方案来解决 Java 过于冗长的问题
我喜欢 Groovy 如何将所有实例变量和类变量公开,并代表您编写 getter 和 setter。 这节省了大量不必要的编码。 另一方面,我的老板讨厌它,这让我非常难过:(
有没有一种方法可以在 Java 中实现 Groovy 的简洁性,而不必使用 Groovy 或其他框架/附加组件?
我有一种感觉我不会喜欢在我工作的地方引入这个外国框架,
所以我正在寻找一种解决方案,
可以在不使用 Groovy 或类似的东西的情况下解决 Java 的冗长问题 - 例如通过 Java 来完成 。只是将一切公开?
I like how Groovy makes all of your instance variables and class variables public and writes getters and setters on your behalf. This saves a lot of unnecessary coding. On the other hand, my boss hates it, making me very sad :(
Is there a way to achieve the conciseness of Groovy in Java without having to use Groovy or some other framework/add-on?
I have a feeling the powers that be will not take kindly to the introduction of this foreign framework where I work.
So what I'm looking for is a solution to the excessive verboseness of Java without using Groovy or something like it.
Can it be done in Java alone - such as by simply making everything public?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尝试我的 bean 注释:http://code.google.com/p/javadude/ wiki/Annotations
注释处理器会为您生成一个超类,其中包含您通常必须编写的许多典型样板文件:
Try out my bean annotations : http://code.google.com/p/javadude/wiki/Annotations
The annotation processor generates a superclass for you with lots of the typical boilerplate you'd normally have to write:
通过使用特定于领域的框架,您可以大大提高 Java 的有效抽象级别。 例如,当您编写 RESTful Web 服务时,Restlet 会处理大部分无聊的细节。 或者,如果您需要将 POJO 与 XML 或 JSON 进行映射,XStream 或 JAXB 等包会自动生成映射逻辑。 为了避免繁琐的 JDBC 编程,请尝试使用 iBatis 或 Hibernate 等持久性管理器。 Freemarker 和 Velocity 等模板系统使生成标记变得更加容易。
即使没有工具支持,您通常也可以找到方法将低级细节分解为通用方法和类,以便您的顶级代码感觉更加抽象。
(您可以考虑询问您的老板您是否可以评估 Groovy 在项目中的支持角色,例如编写一些单元测试。)
You can greatly increase Java's effective abstraction level by using domain-specific frameworks. For example, Restlet takes care of most of the boring details when you write RESTful web services. Or, if you need to map POJOs to and from XML or JSON, packages like XStream or JAXB automatically generate the mapping logic. To avoid tedious JDBC programming, try a persistence manager like iBatis or Hibernate. Templating systems like Freemarker and Velocity make it much easier to generate markup.
Even where there is no tool support, you can often find ways to factor out low level details into common methods and classes so that your top level code feels more abstract.
(You might consider asking your boss if you can evaluate Groovy in a supporting role on your project, for instance to write some of the unit tests.)
您可以使用 Groovy 中的任何标准访问修饰符或类型声明来标记任何属性或方法。
请记住,Groovy 是 Java 的超集。
You can label any property or method with any of the standard access modifiers or type declarations in Groovy.
Remember, Groovy is a superset of Java.
Java 语言就是这样。 如果不迁移到新语言(例如 groovy),您不会发现对核心语言进行太多更改。 获得管理层的支持可能并不像您想象的那样难。 几年前我的处境和你类似。 您可以开始将 Groovy 引入软件的一些有意义的区域。
一个很好的起点是开始使用它进行单元测试。 本质上,当您用 groovy 编写一个类时,它会编译为 Java 字节码。 只要您的类路径上有 groovy jar,其他人可能甚至不知道它是 groovy 代码。 通过使用它进行测试,您可以演示它如何简化您的代码并使事情变得更容易。 您应该能够将其出售给管理层。
从那里开始,继续在其他新领域引入它,动态语言可以为您节省一些工作。 Groovy 和 Java 可以很好地混合在一起,因为它们使用同一组核心库。
The Java language is what it is. You won't find much to change the core language without moving to a new language (like groovy). Getting buy-in from management might not be as hard as you think to move to groovy. I was in a similar position as you a few years ago. You can start introducing groovy into a few areas of your software that make sense.
A great place to start is to start using it for unit testing. Essentially when you write a class in groovy, it compiles down to Java bytecode. As long as you have the groovy jars on your classpath, others may not even know it is groovy code. By using it for testing, you can demonstrate how it simplifies your code and makes things easier. You should be able to sell that to management.
From there, keep introducing it in other new areas where a dynamic language would save you some work. Groovy and Java mix very well together since they use the same set of core libraries.
在这种情况下,一个可以为您编写 getter 和 setter 的 IDE 可能会成为您的朋友。
我发现编写 getter 和 setter 是编写新类中最重复且最无趣的部分。 使用 Eclipse 这样的 IDE,可以 自动为您生成 getter 和 setter,它可以减少编写冗余代码的痛苦。
当然,它不会减少使用
getX
和setX
方法的需要,但至少它使类的实现变得更容易一些并且减少了一些麻烦。负担。In this kind of situation, an IDE which can write the getters and setters for you can be your friend.
I find that having to write the getters and setters are the most repetitive and un-interesting part of writing new classes. With an IDE like Eclipse, which can automatically generate the getters and setters for you, it can reduce the pain of writing the redundant code.
Of course, it won't alleviate the need to use the
getX
andsetX
methods, but at least it makes the implementation of the classes a little bit easier and less of a burden.使用不可变的数据结构。 没有吸气剂,没有吸气剂,没有麻烦。
您可能想尝试一下 Functional Java。 它只是一个常规的 Java 库,但具有强大的抽象和有用的(不可变的)数据结构,让您用更少的代码表达更多内容。 例如,将以下内容翻译为:
... 为:
Go with immutable datastructures. No getters, no setters, no hassle.
You may want to give Functional Java a try. It's just a regular Java library, but comes with powerful abstractions and useful (immutable) datastructures that let you say more in less code. For example, translate this:
... to this:
您可以使用 AOP 来帮助消除一些冗长的内容。
例如,如果您拥有房产:
字符串名称;
然后您可以定义:
@GetterSetter
公共字符串名称(字符串名称){};
然后有一个方面来查找注释,并在其上放置一个注释,以便任何读取(即当参数为空时)或写入(参数不为空)完成后,它将采取适当的操作。
这很容易编写,但是您必须获得 AOP 的支持,这可能具有挑战性。
但是,正如其他人提到的,Eclipse 和 Netbeans 将轻松创建 getter/setter,因此如果您不必对它们做任何特殊的事情,那么它应该非常容易。
我倾向于将我的属性放在自己的类中,这只是 getters/setters,然后我不必查看详细信息,以及 equals 和 hash 方法(如果需要),然后在我的其他类中我只需使用它,并隐藏无聊的额外编码。
You could use AOP to help take out some of the verboseness.
For example, if you have a property:
string name;
You could then define:
@GetterSetter
public String Name(String name) { };
Then have an aspect that looks for the annotation, and put an around on it, so that any reading, which is when the parameter is null, or writing (parameter isn't null) is done then it will take the appropriate action.
This would be pretty easy to write, but then you have to get buy-in for AOP, which may be challenging.
But, as others mentioned, Eclipse and Netbeans will easily create the getters/setters, so if you don't have to do anything special with them then it should be pretty easy.
I tend to put my properties in their own class, which is just getters/setters, then I don't have to look at the verboseness, as well as equals and hash methods if needed, then in my other classes I just use that, and hide the boring extra coding.
除了针对特定任务的更有效的框架(JPA、Swing 等)之外,我不知道有什么可以使源代码更小。
此类功能通常来自您的 IDE。
我使用 Netbeans,它具有编写 getters/setters、自动实现抽象方法和各种其他快捷方式的工具。 当然,源代码仍然很冗长。
I'm not aware of anything to make your source code smaller, apart from more effective frameworks for specific tasks (JPA, Swing, etc.).
That kind if functionality usually comes from your IDE.
I use Netbeans which has tools to write getters/setters, auto implement abstract methods and various other shortcuts. Of course the source code is still just as verbose.