Skip to content

Commit

Permalink
Allow deeper reflection for getDeclaredDeconstructors
Browse files Browse the repository at this point in the history
  • Loading branch information
biboudis committed Jun 20, 2024
1 parent 0eab5db commit 5d18264
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/java.base/share/classes/java/lang/Class.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import jdk.internal.access.SharedSecrets;
import jdk.internal.classfile.impl.BoundAttribute;
import jdk.internal.constant.ConstantUtils;
import jdk.internal.javac.PreviewFeature;
Expand Down Expand Up @@ -2469,7 +2470,7 @@ private Deconstructor<?>[] getDeclaredDeconstructors0(Class<?>[] params, int whi
ByteBuffer assembled_rvta = getAnnotationContents(rvta != null, (BoundAttribute) rvta);

for (int i = 0; i < parameterList.size(); i++) {
Class<?> bindingClass = parameterList.get(i).resolveConstantDesc(MethodHandles.lookup());
Class<?> bindingClass = parameterList.get(i).resolveConstantDesc(MethodHandles.privateLookupIn(this, SharedSecrets.getJavaLangInvokeAccess().implLookup()));
deconstructorBindings.add(new PatternBinding(
currentDeconstructor,
parameterNameList.get(i),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,9 @@ public MethodHandle serializableConstructor(Class<?> decl, Constructor<?> ctorTo
return IMPL_LOOKUP.serializableConstructor(decl, ctorToCall);
}

@Override
public Lookup implLookup() { return IMPL_LOOKUP; }

});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,7 @@ public interface JavaLangInvokeAccess {
* This method should only be used by ReflectionFactory::newConstructorForSerialization.
*/
MethodHandle serializableConstructor(Class<?> decl, Constructor<?> ctorToCall) throws IllegalAccessException;

// TODO: extra check if needed
Lookup implLookup();
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static void main(String[] args) throws NoSuchPatternException, IllegalAcc
testDeconstructorElementsAnnotations();
testDeconstructorAnnotations();
testGenericString();
testGetDeclaredDeconstructors_bug1();
}

public static void testGetMethods() {
Expand Down Expand Up @@ -167,12 +168,30 @@ public static void testGenericString() throws NoSuchPatternException{
assertEquals(method.toGenericString(), "public pattern SimpleDeconstructorsTest$Person1(java.util.List<java.lang.Character>)");
}

public static void testGetDeclaredDeconstructors_bug1() {
Deconstructor<?>[] methods = B.class.getDeclaredDeconstructors();

assertEquals(methods.length, 1);
}

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

class A {
String s;
public A(String s) { this.s = s; }
public pattern A(String s) { match A(this.s); }
}

class B {
A a;
public B(A a) { this.a = a; }
public pattern B(A a) { match B(this.a); }
}

public static class Person1 {
private final String name;
private final String username;
Expand Down

0 comments on commit 5d18264

Please sign in to comment.