package test.byteman; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Test case for BYTEMAN-216 * @see https://issues.jboss.org/browse/BYTEMAN-216 * @author wschell */ public class BytemanBugMonitorEnter { public static void main(String[] args) throws Exception { System.out.println("start"); try { new BytemanBugMonitorEnter().getData("dummy"); } catch (Throwable t) { t.printStackTrace(); } System.out.println("done"); } private List strings = Collections.emptyList(); /** * Reduced original method, so it still triggers the bug. * Original method did a lookup in some cache. */ public Object getData(String name) { List data = null; synchronized (DBIndex.class) { String indexName = "MyKey"; // lookup existing cache entry Map> cache = (Map>) DBIndex.getCustomIndex(indexName); if (cache == null) { // cache miss, create new cache entry cache = new HashMap>(); // create cache entries List cachedData = new ArrayList(); // XXX: this loop triggers the Byteman bug! // XXX: the loop body is irrelevant for (String s : strings) { cachedData.add(s); } cache.put(name, cachedData); DBIndex.addCustomIndex(indexName, cache); } // use cache data = cache.get(name); if (data == null) { data = Collections.emptyList(); } } return data; } /** * Dummy class. */ public static class DBIndex { public static Map> getCustomIndex(String customKey) { return Collections.emptyMap(); } public static void addCustomIndex(String customKey, Map> cache) { } } }