2
2
3
3
import com .amazonaws .services .lambda .runtime .Context ;
4
4
import com .amazonaws .services .lambda .runtime .RequestStreamHandler ;
5
+ import com .amazonaws .services .lambda .runtime .serialization .PojoSerializer ;
6
+ import com .amazonaws .services .lambda .runtime .serialization .events .LambdaEventSerializers ;
7
+ import com .fasterxml .jackson .core .JsonProcessingException ;
5
8
import com .fasterxml .jackson .databind .DeserializationFeature ;
6
9
import com .fasterxml .jackson .databind .MapperFeature ;
7
10
import com .fasterxml .jackson .databind .ObjectMapper ;
@@ -22,7 +25,6 @@ public class JavaClassLoader {
22
25
.enable (DeserializationFeature .READ_DATE_TIMESTAMPS_AS_NANOSECONDS )
23
26
.registerModule (new JodaModule ());
24
27
25
-
26
28
private static Class inputType ;
27
29
private static MethodHandle methodHandle ;
28
30
private static RequestStreamHandler classInstance ;
@@ -53,24 +55,20 @@ private JavaClassLoader(Class inputType, MethodHandle methodHandle) {
53
55
static JavaClassLoader initializeClassLoader (String className , String methodName ) throws ReflectiveOperationException {
54
56
Class loadedClass = classLoader .loadClass (className );
55
57
Class methodInputType = null ;
56
-
57
58
for (Method method : loadedClass .getMethods ()) {
58
59
if (isUserHandlerMethod (method , className , methodName ) == true ) {
59
60
methodInputType = method .getParameterTypes ()[0 ];
60
61
break ;
61
62
}
62
63
}
63
-
64
64
Object classInstance = loadedClass .getDeclaredConstructor ().newInstance ();
65
65
MethodHandle methodHandle = publicLookup .findVirtual (loadedClass , methodName , methodType ).bindTo (classInstance );
66
-
67
66
return new JavaClassLoader (methodInputType , methodHandle );
68
67
}
69
68
70
69
public Object invokeClassMethod (Object inputParam , Context contextParam ) {
71
-
70
+ Object handlerType = mappingInputToHandlerType ( inputParam , inputType );
72
71
try {
73
- Object handlerType = mappingInputToHandlerType (inputParam , inputType );
74
72
return methodHandle .invokeWithArguments (handlerType , contextParam );
75
73
} catch (Throwable e ) {
76
74
throw new RuntimeException ("Error occurred while invoking handler method: " + e );
@@ -89,10 +87,16 @@ private static boolean isUserHandlerMethod(Method method, String className, Stri
89
87
}
90
88
91
89
private Object mappingInputToHandlerType (Object inputParam , Class inputType ) {
92
- if (inputType .isAssignableFrom (Integer .class )) {
93
- return inputParam ;
94
- } else if (inputType .isAssignableFrom (String .class )) {
90
+ if (inputType .isAssignableFrom (Number .class ) || inputType .isAssignableFrom (String .class )) {
95
91
return inputParam ;
92
+ } else if (LambdaEventSerializers .isLambdaSupportedEvent (inputType .getName ())) {
93
+ try {
94
+ PojoSerializer serializer = LambdaEventSerializers .serializerFor (inputType , classLoader );
95
+ String inputParamString = mapper .writeValueAsString (inputParam );
96
+ return serializer .fromJson (inputParamString );
97
+ } catch (JsonProcessingException e ) {
98
+ throw new RuntimeException ("Error occurred while serializing lambda input type: " + e );
99
+ }
96
100
}
97
101
return mapper .convertValue (inputParam , inputType );
98
102
}
0 commit comments