Skip to content

Commit 526eb57

Browse files
committed
Add support for new main methods.
1 parent f7c98bb commit 526eb57

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

byte-buddy-dep/src/main/java/net/bytebuddy/matcher/ElementMatchers.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff 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
/**

byte-buddy-dep/src/test/java/net/bytebuddy/matcher/ElementMatchersTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)