Search code examples
javagremlintinkerpop

How to type this Gremlin response in Java?


I want to do the following gremlin query:

results = traversal.path().by(valueMap()).toList();

I would like to take these objects and ship them back to a strongly typed API. To do that I have to do some unwinding on the gremlin side of things. But first I need a type for the object.

So... what's the return type here? Can I get the return type without casting to Object? Is there any way to inspect this in Java at runtime or something or is Java just unusable as I always suspected?

The IDE offers no suggestion and is giving me a scary warning:

Unchecked assignment: 'java.util.List' to 'java.util.List<java.util.List<java.util.Map<java.lang.String,java.lang.Object>>>'. ** this is what I'm trying for now** Reason: 'traversal.path().by(valueMap())' has raw type, so result of toList is erased

Any ideas?


Solution

  • You can get the result type of the path() step from:

    gremlin> g.V().path().by(valueMap()).next().getClass()
    ==>class org.apache.tinkerpop.gremlin.process.traversal.step.util.MutablePath
    

    You can find the corresponding interface at: https://tinkerpop.apache.org/javadocs/current/full/org/apache/tinkerpop/gremlin/process/traversal/step/util/MutablePath.html

    So, in terms of interfaces the result type is:

    List<Path<Map<String, Object>>>
    

    If you want the result type in terms of actual classes, you can do:

    gremlin> g.V().path().by(valueMap()).toList().getClass()
    ==>class java.util.ArrayList
    gremlin> g.V().valueMap().next().getClass()
    ==>class java.util.LinkedHashMap
    

    and you get:

    ArrayList<MutablePath<LinkedHashMap<String, Object>>>