Skip to content

Commit 5961cb4

Browse files
authoredNov 8, 2019
Merge pull request #226 from theresa-m/fix_7332_invoke
Fix LUDCL optimization
2 parents 4bbb07a + 672148e commit 5961cb4

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed
 

‎src/java.base/share/classes/java/io/ObjectInputStream.java

+41-23
Original file line numberDiff line numberDiff line change
@@ -326,26 +326,30 @@ private static class Logging {
326326
* read* requests
327327
*/
328328

329-
/* ClassCache Entry for caching class.forName results upon enableClassCaching */
330-
private static final ClassCache classCache;
331-
private static final boolean isClassCachingEnabled;
332-
static {
333-
isClassCachingEnabled =
334-
AccessController.doPrivileged(new GetClassCachingSettingAction());
335-
classCache = (isClassCachingEnabled ? new ClassCache() : null);
336-
}
329+
/* ClassCache Entry for caching class.forName results upon enableClassCaching */
330+
private static final ClassCache classCache;
331+
private static final boolean isClassCachingEnabled;
332+
static {
333+
isClassCachingEnabled =
334+
AccessController.doPrivileged(new GetClassCachingSettingAction());
335+
classCache = (isClassCachingEnabled ? new ClassCache() : null);
336+
}
337337

338338

339-
/** if true LUDCL/forName results would be cached, false by default starting Java8 */
340-
private static final class GetClassCachingSettingAction
341-
implements PrivilegedAction<Boolean> {
342-
public Boolean run() {
343-
String property =
344-
System.getProperty("com.ibm.enableClassCaching", "false");
345-
return property.equalsIgnoreCase("true");
346-
}
347-
}
339+
/** if true LUDCL/forName results would be cached, true by default starting Java8 */
340+
private static final class GetClassCachingSettingAction
341+
implements PrivilegedAction<Boolean> {
342+
public Boolean run() {
343+
String property =
344+
System.getProperty("com.ibm.enableClassCaching", "true");
345+
return property.equalsIgnoreCase("true");
346+
}
347+
}
348348
private ClassLoader cachedLudcl;
349+
/* If user code is invoked in the middle of a call to readObject the cachedLudcl
350+
* must be refreshed as the ludcl could have been changed while in user code.
351+
*/
352+
private boolean refreshLudcl = false;
349353

350354
/**
351355
* Creates an ObjectInputStream that reads from the specified InputStream.
@@ -496,7 +500,7 @@ private Object readObjectImpl(Class caller)
496500
ClassLoader oldCachedLudcl = null;
497501
boolean setCached = false;
498502

499-
if ((curContext == null) && (isClassCachingEnabled)) {
503+
if (((null == curContext) || refreshLudcl) && (isClassCachingEnabled)) {
500504
oldCachedLudcl = cachedLudcl;
501505

502506
// If caller is not provided, follow the standard path to get the cachedLudcl.
@@ -509,6 +513,7 @@ private Object readObjectImpl(Class caller)
509513
}
510514

511515
setCached = true;
516+
refreshLudcl = false;
512517
}
513518

514519
// if nested read, passHandle contains handle of enclosing object
@@ -613,10 +618,11 @@ public Object readUnshared() throws IOException, ClassNotFoundException {
613618
ClassLoader oldCachedLudcl = null;
614619
boolean setCached = false;
615620

616-
if ((curContext == null) && (isClassCachingEnabled)) {
621+
if (((null == curContext) || refreshLudcl) && (isClassCachingEnabled)) {
617622
oldCachedLudcl = cachedLudcl;
618623
cachedLudcl = latestUserDefinedLoader();
619624
setCached = true;
625+
refreshLudcl = false;
620626
}
621627

622628
// if nested read, passHandle contains handle of enclosing object
@@ -793,10 +799,15 @@ protected Class<?> resolveClass(ObjectStreamClass desc)
793799
{
794800
String name = desc.getName();
795801
try {
796-
return ((classCache == null) ?
797-
Class.forName(name, false, latestUserDefinedLoader()) :
798-
classCache.get(name, cachedLudcl));
799-
802+
if (null == classCache) {
803+
return Class.forName(name, false, latestUserDefinedLoader());
804+
} else {
805+
if (refreshLudcl) {
806+
cachedLudcl = latestUserDefinedLoader();
807+
refreshLudcl = false;
808+
}
809+
return classCache.get(name, cachedLudcl);
810+
}
800811
} catch (ClassNotFoundException ex) {
801812
Class<?> cl = primClasses.get(name);
802813
if (cl != null) {
@@ -2199,6 +2210,8 @@ private Object readOrdinaryObject(boolean unshared)
21992210
handles.lookupException(passHandle) == null &&
22002211
desc.hasReadResolveMethod())
22012212
{
2213+
/* user code is invoked */
2214+
refreshLudcl = true;
22022215
Object rep = desc.invokeReadResolve(obj);
22032216
if (unshared && rep.getClass().isArray()) {
22042217
rep = cloneArray(rep);
@@ -2319,6 +2332,9 @@ private void readSerialData(Object obj, ObjectStreamClass desc)
23192332
curContext = new SerialCallbackContext(obj, slotDesc);
23202333

23212334
bin.setBlockDataMode(true);
2335+
2336+
/* user code is invoked */
2337+
refreshLudcl = true;
23222338
slotDesc.invokeReadObject(obj, this);
23232339
} catch (ClassNotFoundException ex) {
23242340
/*
@@ -2371,6 +2387,8 @@ private void readSerialData(Object obj, ObjectStreamClass desc)
23712387
slotDesc.hasReadObjectNoDataMethod() &&
23722388
handles.lookupException(passHandle) == null)
23732389
{
2390+
/* user code is invoked */
2391+
refreshLudcl = true;
23742392
slotDesc.invokeReadObjectNoData(obj);
23752393
}
23762394
}

0 commit comments

Comments
 (0)
Please sign in to comment.