Search in sources :

Example 1 with WeakHashMap

use of java.util.WeakHashMap in project jna by java-native-access.

the class CallbacksTest method testDLLCallback.

public void testDLLCallback() throws Exception {
    if (!Platform.HAS_DLL_CALLBACKS) {
        return;
    }
    final boolean[] called = { false };
    class TestCallback implements TestLibrary.VoidCallback, com.sun.jna.win32.DLLCallback {

        @Override
        public void callback() {
            called[0] = true;
        }
    }
    TestCallback cb = new TestCallback();
    lib.callVoidCallback(cb);
    assertTrue("Callback not called", called[0]);
    // Check module information
    Pointer fp = CallbackReference.getFunctionPointer(cb);
    NativeLibrary kernel32 = NativeLibrary.getInstance("kernel32", W32APIOptions.DEFAULT_OPTIONS);
    Function f = kernel32.getFunction("GetModuleHandleExW");
    final int GET_MODULE_HANDLE_FROM_ADDRESS = 0x4;
    PointerByReference pref = new PointerByReference();
    int result = f.invokeInt(new Object[] { Integer.valueOf(GET_MODULE_HANDLE_FROM_ADDRESS), fp, pref });
    assertTrue("GetModuleHandleEx(fptr) failed: " + Native.getLastError(), result != 0);
    f = kernel32.getFunction("GetModuleFileNameW");
    char[] buf = new char[1024];
    result = f.invokeInt(new Object[] { pref.getValue(), buf, buf.length });
    assertTrue("GetModuleFileName(fptr) failed: " + Native.getLastError(), result != 0);
    f = kernel32.getFunction("GetModuleHandleW");
    Pointer handle = f.invokePointer(new Object[] { Native.jnidispatchPath != null ? Native.jnidispatchPath : "jnidispatch" });
    assertNotNull("GetModuleHandle(\"jnidispatch\") failed: " + Native.getLastError(), handle);
    assertEquals("Wrong module HANDLE for DLL function pointer", handle, pref.getValue());
    // Check slot re-use
    Map<Callback, CallbackReference> refs = new WeakHashMap<Callback, CallbackReference>(callbackCache());
    assertTrue("Callback not cached", refs.containsKey(cb));
    CallbackReference ref = refs.get(cb);
    refs = callbackCache();
    Pointer cbstruct = ref.cbstruct;
    Pointer first_fptr = cbstruct.getPointer(0);
    cb = null;
    System.gc();
    for (int i = 0; i < 100 && (ref.get() != null || refs.containsValue(ref)); ++i) {
        // Give the GC a chance to run
        Thread.sleep(10);
        System.gc();
    }
    assertNull("Callback not GC'd", ref.get());
    assertFalse("Callback still in map", refs.containsValue(ref));
    ref = null;
    System.gc();
    for (int i = 0; i < 100 && (cbstruct.peer != 0 || refs.size() > 0); ++i) {
        // Flush weak hash map
        refs.size();
        // Give the GC a chance to run
        Thread.sleep(10);
        System.gc();
    }
    assertEquals("Callback trampoline not freed", 0, cbstruct.peer);
    // Next allocation should be at same place
    called[0] = false;
    cb = new TestCallback();
    lib.callVoidCallback(cb);
    ref = refs.get(cb);
    cbstruct = ref.cbstruct;
    assertTrue("Callback not called", called[0]);
    assertEquals("Same (in-DLL) address should be re-used for DLL callbacks after callback is GCd", first_fptr, cbstruct.getPointer(0));
}
Also used : CbCallback(com.sun.jna.CallbacksTest.TestLibrary.CbCallback) PointerByReference(com.sun.jna.ptr.PointerByReference) WeakHashMap(java.util.WeakHashMap)

Example 2 with WeakHashMap

use of java.util.WeakHashMap in project robovm by robovm.

the class AbstractMapTest method test_values.

/**
     * java.util.AbstractMap#values()
     */
public void test_values() {
    AbstractMap map1 = new HashMap(0);
    assertSame("HashMap(0)", map1.values(), map1.values());
    AbstractMap map2 = new HashMap(10);
    assertSame("HashMap(10)", map2.values(), map2.values());
    Map map3 = Collections.EMPTY_MAP;
    assertSame("EMPTY_MAP", map3.values(), map3.values());
    AbstractMap map4 = new IdentityHashMap(1);
    assertSame("IdentityHashMap", map4.values(), map4.values());
    AbstractMap map5 = new LinkedHashMap(122);
    assertSame("IdentityHashMap", map5.values(), map5.values());
    AbstractMap map6 = new TreeMap();
    assertSame("TreeMap", map6.values(), map6.values());
    AbstractMap map7 = new WeakHashMap();
    assertSame("WeakHashMap", map7.values(), map7.values());
}
Also used : AbstractMap(java.util.AbstractMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) WeakHashMap(java.util.WeakHashMap) IdentityHashMap(java.util.IdentityHashMap) TreeMap(java.util.TreeMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AbstractMap(java.util.AbstractMap) TreeMap(java.util.TreeMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) LinkedHashMap(java.util.LinkedHashMap) WeakHashMap(java.util.WeakHashMap)

Example 3 with WeakHashMap

use of java.util.WeakHashMap in project robovm by robovm.

the class OldAttributedStringTest method test_addAttributeLjava_text_AttributedCharacterIterator$AttributeLjava_lang_ObjectII.

public void test_addAttributeLjava_text_AttributedCharacterIterator$AttributeLjava_lang_ObjectII() {
    AttributedString as = new AttributedString("test");
    as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, "a", 2, 3);
    AttributedCharacterIterator it = as.getIterator();
    assertEquals("non-null value limit", 2, it.getRunLimit(AttributedCharacterIterator.Attribute.LANGUAGE));
    as = new AttributedString("test");
    as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, null, 2, 3);
    it = as.getIterator();
    assertEquals("null value limit", 4, it.getRunLimit(AttributedCharacterIterator.Attribute.LANGUAGE));
    try {
        as = new AttributedString("test");
        as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, null, -1, 3);
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // Expected
    }
    // regression for Harmony-1244
    as = new AttributedString("123", new WeakHashMap());
    try {
        as.addAttribute(null, new TreeSet(), 0, 1);
        fail("should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        as.addAttribute(null, new TreeSet(), -1, 1);
        fail("should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : AttributedString(java.text.AttributedString) TreeSet(java.util.TreeSet) AttributedCharacterIterator(java.text.AttributedCharacterIterator) WeakHashMap(java.util.WeakHashMap)

Example 4 with WeakHashMap

use of java.util.WeakHashMap in project robovm by robovm.

the class OldAttributedStringTest method test_addAttributeLjava_text_AttributedCharacterIterator$AttributeLjava_lang_Object.

/**
     * java.text.AttributedString.addAttribute(AttributedCharacterIterator,
     *        Object)
     */
public void test_addAttributeLjava_text_AttributedCharacterIterator$AttributeLjava_lang_Object() {
    // regression for Harmony-1244
    AttributedString as = new AttributedString("123", new WeakHashMap());
    as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, "english");
    as.addAttribute(AttributedCharacterIterator.Attribute.INPUT_METHOD_SEGMENT, "input method");
    as.addAttribute(AttributedCharacterIterator.Attribute.READING, "reading");
    try {
        as.addAttribute(null, new TreeSet());
        fail("should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        as.addAttribute(null, null);
        fail("should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : AttributedString(java.text.AttributedString) TreeSet(java.util.TreeSet) WeakHashMap(java.util.WeakHashMap)

Example 5 with WeakHashMap

use of java.util.WeakHashMap in project robovm by robovm.

the class OldAttributedStringTest method test_addAttributesLjava_util_MapII.

/**
     * java.text.AttributedString#addAttributes(Map<? extends
     *        AttributedCharacterIterator.Attribute,?>, int, int) Tests of
     *        method java.text.AttributedString#addAttributes(Map<? extends
     *        AttributedCharacterIterator.Attribute,?>, int, int). Case 1: Try
     *        to add attributes to AttributesString. Case 2: Try to add
     *        null-attributes to AttributesString. Case 3: Try to add attributes
     *        to AttributesString using incorrect index.
     */
public void test_addAttributesLjava_util_MapII() {
    AttributedString as = new AttributedString("test");
    Map<AttributedCharacterIterator.Attribute, String> whm = new WeakHashMap<AttributedCharacterIterator.Attribute, String>();
    // case 1: Try to add attributes to AttributesString.
    try {
        whm.put(new TestAttributedCharacterIteratorAttribute("test1"), "value1");
        whm.put(new TestAttributedCharacterIteratorAttribute("test2"), "value2");
        whm.put(new TestAttributedCharacterIteratorAttribute("test3"), "value3");
        as.addAttributes(whm, 0, 3);
    } catch (Exception e) {
        fail("Unexpected exception " + e.toString());
    }
    // case 2: Try to add null-attributes to AttributesString.
    try {
        as.addAttributes(null, 0, 3);
        fail("Expected NullPointerException was not thrown");
    } catch (NullPointerException e) {
    // expected
    }
    // index.
    try {
        as.addAttributes(whm, 0, 0);
        fail("Expected IllegalArgumentException was not thrown");
    } catch (IllegalArgumentException e) {
    // expected
    }
}
Also used : AttributedString(java.text.AttributedString) AttributedString(java.text.AttributedString) WeakHashMap(java.util.WeakHashMap) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Aggregations

WeakHashMap (java.util.WeakHashMap)66 Map (java.util.Map)20 HashMap (java.util.HashMap)19 TreeMap (java.util.TreeMap)15 IdentityHashMap (java.util.IdentityHashMap)13 AbstractMap (java.util.AbstractMap)12 LinkedHashMap (java.util.LinkedHashMap)12 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)8 ColorTransform (sun.java2d.cmm.ColorTransform)8 PCMM (sun.java2d.cmm.PCMM)8 Hashtable (java.util.Hashtable)6 AttributedString (java.text.AttributedString)5 ArrayList (java.util.ArrayList)5 Properties (java.util.Properties)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Comparator (java.util.Comparator)4 List (java.util.List)4 TreeSet (java.util.TreeSet)4 WeakReference (java.lang.ref.WeakReference)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3