-
Notifications
You must be signed in to change notification settings - Fork 843
Fix JavaTranslator's has handling for null arguments
#3274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andrii0lomakin
wants to merge
1
commit into
apache:3.8-dev
Choose a base branch
from
youtrackdb:proper-has-s-s-o-mapping
base: 3.8-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+27
−20
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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.*; | ||||||||||||||||||||||||
| 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; | ||||||||||||||||||||||||
|
|
@@ -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) { | ||||||||||||||||||||||||
|
|
@@ -109,7 +106,7 @@ public String toString() { | |||||||||||||||||||||||
| return StringFactory.translatorString(this); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| //// | ||||||||||||||||||||||||
| /// / | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private Object translateObject(final Object object) { | ||||||||||||||||||||||||
| if (object instanceof Bytecode.Binding) | ||||||||||||||||||||||||
|
|
@@ -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; | ||||||||||||||||||||||||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (found) { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.