4
4
5
5
import com .amazonaws .services .lambda .runtime .ClientContext ;
6
6
import com .amazonaws .services .lambda .runtime .Context ;
7
+ import com .amazonaws .services .lambda .runtime .LambdaRuntimeInternal ;
7
8
import com .amazonaws .services .lambda .runtime .RequestHandler ;
8
9
import com .amazonaws .services .lambda .runtime .RequestStreamHandler ;
9
- import com .amazonaws .services .lambda .runtime .LambdaRuntimeInternal ;
10
-
10
+ import com .amazonaws .services .lambda .runtime .api .client .LambdaRequestHandler .UserFaultHandler ;
11
11
import com .amazonaws .services .lambda .runtime .api .client .api .LambdaClientContext ;
12
12
import com .amazonaws .services .lambda .runtime .api .client .api .LambdaCognitoIdentity ;
13
13
import com .amazonaws .services .lambda .runtime .api .client .api .LambdaContext ;
14
+ import com .amazonaws .services .lambda .runtime .api .client .runtimeapi .InvocationRequest ;
14
15
import com .amazonaws .services .lambda .runtime .api .client .util .UnsafeUtil ;
15
16
import com .amazonaws .services .lambda .runtime .serialization .PojoSerializer ;
16
17
import com .amazonaws .services .lambda .runtime .serialization .events .LambdaEventSerializers ;
17
18
import com .amazonaws .services .lambda .runtime .serialization .factories .GsonFactory ;
18
19
import com .amazonaws .services .lambda .runtime .serialization .factories .JacksonFactory ;
19
20
import com .amazonaws .services .lambda .runtime .serialization .util .Functions ;
20
21
import com .amazonaws .services .lambda .runtime .serialization .util .ReflectUtil ;
21
- import com .amazonaws .services .lambda .runtime .api .client .LambdaRequestHandler .UserFaultHandler ;
22
- import com .amazonaws .services .lambda .runtime .api .client .runtimeapi .InvocationRequest ;
23
22
24
23
import java .io .ByteArrayOutputStream ;
25
24
import java .io .IOException ;
42
41
import java .util .Map ;
43
42
import java .util .Optional ;
44
43
45
- import static com .amazonaws .services .lambda .runtime .api .client .UserFault .filterStackTrace ;
46
- import static com .amazonaws .services .lambda .runtime .api .client .UserFault .makeUserFault ;
47
- import static com .amazonaws .services .lambda .runtime .api .client .UserFault .trace ;
44
+ import static com .amazonaws .services .lambda .runtime .api .client .UserFault .*;
48
45
49
46
public final class EventHandlerLoader {
50
47
private static final byte [] _JsonNull = new byte []{'n' , 'u' , 'l' , 'l' };
@@ -57,12 +54,14 @@ private enum Platform {
57
54
58
55
private static final EnumMap <Platform , Map <Type , PojoSerializer <Object >>> typeCache = new EnumMap <>(Platform .class );
59
56
60
- private EventHandlerLoader () { }
57
+ private EventHandlerLoader () {
58
+ }
61
59
62
60
/**
63
61
* returns the appropriate serializer for the class based on platform and whether the class is a supported event
62
+ *
64
63
* @param platform enum platform
65
- * @param type Type of object used
64
+ * @param type Type of object used
66
65
* @return PojoSerializer
67
66
* @see Platform for which platforms are used
68
67
* @see LambdaEventSerializers for how mixins and modules are added to the serializer
@@ -76,7 +75,7 @@ private static PojoSerializer<Object> getSerializer(Platform platform, Type type
76
75
77
76
// if serializing a Class that is a Lambda supported event, use Jackson with customizations
78
77
if (type instanceof Class ) {
79
- Class <Object > clazz = ((Class )type );
78
+ Class <Object > clazz = ((Class ) type );
80
79
if (LambdaEventSerializers .isLambdaSupportedEvent (clazz .getName ())) {
81
80
return LambdaEventSerializers .serializerFor (clazz , AWSLambda .customerClassLoader );
82
81
}
@@ -150,7 +149,7 @@ private static Platform getPlatform(Context context) {
150
149
}
151
150
152
151
private static boolean isVoid (Type type ) {
153
- return Void .TYPE .equals (type ) || (type instanceof Class ) && Void .class .isAssignableFrom ((Class <?>)type );
152
+ return Void .TYPE .equals (type ) || (type instanceof Class ) && Void .class .isAssignableFrom ((Class <?>) type );
154
153
}
155
154
156
155
/**
@@ -393,7 +392,7 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream, Co
393
392
}
394
393
}
395
394
396
- public static <T > Constructor <T > getConstructor (Class <T > clazz ) throws Exception {
395
+ private static <T > Constructor <T > getConstructor (Class <T > clazz ) throws Exception {
397
396
final Constructor <T > constructor ;
398
397
try {
399
398
constructor = clazz .getConstructor ();
@@ -409,7 +408,7 @@ public static <T> Constructor<T> getConstructor(Class<T> clazz) throws Exception
409
408
return constructor ;
410
409
}
411
410
412
- public static <T > T newInstance (Constructor <? extends T > constructor ) {
411
+ private static <T > T newInstance (Constructor <? extends T > constructor ) {
413
412
try {
414
413
return constructor .newInstance ();
415
414
} catch (UserFault e ) {
@@ -458,15 +457,15 @@ public ClassContext(ParameterizedType type, ClassContext curContext) {
458
457
for (int i = 0 ; i < types .length ; i ++) {
459
458
Type t = types [i ];
460
459
if (t instanceof TypeVariable ) {
461
- types [i ] = curContext .resolveTypeVariable ((TypeVariable )t );
460
+ types [i ] = curContext .resolveTypeVariable ((TypeVariable ) t );
462
461
}
463
462
}
464
463
465
464
Type t = type .getRawType ();
466
465
if (t instanceof Class ) {
467
- this .clazz = (Class )t ;
466
+ this .clazz = (Class ) t ;
468
467
} else if (t instanceof TypeVariable ) {
469
- this .clazz = (Class )((TypeVariable )t ).getGenericDeclaration ();
468
+ this .clazz = (Class ) ((TypeVariable ) t ).getGenericDeclaration ();
470
469
} else {
471
470
throw new RuntimeException ("Type " + t + " is of unexpected type " + t .getClass ());
472
471
}
@@ -499,30 +498,30 @@ private TypeVariable[] getTypeParameters() {
499
498
*
500
499
* @return null of no type found. Otherwise the type found.
501
500
*/
502
- public static Type [] findInterfaceParameters (Class <?> clazz , Class <?> iface ) {
501
+ private static Type [] findInterfaceParameters (Class <?> clazz , Class <?> iface ) {
503
502
LinkedList <ClassContext > clazzes = new LinkedList <>();
504
- clazzes .addFirst (new ClassContext (clazz , (Type [])null ));
503
+ clazzes .addFirst (new ClassContext (clazz , (Type []) null ));
505
504
while (!clazzes .isEmpty ()) {
506
505
final ClassContext curContext = clazzes .removeLast ();
507
506
Type [] interfaces = curContext .clazz .getGenericInterfaces ();
508
507
509
508
for (Type type : interfaces ) {
510
509
if (type instanceof ParameterizedType ) {
511
- ParameterizedType candidate = (ParameterizedType )type ;
510
+ ParameterizedType candidate = (ParameterizedType ) type ;
512
511
Type rawType = candidate .getRawType ();
513
512
if (!(rawType instanceof Class )) {
514
513
//should be impossible
515
514
System .err .println ("raw type is not a class: " + rawType );
516
515
continue ;
517
516
}
518
- Class <?> rawClass = (Class <?>)rawType ;
517
+ Class <?> rawClass = (Class <?>) rawType ;
519
518
if (iface .isAssignableFrom (rawClass )) {
520
519
return new ClassContext (candidate , curContext ).actualTypeArguments ;
521
520
} else {
522
521
clazzes .addFirst (new ClassContext (candidate , curContext ));
523
522
}
524
523
} else if (type instanceof Class ) {
525
- clazzes .addFirst (new ClassContext ((Class <?>)type , curContext ));
524
+ clazzes .addFirst (new ClassContext ((Class <?>) type , curContext ));
526
525
} else {
527
526
//should never happen?
528
527
System .err .println ("Unexpected type class " + type .getClass ().getName ());
@@ -531,17 +530,17 @@ public static Type[] findInterfaceParameters(Class<?> clazz, Class<?> iface) {
531
530
532
531
final Type superClass = curContext .clazz .getGenericSuperclass ();
533
532
if (superClass instanceof ParameterizedType ) {
534
- clazzes .addFirst (new ClassContext ((ParameterizedType )superClass , curContext ));
533
+ clazzes .addFirst (new ClassContext ((ParameterizedType ) superClass , curContext ));
535
534
} else if (superClass != null ) {
536
- clazzes .addFirst (new ClassContext ((Class <?>)superClass , curContext ));
535
+ clazzes .addFirst (new ClassContext ((Class <?>) superClass , curContext ));
537
536
}
538
537
}
539
538
return null ;
540
539
}
541
540
542
541
543
542
@ SuppressWarnings ({"rawtypes" })
544
- public static LambdaRequestHandler wrapRequestHandlerClass (final Class <? extends RequestHandler > clazz ) {
543
+ private static LambdaRequestHandler wrapRequestHandlerClass (final Class <? extends RequestHandler > clazz ) {
545
544
Type [] ptypes = findInterfaceParameters (clazz , RequestHandler .class );
546
545
if (ptypes == null ) {
547
546
return new UserFaultHandler (makeUserFault ("Class "
@@ -555,7 +554,7 @@ public static LambdaRequestHandler wrapRequestHandlerClass(final Class<? extends
555
554
556
555
for (Type t : ptypes ) {
557
556
if (t instanceof TypeVariable ) {
558
- Type [] bounds = ((TypeVariable )t ).getBounds ();
557
+ Type [] bounds = ((TypeVariable ) t ).getBounds ();
559
558
boolean foundBound = false ;
560
559
if (bounds != null ) {
561
560
for (Type bound : bounds ) {
@@ -588,7 +587,7 @@ public static LambdaRequestHandler wrapRequestHandlerClass(final Class<? extends
588
587
}
589
588
}
590
589
591
- public static LambdaRequestHandler wrapRequestStreamHandlerClass (final Class <? extends RequestStreamHandler > clazz ) {
590
+ private static LambdaRequestHandler wrapRequestStreamHandlerClass (final Class <? extends RequestStreamHandler > clazz ) {
592
591
final Constructor <? extends RequestStreamHandler > constructor ;
593
592
try {
594
593
constructor = getConstructor (clazz );
@@ -600,7 +599,7 @@ public static LambdaRequestHandler wrapRequestStreamHandlerClass(final Class<? e
600
599
}
601
600
}
602
601
603
- public static LambdaRequestHandler loadStreamingRequestHandler (Class <?> clazz ) {
602
+ private static LambdaRequestHandler loadStreamingRequestHandler (Class <?> clazz ) {
604
603
if (RequestStreamHandler .class .isAssignableFrom (clazz )) {
605
604
return wrapRequestStreamHandlerClass (clazz .asSubclass (RequestStreamHandler .class ));
606
605
} else if (RequestHandler .class .isAssignableFrom (clazz )) {
@@ -730,10 +729,9 @@ private static final boolean lastParameterIsContext(Class<?>[] params) {
730
729
public int compare (Method lhs , Method rhs ) {
731
730
732
731
//1. Non bridge methods are preferred over bridge methods.
733
- if (! lhs .isBridge () && rhs .isBridge ()) {
732
+ if (!lhs .isBridge () && rhs .isBridge ()) {
734
733
return -1 ;
735
- }
736
- else if (!rhs .isBridge () && lhs .isBridge ()) {
734
+ } else if (!rhs .isBridge () && lhs .isBridge ()) {
737
735
return 1 ;
738
736
}
739
737
@@ -828,13 +826,13 @@ private static LambdaRequestHandler loadEventPojoHandler(HandlerInfo handlerInfo
828
826
}
829
827
830
828
@ SuppressWarnings ({"rawtypes" })
831
- public static LambdaRequestHandler wrapPojoHandler (RequestHandler instance , Type pType , Type rType ) {
829
+ private static LambdaRequestHandler wrapPojoHandler (RequestHandler instance , Type pType , Type rType ) {
832
830
return wrapRequestStreamHandler (new PojoHandlerAsStreamHandler (instance , Optional .ofNullable (pType ),
833
831
isVoid (rType ) ? Optional .<Type >empty () : Optional .of (rType )
834
832
));
835
833
}
836
834
837
- public static String exceptionToString (Throwable t ) {
835
+ private static String exceptionToString (Throwable t ) {
838
836
StringWriter writer = new StringWriter (65536 );
839
837
try (PrintWriter wrapped = new PrintWriter (writer )) {
840
838
t .printStackTrace (wrapped );
@@ -849,7 +847,7 @@ public static String exceptionToString(Throwable t) {
849
847
return buffer .toString ();
850
848
}
851
849
852
- public static LambdaRequestHandler wrapRequestStreamHandler (final RequestStreamHandler handler ) {
850
+ private static LambdaRequestHandler wrapRequestStreamHandler (final RequestStreamHandler handler ) {
853
851
return new LambdaRequestHandler () {
854
852
private final ByteArrayOutputStream output = new ByteArrayOutputStream (1024 );
855
853
private Functions .V2 <String , String > log4jContextPutMethod = null ;
@@ -860,14 +858,15 @@ private void safeAddRequestIdToLog4j(String log4jContextClassName,
860
858
Class <?> log4jContextClass = ReflectUtil .loadClass (AWSLambda .customerClassLoader , log4jContextClassName );
861
859
log4jContextPutMethod = ReflectUtil .loadStaticV2 (log4jContextClass , "put" , false , String .class , contextMapValueClass );
862
860
log4jContextPutMethod .call ("AWSRequestId" , request .getId ());
863
- } catch (Exception e ) {}
861
+ } catch (Exception e ) {
862
+ }
864
863
}
865
864
866
865
public ByteArrayOutputStream call (InvocationRequest request ) throws Error , Exception {
867
866
output .reset ();
868
867
869
868
LambdaCognitoIdentity cognitoIdentity = null ;
870
- if (request .getCognitoIdentity () != null && !request .getCognitoIdentity ().isEmpty ()) {
869
+ if (request .getCognitoIdentity () != null && !request .getCognitoIdentity ().isEmpty ()) {
871
870
cognitoIdentity = getCognitoSerializer ().fromJson (request .getCognitoIdentity ());
872
871
}
873
872
0 commit comments