Example usage for java.lang.ref Reference get

List of usage examples for java.lang.ref Reference get

Introduction

In this page you can find the example usage for java.lang.ref Reference get.

Prototype

@HotSpotIntrinsicCandidate
public T get() 

Source Link

Document

Returns this reference object's referent.

Usage

From source file:org.modelmapper.internal.util.TypeResolver.java

private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> targetType) {
    Reference<Map<TypeVariable<?>, Type>> ref = typeVariableCache.get(targetType);
    Map<TypeVariable<?>, Type> map = ref != null ? ref.get() : null;

    if (map == null) {
        map = new HashMap<TypeVariable<?>, Type>();

        // Populate interfaces
        buildTypeVariableMap(targetType.getGenericInterfaces(), map);

        // Populate super classes and interfaces
        Type genericType = targetType.getGenericSuperclass();
        Class<?> type = targetType.getSuperclass();
        while (type != null && !Object.class.equals(type)) {
            if (genericType instanceof ParameterizedType)
                buildTypeVariableMap((ParameterizedType) genericType, map);
            buildTypeVariableMap(type.getGenericInterfaces(), map);

            genericType = type.getGenericSuperclass();
            type = type.getSuperclass();
        }//from   ww  w.j av  a  2s  . c om

        // Populate enclosing classes
        type = targetType;
        while (type.isMemberClass()) {
            genericType = type.getGenericSuperclass();
            if (genericType instanceof ParameterizedType)
                buildTypeVariableMap((ParameterizedType) genericType, map);

            type = type.getEnclosingClass();
        }

        typeVariableCache.put(targetType, new WeakReference<Map<TypeVariable<?>, Type>>(map));
    }

    return map;
}

From source file:antre.TypeResolver.java

private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> targetType) {
    Reference<Map<TypeVariable<?>, Type>> ref = typeVariableCache.get(targetType);
    Map<TypeVariable<?>, Type> map = ref != null ? ref.get() : null;

    if (map == null) {
        map = new HashMap<TypeVariable<?>, Type>();

        // Populate interfaces
        buildTypeVariableMap(targetType.getGenericInterfaces(), map);

        // Populate super classes and interfaces
        Type genericType = targetType.getGenericSuperclass();
        Class<?> type = targetType.getSuperclass();
        while (type != null && !Object.class.equals(type)) {
            if (genericType instanceof ParameterizedType)
                buildTypeVariableMap((ParameterizedType) genericType, map);
            buildTypeVariableMap(type.getGenericInterfaces(), map);

            genericType = type.getGenericSuperclass();
            type = type.getSuperclass();
        }/*from  w ww. ja  v a2s.co m*/

        // Populate enclosing classes
        type = targetType;
        while (type.isMemberClass()) {
            genericType = type.getGenericSuperclass();
            if (genericType instanceof ParameterizedType)
                buildTypeVariableMap((ParameterizedType) genericType, map);

            type = type.getEnclosingClass();
        }

        if (cacheEnabled)
            typeVariableCache.put(targetType, new WeakReference<Map<TypeVariable<?>, Type>>(map));
    }

    return map;
}

From source file:com.lrs.enviroment.Metadata.java

private static Metadata getFromMap() {
    Reference<Metadata> metadata = holder.get();
    return metadata == null ? null : metadata.get();
}

From source file:com.lrs.enviroment.Metadata.java

/**
 * Loads and returns a new Metadata object for the given File.
 *
 * @param file The File/*w w  w. ja v a  2  s .  com*/
 * @return A Metadata object
 */
public static Metadata getInstance(File file) {
    Reference<Metadata> ref = holder.get();
    if (ref != null) {
        Metadata metadata = ref.get();
        if (metadata != null && metadata.getMetadataFile() != null && metadata.getMetadataFile().equals(file)) {
            return metadata;
        }
        createAndBindNew(file);
    }
    return createAndBindNew(file);
}

From source file:io.werval.runtime.util.TypeResolver.java

private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> targetType) {
    Reference<Map<TypeVariable<?>, Type>> ref = CACHE.get(targetType);
    Map<TypeVariable<?>, Type> map = ref != null ? ref.get() : null;

    if (map == null) {
        map = new HashMap<>();

        // Populate interfaces
        buildTypeVariableMap(targetType.getGenericInterfaces(), map);

        // Populate super classes and interfaces
        Type genericType = targetType.getGenericSuperclass();
        Class<?> type = targetType.getSuperclass();
        while (type != null && !Object.class.equals(type)) {
            if (genericType instanceof ParameterizedType) {
                buildTypeVariableMap((ParameterizedType) genericType, map);
            }//from   ww  w.  j a v  a 2 s .c  o  m
            buildTypeVariableMap(type.getGenericInterfaces(), map);

            genericType = type.getGenericSuperclass();
            type = type.getSuperclass();
        }

        // Populate enclosing classes
        type = targetType;
        while (type.isMemberClass()) {
            genericType = type.getGenericSuperclass();
            if (genericType instanceof ParameterizedType) {
                buildTypeVariableMap((ParameterizedType) genericType, map);
            }

            type = type.getEnclosingClass();
        }

        if (cacheEnabled) {
            CACHE.put(targetType, new WeakReference<>(map));
        }
    }

    return map;
}

From source file:net.jodah.typetools.TypeResolver.java

private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> targetType,
        Class<?> functionalInterface) {
    Reference<Map<TypeVariable<?>, Type>> ref = typeVariableCache.get(targetType);
    Map<TypeVariable<?>, Type> map = ref != null ? ref.get() : null;

    if (map == null) {
        map = new HashMap<TypeVariable<?>, Type>();

        // Populate lambdas
        if (functionalInterface != null)
            populateLambdaArgs(functionalInterface, targetType, map);

        // Populate interfaces
        populateSuperTypeArgs(targetType.getGenericInterfaces(), map, functionalInterface != null);

        // Populate super classes and interfaces
        Type genericType = targetType.getGenericSuperclass();
        Class<?> type = targetType.getSuperclass();
        while (type != null && !Object.class.equals(type)) {
            if (genericType instanceof ParameterizedType)
                populateTypeArgs((ParameterizedType) genericType, map, false);
            populateSuperTypeArgs(type.getGenericInterfaces(), map, false);

            genericType = type.getGenericSuperclass();
            type = type.getSuperclass();
        }/*w w w.  j  a v  a  2  s.  c  o m*/

        // Populate enclosing classes
        type = targetType;
        while (type.isMemberClass()) {
            genericType = type.getGenericSuperclass();
            if (genericType instanceof ParameterizedType)
                populateTypeArgs((ParameterizedType) genericType, map, functionalInterface != null);

            type = type.getEnclosingClass();
        }

        if (CACHE_ENABLED)
            typeVariableCache.put(targetType, new WeakReference<Map<TypeVariable<?>, Type>>(map));
    }

    return map;
}

From source file:org.hsweb.concureent.cache.monitor.SimpleMonitorCache.java

@Override
protected Object lookup(Object key) {
    Object value = super.lookup(key);
    if (value != null && value instanceof Reference) {
        Reference reference = (Reference) value;
        value = reference.get();
        if (value == null)
            evict(key);/*from ww w . j a v a  2 s .  c  om*/
    }
    return value;
}

From source file:WeakCacheMap.java

/** {@inheritDoc} */
@Override//from   ww  w. jav a2  s.  co m
public V get(K key) {
    cleanQueue();

    Reference<V> reference = cacheMap.get(key);

    if (reference == null)
        return null;

    V res = reference.get();
    return res;
}

From source file:org.fastmongo.odm.dbobject.mapping.core.DbObjectToDomainConverterTest.java

@Test
public void testGarbageCollectedRootObjects() {
    DBObject db;/*  www  .j av a 2s .  c  o  m*/
    {
        Group root = TestDomainObjectsFixtures.createGroup(5, 10);
        db = toDbConverter.toDbObject(root);
    }

    Group root = fromDbConverter.fromDbObject(db, Group.class);
    Reference<Group> ref = new WeakReference<>(root);
    List<User> list = root.getUsers();

    Assert.assertNotNull(ref.get());

    //noinspection UnusedAssignment
    root = null;

    Runtime.getRuntime().gc();

    int limit = 10;
    int wait = 0;
    while (ref.get() != null && wait++ < limit) {
        if (ref.get() == null) {
            break;
        }

        LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100)); // wait a bit
    }

    Assert.assertNull(ref.get());

    // touch proxy, it should not throw NPE as before
    Assert.assertTrue(!list.isEmpty());
}

From source file:de.micromata.genome.util.event.SimpleEventClassRegistry.java

/**
 * Removes the listener.//from  w w  w  .  ja v  a 2s  . c o  m
 *
 * @param event the event
 * @param listener the listener
 */
protected void removeListener(Class<? extends MgcEvent> event, Class<? extends MgcEventListener<?>> listener) {
    Map<Class<? extends MgcEvent>, List<Reference<Class<? extends MgcEventListener<?>>>>> nmp = new ReferenceMap<>(
            ReferenceMap.ReferenceStrength.WEAK, ReferenceMap.ReferenceStrength.HARD);
    nmp.putAll(listenerMap);
    List<Reference<Class<? extends MgcEventListener<?>>>> list = nmp.get(event);
    if (list == null) {
        return;
    }
    for (ListIterator<Reference<Class<? extends MgcEventListener<?>>>> lit = list.listIterator(); lit
            .hasNext();) {
        Reference<Class<? extends MgcEventListener<?>>> ref = lit.next();
        Class<? extends MgcEventListener<?>> cls = ref.get();
        if (cls == listener) {
            lit.remove();
        }
    }
    listenerMap = nmp;

}