Skip to content

Commit 15429f0

Browse files
authored
Scan lambda handler classes that do not implement an interface (#25)
1 parent c83fce6 commit 15429f0

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

src/main/java/software/amazon/lambda/snapstart/ByteCodeIntrospector.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,29 @@ private static Set<String> setOf(String ... strings) {
4949
};
5050

5151
boolean isLambdaHandler(XClass xClass) {
52-
return implementsLambdaInterface(xClass) || hasLambdaHandlerMethod(xClass);
52+
return implementsLambdaInterface(xClass) ||
53+
hasLambdaHandlerMethod(xClass) ||
54+
(hasHandlerInClassName(xClass) && hasHandleRequestMethod(xClass));
55+
}
56+
57+
/**
58+
* Returns true if the class has the word "Handler" in the name.
59+
*/
60+
private boolean hasHandlerInClassName(XClass xClass) {
61+
return xClass.toString().contains("Handler");
62+
}
63+
64+
/**
65+
* Returns true if the class has a method called "handleRequest"
66+
*/
67+
private boolean hasHandleRequestMethod(XClass xClass) {
68+
List<? extends XMethod> methods = xClass.getXMethods();
69+
for (XMethod method : methods) {
70+
if (method.getName().equals("handleRequest")) {
71+
return true;
72+
}
73+
}
74+
return false;
5375
}
5476

5577
boolean hasLambdaHandlerMethod(XClass xClass) {

src/test/java/software/amazon/lambda/snapstart/LambdaHandlerInitedWithRandomValueTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ public void testClassImplementingFunctionalInterface() {
158158
assertThat(bugCollection, containsExactly(1, snapStartBugMatcher().inClass("ImplementsFunctionalInterface").atField("random").atLine(8).build()));
159159
}
160160

161+
@Test
162+
public void testLambdaWithNoInterface() {
163+
BugCollection bugCollection = findBugsInClasses("LambdaHandlerWithNoInterface");
164+
assertThat(bugCollection, containsExactly(1, snapStartBugMatcher().inClass("LambdaHandlerWithNoInterface").atField("random").atLine(7).build()));
165+
}
166+
161167
@Test
162168
public void testLambdaWithDependencyInjection() {
163169
BugCollection bugCollection = findBugsInClasses("DependencyInjection", "MyDependency");
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package software.amazon.lambda.snapstart.lambdaexamples;
2+
3+
import java.util.UUID;
4+
5+
public class LambdaHandlerWithNoInterface {
6+
7+
private final UUID random = UUID.randomUUID();
8+
9+
public String handleRequest(){
10+
return random.toString();
11+
}
12+
}

0 commit comments

Comments
 (0)