88import java .lang .invoke .MethodHandle ;
99import java .lang .invoke .MethodHandles ;
1010import java .lang .invoke .MethodType ;
11- import java .lang .reflect .Method ;
1211import javax .annotation .Nullable ;
1312
1413final class RequestAccess {
14+ private static final String LAMBDA_REQUEST_CLASS_PREFIX = "com.amazonaws.services.lambda.model." ;
1515 private static final String SECRETS_MANAGER_REQUEST_CLASS_PREFIX =
1616 "com.amazonaws.services.secretsmanager.model." ;
17- private static final String LAMBDA_REQUEST_CLASS_PREFIX = "com.amazonaws.services.lambda.model." ;
1817 private static final String STEP_FUNCTIONS_REQUEST_CLASS_PREFIX =
1918 "com.amazonaws.services.stepfunctions.model." ;
2019
@@ -34,11 +33,7 @@ static String getLambdaArn(Object request) {
3433 }
3534 try {
3635 Object config = access .getLambdaConfiguration .invoke (request );
37- if (config == null ) {
38- return null ;
39- }
40- Method method = config .getClass ().getMethod ("getFunctionArn" );
41- return (String ) method .invoke (config );
36+ return invokeOrNull (access .getLambdaArnFromConfiguration , config );
4237 } catch (Throwable t ) {
4338 return null ;
4439 }
@@ -130,6 +125,7 @@ private static String invokeOrNull(@Nullable MethodHandle method, Object obj) {
130125
131126 @ Nullable private final MethodHandle getBucketName ;
132127 @ Nullable private final MethodHandle getLambdaConfiguration ;
128+ @ Nullable private final MethodHandle getLambdaArnFromConfiguration ;
133129 @ Nullable private final MethodHandle getLambdaName ;
134130 @ Nullable private final MethodHandle getLambdaResourceMappingId ;
135131 @ Nullable private final MethodHandle getQueueUrl ;
@@ -153,6 +149,8 @@ private RequestAccess(Class<?> clz) {
153149
154150 boolean isLambda = clz .getName ().startsWith (LAMBDA_REQUEST_CLASS_PREFIX );
155151 getLambdaConfiguration = isLambda ? findLambdaGetConfigurationMethod (clz ) : null ;
152+ getLambdaArnFromConfiguration =
153+ getLambdaConfiguration != null ? findGetLambdaArnMethod () : null ;
156154 getLambdaName = isLambda ? findAccessorOrNull (clz , "getFunctionName" ) : null ;
157155 getLambdaResourceMappingId = isLambda ? findAccessorOrNull (clz , "getUUID" ) : null ;
158156 boolean isSecretsManager = clz .getName ().startsWith (SECRETS_MANAGER_REQUEST_CLASS_PREFIX );
@@ -188,4 +186,15 @@ private static MethodHandle findLambdaGetConfigurationMethod(Class<?> clz) {
188186 return null ;
189187 }
190188 }
189+
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 ;
198+ }
199+ }
191200}
0 commit comments