Skip to content

Commit bf8f2a9

Browse files
authored
Updates to ReflectionUtils Implementation (Azure#26268)
Updates to ReflectionUtils implementation
1 parent 09a1a88 commit bf8f2a9

File tree

5 files changed

+49
-22
lines changed

5 files changed

+49
-22
lines changed

sdk/core/azure-core/src/main/java9/com/azure/core/implementation/ReflectionUtils.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,29 @@ final class ReflectionUtils implements ReflectionUtilsApi {
3434
public MethodHandles.Lookup getLookupToUse(Class<?> targetClass) throws Throwable {
3535
Module responseModule = targetClass.getModule();
3636

37-
/*
38-
* First check if the response class's module is exported to all unnamed modules. If it is we will use
39-
* MethodHandles.publicLookup() which is meant for creating MethodHandle instances for publicly accessible
40-
* classes.
41-
*/
42-
if (responseModule.isExported("")) {
43-
return MethodHandles.publicLookup();
37+
// The unnamed module is opened unconditionally, have Core read it and use a private proxy lookup to enable all
38+
// lookup scenarios.
39+
if (!responseModule.isNamed()) {
40+
CORE_MODULE.addReads(responseModule);
41+
return MethodHandles.privateLookupIn(targetClass, LOOKUP);
42+
}
43+
44+
45+
// If the response module is the Core module return the Core private lookup.
46+
if (responseModule == CORE_MODULE) {
47+
return LOOKUP;
4448
}
4549

46-
/*
47-
* Otherwise, we use the MethodHandles.Lookup which is associated to this (com.azure.core) module, and more
48-
* specifically, is tied to this class (ReflectionUtils). But, in order to use this lookup we need to ensure
49-
* that the com.azure.core module reads the response class's module as the lookup won't have permissions
50-
* necessary to create the MethodHandle instance without it.
51-
*
52-
* This logic is safe due to the fact that any SDK module calling into this code path will already need to open
53-
* to com.azure.core as it needs to perform other reflective operations on classes in the module. Adding the
54-
* com.azure.core reads is handling specifically required by MethodHandle.
55-
*/
56-
if (!CORE_MODULE.canRead(responseModule)) {
50+
// Next check if the target class module is opened either unconditionally or to Core's module. If so, also use
51+
// a private proxy lookup to enable all lookup scenarios.
52+
if (responseModule.isOpen(targetClass.getPackageName())
53+
|| responseModule.isOpen(targetClass.getPackageName(), CORE_MODULE)) {
5754
CORE_MODULE.addReads(responseModule);
55+
return MethodHandles.privateLookupIn(targetClass, LOOKUP);
5856
}
5957

60-
return LOOKUP;
58+
// Otherwise, return the public lookup as there are no specialty ways to access the other module.
59+
return MethodHandles.publicLookup();
6160
}
6261

6362
public int getJavaImplementationMajorVersion() {
@@ -66,4 +65,4 @@ public int getJavaImplementationMajorVersion() {
6665

6766
ReflectionUtils() {
6867
}
69-
}
68+
}

sdk/mixedreality/azure-mixedreality-authentication/src/main/java/module-info.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@
1010
exports com.azure.mixedreality.authentication;
1111

1212
opens com.azure.mixedreality.authentication.implementation.models
13-
to com.fasterxml.jackson.databind;
14-
exports com.azure.mixedreality.authentication.implementation.models
1513
to com.fasterxml.jackson.databind, com.azure.core;
1614
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.sdk.template.implementation;
5+
6+
/**
7+
* This is an implementation sample class for template app.
8+
*/
9+
public class Hidden {
10+
/**
11+
* Implementation sample method.
12+
*
13+
* @return implementation message
14+
*/
15+
public String getImplementationMessage() {
16+
return "hidden";
17+
}
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* This package contains implementation classes for the template project.
6+
*/
7+
package com.azure.sdk.template.implementation;

sdk/template/azure-sdk-template/src/main/java/module-info.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
// Licensed under the MIT License.
33

44
module com.azure.sdk.template {
5+
// All public packages should be exported unconditionally so that all consumers may access their public APIs.
56
exports com.azure.sdk.template;
7+
8+
// Implementation packages that contain service request or response models need to be opened to azure-core
9+
// and Jackson Databind to allow their reflective operations to perform correctly.
10+
opens com.azure.sdk.template.implementation to com.azure.core, com.fasterxml.jackson.databind;
611
}

0 commit comments

Comments
 (0)