Guillaume
Guillaume

Reputation: 5557

Java object graph visitor library

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:

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.

Upvotes: 11

Views: 5097

Answers (5)

Alain O'Dea
Alain O'Dea

Reputation: 21706

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.

Upvotes: 1

user456837
user456837

Reputation: 183

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

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

Upvotes: 2

Simon Thum
Simon Thum

Reputation: 594

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.

Upvotes: 2

Jatin
Jatin

Reputation: 709

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

Upvotes: 1

Adamski
Adamski

Reputation: 54715

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());
  }
}

Upvotes: 1

Related Questions