use of java.lang.ref.Reference in project robovm by robovm.
the class ReferenceTest method test_enqueue.
/**
* java.lang.ref.Reference#enqueue()
*/
public void test_enqueue() {
ReferenceQueue rq = new ReferenceQueue();
obj = new Object();
Reference ref = new SoftReference(obj, rq);
assertTrue("Enqueue failed.", (!ref.isEnqueued()) && ((ref.enqueue()) && (ref.isEnqueued())));
assertTrue("Not properly enqueued.", rq.poll().get() == obj);
// This fails...
assertTrue("Should remain enqueued.", !ref.isEnqueued());
assertTrue("Can not enqueue twice.", (!ref.enqueue()) && (rq.poll() == null));
rq = new ReferenceQueue();
obj = new Object();
ref = new WeakReference(obj, rq);
assertTrue("Enqueue failed2.", (!ref.isEnqueued()) && ((ref.enqueue()) && (ref.isEnqueued())));
assertTrue("Not properly enqueued2.", rq.poll().get() == obj);
// This
assertTrue("Should remain enqueued2.", !ref.isEnqueued());
// fails.
assertTrue("Can not enqueue twice2.", (!ref.enqueue()) && (rq.poll() == null));
ref = new PhantomReference(obj, rq);
assertTrue("Enqueue failed3.", (!ref.isEnqueued()) && ((ref.enqueue()) && (ref.isEnqueued())));
assertNull("Not properly enqueued3.", rq.poll().get());
// This
assertTrue("Should remain enqueued3.", !ref.isEnqueued());
// fails.
assertTrue("Can not enqueue twice3.", (!ref.enqueue()) && (rq.poll() == null));
}
use of java.lang.ref.Reference in project robovm by robovm.
the class ReferenceTest method test_subclass.
/**
* Makes sure that overridden versions of clear() and enqueue()
* get called, and that clear/enqueue/finalize happen in the
* right order for WeakReferences.
*
* java.lang.ref.Reference#clear()
* java.lang.ref.Reference#enqueue()
* java.lang.Object#finalize()
*/
public void test_subclass() {
error = null;
testObjectFinalized = false;
twr = null;
class TestObject {
public TestWeakReference testWeakReference = null;
public void setTestWeakReference(TestWeakReference twr) {
testWeakReference = twr;
}
protected void finalize() {
testObjectFinalized = true;
}
}
final ReferenceQueue rq = new ReferenceQueue();
class TestThread extends Thread {
public void run() {
// Create the object in a separate thread to ensure it will be
// gc'ed
TestObject testObj = new TestObject();
twr = new TestWeakReference(testObj, rq);
testObj.setTestWeakReference(twr);
testObj = null;
}
}
Reference ref;
try {
Thread t = new TestThread();
t.start();
t.join();
FinalizationTester.induceFinalization();
// Give up after five seconds.
ref = rq.remove(5000L);
assertNotNull("Object not garbage collected.", ref);
assertTrue("Unexpected reference.", ref == twr);
assertNull("Object could not be reclaimed.", twr.get());
//assertTrue("Overridden clear() should have been called.",
// twr.clearSeen);
//assertTrue("Overridden enqueue() should have been called.",
// twr.enqueueSeen);
assertTrue("finalize() should have been called.", testObjectFinalized);
} catch (InterruptedException e) {
fail("InterruptedException : " + e.getMessage());
}
}
use of java.lang.ref.Reference in project morphia by mongodb.
the class ReferenceMap method purge.
/**
* Purges stale mappings from this map.<P>
* <p/>
* Ordinarily, stale mappings are only removed during a write operation; typically a write operation will occur often enough that
* you'll
* never need to manually invoke this method.<P>
* <p/>
* Note that this method is not synchronized! Special care must be taken if, for instance, you want stale mappings to be removed on a
* periodic basis by some background thread.
*/
private void purge() {
Reference ref = queue.poll();
while (ref != null) {
purge(ref);
ref = queue.poll();
}
}
use of java.lang.ref.Reference in project spring-loaded by spring-projects.
the class JVMPlugin method clearThreadGroupContext.
private boolean clearThreadGroupContext(Class<?> clazz) {
boolean beanInfoCacheCleared = false;
try {
if (threadGroupContextClass == null) {
threadGroupContextClass = Class.forName("java.beans.ThreadGroupContext", true, Introspector.class.getClassLoader());
}
if (threadGroupContextClass != null) {
if (threadGroupContext_contextsField == null) {
threadGroupContext_contextsField = threadGroupContextClass.getDeclaredField("contexts");
threadGroupContext_removeBeanInfoMethod = threadGroupContextClass.getDeclaredMethod("removeBeanInfo", Class.class);
}
if (threadGroupContext_contextsField != null) {
threadGroupContext_contextsField.setAccessible(true);
Object threadGroupContext_contextsField_value = threadGroupContext_contextsField.get(null);
if (threadGroupContext_contextsField_value == null) {
beanInfoCacheCleared = true;
} else {
if (threadGroupContext_contextsField_value instanceof Map) {
// Indicates Java 7 up to rev21
Map<?, ?> m = (Map<?, ?>) threadGroupContext_contextsField_value;
Collection<?> threadGroupContexts = m.values();
for (Object o : threadGroupContexts) {
threadGroupContext_removeBeanInfoMethod.setAccessible(true);
threadGroupContext_removeBeanInfoMethod.invoke(o, clazz);
}
beanInfoCacheCleared = true;
} else {
// At update Java7u21 it changes
Class weakIdentityMapClazz = threadGroupContext_contextsField.getType();
Field tableField = weakIdentityMapClazz.getDeclaredField("table");
tableField.setAccessible(true);
Reference<?>[] refs = (Reference[]) tableField.get(threadGroupContext_contextsField_value);
Field valueField = null;
if (refs != null) {
for (int i = 0; i < refs.length; i++) {
Reference<?> r = refs[i];
Object o = (r == null ? null : r.get());
if (o != null) {
if (valueField == null) {
valueField = r.getClass().getDeclaredField("value");
}
valueField.setAccessible(true);
Object threadGroupContext = valueField.get(r);
threadGroupContext_removeBeanInfoMethod.setAccessible(true);
threadGroupContext_removeBeanInfoMethod.invoke(threadGroupContext, clazz);
}
}
}
beanInfoCacheCleared = true;
}
}
}
}
} catch (Throwable t) {
System.err.println("Unexpected problem clearing ThreadGroupContext beaninfo: ");
t.printStackTrace();
}
return beanInfoCacheCleared;
}
use of java.lang.ref.Reference in project intellij-community by JetBrains.
the class AbstractClassGenerator method create.
protected Object create(Object key) {
try {
Class gen = null;
synchronized (source) {
ClassLoader loader = getClassLoader();
Map cache2 = null;
cache2 = (Map) source.cache.get(loader);
if (cache2 == null) {
cache2 = new HashMap();
cache2.put(NAME_KEY, new HashSet());
source.cache.put(loader, cache2);
} else if (useCache) {
Reference ref = (Reference) cache2.get(key);
gen = (Class) SoftReference.dereference(ref);
}
if (gen == null) {
Object save = CURRENT.get();
CURRENT.set(this);
try {
this.key = key;
if (attemptLoad) {
try {
gen = loader.loadClass(getClassName());
} catch (ClassNotFoundException e) {
// ignore
}
}
if (gen == null) {
byte[] b = strategy.generate(this);
String className = ClassNameReader.getClassName(new $ClassReader(b));
getClassNameCache(loader).add(className);
gen = ReflectUtils.defineClass(className, b, loader);
}
if (useCache) {
cache2.put(key, new WeakReference(gen));
}
return firstInstance(gen);
} finally {
CURRENT.set(save);
}
}
}
return firstInstance(gen);
} catch (RuntimeException e) {
throw e;
} catch (Error e) {
throw e;
} catch (Exception e) {
throw new CodeGenerationException(e);
}
}
Aggregations