Skip to content
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

Painless: Move More Logic to PainlessLookup #32689

Merged
merged 75 commits into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
1419751
Parially added constructor.
jdconrad Jul 26, 2018
6469598
Add method type to method.
jdconrad Jul 27, 2018
54b8992
Merge branch 'master' into clean18
jdconrad Jul 27, 2018
8581deb
Merge branch 'clean16' into clean19
jdconrad Jul 27, 2018
641c383
Add PainlessConstructor.
jdconrad Jul 27, 2018
5b449d8
Clean up method.
jdconrad Jul 27, 2018
3c5db32
Merge branch 'master' into clean19
jdconrad Jul 27, 2018
c19b95d
Merge branch 'clean19' into clean20
jdconrad Jul 27, 2018
266a92d
Fixes.
jdconrad Jul 27, 2018
2875c48
Clean up fields.
jdconrad Jul 27, 2018
3f17455
Merge branch 'master' into clean19
jdconrad Jul 30, 2018
b9299d6
Merge branch 'clean19' into clean20
jdconrad Jul 30, 2018
aa833d9
Merge branch 'clean20' into clean21
jdconrad Jul 30, 2018
0c9c174
Reponse to PR comments.
jdconrad Jul 30, 2018
683361a
Merge branch 'clean19' into clean20
jdconrad Jul 30, 2018
6757987
Merge branch 'clean20' into clean21
jdconrad Jul 30, 2018
ccabb90
Merge branch 'master' into clean19
jdconrad Jul 30, 2018
55004f1
Merge branch 'clean19' into clean20
jdconrad Jul 30, 2018
9ca9381
Merge branch 'master' into clean20
jdconrad Jul 31, 2018
f379f2d
Merge branch 'clean20' into clean21
jdconrad Jul 31, 2018
98995f2
Start to clean up PainlessLookup.
jdconrad Jul 31, 2018
8bd9a28
Complete method lookup.
jdconrad Jul 31, 2018
6eaca81
Add lookup constructor.
jdconrad Jul 31, 2018
aab122a
Add field lookup.
jdconrad Jul 31, 2018
509e607
Merge branch 'master' into clean20
jdconrad Jul 31, 2018
aff5c3e
Reponse to PR comments.
jdconrad Jul 31, 2018
dbc3e27
Merge branch 'clean20' into clean21
jdconrad Jul 31, 2018
43d5e49
Merge branch 'clean21' into clean22
jdconrad Jul 31, 2018
dfa6de1
Merge branch 'master' into clean20
jdconrad Jul 31, 2018
36f41af
Merge branch 'clean20' into clean21
jdconrad Jul 31, 2018
2265836
Merge branch 'clean21' into clean22
jdconrad Jul 31, 2018
57e2a46
Add local methods to def bootstrap for consistency in look ups.
jdconrad Aug 1, 2018
bd2d854
Merge branch 'master' into clean21
jdconrad Aug 1, 2018
7187e4f
Merge branch 'master' into clean21
jdconrad Aug 1, 2018
69da907
Merge branch 'clean21' into clean22
jdconrad Aug 1, 2018
eb3bb9f
Merge branch 'clean22' into clean24
jdconrad Aug 1, 2018
4da7113
Add FunctionReference.
jdconrad Aug 1, 2018
1d8b547
Progress.
jdconrad Aug 1, 2018
e51f3df
Progress.
jdconrad Aug 1, 2018
a082624
Progress.
jdconrad Aug 1, 2018
89e936e
Fix.
jdconrad Aug 1, 2018
cf430a9
Progress.
jdconrad Aug 1, 2018
c4e8357
Large clean up of function reference.
jdconrad Aug 1, 2018
bde26b4
A bit more clean up.
jdconrad Aug 1, 2018
b3ed1b7
Merge branch 'master' into clean22
jdconrad Aug 1, 2018
264f6a4
Merge branch 'clean22' into clean24
jdconrad Aug 1, 2018
2cf04ec
Merge branch 'clean24' into clean25
jdconrad Aug 1, 2018
02ab981
Move runtime method lookup to PainlessLookup.
jdconrad Aug 1, 2018
551dc4e
Add getter/setter lookup methods to PainlessLookup.
jdconrad Aug 1, 2018
c1d9d25
Rename.
jdconrad Aug 1, 2018
4255c3c
Merge branch 'master' into clean22
jdconrad Aug 2, 2018
9d5f02f
Remove unnecessary sorted classes list.
jdconrad Aug 2, 2018
a255273
Merge branch 'clean22' into clean24
jdconrad Aug 2, 2018
4dbb100
Merge branch 'clean24' into clean25
jdconrad Aug 2, 2018
9609b45
Merge branch 'clean25' into clean26
jdconrad Aug 2, 2018
64933d1
Merge branch 'master' into clean22
jdconrad Aug 2, 2018
61737be
Merge branch 'clean22' into clean24
jdconrad Aug 2, 2018
67f4140
Merge branch 'clean24' into clean25
jdconrad Aug 2, 2018
8471f6b
Merge branch 'clean25' into clean26
jdconrad Aug 2, 2018
d7d0292
Remove extraneous line.
jdconrad Aug 2, 2018
aac6d88
Change Object to Map.
jdconrad Aug 2, 2018
4051638
Fix import.
jdconrad Aug 2, 2018
b65f459
Merge branch 'master' into clean24
jdconrad Aug 6, 2018
f071c78
Merge branch 'clean24' into clean25
jdconrad Aug 6, 2018
8e2a6af
Merge branch 'master' into clean25
jdconrad Aug 6, 2018
9921693
Merge branch 'clean25' into clean26
jdconrad Aug 6, 2018
af80f8e
Merge branch 'master' into clean25
jdconrad Aug 7, 2018
0b8ebdf
Fix formatting.
jdconrad Aug 7, 2018
b7812d6
Add FunctionRef back.
jdconrad Aug 7, 2018
c7ebe54
Remove extraneous character.
jdconrad Aug 7, 2018
28f6e60
Merge branch 'master' into clean25
jdconrad Aug 7, 2018
b98030a
Merge branch 'clean25' into clean26
jdconrad Aug 7, 2018
fc578ed
Merge branch 'master' into clean26
jdconrad Aug 7, 2018
1d3d2fc
Merge branch 'master' into clean26
jdconrad Aug 8, 2018
804f529
Return null instead of throwing exceptions for all methods in
jdconrad Aug 8, 2018
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 @@ -20,7 +20,6 @@
package org.elasticsearch.painless;

import org.elasticsearch.painless.Locals.LocalMethod;
import org.elasticsearch.painless.lookup.PainlessClass;
import org.elasticsearch.painless.lookup.PainlessLookup;
import org.elasticsearch.painless.lookup.PainlessLookupUtility;
import org.elasticsearch.painless.lookup.PainlessMethod;
Expand All @@ -38,6 +37,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.elasticsearch.painless.lookup.PainlessLookupUtility.typeToCanonicalTypeName;

/**
* Support for dynamic type (def).
* <p>
Expand Down Expand Up @@ -167,52 +168,6 @@ static MethodHandle arrayLengthGetter(Class<?> arrayType) {
}
}

/**
* Looks up method entry for a dynamic method call.
* <p>
* A dynamic method call for variable {@code x} of type {@code def} looks like:
* {@code x.method(args...)}
* <p>
* This method traverses {@code recieverClass}'s class hierarchy (including interfaces)
* until it finds a matching whitelisted method. If one is not found, it throws an exception.
* Otherwise it returns the matching method.
* <p>
* @params painlessLookup the whitelist
* @param receiverClass Class of the object to invoke the method on.
* @param name Name of the method.
* @param arity arity of method
* @return matching method to invoke. never returns null.
* @throws IllegalArgumentException if no matching whitelisted method was found.
*/
static PainlessMethod lookupMethodInternal(PainlessLookup painlessLookup, Class<?> receiverClass, String name, int arity) {
String key = PainlessLookupUtility.buildPainlessMethodKey(name, arity);
// check whitelist for matching method
for (Class<?> clazz = receiverClass; clazz != null; clazz = clazz.getSuperclass()) {
PainlessClass struct = painlessLookup.lookupPainlessClass(clazz);

if (struct != null) {
PainlessMethod method = struct.methods.get(key);
if (method != null) {
return method;
}
}

for (Class<?> iface : clazz.getInterfaces()) {
struct = painlessLookup.lookupPainlessClass(iface);

if (struct != null) {
PainlessMethod method = struct.methods.get(key);
if (method != null) {
return method;
}
}
}
}

throw new IllegalArgumentException("Unable to find dynamic method [" + name + "] with [" + arity + "] arguments " +
"for class [" + receiverClass.getCanonicalName() + "].");
}

/**
* Looks up handle for a dynamic method call, with lambda replacement
* <p>
Expand Down Expand Up @@ -241,7 +196,14 @@ static MethodHandle lookupMethod(PainlessLookup painlessLookup, Map<String, Loca
int numArguments = callSiteType.parameterCount();
// simple case: no lambdas
if (recipeString.isEmpty()) {
return lookupMethodInternal(painlessLookup, receiverClass, name, numArguments - 1).methodHandle;
PainlessMethod painlessMethod = painlessLookup.lookupRuntimePainlessMethod(receiverClass, name, numArguments - 1);

if (painlessMethod == null) {
throw new IllegalArgumentException("dynamic method " +
"[" + typeToCanonicalTypeName(receiverClass) + ", " + name + "/" + (numArguments - 1) + "] not found");
}

return painlessMethod.methodHandle;
}

// convert recipe string to a bitset for convenience (the code below should be refactored...)
Expand All @@ -264,7 +226,13 @@ static MethodHandle lookupMethod(PainlessLookup painlessLookup, Map<String, Loca

// lookup the method with the proper arity, then we know everything (e.g. interface types of parameters).
// based on these we can finally link any remaining lambdas that were deferred.
PainlessMethod method = lookupMethodInternal(painlessLookup, receiverClass, name, arity);
PainlessMethod method = painlessLookup.lookupRuntimePainlessMethod(receiverClass, name, arity);

if (method == null) {
throw new IllegalArgumentException(
"dynamic method [" + typeToCanonicalTypeName(receiverClass) + ", " + name + "/" + arity + "] not found");
}

MethodHandle handle = method.methodHandle;

int replaced = 0;
Expand Down Expand Up @@ -332,15 +300,23 @@ static MethodHandle lookupMethod(PainlessLookup painlessLookup, Map<String, Loca
static MethodHandle lookupReference(PainlessLookup painlessLookup, Map<String, LocalMethod> localMethods,
MethodHandles.Lookup methodHandlesLookup, String interfaceClass, Class<?> receiverClass, String name) throws Throwable {
Class<?> interfaceType = painlessLookup.canonicalTypeNameToType(interfaceClass);
if (interfaceType == null) {
throw new IllegalArgumentException("type [" + interfaceClass + "] not found");
}
PainlessMethod interfaceMethod = painlessLookup.lookupFunctionalInterfacePainlessMethod(interfaceType);
if (interfaceMethod == null) {
throw new IllegalArgumentException("Class [" + interfaceClass + "] is not a functional interface");
}
int arity = interfaceMethod.typeParameters.size();
PainlessMethod implMethod = lookupMethodInternal(painlessLookup, receiverClass, name, arity);
PainlessMethod implMethod = painlessLookup.lookupRuntimePainlessMethod(receiverClass, name, arity);
if (implMethod == null) {
throw new IllegalArgumentException(
"dynamic method [" + typeToCanonicalTypeName(receiverClass) + ", " + name + "/" + arity + "] not found");
}

return lookupReferenceInternal(painlessLookup, localMethods, methodHandlesLookup,
interfaceType, PainlessLookupUtility.typeToCanonicalTypeName(implMethod.targetClass),
implMethod.javaMethod.getName(), 1);
interfaceType, PainlessLookupUtility.typeToCanonicalTypeName(implMethod.targetClass),
implMethod.javaMethod.getName(), 1);
}

/** Returns a method handle to an implementation of clazz, given method reference signature. */
Expand Down Expand Up @@ -389,27 +365,12 @@ private static MethodHandle lookupReferenceInternal(PainlessLookup painlessLooku
*/
static MethodHandle lookupGetter(PainlessLookup painlessLookup, Class<?> receiverClass, String name) {
// first try whitelist
for (Class<?> clazz = receiverClass; clazz != null; clazz = clazz.getSuperclass()) {
PainlessClass struct = painlessLookup.lookupPainlessClass(clazz);

if (struct != null) {
MethodHandle handle = struct.getterMethodHandles.get(name);
if (handle != null) {
return handle;
}
}

for (final Class<?> iface : clazz.getInterfaces()) {
struct = painlessLookup.lookupPainlessClass(iface);
MethodHandle getter = painlessLookup.lookupRuntimeGetterMethodHandle(receiverClass, name);

if (struct != null) {
MethodHandle handle = struct.getterMethodHandles.get(name);
if (handle != null) {
return handle;
}
}
}
if (getter != null) {
return getter;
}

// special case: arrays, maps, and lists
if (receiverClass.isArray() && "length".equals(name)) {
// arrays expose .length as a read-only getter
Expand All @@ -426,12 +387,12 @@ static MethodHandle lookupGetter(PainlessLookup painlessLookup, Class<?> receive
int index = Integer.parseInt(name);
return MethodHandles.insertArguments(LIST_GET, 1, index);
} catch (NumberFormatException exception) {
throw new IllegalArgumentException( "Illegal list shortcut value [" + name + "].");
throw new IllegalArgumentException("Illegal list shortcut value [" + name + "].");
}
}

throw new IllegalArgumentException("Unable to find dynamic field [" + name + "] " +
"for class [" + receiverClass.getCanonicalName() + "].");
throw new IllegalArgumentException(
"dynamic getter [" + typeToCanonicalTypeName(receiverClass) + ", " + name + "] not found");
}

/**
Expand Down Expand Up @@ -460,27 +421,12 @@ static MethodHandle lookupGetter(PainlessLookup painlessLookup, Class<?> receive
*/
static MethodHandle lookupSetter(PainlessLookup painlessLookup, Class<?> receiverClass, String name) {
// first try whitelist
for (Class<?> clazz = receiverClass; clazz != null; clazz = clazz.getSuperclass()) {
PainlessClass struct = painlessLookup.lookupPainlessClass(clazz);

if (struct != null) {
MethodHandle handle = struct.setterMethodHandles.get(name);
if (handle != null) {
return handle;
}
}

for (final Class<?> iface : clazz.getInterfaces()) {
struct = painlessLookup.lookupPainlessClass(iface);
MethodHandle setter = painlessLookup.lookupRuntimeSetterMethodHandle(receiverClass, name);

if (struct != null) {
MethodHandle handle = struct.setterMethodHandles.get(name);
if (handle != null) {
return handle;
}
}
}
if (setter != null) {
return setter;
}

// special case: maps, and lists
if (Map.class.isAssignableFrom(receiverClass)) {
// maps allow access like mymap.key
Expand All @@ -494,12 +440,12 @@ static MethodHandle lookupSetter(PainlessLookup painlessLookup, Class<?> receive
int index = Integer.parseInt(name);
return MethodHandles.insertArguments(LIST_SET, 1, index);
} catch (final NumberFormatException exception) {
throw new IllegalArgumentException( "Illegal list shortcut value [" + name + "].");
throw new IllegalArgumentException("Illegal list shortcut value [" + name + "].");
}
}

throw new IllegalArgumentException("Unable to find dynamic field [" + name + "] " +
"for class [" + receiverClass.getCanonicalName() + "].");
throw new IllegalArgumentException(
"dynamic getter [" + typeToCanonicalTypeName(receiverClass) + ", " + name + "] not found");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public static FunctionRef create(PainlessLookup painlessLookup, Map<String, Loca
PainlessMethod interfaceMethod;

try {
try {
interfaceMethod = painlessLookup.lookupFunctionalInterfacePainlessMethod(targetClass);
} catch (IllegalArgumentException iae) {
interfaceMethod = painlessLookup.lookupFunctionalInterfacePainlessMethod(targetClass);

if (interfaceMethod == null) {
throw new IllegalArgumentException("cannot convert function reference [" + typeName + "::" + methodName + "] " +
"to a non-functional interface [" + targetClassName + "]", iae);
"to a non-functional interface [" + targetClassName + "]");
}

String interfaceMethodName = interfaceMethod.javaMethod.getName();
Expand Down Expand Up @@ -116,14 +116,12 @@ public static FunctionRef create(PainlessLookup painlessLookup, Map<String, Loca
throw new IllegalStateException("internal error");
}

PainlessConstructor painlessConstructor;
PainlessConstructor painlessConstructor = painlessLookup.lookupPainlessConstructor(typeName, interfaceTypeParametersSize);

try {
painlessConstructor = painlessLookup.lookupPainlessConstructor(typeName, interfaceTypeParametersSize);
} catch (IllegalArgumentException iae) {
if (painlessConstructor == null) {
throw new IllegalArgumentException("function reference [" + typeName + "::new/" + interfaceTypeParametersSize + "] " +
"matching [" + targetClassName + ", " + interfaceMethodName + "/" + interfaceTypeParametersSize + "] " +
"not found", iae);
"not found");
}

delegateClassName = painlessConstructor.javaConstructor.getDeclaringClass().getName();
Expand All @@ -140,24 +138,21 @@ public static FunctionRef create(PainlessLookup painlessLookup, Map<String, Loca
}

boolean captured = numberOfCaptures == 1;
PainlessMethod painlessMethod;
PainlessMethod painlessMethod =
painlessLookup.lookupPainlessMethod(typeName, true, methodName, interfaceTypeParametersSize);

try {
painlessMethod = painlessLookup.lookupPainlessMethod(typeName, true, methodName, interfaceTypeParametersSize);
if (painlessMethod == null) {
painlessMethod = painlessLookup.lookupPainlessMethod(typeName, false, methodName,
captured ? interfaceTypeParametersSize : interfaceTypeParametersSize - 1);

if (captured) {
throw new IllegalStateException("internal error");
}
} catch (IllegalArgumentException staticIAE) {
try {
painlessMethod = painlessLookup.lookupPainlessMethod(typeName, false, methodName,
captured ? interfaceTypeParametersSize : interfaceTypeParametersSize - 1);
} catch (IllegalArgumentException iae) {
if (painlessMethod == null) {
throw new IllegalArgumentException(
"function reference " + "[" + typeName + "::" + methodName + "/" + interfaceTypeParametersSize + "] " +
"matching [" + targetClassName + ", " + interfaceMethodName + "/" + interfaceTypeParametersSize + "] " +
"not found", iae);
"not found");
}
} else if (captured) {
throw new IllegalStateException("internal error");
}

delegateClassName = painlessMethod.javaMethod.getDeclaringClass().getName();
Expand Down
Loading