use of java.lang.ref.Reference in project brave by openzipkin.
the class MutableSpanMapTest method remove_resolvesHashCodeCollisions.
@Test
public void remove_resolvesHashCodeCollisions() {
// intentionally clash on hashCode, but not equals
TraceContext context1 = context.toBuilder().spanId(1).build();
TraceContext context2 = context.toBuilder().spanId(-2L).build();
// sanity check
assertThat(context1.hashCode()).isEqualTo(context2.hashCode());
assertThat(context1).isNotEqualTo(context2);
map.getOrCreate(context1);
map.getOrCreate(context2);
map.remove(context1);
assertThat(map.delegate.keySet()).extracting(o -> ((Reference) o).get()).containsOnly(context2);
}
use of java.lang.ref.Reference in project Payara by payara.
the class WebappClassLoader method checkThreadLocalMapForLeaks.
/**
* Analyzes the given thread local map object. Also pass in the field that
* points to the internal table to save re-calculating it on every
* call to this method.
*/
private void checkThreadLocalMapForLeaks(Object map, Field internalTableField) throws IllegalAccessException, NoSuchFieldException {
if (map != null) {
Object[] table = (Object[]) internalTableField.get(map);
if (table != null) {
for (int j = 0; j < table.length; j++) {
if (table[j] != null) {
boolean potentialLeak = false;
// Check the key
Object key = ((Reference<?>) table[j]).get();
if (this.equals(key) || loadedByThisOrChild(key)) {
potentialLeak = true;
}
// Check the value
Field valueField = table[j].getClass().getDeclaredField("value");
valueField.setAccessible(true);
Object value = valueField.get(table[j]);
if (this.equals(value) || loadedByThisOrChild(value)) {
potentialLeak = true;
}
if (potentialLeak) {
Object[] args = new Object[5];
args[0] = contextName;
if (key != null) {
args[1] = getPrettyClassName(key.getClass());
try {
args[2] = key.toString();
} catch (Exception e) {
logger.log(Level.SEVERE, getString(LogFacade.CHECK_THREAD_LOCALS_FOR_LEAKS_BAD_KEY, args[1]), e);
args[2] = getString(LogFacade.CHECK_THREAD_LOCALS_FOR_LEAKS_UNKNOWN);
}
}
if (value != null) {
args[3] = getPrettyClassName(value.getClass());
try {
args[4] = value.toString();
} catch (Exception e) {
logger.log(Level.SEVERE, getString(LogFacade.CHECK_THREAD_LOCALS_FOR_LEAKS_BAD_VALUE, args[3]), e);
args[4] = getString(LogFacade.CHECK_THREAD_LOCALS_FOR_LEAKS_UNKNOWN);
}
}
if (value == null) {
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, getString(LogFacade.CHECK_THREAD_LOCALS_FOR_LEAKS_DEBUG, args));
}
} else {
logger.log(Level.SEVERE, getString(LogFacade.CHECK_THREAD_LOCALS_FOR_LEAKS, args));
}
}
}
}
}
}
}
use of java.lang.ref.Reference in project graal by oracle.
the class HotSpotGraalMBean method optionsFor.
public OptionValues optionsFor(OptionValues initialValues, ResolvedJavaMethod forMethod) {
ensureRegistered(true);
if (forMethod instanceof HotSpotResolvedJavaMethod) {
HotSpotResolvedObjectType type = ((HotSpotResolvedJavaMethod) forMethod).getDeclaringClass();
if (type instanceof HotSpotResolvedJavaType) {
Class<?> clazz = ((HotSpotResolvedJavaType) type).mirror();
Reference<ClassLoader> addNewRef = new WeakReference<>(clazz.getClassLoader());
if (!loaders.contains(addNewRef)) {
EconomicSet<Reference<ClassLoader>> newLoaders = EconomicSet.create(RefEquivalence.INSTANCE, loaders);
newLoaders.add(addNewRef);
this.loaders = newLoaders;
}
}
}
return currentMap(initialValues, forMethod);
}
use of java.lang.ref.Reference in project graal by oracle.
the class HotSpotGraalMBean method dumpMethod.
public void dumpMethod(String className, String methodName, String filter, String host, int port) throws javax.management.MBeanException {
String jvmName = MetaUtil.toInternalName(className);
methodDumps.add(new Dump(host, port, jvmName, methodName, filter));
ClassNotFoundException last = null;
EconomicSet<Class<?>> found = EconomicSet.create();
Iterator<Reference<ClassLoader>> it = loaders.iterator();
while (it.hasNext()) {
Reference<ClassLoader> ref = it.next();
ClassLoader loader = ref.get();
if (loader == null) {
it.remove();
continue;
}
try {
Class<?> clazz = Class.forName(className, false, loader);
if (found.add(clazz)) {
ResolvedJavaType type = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess().lookupJavaType(clazz);
if (compiler != null) {
for (ResolvedJavaMethod method : type.getDeclaredMethods()) {
if (methodName.equals(method.getName()) && method instanceof HotSpotResolvedJavaMethod) {
HotSpotResolvedJavaMethod hotSpotMethod = (HotSpotResolvedJavaMethod) method;
compiler.compileMethod(new HotSpotCompilationRequest(hotSpotMethod, -1, 0L), false);
}
}
}
}
} catch (ClassNotFoundException ex) {
last = ex;
}
}
if (found.isEmpty()) {
throw new javax.management.MBeanException(last, "Cannot find class " + className + " to schedule recompilation");
}
}
use of java.lang.ref.Reference in project tomcat70 by apache.
the class WebappClassLoaderBase method clearCache.
private void clearCache(Class<?> target, String mapName) throws SecurityException, ClassCastException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
Field f = target.getDeclaredField(mapName);
f.setAccessible(true);
Map<?, ?> map = (Map<?, ?>) f.get(null);
Iterator<?> keys = map.keySet().iterator();
while (keys.hasNext()) {
Object key = keys.next();
if (key instanceof Reference) {
Object clazz = ((Reference<?>) key).get();
if (loadedByThisOrChild(clazz)) {
keys.remove();
}
}
}
}
Aggregations