为什么 Java 元编程支持不是更好?

发布于 2024-12-24 17:44:26 字数 622 浏览 1 评论 0原文

我总是问自己这个问题,特别是当我看到 JPA 2.0 元模型如何工作时...

例如,在 JPA 2.0 中,我们可以使用处理器,为实体 Entity 创建一个元模型类 Entity_

然后在 JPA 2.0 Criteria 中就可以实现api 使用此元模型来具有强类型标准。

例如,您可以编写:

criteriaBuilder.equal(u.get(User_.username), username);

而不是使用字段名作为字符串“用户名”。

我只是想知道为什么它不在 Java 中本地处理,而无需处理处理器和一些额外的元数据类。

它不仅适用于 JPA,还可以利用反射 api。

那么有什么原因导致我们不能直接在java类中访问元数据呢? 有人可以提供这样一个功能的缺点吗? 我想这可能是封装的问题,不是吗?


有关 JPA2 元模型的更多信息: http://www.inze.be/andries/2010/ 09/19/jpa2-元模型-示例/

I've always asked myself this question, particularly when i see how JPA 2.0 metamodel works...

For exemple in JPA 2.0 we can, with a processor, for an entity Entity, create a metamodel class Entity_

It's then possible in JPA 2.0 Criteria api to use this metamodel to have strongly typed criterias.

For exemple, you would write:

criteriaBuilder.equal(u.get(User_.username), username);

Instead of using the fieldname as string "username".

I just wonder why it's not handled natively in Java, without having to deal with a processor and a couple of extra metadata classes.

It's not just for JPA, it could leverage too the reflection api.

So are there reasons why we can't access the metadata directly in the java classes?
Can someone provide a drawback of having such a feature?
I guess it may be a problem for encapsulation no?


For more infos about JPA2 metamodel:
http://www.inze.be/andries/2010/09/19/jpa2-metamodel-example/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

客…行舟 2024-12-31 17:44:26

Java 7 不支持方法或字段/属性作为第一类对象。没有添加这些的原因是为了保持语言简单。

然而,在我看来,Oracle 采取了不同的观点(至少对于方法),您将能够在 Java 8 中拥有方法引用,并且可能使用字段/属性引用的 JSR 之一将被接受并合并到 Java 8 中或 9.


目前支持一些关于如何处理方法的文章。 Java 8 将为该功能添加语法糖。

http://blogs.oracle.com/jrose/entry/method_handles_in_a_nutshell

http://java.sun.com/developer/technicalArticles/DynTypeLang/

http://www.java7developer.com/blog/?p=191

Java 7 doesn't support methods or fields/properties as first class objects. The reason these haven't been added is to keep the language simple.

However, it appears to me that Oracle has taken a different view (at least for methods) and you will be able to have method references in Java 8 and possibly one of the JSRs using field/property references will be accepted and merged into Java 8 or 9.


Some articles on how method handles are currently support. Java 8 will add syntactic sugar for this functionality.

http://blogs.oracle.com/jrose/entry/method_handles_in_a_nutshell

http://java.sun.com/developer/technicalArticles/DynTypeLang/

http://www.java7developer.com/blog/?p=191

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文