[ 
https://issues.apache.org/jira/browse/FLINK-1062?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14118201#comment-14118201
 ] 

Stephan Ewen commented on FLINK-1062:
-------------------------------------

Here is some of my old playground code that gets all generic types from lambdas 
in a pretty safe way:

{code}
public class ClassWithLambdas {
        
        
        public static void main(String[] args) throws Exception {
        
                SomeFunction func = (map) -> 
Arrays.asList(map.get("hello").iterator().next().longValue());
                
                SomeFunction2<Double, Long> func2 = (map) -> 
Arrays.asList(map.get("hello").iterator().next().longValue());
                
                SerializedLambda sl1 = getSerializedLambda(func);
                SerializedLambda sl2 = getSerializedLambda(func2);              
                
                Method m1 = getLambdaMethod(sl1);
                Method m2 = getLambdaMethod(sl2);
                
                System.out.println(m1);
                System.out.println(m1.getGenericReturnType());
                for (Type t : m1.getGenericParameterTypes()) {
                        System.out.println(t);
                }
                
                System.out.println("\n-----\n");
                
                System.out.println(m2);
                System.out.println(m2.getGenericReturnType());
                for (Type t : m2.getGenericParameterTypes()) {
                        System.out.println(t);
                }
        }
        
        // 
--------------------------------------------------------------------------------------------
 
        
        public static SerializedLambda getSerializedLambda(Object function) {
                if (function == null || !(function instanceof 
java.io.Serializable)) {
                        throw new IllegalArgumentException();
                }
                
                for (Class<?> clazz = function.getClass(); clazz != null; clazz 
= clazz.getSuperclass()) {
                try {
                    Method replaceMethod = 
clazz.getDeclaredMethod("writeReplace");
                    replaceMethod.setAccessible(true);
                    Object serializedForm = replaceMethod.invoke(function);

                    if (serializedForm instanceof SerializedLambda) {
                        return (SerializedLambda) serializedForm;
                    }
                }
                catch (NoSuchMethodError e) {
                    // fall through the loop and try the next class
                }
                catch (Throwable t) {
                    throw new RuntimeException("Error while extracting 
serialized lambda", t);
                }
            }
                
                return null;
        }
        
        public static Method getLambdaMethod(SerializedLambda lambda) throws 
Exception {
                String implClassName = lambda.getImplClass().replace('/', '.');
                Class<?> implClass = Class.forName(implClassName);
                
                String lambdaName = lambda.getImplMethodName();
                
                for (Method m : implClass.getDeclaredMethods()) {
                        if (m.getName().equals(lambdaName)) {
                                return m;
                        }
                }
                
                return null;
        }
        
        // 
--------------------------------------------------------------------------------------------
        
        public static interface SomeFunction extends java.io.Serializable {
                
                List<Long> applyTheFunction(Map<String, Set<Double>> theMap) 
throws IOException;
        }
        
        public static interface SomeFunction2<I, O> extends 
java.io.Serializable {
                
                List<O> applyTheFunction(Map<String, Set<I>> theMap) throws 
IOException;
        }
}
{code}

> Type Extraction for Lambdas
> ---------------------------
>
>                 Key: FLINK-1062
>                 URL: https://issues.apache.org/jira/browse/FLINK-1062
>             Project: Flink
>          Issue Type: Improvement
>          Components: Java API
>    Affects Versions: 0.7-incubating
>            Reporter: Stephan Ewen
>            Assignee: Timo Walther
>             Fix For: 0.7-incubating
>
>
> Lambdas currently work only for {{filter}} and {{reduce(a,b)}}, because 
> Lambda type extraction is not in place right now.
> We need to extend the type extraction for lambdas to support the other 
> functions.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to