Przeglądaj źródła

Remove Painless Type in favor of Java Class in FunctionRef. (#28429)

Jack Conradson 7 lat temu
rodzic
commit
1d01bcf421

+ 2 - 2
modules/lang-painless/src/main/java/org/elasticsearch/painless/Def.java

@@ -363,10 +363,10 @@ public final class Def {
                  }
                  throw new IllegalArgumentException("Unknown call [" + call + "] with [" + arity + "] arguments.");
              }
-             ref = new FunctionRef(clazz, interfaceMethod, call, handle.type(), captures.length);
+             ref = new FunctionRef(clazz.clazz, interfaceMethod, call, handle.type(), captures.length);
          } else {
              // whitelist lookup
-             ref = new FunctionRef(definition, clazz, type, call, captures.length);
+             ref = new FunctionRef(definition, clazz.clazz, type, call, captures.length);
          }
          final CallSite callSite = LambdaBootstrap.lambdaBootstrap(
              lookup,

+ 11 - 11
modules/lang-painless/src/main/java/org/elasticsearch/painless/FunctionRef.java

@@ -20,8 +20,6 @@
 package org.elasticsearch.painless;
 
 import org.elasticsearch.painless.Definition.Method;
-import org.elasticsearch.painless.Definition.Type;
-import org.elasticsearch.painless.api.Augmentation;
 
 import java.lang.invoke.MethodType;
 import java.lang.reflect.Modifier;
@@ -75,8 +73,9 @@ public class FunctionRef {
      * @param call the right hand side of a method reference expression
      * @param numCaptures number of captured arguments
      */
-    public FunctionRef(Definition definition, Type expected, String type, String call, int numCaptures) {
-        this(expected, expected.struct.getFunctionalMethod(), lookup(definition, expected, type, call, numCaptures > 0), numCaptures);
+    public FunctionRef(Definition definition, Class<?> expected, String type, String call, int numCaptures) {
+        this(expected, definition.ClassToType(expected).struct.getFunctionalMethod(),
+                lookup(definition, expected, type, call, numCaptures > 0), numCaptures);
     }
 
     /**
@@ -86,11 +85,11 @@ public class FunctionRef {
      * @param delegateMethod implementation method
      * @param numCaptures number of captured arguments
      */
-    public FunctionRef(Type expected, Method interfaceMethod, Method delegateMethod, int numCaptures) {
+    public FunctionRef(Class<?> expected, Method interfaceMethod, Method delegateMethod, int numCaptures) {
         MethodType delegateMethodType = delegateMethod.getMethodType();
 
         interfaceMethodName = interfaceMethod.name;
-        factoryMethodType = MethodType.methodType(expected.clazz,
+        factoryMethodType = MethodType.methodType(expected,
                 delegateMethodType.dropParameterTypes(numCaptures, delegateMethodType.parameterCount()));
         interfaceMethodType = interfaceMethod.getMethodType().dropParameterTypes(0, 1);
 
@@ -128,9 +127,10 @@ public class FunctionRef {
      * Creates a new FunctionRef (low level).
      * It is for runtime use only.
      */
-    public FunctionRef(Type expected, Method interfaceMethod, String delegateMethodName, MethodType delegateMethodType, int numCaptures) {
+    public FunctionRef(Class<?> expected,
+                       Method interfaceMethod, String delegateMethodName, MethodType delegateMethodType, int numCaptures) {
         interfaceMethodName = interfaceMethod.name;
-        factoryMethodType = MethodType.methodType(expected.clazz,
+        factoryMethodType = MethodType.methodType(expected,
             delegateMethodType.dropParameterTypes(numCaptures, delegateMethodType.parameterCount()));
         interfaceMethodType = interfaceMethod.getMethodType().dropParameterTypes(0, 1);
 
@@ -150,14 +150,14 @@ public class FunctionRef {
     /**
      * Looks up {@code type::call} from the whitelist, and returns a matching method.
      */
-    private static Definition.Method lookup(Definition definition, Definition.Type expected,
+    private static Definition.Method lookup(Definition definition, Class<?> expected,
                                             String type, String call, boolean receiverCaptured) {
         // check its really a functional interface
         // for e.g. Comparable
-        Method method = expected.struct.getFunctionalMethod();
+        Method method = definition.ClassToType(expected).struct.getFunctionalMethod();
         if (method == null) {
             throw new IllegalArgumentException("Cannot convert function reference [" + type + "::" + call + "] " +
-                                               "to [" + expected.name + "], not a functional interface");
+                                               "to [" + Definition.ClassToName(expected) + "], not a functional interface");
         }
 
         // lookup requested method

+ 1 - 1
modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ECapturingFunctionRef.java

@@ -77,7 +77,7 @@ public final class ECapturingFunctionRef extends AExpression implements ILambda
             if (captured.type.dynamic == false) {
                 try {
                     ref = new FunctionRef(
-                        locals.getDefinition(), locals.getDefinition().ClassToType(expected), captured.type.name, call, 1);
+                        locals.getDefinition(), expected, captured.type.name, call, 1);
 
                     // check casts between the interface method and the delegate method are legal
                     for (int i = 0; i < ref.interfaceMethod.arguments.size(); ++i) {

+ 2 - 2
modules/lang-painless/src/main/java/org/elasticsearch/painless/node/EFunctionRef.java

@@ -76,7 +76,7 @@ public final class EFunctionRef extends AExpression implements ILambda {
                         throw new IllegalArgumentException("Cannot convert function reference [" + type + "::" + call + "] " +
                                                            "to [" + Definition.ClassToName(expected) + "], function not found");
                     }
-                    ref = new FunctionRef(locals.getDefinition().ClassToType(expected), interfaceMethod, delegateMethod, 0);
+                    ref = new FunctionRef(expected, interfaceMethod, delegateMethod, 0);
 
                     // check casts between the interface method and the delegate method are legal
                     for (int i = 0; i < interfaceMethod.arguments.size(); ++i) {
@@ -91,7 +91,7 @@ public final class EFunctionRef extends AExpression implements ILambda {
                     }
                 } else {
                     // whitelist lookup
-                    ref = new FunctionRef(locals.getDefinition(), locals.getDefinition().ClassToType(expected), type, call, 0);
+                    ref = new FunctionRef(locals.getDefinition(), expected, type, call, 0);
                 }
 
             } catch (IllegalArgumentException e) {

+ 1 - 1
modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ELambda.java

@@ -183,7 +183,7 @@ public final class ELambda extends AExpression implements ILambda {
         } else {
             defPointer = null;
             try {
-                ref = new FunctionRef(locals.getDefinition().ClassToType(expected), interfaceMethod, desugared.method, captures.size());
+                ref = new FunctionRef(expected, interfaceMethod, desugared.method, captures.size());
             } catch (IllegalArgumentException e) {
                 throw createError(e);
             }

+ 1 - 3
modules/lang-painless/src/test/java/org/elasticsearch/painless/AnalyzerCasterTests.java

@@ -20,13 +20,11 @@
 package org.elasticsearch.painless;
 
 import org.elasticsearch.painless.Definition.Cast;
-import org.elasticsearch.painless.spi.Whitelist;
+
 import org.elasticsearch.test.ESTestCase;
 
 public class AnalyzerCasterTests extends ESTestCase {
 
-    private static final Definition definition = new Definition(Whitelist.BASE_WHITELISTS);
-
     private static void assertCast(Class<?> actual, Class<?> expected, boolean mustBeExplicit) {
         Location location = new Location("dummy", 0);