@@ -31,12 +31,10 @@ static String getLambdaArn(Object request) {
3131 if (access .getLambdaConfiguration == null ) {
3232 return null ;
3333 }
34- try {
35- Object config = access .getLambdaConfiguration .invoke (request );
36- return invokeOrNull (access .getLambdaArnFromConfiguration , config );
37- } catch (Throwable t ) {
38- return null ;
39- }
34+ Object config = invokeOrNull (access .getLambdaConfiguration , request , Object .class );
35+ return config != null
36+ ? invokeOrNull (LambdaFunctionConfigurationAccess .getLambdaArnFromConfiguration , config )
37+ : null ;
4038 }
4139
4240 @ Nullable
@@ -113,19 +111,24 @@ static String getTargetArn(Object request) {
113111
114112 @ Nullable
115113 private static String invokeOrNull (@ Nullable MethodHandle method , Object obj ) {
114+ return invokeOrNull (method , obj , String .class );
115+ }
116+
117+ @ Nullable
118+ private static <T > T invokeOrNull (
119+ @ Nullable MethodHandle method , Object obj , Class <T > returnType ) {
116120 if (method == null ) {
117121 return null ;
118122 }
119123 try {
120- return ( String ) method .invoke (obj );
124+ return returnType . cast ( method .invoke (obj ) );
121125 } catch (Throwable t ) {
122126 return null ;
123127 }
124128 }
125129
126130 @ Nullable private final MethodHandle getBucketName ;
127131 @ Nullable private final MethodHandle getLambdaConfiguration ;
128- @ Nullable private final MethodHandle getLambdaArnFromConfiguration ;
129132 @ Nullable private final MethodHandle getLambdaName ;
130133 @ Nullable private final MethodHandle getLambdaResourceMappingId ;
131134 @ Nullable private final MethodHandle getQueueUrl ;
@@ -149,8 +152,6 @@ private RequestAccess(Class<?> clz) {
149152
150153 boolean isLambda = clz .getName ().startsWith (LAMBDA_REQUEST_CLASS_PREFIX );
151154 getLambdaConfiguration = isLambda ? findLambdaGetConfigurationMethod (clz ) : null ;
152- getLambdaArnFromConfiguration =
153- getLambdaConfiguration != null ? findGetLambdaArnMethod () : null ;
154155 getLambdaName = isLambda ? findAccessorOrNull (clz , "getFunctionName" ) : null ;
155156 getLambdaResourceMappingId = isLambda ? findAccessorOrNull (clz , "getUUID" ) : null ;
156157 boolean isSecretsManager = clz .getName ().startsWith (SECRETS_MANAGER_REQUEST_CLASS_PREFIX );
@@ -187,14 +188,18 @@ private static MethodHandle findLambdaGetConfigurationMethod(Class<?> clz) {
187188 }
188189 }
189190
190- @ Nullable
191- private static MethodHandle findGetLambdaArnMethod () {
192- try {
193- Class <?> lambdaConfigurationClass =
194- Class .forName ("com.amazonaws.services.lambda.model.FunctionConfiguration" );
195- return findAccessorOrNull (lambdaConfigurationClass , "getFunctionArn" );
196- } catch (Throwable t ) {
197- return null ;
191+ private static class LambdaFunctionConfigurationAccess {
192+ static final MethodHandle getLambdaArnFromConfiguration = findGetLambdaArnMethod ();
193+
194+ @ Nullable
195+ private static MethodHandle findGetLambdaArnMethod () {
196+ try {
197+ Class <?> lambdaConfigurationClass =
198+ Class .forName ("com.amazonaws.services.lambda.model.FunctionConfiguration" );
199+ return findAccessorOrNull (lambdaConfigurationClass , "getFunctionArn" );
200+ } catch (Throwable t ) {
201+ return null ;
202+ }
198203 }
199204 }
200205}
0 commit comments