Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import org.apache.commons.configuration2.BaseConfiguration;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.MapConfiguration;
import org.apache.tinkerpop.gremlin.process.traversal.Bytecode;
import org.apache.tinkerpop.gremlin.process.traversal.Translator;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.TraversalSource;
import org.apache.tinkerpop.gremlin.process.traversal.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like your IDE has auto style formatting which differs from the conventions used in the tinkerpop project and thus the wildcard import has been added and various whitespace changes as well. According to https://tinkerpop.apache.org/docs/current/dev/developer/#_code_style we try to keep the style consistent with the existing conventions.

Contributors should examine the current code base to determine what the code style patterns are and should match their style to what is already present. Of specific note however, TinkerPop does not use "import wildcards" - IDEs should be adjusted accordingly to not auto-wildcard the imports.

import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.lambda.CardinalityValueTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.BulkSet;
Expand Down Expand Up @@ -60,7 +57,7 @@ public final class JavaTranslator<S extends TraversalSource, T extends Traversal
private final S traversalSource;
private final Class<?> anonymousTraversal;
private static final Map<Class<?>, Map<String, List<ReflectedMethod>>> GLOBAL_METHOD_CACHE = new ConcurrentHashMap<>();
private final Map<Class<?>, Map<String,Method>> localMethodCache = new ConcurrentHashMap<>();
private final Map<Class<?>, Map<String, Method>> localMethodCache = new ConcurrentHashMap<>();
private final Method anonymousTraversalStart;

private JavaTranslator(final S traversalSource) {
Expand Down Expand Up @@ -109,7 +106,7 @@ public String toString() {
return StringFactory.translatorString(this);
}

////
/// /

private Object translateObject(final Object object) {
if (object instanceof Bytecode.Binding)
Expand Down Expand Up @@ -287,12 +284,12 @@ private Object invokeMethod(final Object delegate, final Class<?> returnType, fi
// do something far more drastic that doesn't involve reflection.
if (i < argumentsCopy.length && (null == argumentsCopy[i] ||
(argumentsCopy[i] != null && (
parameters[i].getType().isAssignableFrom(argumentsCopy[i].getClass()) ||
(parameters[i].getType().isPrimitive() &&
(Number.class.isAssignableFrom(argumentsCopy[i].getClass()) ||
argumentsCopy[i].getClass().equals(Boolean.class) ||
argumentsCopy[i].getClass().equals(Byte.class) ||
argumentsCopy[i].getClass().equals(Character.class))))))) {
parameters[i].getType().isAssignableFrom(argumentsCopy[i].getClass()) ||
(parameters[i].getType().isPrimitive() &&
(Number.class.isAssignableFrom(argumentsCopy[i].getClass()) ||
argumentsCopy[i].getClass().equals(Boolean.class) ||
argumentsCopy[i].getClass().equals(Byte.class) ||
argumentsCopy[i].getClass().equals(Character.class))))))) {
newArguments[i] = argumentsCopy[i];
} else {
found = false;
Expand All @@ -301,20 +298,30 @@ private Object invokeMethod(final Object delegate, final Class<?> returnType, fi
}
}

// special case has() where the first arg is null - sometimes this can end up with the T being
// null and in 3.5.x that generates an exception which raises badly in the translator. it is
// safer to force this to the String form by letting this "found" version pass. In java this
// form of GraphTraversal can't be produced because of validations for has(T, ...) but in
// other language it might be allowed which means that has((T) null, ...) from something like
// special cases of has() where the first arg is null or third arg is null - sometimes this can end up with the T being
// null or in the second case calling has that accepts predicate instead of string as parameter in the last argument.
// That generates an exception which raises badly in the translator. it is
// safer to force this to the String form by letting this "found" version pass. In Java
// form of GraphTraversal generated in the first case can't be produced because of validations
// for has(T, ...) but in other language it might be allowed which means that has((T) null, ...) from something like
// python will end up as has((String) null, ...) which filters rather than generates an
// exception. calling the T version even in a non-JVM language seems odd and more likely the
// caller was shooting for the String one, but ugh who knows. this issue showcases again how
// badly bytecode should either change to use gremlin-language and go away or bytecode should
// get a safer way to be translated to a traversal with more explicit calls that don't rely
// on reflection.
if (methodName.equals(GraphTraversal.Symbols.has) && newArguments.length > 0 && null == newArguments[0] &&
method.getParameterTypes()[0].isAssignableFrom(org.apache.tinkerpop.gremlin.structure.T.class)) {
found = false;
var parametersTypes = method.getParameterTypes();
if (methodName.equals(GraphTraversal.Symbols.has) && newArguments.length > 0) {
//first case has((T)null, ...) instead of has((String)null, ...)
if (null == newArguments[0] &&
parametersTypes[0].isAssignableFrom(org.apache.tinkerpop.gremlin.structure.T.class)) {
found = false;
} else if (newArguments.length == 3 && newArguments[2] == null && parametersTypes[0] == String.class &&
parametersTypes[1] == String.class &&
parametersTypes[2] == P.class) {
//the second case has(String, String, (P)(null)) instead of has(String,String, (Object)null)
found = false;
}
Comment on lines +319 to +324
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised the has overloads with 2 method args have not also surfaced also a problem (it could just be by luck depending on the order of the methods loaded in the global cache). You could change the logic to address both the 2 and 3 arg method overloads:

Suggested change
} else if (newArguments.length == 3 && newArguments[2] == null && parametersTypes[0] == String.class &&
parametersTypes[1] == String.class &&
parametersTypes[2] == P.class) {
//the second case has(String, String, (P)(null)) instead of has(String,String, (Object)null)
found = false;
}
} else if (newArguments[newArguments.length - 1] == null && parametersTypes[newArguments.length - 1] == P.class) {
//the second case has(String, String, (P)(null)) instead of has(String,String, (Object)null)
//or has(String, (P)(null)) instead of has(String, (Object)null)
found = false;
}

}

if (found) {
Expand Down