|
16 | 16 |
|
17 | 17 | package org.springframework.boot.loader.tools; |
18 | 18 |
|
| 19 | +import java.io.ByteArrayInputStream; |
19 | 20 | import java.io.File; |
| 21 | +import java.lang.reflect.Modifier; |
20 | 22 | import java.util.ArrayList; |
21 | 23 | import java.util.List; |
22 | 24 | import java.util.jar.JarFile; |
23 | 25 |
|
| 26 | +import net.bytebuddy.ByteBuddy; |
| 27 | +import net.bytebuddy.ClassFileVersion; |
| 28 | +import net.bytebuddy.description.method.MethodDescription; |
| 29 | +import net.bytebuddy.dynamic.scaffold.InstrumentedType; |
| 30 | +import net.bytebuddy.implementation.Implementation; |
| 31 | +import net.bytebuddy.implementation.bytecode.ByteCodeAppender; |
| 32 | +import net.bytebuddy.jar.asm.MethodVisitor; |
| 33 | +import net.bytebuddy.jar.asm.Opcodes; |
24 | 34 | import org.junit.jupiter.api.BeforeEach; |
25 | 35 | import org.junit.jupiter.api.Test; |
26 | 36 | import org.junit.jupiter.api.io.TempDir; |
@@ -179,6 +189,88 @@ void doWithJarMainMethods() throws Exception { |
179 | 189 | } |
180 | 190 | } |
181 | 191 |
|
| 192 | + @Test |
| 193 | + void packagePrivateMainMethod() throws Exception { |
| 194 | + this.testJarFile.addFile("a/b/c/D.class", packagePrivateMainMethod(ClassFileVersion.JAVA_V25)); |
| 195 | + ClassNameCollector callback = new ClassNameCollector(); |
| 196 | + try (JarFile jarFile = this.testJarFile.getJarFile()) { |
| 197 | + MainClassFinder.doWithMainClasses(jarFile, null, callback); |
| 198 | + assertThat(callback.getClassNames()).hasToString("[a.b.c.D]"); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + @Test |
| 203 | + void packagePrivateMainMethodBeforeJava25() throws Exception { |
| 204 | + this.testJarFile.addFile("a/b/c/D.class", packagePrivateMainMethod(ClassFileVersion.JAVA_V24)); |
| 205 | + ClassNameCollector callback = new ClassNameCollector(); |
| 206 | + try (JarFile jarFile = this.testJarFile.getJarFile()) { |
| 207 | + MainClassFinder.doWithMainClasses(jarFile, null, callback); |
| 208 | + assertThat(callback.getClassNames()).isEmpty(); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + void parameterlessMainMethod() throws Exception { |
| 214 | + this.testJarFile.addFile("a/b/c/D.class", parameterlessMainMethod(ClassFileVersion.JAVA_V25)); |
| 215 | + ClassNameCollector callback = new ClassNameCollector(); |
| 216 | + try (JarFile jarFile = this.testJarFile.getJarFile()) { |
| 217 | + MainClassFinder.doWithMainClasses(jarFile, null, callback); |
| 218 | + assertThat(callback.getClassNames()).hasToString("[a.b.c.D]"); |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + void parameterlessMainMethodBeforeJava25() throws Exception { |
| 224 | + this.testJarFile.addFile("a/b/c/D.class", parameterlessMainMethod(ClassFileVersion.JAVA_V24)); |
| 225 | + ClassNameCollector callback = new ClassNameCollector(); |
| 226 | + try (JarFile jarFile = this.testJarFile.getJarFile()) { |
| 227 | + MainClassFinder.doWithMainClasses(jarFile, null, callback); |
| 228 | + assertThat(callback.getClassNames()).isEmpty(); |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + private ByteArrayInputStream packagePrivateMainMethod(ClassFileVersion classFileVersion) { |
| 233 | + byte[] bytecode = new ByteBuddy(classFileVersion).subclass(Object.class) |
| 234 | + .defineMethod("main", void.class, Modifier.STATIC) |
| 235 | + .withParameter(String[].class) |
| 236 | + .intercept(new EmptyBodyImplementation()) |
| 237 | + .make() |
| 238 | + .getBytes(); |
| 239 | + return new ByteArrayInputStream(bytecode); |
| 240 | + } |
| 241 | + |
| 242 | + private ByteArrayInputStream parameterlessMainMethod(ClassFileVersion classFileVersion) { |
| 243 | + byte[] bytecode = new ByteBuddy(classFileVersion).subclass(Object.class) |
| 244 | + .defineMethod("main", void.class, Modifier.STATIC | Modifier.PUBLIC) |
| 245 | + .intercept(new EmptyBodyImplementation()) |
| 246 | + .make() |
| 247 | + .getBytes(); |
| 248 | + return new ByteArrayInputStream(bytecode); |
| 249 | + } |
| 250 | + |
| 251 | + static class EmptyBodyImplementation implements Implementation { |
| 252 | + |
| 253 | + @Override |
| 254 | + public InstrumentedType prepare(InstrumentedType instrumentedType) { |
| 255 | + return instrumentedType; |
| 256 | + } |
| 257 | + |
| 258 | + @Override |
| 259 | + public ByteCodeAppender appender(Target implementationTarget) { |
| 260 | + return new ByteCodeAppender() { |
| 261 | + |
| 262 | + @Override |
| 263 | + public Size apply(MethodVisitor methodVisitor, Context implementationContext, |
| 264 | + MethodDescription instrumentedMethod) { |
| 265 | + methodVisitor.visitInsn(Opcodes.RETURN); |
| 266 | + return Size.ZERO; |
| 267 | + } |
| 268 | + |
| 269 | + }; |
| 270 | + } |
| 271 | + |
| 272 | + } |
| 273 | + |
182 | 274 | static class ClassNameCollector implements MainClassCallback<Object> { |
183 | 275 |
|
184 | 276 | private final List<String> classNames = new ArrayList<>(); |
|
0 commit comments