Skip to content

Commit

Permalink
Fix NPE for null ClassLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
biboudis committed Jun 24, 2024
1 parent ebf223a commit e61f2d3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/java.base/share/classes/java/lang/Class.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
import sun.reflect.annotation.*;
import sun.reflect.misc.ReflectUtil;

import static java.lang.ClassLoader.getPlatformClassLoader;
import static java.lang.constant.ConstantDescs.CD_void;

/**
Expand Down Expand Up @@ -2308,7 +2309,7 @@ public Constructor<?>[] getConstructors() throws SecurityException {
private Method[] filterOutDeconstructorsFromMethods(Method[] in) {
Set<String> isPattern = new HashSet<>();
ClassModel cm = null;
try (InputStream resource = ClassLoader.getSystemResourceAsStream(getName() + ".class")) {
try (InputStream resource = ClassLoader.getSystemResourceAsStream(getResourcePath())) {
if (resource == null) {
return in;
}
Expand Down Expand Up @@ -2413,7 +2414,8 @@ public Deconstructor<?>[] getDeclaredDeconstructors() throws SecurityException {

private Deconstructor<?>[] getDeclaredDeconstructors0(Class<?>[] params, int which) {
ArrayList<Deconstructor<?>> decs = new ArrayList<>();
try(InputStream is = ClassLoader.getSystemResourceAsStream(getName() + ".class")) {
if (this.isPrimitive()) return new Deconstructor<?>[0];
try(InputStream is = ClassLoader.getSystemResourceAsStream(getResourcePath())) {
byte[] bytes = is.readAllBytes();
ClassModel cm = ClassFile.of().parse(bytes);
for (MethodModel mm : cm.methods()) {
Expand Down Expand Up @@ -2489,6 +2491,10 @@ private Deconstructor<?>[] getDeclaredDeconstructors0(Class<?>[] params, int whi
return decs.toArray(new Deconstructor<?>[decs.size()]);
}

private String getResourcePath() {
return this.getName().replace('.', '/') + ".class";
}

private static ByteBuffer getAnnotationContents(boolean exists, BoundAttribute<?> boundAttribute) {
if (exists) {
byte rvpaBytes[] = boundAttribute.contents();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static void main(String[] args) throws NoSuchPatternException, IllegalAcc
testDeconstructorAnnotations();
testGenericString();
testGetDeclaredDeconstructors_bug1();
testGetDeclaredDeconstructors_bug2();
}

public static void testGetMethods() {
Expand Down Expand Up @@ -174,6 +175,16 @@ public static void testGetDeclaredDeconstructors_bug1() {
assertEquals(methods.length, 1);
}

public static void testGetDeclaredDeconstructors_bug2() {
Deconstructor<?>[] methods = null;

methods = String.class.getDeclaredDeconstructors();
assertEquals(methods.length, 0);

methods = int.class.getDeclaredDeconstructors();
assertEquals(methods.length, 0);
}

static void assertEquals(Object actual, Object expected) {
if (!Objects.equals(expected, actual)) {
throw new AssertionError("Expected: " + expected + ", but got: " + actual);
Expand Down

0 comments on commit e61f2d3

Please sign in to comment.