Github user StephanEwen commented on the pull request:

    https://github.com/apache/incubator-flink/pull/85#issuecomment-50670878
  
    Simple type extraction from Lambdas (limited to non-generic parameters):
    
    ```java
        public static Class<?>[] getLambdaParameters(Object lambda) {
                for (Class<?> clazz = lambda.getClass(); clazz != null; clazz = 
clazz.getSuperclass()) {
                        try {
                                Method replaceMethod = 
clazz.getDeclaredMethod("writeReplace");
                                replaceMethod.setAccessible(true);
                                Object serializedForm = 
replaceMethod.invoke(lambda);
                                
                                if (serializedForm instanceof SerializedLambda) 
{
                                        SerializedLambda sl = 
(SerializedLambda) serializedForm;
                                        return getTypesFromSerializedLambda(sl);
                                }
                        }
                        catch (NoSuchMethodException e) {
                                // fall through the loop and try the next class
                        }
                        catch (Throwable t) {
                                throw new RuntimeException(t);
                        }
                }
                
                throw new IllegalArgumentException("Not a serialized Lambda");
        }
        
        public static Class<?>[] getTypesFromSerializedLambda(SerializedLambda 
sl) throws Exception {
                String sig = sl.getImplMethodSignature();
                
                if (!sig.startsWith("(")) {
                        throw new Exception("Parse Error");
                }
                
                String parameters = sig.substring(1, sig.indexOf(')'));
                String[] params = parameters.split(";");
                
                List<Class<?>> classes = new ArrayList<>();
                
                for (String p : params) {
                        if (!p.startsWith("L")) {
                                throw new Exception("Parse Error");
                        }
                        
                        p = p.substring(1);
                        p = p.replace('/', '.');
                        classes.add(Class.forName(p));
                }
                
                
                return (Class<?>[]) classes.toArray(new 
Class<?>[classes.size()]);
        }
    ```
    
    



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to