Java Code Examples for java.util.Map#getClass()

The following examples show how to use java.util.Map#getClass() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
@SuppressWarnings("unchecked")
public void putAll(Map<? extends K, ? extends V> m) {
    if (m == null) {
        throw new NullPointerException();
    }
    if (m.getClass() == getClass()) {
        LinkedIdentityHashMap<K, V> that = (LinkedIdentityHashMap<K, V>) m;
        map.putAll(that.map);

    } else {
        for (K key : m.keySet()) {
            map.put(id(key), m.get(key));
        }
    }
}
 
Example 2
Source Project: flink   File: CommonTestUtils.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static void setEnv(Map<String, String> newenv, boolean clearExisting) {
	try {
		Map<String, String> env = System.getenv();
		Class<?> clazz = env.getClass();
		Field field = clazz.getDeclaredField("m");
		field.setAccessible(true);
		Map<String, String> map = (Map<String, String>) field.get(env);
		if (clearExisting) {
			map.clear();
		}
		map.putAll(newenv);

		// only for Windows
		Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
		try {
			Field theCaseInsensitiveEnvironmentField =
				processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
			theCaseInsensitiveEnvironmentField.setAccessible(true);
			Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
			if (clearExisting) {
				cienv.clear();
			}
			cienv.putAll(newenv);
		} catch (NoSuchFieldException ignored) {}

	} catch (Exception e1) {
		throw new RuntimeException(e1);
	}
}
 
Example 3
private static void setEnv(String key, String value) {
  try {
    Map<String, String> env = System.getenv();
    Class<?> cl = env.getClass();
    Field field = cl.getDeclaredField("m");
    field.setAccessible(true);
    Map<String, String> writableEnv = (Map<String, String>) field.get(env);
    writableEnv.put(key, value);
  } catch (Exception e) {
    throw new IllegalStateException("Failed to set environment variable", e);
  }
}
 
Example 4
private Map<String, String> getEnv() {
    try {
        Map<String, String> theUnmodifiableEnvironment = System.getenv();
        Class<?> cu = theUnmodifiableEnvironment.getClass();
        Field m = cu.getDeclaredField("m");
        m.setAccessible(true);
        @SuppressWarnings("unchecked")
        Map<String, String> result = (Map<String, String>)m.get(theUnmodifiableEnvironment);
        return result;
    } catch (Exception e) {
        throw new NativeIntegrationException("Unable to get mutable environment map", e);
    }
}
 
Example 5
private Map<String, String> getEnv() {
    try {
        Map<String, String> theUnmodifiableEnvironment = System.getenv();
        Class<?> cu = theUnmodifiableEnvironment.getClass();
        Field m = cu.getDeclaredField("m");
        m.setAccessible(true);
        @SuppressWarnings("unchecked")
        Map<String, String> result = (Map<String, String>)m.get(theUnmodifiableEnvironment);
        return result;
    } catch (Exception e) {
        throw new NativeIntegrationException("Unable to get mutable environment map", e);
    }
}
 
Example 6
public static void setEnv(String key, String value) {
  try {
    Map<String, String> env = System.getenv();
    Class<?> cl = env.getClass();
    Field field = cl.getDeclaredField("m");
    field.setAccessible(true);
    Map<String, String> writableEnv = (Map<String, String>) field.get(env);
    writableEnv.put(key, value);
  } catch (Exception e) {
    throw new IllegalStateException("Failed to set environment variable", e);
  }
}
 
Example 7
/**
    * Copy a Map<K, V> to another Map
    * 
    * @param map
    *            the map to copy
    * @return the copied map
    */
   public Map<K, V> copy(Map<K, V> map) {
Map<K, V> mapCopy = null;

if (map instanceof HashMap) {
    mapCopy = new HashMap<K, V>(map);
} else if (map instanceof Hashtable) {
    mapCopy = new Hashtable<K, V>(map);
} else if (map instanceof LinkedHashMap) {
    mapCopy = new LinkedHashMap<K, V>(map);
} else if (map instanceof TreeMap) {
    mapCopy = new TreeMap<K, V>(map);
} else {
    throw new IllegalArgumentException(
	    "copy implementation not supported for Map class: "
		    + map.getClass());
}

/*
 * Set<K> keys = map.keySet();
 * 
 * for (Iterator<K> iterator = keys.iterator(); iterator.hasNext();) { K
 * key = (K) iterator.next(); V value = map.get(key);
 * 
 * mapCopy.put(key, value); }
 */

return mapCopy;
   }
 
Example 8
private static Map<String, String> getWritableEnvironmentMap() throws NoSuchFieldException, IllegalAccessException {
    Map<String, String> env = System.getenv();
    Class<?> cl = env.getClass();
    Field field = cl.getDeclaredField("m");
    field.setAccessible(true);

    @SuppressWarnings("unchecked")
    Map<String, String> result = (Map<String, String>) field.get(env);
    return result;
}
 
Example 9
@SuppressWarnings("unchecked") public static Map<String, String> getenv() {
	try {
		final Map<String, String> theUnmodifiableEnvironment = System.getenv();
		final Class<?> cu = theUnmodifiableEnvironment.getClass();
		final Field m = cu.getDeclaredField("m");
		m.setAccessible(true);
		return (Map<String, String>) m.get(theUnmodifiableEnvironment);
	} catch (final Exception ex2) {
	}
	return new HashMap<String, String>();
}
 
Example 10
public static <K, V> V doFill(Map<K, V> map, K key, V value) {
  if (map instanceof OffHeapHashMap<?, ?>) {
    return ((OffHeapHashMap<K, V>) map).fill(key, value);
  } else if (map instanceof Segment<?, ?>) {
    return ((Segment<K, V>) map).fill(key, value);
  } else if (map instanceof AbstractConcurrentOffHeapMap<?, ?>) {
    return ((AbstractConcurrentOffHeapMap<K, V>) map).fill(key, value);
  } else {
    throw new AssertionError("Unexpected type : " + map.getClass());
  }
}
 
Example 11
public static <K, V> MetadataTuple<V> doComputeWithMetadata(Map<K, V> map, K key, BiFunction<? super K, ? super MetadataTuple<V>, ? extends MetadataTuple<V>> function) {
  if (map instanceof OffHeapHashMap<?, ?>) {
    return ((OffHeapHashMap<K, V>) map).computeWithMetadata(key, function);
  } else if (map instanceof Segment<?, ?>) {
    return ((Segment<K, V>) map).computeWithMetadata(key, function);
  } else if (map instanceof AbstractConcurrentOffHeapMap<?, ?>) {
    return ((AbstractConcurrentOffHeapMap<K, V>) map).computeWithMetadata(key, function);
  } else {
    throw new AssertionError("Unexpected type : " + map.getClass());
  }
}
 
Example 12
public static <K, V> MetadataTuple<V> doComputeIfAbsentWithMetadata(Map<K, V> map, K key, Function<? super K, ? extends MetadataTuple<V>> function) {
  if (map instanceof OffHeapHashMap<?, ?>) {
    return ((OffHeapHashMap<K, V>) map).computeIfAbsentWithMetadata(key, function);
  } else if (map instanceof Segment<?, ?>) {
    return ((Segment<K, V>) map).computeIfAbsentWithMetadata(key, function);
  } else if (map instanceof AbstractConcurrentOffHeapMap<?, ?>) {
    return ((AbstractConcurrentOffHeapMap<K, V>) map).computeIfAbsentWithMetadata(key, function);
  } else {
    throw new AssertionError("Unexpected type : " + map.getClass());
  }
}
 
Example 13
public static <K, V> MetadataTuple<V> doComputeIfPresentWithMetadata(Map<K, V> map, K key, BiFunction<? super K, ? super MetadataTuple<V>, ? extends MetadataTuple<V>> function) {
  if (map instanceof OffHeapHashMap<?, ?>) {
    return ((OffHeapHashMap<K, V>) map).computeIfPresentWithMetadata(key, function);
  } else if (map instanceof Segment<?, ?>) {
    return ((Segment<K, V>) map).computeIfPresentWithMetadata(key, function);
  } else if (map instanceof AbstractConcurrentOffHeapMap<?, ?>) {
    return ((AbstractConcurrentOffHeapMap<K, V>) map).computeIfPresentWithMetadata(key, function);
  } else {
    throw new AssertionError("Unexpected type : " + map.getClass());
  }
}
 
Example 14
private Map<String, String> getEnv() {
    try {
        Map<String, String> theUnmodifiableEnvironment = System.getenv();
        Class<?> cu = theUnmodifiableEnvironment.getClass();
        Field m = cu.getDeclaredField("m");
        m.setAccessible(true);
        @SuppressWarnings("unchecked")
        Map<String, String> result = (Map<String, String>)m.get(theUnmodifiableEnvironment);
        return result;
    } catch (Exception e) {
        throw new NativeIntegrationException("Unable to get mutable environment map", e);
    }
}
 
Example 15
private static void setEnv(String key, String value) {
  try {
    Map<String, String> env = System.getenv();
    Class<?> cl = env.getClass();
    Field field = cl.getDeclaredField("m");
    field.setAccessible(true);
    Map<String, String> writableEnv = (Map<String, String>) field.get(env);
    writableEnv.put(key, value);
  } catch (Exception e) {
    throw new IllegalStateException("Failed to set environment variable", e);
  }
}
 
Example 16
@SuppressWarnings("unchecked")
public static Map<String, String> getenv() {
    try {
        Map<String, String> theUnmodifiableEnvironment = System.getenv();
        Class<?> cu = theUnmodifiableEnvironment.getClass();
        Field m = cu.getDeclaredField("m");
        m.setAccessible(true);
        return (Map<String, String>) m.get(theUnmodifiableEnvironment);
    } catch (Exception ex2) {
        logger.debug("Error when reading the JVM System Environment Cache : " + ex2.getMessage());
    }
    return new HashMap<>();
}
 
Example 17
static void setEnv(String key, String value) {
  try {
    Map<String, String> env = System.getenv();
    Class<?> cl = env.getClass();
    Field field = cl.getDeclaredField("m");
    field.setAccessible(true);
    Map<String, String> writableEnv = (Map<String, String>) field.get(env);
    writableEnv.put(key, value);
  } catch (Exception e) {
    throw new IllegalStateException("Failed to set environment variable", e);
  }
}
 
Example 18
Source Project: netbeans   File: ProxyLookup.java    License: Apache License 2.0 4 votes vote down vote up
private static boolean isUnmodifiable(Map<?,?> map) {
    return map.getClass() == unmodifiableClass;
}
 
Example 19
AbstractChronicleMapConverter(@NotNull Map<K, V> map) {
    this.map = map;
    this.mapClazz = map.getClass();
}
 
Example 20
Source Project: simple-netty-source   File: Bootstrap.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Returns {@code true} if and only if the specified {@code map} is an
 * ordered map, like {@link LinkedHashMap} is.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
static boolean isOrderedMap(Map<?, ?> map) {
    Class<?> mapType = map.getClass();
    if (LinkedHashMap.class.isAssignableFrom(mapType)) {
        // LinkedHashMap is an ordered map.
        return true;
    }

    // Not a LinkedHashMap - start autodetection.

    // Detect Apache Commons Collections OrderedMap implementations.
    Class<?> type = mapType;
    while (type != null) {
        for (Class<?> i: type.getInterfaces()) {
            if (i.getName().endsWith("OrderedMap")) {
                // Seems like it's an ordered map - guessed from that
                // it implements OrderedMap interface.
                return true;
            }
        }
        type = type.getSuperclass();
    }

    // Does not implement OrderedMap interface.  As a last resort, try to
    // create a new instance and test if the insertion order is maintained.
    Map<Object, Object> newMap;
    try {
        newMap = (Map<Object, Object>) mapType.newInstance();
    } catch (Exception e) {
        // No default constructor - cannot proceed anymore.
        return false;
    }

    // Run some tests.
    List<String> expectedKeys = new ArrayList<String>();
    String dummyValue = "dummyValue";
    for (short element: ORDER_TEST_SAMPLES) {
        String key = String.valueOf(element);
        newMap.put(key, dummyValue);
        expectedKeys.add(key);

        Iterator<String> it = expectedKeys.iterator();
        for (Object actualKey: newMap.keySet()) {
            if (!it.next().equals(actualKey)) {
                // Did not pass the test.
                return false;
            }
        }
    }

    // The specified map passed the insertion order test.
    return true;
}