File tree Expand file tree Collapse file tree 2 files changed +38
-3
lines changed
main/java/net/bytebuddy/matcher
test/java/net/bytebuddy/matcher Expand file tree Collapse file tree 2 files changed +38
-3
lines changed Original file line number Diff line number Diff line change @@ -1595,13 +1595,36 @@ public static <T extends MethodDescription> ElementMatcher.Junction<T> isDefault
15951595 }
15961596
15971597 /**
1598- * Matches a Java <i>main</i> method as an application entry point.
1598+ * Matches a Java <i>main</i> method as an application entry point. This method matches all methods that
1599+ * qualify as main method since Java 25.
15991600 *
16001601 * @param <T> The type of the matched object.
16011602 * @return A matcher that matches a Java <i>main</i> method.
16021603 */
16031604 public static <T extends MethodDescription > ElementMatcher .Junction <T > isMain () {
1604- return named ("main" ).and (takesArguments (String [].class )).and (returns (TypeDescription .ForLoadedType .of (void .class )).and (isStatic ()).and (isPublic ()));
1605+ return isMain (true );
1606+ }
1607+
1608+ /**
1609+ * Matches a Java <i>main</i> method as an application entry point.
1610+ *
1611+ * @param modernized {@code true} if modernized main methods as specified since Java 25 should be matched.
1612+ * @param <T> The type of the matched object.
1613+ * @return A matcher that matches a Java <i>main</i> method.
1614+ */
1615+ public static <T extends MethodDescription > ElementMatcher .Junction <T > isMain (boolean modernized ) {
1616+ if (modernized ) {
1617+ return named ("main" )
1618+ .and (takesArguments (String [].class ).or (takesArguments (0 )))
1619+ .and (returns (TypeDescription .ForLoadedType .of (void .class )))
1620+ .and (not (isPrivate ()));
1621+ } else {
1622+ return named ("main" )
1623+ .and (takesArguments (String [].class ))
1624+ .and (returns (TypeDescription .ForLoadedType .of (void .class )))
1625+ .and (isStatic ())
1626+ .and (isPublic ());
1627+ }
16051628 }
16061629
16071630 /**
Original file line number Diff line number Diff line change @@ -938,9 +938,17 @@ public void testIsVirtual() throws Exception {
938938
939939 @ Test
940940 public void testIsMain () throws Exception {
941+ assertThat (ElementMatchers .isMain (false )
942+ .matches (new MethodDescription .ForLoadedMethod (MainMethod .class .getDeclaredMethod ("main" , String [].class ))), is (true ));
941943 assertThat (ElementMatchers .isMain ()
942944 .matches (new MethodDescription .ForLoadedMethod (MainMethod .class .getDeclaredMethod ("main" , String [].class ))), is (true ));
943- assertThat (ElementMatchers .isFinalizer ()
945+ assertThat (ElementMatchers .isMain (false )
946+ .matches (new MethodDescription .ForLoadedMethod (MainMethod .class .getDeclaredMethod ("main" ))), is (false ));
947+ assertThat (ElementMatchers .isMain ()
948+ .matches (new MethodDescription .ForLoadedMethod (MainMethod .class .getDeclaredMethod ("main" ))), is (true ));
949+ assertThat (ElementMatchers .isMain (false )
950+ .matches (new MethodDescription .ForLoadedMethod (Object .class .getDeclaredMethod ("toString" ))), is (false ));
951+ assertThat (ElementMatchers .isMain ()
944952 .matches (new MethodDescription .ForLoadedMethod (Object .class .getDeclaredMethod ("toString" ))), is (false ));
945953 }
946954
@@ -1577,6 +1585,10 @@ void method() {
15771585
15781586 private static class MainMethod {
15791587
1588+ void main () {
1589+ /* empty */
1590+ }
1591+
15801592 public static void main (String [] args ) {
15811593 /* empty */
15821594 }
You can’t perform that action at this time.
0 commit comments