Skip to content

[#978] remove ClassFactory.cleanCache() in finalize method #1837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2025 Contributors to the Eclipse Foundation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand All @@ -17,6 +18,7 @@
import java.lang.ref.WeakReference;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.logging.Level;
Expand All @@ -42,35 +44,17 @@ public final class ClassFactory {

/**
* Cache from a class to its default constructor.
*
* To avoid synchronization among threads, we use {@link ThreadLocal}.
*/
private static final ThreadLocal<Map<Class, WeakReference<Constructor>>> tls = new ThreadLocal<>() {
@Override
public Map<Class, WeakReference<Constructor>> initialValue() {
return new WeakHashMap<>();
}
};
private static final Map<Class, WeakReference<Constructor>> constructorClassCache = Collections.synchronizedMap(new WeakHashMap<>());

private ClassFactory() {}

public static void cleanCache() {
if (tls != null) {
try {
tls.remove();
} catch (Exception e) {
logger.log(Level.WARNING, "Unable to clean Thread Local cache of classes used in Unmarshaller: {0}", e.getLocalizedMessage());
}
}
}

/**
* Creates a new instance of the class but throw exceptions without catching it.
*/
public static <T> T create0( final Class<T> clazz ) throws IllegalAccessException, InvocationTargetException, InstantiationException {
Map<Class,WeakReference<Constructor>> m = tls.get();
Constructor<T> cons = null;
WeakReference<Constructor> consRef = m.get(clazz);
WeakReference<Constructor> consRef = constructorClassCache.get(clazz);
if(consRef!=null)
cons = consRef.get();
if(cons==null) {
Expand Down Expand Up @@ -98,7 +82,7 @@ public Constructor<T> run() {
}
}

m.put(clazz,new WeakReference<>(cons));
constructorClassCache.put(clazz, new WeakReference<>(cons));
}

return cons.newInstance(emptyObject);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2025 Contributors to the Eclipse Foundation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand All @@ -14,7 +15,6 @@
import org.glassfish.jaxb.runtime.api.ClassResolver;
import org.glassfish.jaxb.core.unmarshaller.DOMScanner;
import org.glassfish.jaxb.core.unmarshaller.InfosetScanner;
import org.glassfish.jaxb.core.v2.ClassFactory;
import org.glassfish.jaxb.runtime.v2.runtime.AssociationMap;
import org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl;
import org.glassfish.jaxb.runtime.v2.runtime.JaxBeanInfo;
Expand Down Expand Up @@ -58,7 +58,7 @@
* @author
* <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
*/
public final class UnmarshallerImpl extends AbstractUnmarshallerImpl implements ValidationEventHandler, Closeable
public final class UnmarshallerImpl extends AbstractUnmarshallerImpl implements ValidationEventHandler, Closeable
{
/** Owning {@link JAXBContext} */
protected final JAXBContextImpl context;
Expand Down Expand Up @@ -574,23 +574,9 @@ public void setListener(Listener listener) {
public UnmarshallingContext getContext() {
return coordinator;
}

@Override
@SuppressWarnings("FinalizeDeclaration")
protected void finalize() throws Throwable {
try {
ClassFactory.cleanCache();
} finally {
super.finalize();
}
}

/**
* Must be called from same thread which created the UnmarshallerImpl instance.
*/
@Override
public void close() throws IOException {
ClassFactory.cleanCache();
}

}
Loading