[
https://issues.apache.org/jira/browse/FLINK-4793?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15565608#comment-15565608
]
ASF GitHub Bot commented on FLINK-4793:
---------------------------------------
Github user StephanEwen commented on a diff in the pull request:
https://github.com/apache/flink/pull/2621#discussion_r82808001
--- Diff:
flink-core/src/main/java/org/apache/flink/api/java/typeutils/TypeExtractionUtils.java
---
@@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.typeutils;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.functions.Function;
+import static org.objectweb.asm.Type.getConstructorDescriptor;
+import static org.objectweb.asm.Type.getMethodDescriptor;
+
+@Internal
+public class TypeExtractionUtils {
+
+ private TypeExtractionUtils() {
+ // do not allow instantiation
+ }
+
+ /**
+ * Similar to a Java 8 Executable but with a return type.
+ */
+ public static class LambdaExecutable {
+
+ private Type[] parameterTypes;
+ private Type returnType;
+ private String name;
+ private Object executable;
+
+ public LambdaExecutable(Constructor<?> constructor) {
+ this.parameterTypes =
constructor.getGenericParameterTypes();
+ this.returnType = constructor.getDeclaringClass();
+ this.name = constructor.getName();
+ this.executable = constructor;
+ }
+
+ public LambdaExecutable(Method method) {
+ this.parameterTypes = method.getGenericParameterTypes();
+ this.returnType = method.getGenericReturnType();
+ this.name = method.getName();
+ this.executable = method;
+ }
+
+ public Type[] getParameterTypes() {
+ return parameterTypes;
+ }
+
+ public Type getReturnType() {
+ return returnType;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public boolean executablesEquals(Method m) {
+ return executable.equals(m);
+ }
+
+ public boolean executablesEquals(Constructor<?> c) {
+ return executable.equals(c);
+ }
+ }
+
+ public static LambdaExecutable checkAndExtractLambda(Function function)
{
+ try {
+ // get serialized lambda
+ Object serializedLambda = null;
+ for (Class<?> clazz = function.getClass(); clazz !=
null; clazz = clazz.getSuperclass()) {
+ try {
+ Method replaceMethod =
clazz.getDeclaredMethod("writeReplace");
+ replaceMethod.setAccessible(true);
+ Object serialVersion =
replaceMethod.invoke(function);
+
+ // check if class is a lambda function
+ if
(serialVersion.getClass().getName().equals("java.lang.invoke.SerializedLambda"))
{
+
+ // check if SerializedLambda
class is present
+ try {
+
Class.forName("java.lang.invoke.SerializedLambda");
+ }
+ catch (Exception e) {
+ throw new
UnsupportedOperationException("User code tries to use lambdas, but framework is
running with a Java version < 8");
+ }
+ serializedLambda =
serialVersion;
+ break;
+ }
+ }
+ catch (NoSuchMethodException e) {
+ // thrown if the method is not there.
fall through the loop
+ }
+ }
+
+ // not a lambda method -> return null
+ if (serializedLambda == null) {
+ return null;
+ }
+
+ // find lambda method
+ Method implClassMethod =
serializedLambda.getClass().getDeclaredMethod("getImplClass");
+ Method implMethodNameMethod =
serializedLambda.getClass().getDeclaredMethod("getImplMethodName");
+ Method implMethodSig =
serializedLambda.getClass().getDeclaredMethod("getImplMethodSignature");
+
+ String className = (String)
implClassMethod.invoke(serializedLambda);
+ String methodName = (String)
implMethodNameMethod.invoke(serializedLambda);
+ String methodSig = (String)
implMethodSig.invoke(serializedLambda);
+
+ Class<?> implClass =
Class.forName(className.replace('/', '.'), true,
Thread.currentThread().getContextClassLoader());
+
+ // find constructor
+ if (methodName.equals("<init>")) {
+ Constructor<?>[] constructors =
implClass.getDeclaredConstructors();
+ for (Constructor<?> constructor : constructors)
{
+
if(getConstructorDescriptor(constructor).equals(methodSig)) {
+ return new
LambdaExecutable(constructor);
+ }
+ }
+ }
+ // find method
+ else {
+ List<Method> methods =
getAllDeclaredMethods(implClass);
+ for (Method method : methods) {
+ if(method.getName().equals(methodName)
&& getMethodDescriptor(method).equals(methodSig)) {
+ return new
LambdaExecutable(method);
+ }
+ }
+ }
+ throw new Exception("No lambda method found.");
+ }
+ catch (Exception e) {
+ throw new RuntimeException("Could not extract lambda
method out of function: " + e.getClass().getSimpleName() + " - " +
e.getMessage(), e);
--- End diff --
RuntimeException is usually used for programming errors (wrong arguments,
null divisions, ...).
Something that is expected to fail sometimes beyond the programmers control
(I/O, code analysis, etc) should use checked exceptions.
> Using a local method with :: notation in Java 8 causes index out of bounds
> --------------------------------------------------------------------------
>
> Key: FLINK-4793
> URL: https://issues.apache.org/jira/browse/FLINK-4793
> Project: Flink
> Issue Type: Bug
> Reporter: Ted Dunning
> Assignee: Timo Walther
>
> I tried to use the toString method on an object as a map function:
> {code}
> .<String>map(Trade::toString)
> {code}
> This caused an index out of bounds error:
> {code}
> java.lang.ArrayIndexOutOfBoundsException: -1
> at
> org.apache.flink.api.java.typeutils.TypeExtractor.getUnaryOperatorReturnType(TypeExtractor.java:351)
> at
> org.apache.flink.api.java.typeutils.TypeExtractor.getUnaryOperatorReturnType(TypeExtractor.java:305)
> at
> org.apache.flink.api.java.typeutils.TypeExtractor.getMapReturnTypes(TypeExtractor.java:120)
> at
> org.apache.flink.streaming.api.datastream.DataStream.map(DataStream.java:506)
> at
> com.mapr.aggregate.AggregateTest.testAggregateTrades(AggregateTest.java:81)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at
> org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
> at
> org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
> at
> org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
> at
> org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
> at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
> at
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
> at
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
> at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
> at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
> at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
> at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
> at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
> at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
> at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
> at
> com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
> {code}
> On the other hand, if I use a public static method, like this:
> {code}
> .<Trade>map(Trade::fromString)
> {code}
> All is good. fromString and toString are defined like this:
> {code}
> public static Trade fromString(String s) throws IOException {
> return mapper.readValue(s, Trade.class);
> }
> @Override
> public String toString() {
> return String.format("{\"%s\", %d, %d, %.2f}", symbol, time, volume,
> price);
> }
> {code}
> This might be a viable restriction on what functions I can use, but there
> certainly should be a better error message, if so.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)