private String argAsString(final Object arg) {
        if (arg == null)
            return "null";

        if (arg instanceof String)
            return String.format("\"%s\"", StringEscapeUtils.escapeJava((String) arg));
        if (arg instanceof Boolean)
            return arg.toString();

        if (arg instanceof Byte)
            return String.format("%sB", arg);
        if (arg instanceof Short)
            return String.format("%sS", arg);
        if (arg instanceof Integer)
            return arg.toString();
        if (arg instanceof Long)
            return String.format("%sL", arg);

        if (arg instanceof BigInteger)
            return String.format("%sN", arg);
        if (arg instanceof Float) {
            if (NumberHelper.isNaN(arg))
                return "NaN";
            if (NumberHelper.isPositiveInfinity(arg))
                return "+Infinity";
            if (NumberHelper.isNegativeInfinity(arg))
                return "-Infinity";

            return String.format("%sF", arg);
        }
        if (arg instanceof Double) {
            if (NumberHelper.isNaN(arg))
                return "NaN";
            if (NumberHelper.isPositiveInfinity(arg))
                return "+Infinity";
            if (NumberHelper.isNegativeInfinity(arg))
                return "-Infinity";
            return String.format("%sD", arg);
        }
        if (arg instanceof BigDecimal)
            return String.format("%sM", arg);

        if (arg instanceof OffsetDateTime)
            return String.format("datetime(\"%s\")", format(((OffsetDateTime) arg).toInstant()));

        if (arg instanceof Enum) {
            // special handling for enums with additional interfaces
            if (arg instanceof T)
                return String.format("T.%s", arg);
            if (arg instanceof Order)
                return String.format("Order.%s", arg);
            if (arg instanceof Column)
                return String.format("Column.%s", arg);
            if (arg instanceof Operator)
                return String.format("Operator.%s", arg);
            if (arg instanceof SackFunctions.Barrier)
                return String.format("Barrier.%s", arg);

            return String.format("%s.%s", arg.getClass().getSimpleName(), arg);
        }

        if (arg instanceof Vertex)
            return String.format("new ReferenceVertex(%s,\"%s\")", argAsString(((Vertex) arg).id()), ((Vertex) arg).label());

        if (arg instanceof P) {
            return asString((P<?>) arg);
        }

        if (arg instanceof GremlinLang || arg instanceof DefaultTraversal) {
            final GremlinLang gremlinLang = arg instanceof GremlinLang ? (GremlinLang) arg : ((DefaultTraversal) arg).getGremlinLang();
            parameters.putAll(gremlinLang.getParameters());
            return gremlinLang.getGremlin("__");
        }

        if (arg instanceof GValue) {
            final GValue gValue = (GValue) arg;
            String key = gValue.getName();

            if (key == null) {
                return argAsString(((GValue<?>) arg).get());
            }

            if (!SourceVersion.isIdentifier(key)) {
                throw new IllegalArgumentException(String.format("Invalid parameter name [%s].", key));
            }

            if (parameters.containsKey(key)) {
                if (!Objects.equals(parameters.get(key), gValue.get())) {
                    throw new IllegalArgumentException(String.format("Parameter with name [%s] already defined.", key));
                }
            } else {
                parameters.put(key, gValue.get());
            }
            return key;
        }

        if (arg instanceof Map) {
            return asString((Map) arg);
        }

        if (arg instanceof Set) {
            return asString((Set) arg);
        }

        // handle all iterables in similar way
        if (arg instanceof List || arg instanceof Object[] || arg.getClass().isArray()) {
            return asString(asIterator(arg));
        }

        if (arg instanceof Class) {
            return ((Class) arg).getSimpleName();
        }

        return asParameter(arg);
    }
This class helps to build a gremlin-lang compatible string representation based on a {@link TraversalSource}
and then a {@link Traversal}.
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/GremlinLang.java