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

The following examples show how to use java.util.Map#getClass() . 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
Source File: LinkedIdentityHashMap.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@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 File: Environment.java    From Eclipse-Environment-Variables with MIT License 5 votes vote down vote up
@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 3
Source File: Environment.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
@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 4
Source File: YarnServiceTest.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
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 5
Source File: ReflectiveEnvironment.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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
Source File: AbstractOffHeapMapIT.java    From offheap-store with Apache License 2.0 5 votes vote down vote up
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 7
Source File: AbstractOffHeapMapIT.java    From offheap-store with Apache License 2.0 5 votes vote down vote up
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 8
Source File: AbstractOffHeapMapIT.java    From offheap-store with Apache License 2.0 5 votes vote down vote up
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 9
Source File: AbstractOffHeapMapIT.java    From offheap-store with Apache License 2.0 5 votes vote down vote up
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 10
Source File: EmbeddedGobblinYarnAppLauncher.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
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 11
Source File: EnvironmentHelper.java    From verify-service-provider with MIT License 5 votes vote down vote up
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 12
Source File: MapCopier.java    From aceql-http with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
    * 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 13
Source File: GobblinYarnAppLauncherTest.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
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 14
Source File: CommonTestUtils.java    From flink with 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 15
Source File: ReflectiveEnvironment.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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 16
Source File: ReflectiveEnvironment.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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 17
Source File: YarnServiceTestWithExpiration.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
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 18
Source File: Bootstrap.java    From simple-netty-source with 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;
}
 
Example 19
Source File: AbstractChronicleMapConverter.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
AbstractChronicleMapConverter(@NotNull Map<K, V> map) {
    this.map = map;
    this.mapClazz = map.getClass();
}
 
Example 20
Source File: ProxyLookup.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static boolean isUnmodifiable(Map<?,?> map) {
    return map.getClass() == unmodifiableClass;
}