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);
}
}
use of java.lang.ref.Reference in project ACS by ACS-Community.
the class GarbageCollectionTestHelper method waitForGC.
public boolean waitForGC() {
waitingThread = Thread.currentThread();
Reference retRef = null;
System.gc();
while (true) {
try {
//@
System.err.println("refQ:waiting to remove()");
retRef = refQ.remove();
//@
System.err.println("refQ:removed()");
break;
} catch (InterruptedException ex) {
if (cancelFlag) {
break;
}
// ignore and continue
continue;
}
}
waitingThread = null;
return (retRef != null);
}
use of java.lang.ref.Reference in project robovm by robovm.
the class ReferenceQueueTest method test_removeJ.
/**
* java.lang.ref.ReferenceQueue#remove(long)
*/
public void test_removeJ() {
try {
assertNull("Queue should be empty. (poll)", rq.poll());
assertNull("Queue should be empty. (remove(1))", rq.remove((long) 1));
Thread ct = new Thread(new ChildThread());
ct.start();
Reference ret = rq.remove(0L);
assertNotNull("Delayed remove failed.", ret);
} catch (InterruptedException e) {
fail("InterruptedExeException during test : " + e.getMessage());
} catch (Exception e) {
fail("Exception during test : " + e.getMessage());
}
Object obj = new Object();
WeakReference wr = new WeakReference(obj, rq);
Boolean b = new Boolean(true);
SoftReference sr = new SoftReference(b, rq);
String str = "Test";
PhantomReference pr = new PhantomReference(str, rq);
pr.enqueue();
wr.enqueue();
sr.enqueue();
try {
Reference result = rq.remove(1L);
assertTrue((Boolean) result.get());
result = rq.remove(1L);
assertEquals(obj, result.get());
result = rq.remove(1L);
assertNull(result.get());
} catch (IllegalArgumentException e1) {
fail("IllegalArgumentException was thrown.");
} catch (InterruptedException e1) {
fail("InterruptedException was thrown.");
}
rq = new ReferenceQueue();
isThrown = false;
assertNull(rq.poll());
class RemoveThread extends Thread {
public void run() {
try {
rq.remove(1000L);
} catch (InterruptedException ie) {
isThrown = true;
}
}
}
RemoveThread rt = new RemoveThread();
rt.start();
try {
Thread.sleep(10);
} catch (InterruptedException ie) {
}
rt.interrupt();
try {
Thread.sleep(10);
} catch (InterruptedException ie) {
}
assertTrue(isThrown);
assertNull(rq.poll());
try {
rq.remove(-1);
fail("IllegalArgumentException expected.");
} catch (IllegalArgumentException iae) {
//expected
} catch (InterruptedException e) {
fail("Unexpected InterruptedException.");
}
}
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());
}
}
Aggregations