Skip to content

Commit e1d220c

Browse files
[#978] remove ClassFactory.cleanCache() and replace threadLocal map with classic map
1 parent ccae74a commit e1d220c

File tree

2 files changed

+7
-38
lines changed

2 files changed

+7
-38
lines changed

jaxb-ri/core/src/main/java/org/glassfish/jaxb/core/v2/ClassFactory.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2025 Contributors to the Eclipse Foundation. All rights reserved.
34
*
45
* This program and the accompanying materials are made available under the
56
* terms of the Eclipse Distribution License v. 1.0, which is available at
@@ -42,35 +43,17 @@ public final class ClassFactory {
4243

4344
/**
4445
* Cache from a class to its default constructor.
45-
*
46-
* To avoid synchronization among threads, we use {@link ThreadLocal}.
4746
*/
48-
private static final ThreadLocal<Map<Class, WeakReference<Constructor>>> tls = new ThreadLocal<>() {
49-
@Override
50-
public Map<Class, WeakReference<Constructor>> initialValue() {
51-
return new WeakHashMap<>();
52-
}
53-
};
47+
private static final Map<Class, WeakReference<Constructor>> constructorClassCache = new WeakHashMap<>();
5448

5549
private ClassFactory() {}
5650

57-
public static void cleanCache() {
58-
if (tls != null) {
59-
try {
60-
tls.remove();
61-
} catch (Exception e) {
62-
logger.log(Level.WARNING, "Unable to clean Thread Local cache of classes used in Unmarshaller: {0}", e.getLocalizedMessage());
63-
}
64-
}
65-
}
66-
6751
/**
6852
* Creates a new instance of the class but throw exceptions without catching it.
6953
*/
7054
public static <T> T create0( final Class<T> clazz ) throws IllegalAccessException, InvocationTargetException, InstantiationException {
71-
Map<Class,WeakReference<Constructor>> m = tls.get();
7255
Constructor<T> cons = null;
73-
WeakReference<Constructor> consRef = m.get(clazz);
56+
WeakReference<Constructor> consRef = constructorClassCache.get(clazz);
7457
if(consRef!=null)
7558
cons = consRef.get();
7659
if(cons==null) {
@@ -98,7 +81,7 @@ public Constructor<T> run() {
9881
}
9982
}
10083

101-
m.put(clazz,new WeakReference<>(cons));
84+
constructorClassCache.put(clazz, new WeakReference<>(cons));
10285
}
10386

10487
return cons.newInstance(emptyObject);

jaxb-ri/runtime/impl/src/main/java/org/glassfish/jaxb/runtime/v2/runtime/unmarshaller/UnmarshallerImpl.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2025 Contributors to the Eclipse Foundation. All rights reserved.
34
*
45
* This program and the accompanying materials are made available under the
56
* terms of the Eclipse Distribution License v. 1.0, which is available at
@@ -14,7 +15,6 @@
1415
import org.glassfish.jaxb.runtime.api.ClassResolver;
1516
import org.glassfish.jaxb.core.unmarshaller.DOMScanner;
1617
import org.glassfish.jaxb.core.unmarshaller.InfosetScanner;
17-
import org.glassfish.jaxb.core.v2.ClassFactory;
1818
import org.glassfish.jaxb.runtime.v2.runtime.AssociationMap;
1919
import org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl;
2020
import org.glassfish.jaxb.runtime.v2.runtime.JaxBeanInfo;
@@ -58,7 +58,7 @@
5858
* @author
5959
* <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
6060
*/
61-
public final class UnmarshallerImpl extends AbstractUnmarshallerImpl implements ValidationEventHandler, Closeable
61+
public final class UnmarshallerImpl extends AbstractUnmarshallerImpl implements ValidationEventHandler, Closeable
6262
{
6363
/** Owning {@link JAXBContext} */
6464
protected final JAXBContextImpl context;
@@ -574,23 +574,9 @@ public void setListener(Listener listener) {
574574
public UnmarshallingContext getContext() {
575575
return coordinator;
576576
}
577-
578-
@Override
579-
@SuppressWarnings("FinalizeDeclaration")
580-
protected void finalize() throws Throwable {
581-
try {
582-
ClassFactory.cleanCache();
583-
} finally {
584-
super.finalize();
585-
}
586-
}
587577

588-
/**
589-
* Must be called from same thread which created the UnmarshallerImpl instance.
590-
*/
591578
@Override
592579
public void close() throws IOException {
593-
ClassFactory.cleanCache();
594580
}
595-
581+
596582
}

0 commit comments

Comments
 (0)