Java 对象图访问者库

发布于 2024-09-11 18:31:59 字数 371 浏览 5 评论 0原文

你知道一个好的java对象图访问者库吗?

我想访问一个对象及其子组件,并在某些条件匹配时执行一些操作。

示例用法:

  • 在一个巨大的域对象图上,重置 每个 id 设为 null
  • 在巨大的域对象图上将 ,将每个 Set 替换为 TreeSet 包含相同内容的实例 元素。

我想要一个库,而不是自定义代码,因为遍历对象图可能很棘手。你必须处理集合、数组、代理等等...... 我曾考虑过重用 XStream 的一部分来实现这一点,但这看起来并不那么容易:Xstream 访问者更面向对象转换而不是对象自我修改。

Do you know a good java object graph visitor library?

I want to visit an object and its sub components and perform some actions when some conditions are matched.

Example usage:

  • on a huge domain object graph, reset
    each id to null
  • on a huge domain object graph, replace each Set with a TreeSet
    instance containing the same
    elements.

I want a library, not custom code because traversing an Object graph can be tricky. You have to handle collections, arrays, proxies, and so on...
I have think about reuse part of XStream to achieve this, but it doesn't look so easy: Xstream visitor is more oriented on object transformation than object self modification.

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

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

发布评论

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

评论(5

作业与我同在 2024-09-18 18:31:59

我一直在寻找同样的东西,并找到了这个。

http://code.google.com/p/behaim/

I've been looking for the same thing, and found this.

http://code.google.com/p/behaim/

心如荒岛 2024-09-18 18:31:59

恰巧,我也做过这样的事。并不是真正的图书馆,但它可以很容易地发展成为一个图书馆。

但我偶然发现了这个,因为我正在寻找更好的东西。我不能透露它,而且它肯定还没有达到我会这样做的状态,但也许这样的东西应该作为操作系统存在?

我所拥有的让我可以逐个实例地遍历和修改类型安全的对象图,可以选择复制它以使原始对象保持不变。顺便说一句,Java。同样有效的是掌握图表中的关系(边缘,如果你愿意的话)。

我可以想象的是操作(例如修改、扩展、复制、折叠、遍历)和相应实现的明确定义。诸如识别子图之类的正交方面将被适当地排除。

任何对这样的项目感兴趣的人请回复,也许我们可以开始一些事情。

As it happens, I have done such a thing. Not really a library, but it could grow into one easily.

But I stumbled upon this since I was looking for something better. I can't give it out, and it definetely isn't yet in a state where I would do this, but maybe such a thing should come to live as OS?

What I have lets me traverse and modify an object graph type-safe, instance-by-instance, optionally duplicating it such that the original remains untouched. Java BTW. What also works a bit is grasping relationships in the graph (the edges, if you want).

What I could envision is a clear-cut definition of operations (such as modify, extend, duplicate, collapse, traverse) and respective implemetations. Orthogonal aspects such as identifyig subgraphs would be properly factored out.

Anyone interested in such a project please respond, maybe we can get something started.

阪姬 2024-09-18 18:31:59

为什么需要一个图书馆才能做到这一点?

假设您指定这是一个域对象图,那么为什么不定义和实现相关接口以允许不同的访问者实现访问您的域对象呢?其中一种实现可以(根据您的指定)将每个 ID 重置为 null

示例

首先定义可访问或充当访问者的对象要实现的接口。

public interface Visitable {
  void visit(Visitor visitor);
}

public interface Visitor {
  void visitDomainObjectA(DomainObjectA obj);
  void visitDomainObjectB(DomainObjectB obj);
}

现在定义两个域对象类,两者都可以访问。

public abstract class DomainObject implements Visitable {
  private Object id;

  public Object getId() { return this.id; }
  public void setId(Object id) { this.id = id; }
}

public class DomainObjectA extends DomainObject {
  public void visit(Visitor visitor) {
    visitor.visitDomainObjectA(this);
  }
}

public class DomainObjectB extends DomainObject {
  public void visit(Visitor visitor) {
    visitor.visitDomainObjectB(this);
  }
}

现在定义一个具体的 Visitor 实现来执行一些有用的操作:

public class MyVisitor implements Visitor {
  public void visitDomainObjectA(DomainObjectA doa) {
    doa.setId(null);
  }

  public void visitDomainObjectB(DomainObjectB dob) {
    doa.setId(UUID.randomUUID());
  }
}

Why do you need a library in order to do that?

Given that you specify this is a domain object graph then why not define and implement relevant interfaces to allow your domain objects to be visited by different visitor implementations? One of the implementations could (as you specify) reset each ID to null.

Example

First define the interfaces to be implemented by objects that can be visited or act as visitors.

public interface Visitable {
  void visit(Visitor visitor);
}

public interface Visitor {
  void visitDomainObjectA(DomainObjectA obj);
  void visitDomainObjectB(DomainObjectB obj);
}

Now define two domain object classes, both of which can be visited.

public abstract class DomainObject implements Visitable {
  private Object id;

  public Object getId() { return this.id; }
  public void setId(Object id) { this.id = id; }
}

public class DomainObjectA extends DomainObject {
  public void visit(Visitor visitor) {
    visitor.visitDomainObjectA(this);
  }
}

public class DomainObjectB extends DomainObject {
  public void visit(Visitor visitor) {
    visitor.visitDomainObjectB(this);
  }
}

Now define a concrete Visitor implementation that does something useful:

public class MyVisitor implements Visitor {
  public void visitDomainObjectA(DomainObjectA doa) {
    doa.setId(null);
  }

  public void visitDomainObjectB(DomainObjectB dob) {
    doa.setId(UUID.randomUUID());
  }
}
浊酒尽余欢 2024-09-18 18:31:59

将对象图编组为 XML 并使用一些标准 XML 处理/操作库怎么样?

How about marshalling your object graph into XML and using some standard XML handling/manipulation library?

不如归去 2024-09-18 18:31:59

可能值得尝试像 Neo4jTitanDB.它将让您通过使用查询来横切数据集并探索关系来影响访问。

它们都具有广泛的 Java API 来促进数据加载和查询。

It might be worth trying an graph database like Neo4j or TitanDB. It will let you affect visitation by using queries to cross-cut your data set and explore relationships.

Both of these have extensive Java APIs to facilitate data loading and querying.

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