use of java.lang.ref.Reference in project freemarker by apache.
the class TransformBlock method getSortedNamedArgs.
/**
* Returns the named args by source-code order; it's not meant to be used during template execution, too slow for
* that!
*/
private List getSortedNamedArgs() {
Reference ref = sortedNamedArgsCache;
if (ref != null) {
List res = (List) ref.get();
if (res != null)
return res;
}
List res = MiscUtil.sortMapOfExpressions(namedArgs);
sortedNamedArgsCache = new SoftReference(res);
return res;
}
use of java.lang.ref.Reference in project freemarker by apache.
the class ClassIntrospectorBuilder method build.
/**
* Returns an instance that is possibly shared (singleton). Note that this comes with its own "shared lock",
* since everyone who uses this object will have to lock with that common object.
*/
ClassIntrospector build() {
if ((methodAppearanceFineTuner == null || methodAppearanceFineTuner instanceof SingletonCustomizer) && (methodSorter == null || methodSorter instanceof SingletonCustomizer)) {
// Instance can be cached.
ClassIntrospector instance;
synchronized (INSTANCE_CACHE) {
Reference instanceRef = (Reference) INSTANCE_CACHE.get(this);
instance = instanceRef != null ? (ClassIntrospector) instanceRef.get() : null;
if (instance == null) {
// prevent any aliasing issues
ClassIntrospectorBuilder thisClone = (ClassIntrospectorBuilder) clone();
instance = new ClassIntrospector(thisClone, new Object(), true, true);
INSTANCE_CACHE.put(thisClone, new WeakReference(instance, INSTANCE_CACHE_REF_QUEUE));
}
}
removeClearedReferencesFromInstanceCache();
return instance;
} else {
// BeansWrapper.
return new ClassIntrospector(this, new Object(), true, false);
}
}
use of java.lang.ref.Reference in project ovirt-engine by oVirt.
the class ReapedMapTest method setUpGCExpectations.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void setUpGCExpectations(final int gcAfter) {
ReferenceQueue<Integer> queue = mock(ReferenceQueue.class);
map = new ReapedMap<>(10000, false, queue);
final IdAwareReference ref = mock(IdAwareReference.class);
when(ref.getKey()).thenReturn("three").thenReturn(null);
// the gcAfter queue poll simulates a GC event and triggers deletion
// on the reapable map
when(queue.poll()).thenAnswer(new Answer<Reference<Integer>>() {
private int times = 0;
@Override
public Reference<Integer> answer(InvocationOnMock invocation) throws Throwable {
return times++ == gcAfter ? ref : null;
}
});
}
use of java.lang.ref.Reference in project openj9 by eclipse.
the class Test_Reference method test_enqueue.
/**
* @tests java.lang.ref.Reference#enqueue()
*/
@Test
public void test_enqueue() {
ReferenceQueue rq = new ReferenceQueue();
obj = new Object();
Reference ref = new SoftReference(obj, rq);
AssertJUnit.assertTrue("Enqueue failed.", (!ref.isEnqueued()) && ((ref.enqueue()) && (ref.isEnqueued())));
if (isJava8 || disableClearBeforeEnqueue) {
AssertJUnit.assertTrue("Not properly enqueued.", rq.poll().get() == obj);
} else {
AssertJUnit.assertTrue("Not properly enqueued.", rq.poll().get() == null);
}
// This fails.
AssertJUnit.assertTrue("Should remain enqueued.", !ref.isEnqueued());
AssertJUnit.assertTrue("Can not enqueue twice.", (!ref.enqueue()) && (rq.poll() == null));
rq = new ReferenceQueue();
obj = new Object();
ref = new WeakReference(obj, rq);
AssertJUnit.assertTrue("Enqueue failed2.", (!ref.isEnqueued()) && ((ref.enqueue()) && (ref.isEnqueued())));
if (isJava8 || disableClearBeforeEnqueue) {
AssertJUnit.assertTrue("Not properly enqueued2.", rq.poll().get() == obj);
} else {
AssertJUnit.assertTrue("Not properly enqueued2.", rq.poll().get() == null);
}
// This fails.
AssertJUnit.assertTrue("Should remain enqueued2.", !ref.isEnqueued());
AssertJUnit.assertTrue("Can not enqueue twice2.", (!ref.enqueue()) && (rq.poll() == null));
}
use of java.lang.ref.Reference in project openj9 by eclipse.
the class Test_Reference method test_get.
/**
* @tests java.lang.ref.Reference#get()
*/
@Test
public void test_get() {
// SM.
obj = new Object();
Reference ref = new WeakReference(obj, new ReferenceQueue());
AssertJUnit.assertTrue("Get succeeded.", ref.get() == obj);
}
Aggregations