Skip to content

Commit 8b8ef56

Browse files
committed
Avoid use of types that are deprecated for removal, and add profiles for Java 26.
1 parent 202907b commit 8b8ef56

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed

byte-buddy-agent/src/main/java/net/bytebuddy/agent/Installer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private Installer() {
5252
/**
5353
* <p>
5454
* Returns the instrumentation that was loaded by the Byte Buddy agent. When a security manager is active,
55-
* the {@link RuntimePermission} for {@code getInstrumentation} is required by the caller.
55+
* the {@code RuntimePermission} for {@code getInstrumentation} is required by the caller.
5656
* </p>
5757
* <p>
5858
* <b>Important</b>: This method must only be invoked via the {@link ClassLoader#getSystemClassLoader()} where any
@@ -65,9 +65,12 @@ public static Instrumentation getInstrumentation() {
6565
try {
6666
Object securityManager = System.class.getMethod("getSecurityManager").invoke(null);
6767
if (securityManager != null) {
68+
Object permission = Class.forName("java.lang.RuntimePermission")
69+
.getConstructor(String.class)
70+
.newInstance("net.bytebuddy.agent.getInstrumentation");
6871
Class.forName("java.lang.SecurityManager")
6972
.getMethod("checkPermission", Permission.class)
70-
.invoke(securityManager, new RuntimePermission("net.bytebuddy.agent.getInstrumentation"));
73+
.invoke(securityManager, permission);
7174
}
7275
} catch (NoSuchMethodException ignored) {
7376
/* security manager not available on current VM */
@@ -82,6 +85,8 @@ public static Instrumentation getInstrumentation() {
8285
}
8386
} catch (IllegalAccessException exception) {
8487
throw new IllegalStateException("Failed to access security manager", exception);
88+
} catch (InstantiationException exception) {
89+
throw new IllegalStateException("Failed to instantiate runtime permission", exception);
8590
}
8691
Instrumentation instrumentation = Installer.instrumentation;
8792
if (instrumentation == null) {

byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/loading/ClassInjector.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@
4646
import java.io.IOException;
4747
import java.io.OutputStream;
4848
import java.lang.instrument.Instrumentation;
49-
import java.lang.reflect.*;
49+
import java.lang.reflect.AccessibleObject;
50+
import java.lang.reflect.Field;
51+
import java.lang.reflect.InvocationTargetException;
52+
import java.lang.reflect.Method;
5053
import java.net.URL;
5154
import java.security.Permission;
5255
import java.security.PrivilegedAction;
@@ -73,15 +76,32 @@
7376
public interface ClassInjector {
7477

7578
/**
76-
* A permission for the {@code suppressAccessChecks} permission.
79+
* A permission for the {@code suppressAccessChecks} permission or {@code null} if not supported.
7780
*/
78-
Permission SUPPRESS_ACCESS_CHECKS = new ReflectPermission("suppressAccessChecks");
81+
@MaybeNull
82+
Permission SUPPRESS_ACCESS_CHECKS = suppressAccessChecks();
7983

8084
/**
8185
* Determines the default behavior for type injections when a type is already loaded.
8286
*/
8387
boolean ALLOW_EXISTING_TYPES = false;
8488

89+
/**
90+
* Creates a permission for the {@code suppressAccessChecks} permission or {@code null} if not supported.
91+
*
92+
* @return A permission for the {@code suppressAccessChecks} permission or {@code null} if not supported.
93+
*/
94+
@MaybeNull
95+
static Permission suppressAccessChecks() {
96+
try {
97+
return (Permission) Class.forName("java.lang.reflect.ReflectPermission")
98+
.getConstructor(String.class)
99+
.newInstance("suppressAccessChecks");
100+
} catch (Exception ignored) {
101+
return null;
102+
}
103+
}
104+
85105
/**
86106
* Indicates if this class injector is available on the current VM.
87107
*

byte-buddy-dep/src/main/java/net/bytebuddy/utility/dispatcher/JavaDispatcher.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,12 @@ public T run() {
181181
try {
182182
Object securityManager = System.class.getMethod("getSecurityManager").invoke(null);
183183
if (securityManager != null) {
184+
Object permission = Class.forName("java.lang.RuntimePermission")
185+
.getConstructor(String.class)
186+
.newInstance("net.bytebuddy.createJavaDispatcher");
184187
Class.forName("java.lang.SecurityManager")
185188
.getMethod("checkPermission", Permission.class)
186-
.invoke(securityManager, new RuntimePermission("net.bytebuddy.createJavaDispatcher"));
189+
.invoke(securityManager, permission);
187190
}
188191
} catch (NoSuchMethodException ignored) {
189192
/* security manager not available on current VM */
@@ -198,6 +201,8 @@ public T run() {
198201
}
199202
} catch (IllegalAccessException exception) {
200203
throw new IllegalStateException("Failed to access security manager", exception);
204+
} catch (InstantiationException exception) {
205+
throw new IllegalStateException("Failed to instantiate runtime permission", exception);
201206
}
202207
Map<Method, Dispatcher> dispatchers = generate
203208
? new LinkedHashMap<Method, Dispatcher>()

pom.xml

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
<bytecode.test.version>1.6</bytecode.test.version>
7171
<pitest.target>net.bytebuddy</pitest.target>
7272
<version.asm>9.8</version.asm>
73-
<version.asmjdkbridge>0.0.10</version.asmjdkbridge>
73+
<version.asmjdkbridge>0.0.11</version.asmjdkbridge>
7474
<version.jna>5.12.1</version.jna>
7575
<version.junit>4.13.2</version.junit>
7676
<version.mockito>2.28.2</version.mockito>
@@ -812,6 +812,21 @@
812812
<jacoco.skip>true</jacoco.skip>
813813
</properties>
814814
</profile>
815+
<!-- Runs the build with compatibility for Java 26 JVMs. -->
816+
<profile>
817+
<id>java26-compatibility</id>
818+
<activation>
819+
<activeByDefault>false</activeByDefault>
820+
<jdk>26</jdk>
821+
</activation>
822+
<properties>
823+
<sourcecode.main.version>8</sourcecode.main.version>
824+
<sourcecode.test.version>8</sourcecode.test.version>
825+
<bytecode.main.version>8</bytecode.main.version>
826+
<bytecode.test.version>8</bytecode.test.version>
827+
<jacoco.skip>true</jacoco.skip>
828+
</properties>
829+
</profile>
815830
<!-- Builds using a byte code target for Java 6. -->
816831
<profile>
817832
<id>java6</id>
@@ -1039,6 +1054,18 @@
10391054
<spotbugs.skip>true</spotbugs.skip>
10401055
</properties>
10411056
</profile>
1057+
<!-- Builds using a byte code target for Java 26. -->
1058+
<profile>
1059+
<id>java26</id>
1060+
<activation>
1061+
<activeByDefault>false</activeByDefault>
1062+
</activation>
1063+
<properties>
1064+
<bytecode.main.version>26</bytecode.main.version>
1065+
<bytecode.test.version>26</bytecode.test.version>
1066+
<spotbugs.skip>true</spotbugs.skip>
1067+
</properties>
1068+
</profile>
10421069
<!-- Enables dynamic agent loading when running a JDK 22 or newer. -->
10431070
<profile>
10441071
<id>surefire-enable-dynamic-attach</id>

0 commit comments

Comments
 (0)