Skip to content

Commit bb7cd0d

Browse files
committed
Add JavaLangAccess utility and basic test
Introduce `JavaLangAccessUtils` to centralize access to JavaLangAccess internals across JDK versions by resolving `sun.misc.JavaLangAccess` first and falling back to `jdk.internal.access.JavaLangAccess`. Add `JavaLangAccessUtilsTest` to verify the class-name constants and resolved class are available.
1 parent dfc97f4 commit bb7cd0d

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.microsphere.internal.access;
19+
20+
import io.microsphere.annotation.Nonnull;
21+
import io.microsphere.util.Utils;
22+
23+
import static io.microsphere.util.ClassLoaderUtils.loadClass;
24+
import static io.microsphere.util.ObjectUtils.defaultIfNull;
25+
import static java.lang.ClassLoader.getSystemClassLoader;
26+
27+
/**
28+
* The utility class for {@link sun.misc.JavaLangAccess} or {@link jdk.internal.access.JavaLangAccess}
29+
*
30+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
31+
* @see sun.misc.JavaLangAccess
32+
* @see jdk.internal.access.JavaLangAccess
33+
* @since 1.0.0
34+
*/
35+
public abstract class JavaLangAccessUtils implements Utils {
36+
37+
/**
38+
* The class name of {@link sun.misc.JavaLangAccess}
39+
*/
40+
public static final String LEGACY_JAVA_LANG_ACCESS_CLASS_NAME = "sun.misc.JavaLangAccess";
41+
42+
/**
43+
* The class name of {@link jdk.internal.access.JavaLangAccess}
44+
*/
45+
public static final String JAVA_LANG_ACCESS_CLASS_NAME = "jdk.internal.access.JavaLangAccess";
46+
47+
/**
48+
* The {@link Class} of {@link sun.misc.JavaLangAccess} or {@link jdk.internal.access.JavaLangAccess}
49+
*/
50+
@Nonnull
51+
public static final Class<?> JAVA_LANG_ACCESS_CLASS = loadJavaLangAccessClass();
52+
53+
private static Class<?> loadJavaLangAccessClass() {
54+
ClassLoader classLoader = getSystemClassLoader();
55+
Class<?> javaLangAccessClass = loadClass(classLoader, LEGACY_JAVA_LANG_ACCESS_CLASS_NAME);
56+
return defaultIfNull(javaLangAccessClass, () -> loadClass(classLoader, JAVA_LANG_ACCESS_CLASS_NAME));
57+
}
58+
59+
private JavaLangAccessUtils() {
60+
}
61+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.microsphere.internal.access;
19+
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import static io.microsphere.internal.access.JavaLangAccessUtils.JAVA_LANG_ACCESS_CLASS;
24+
import static io.microsphere.internal.access.JavaLangAccessUtils.JAVA_LANG_ACCESS_CLASS_NAME;
25+
import static io.microsphere.internal.access.JavaLangAccessUtils.LEGACY_JAVA_LANG_ACCESS_CLASS_NAME;
26+
import static org.junit.jupiter.api.Assertions.assertNotNull;
27+
28+
/**
29+
* {@link JavaLangAccessUtils} Test
30+
*
31+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
32+
* @see JavaLangAccessUtils
33+
* @since 1.0.0
34+
*/
35+
class JavaLangAccessUtilsTest {
36+
37+
@Test
38+
void testConstants() {
39+
assertNotNull(LEGACY_JAVA_LANG_ACCESS_CLASS_NAME);
40+
assertNotNull(JAVA_LANG_ACCESS_CLASS_NAME);
41+
assertNotNull(JAVA_LANG_ACCESS_CLASS);
42+
}
43+
}

0 commit comments

Comments
 (0)