From 7a87a18c9b11872a21142b2e9dafdf3649775583 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 17 Jul 2012 21:09:21 +0200 Subject: [PATCH] Use reflection to instance java.util.concurrent.LinkedTransferQueue to make Android not fail. See #458 --- .../netty/util/internal/QueueFactory.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/jboss/netty/util/internal/QueueFactory.java b/src/main/java/org/jboss/netty/util/internal/QueueFactory.java index 6dfe83a39e..f08b42f442 100644 --- a/src/main/java/org/jboss/netty/util/internal/QueueFactory.java +++ b/src/main/java/org/jboss/netty/util/internal/QueueFactory.java @@ -41,14 +41,16 @@ public final class QueueFactory { * @param itemClass the {@link Class} type which will be used as {@link BlockingQueue} items * @return queue the {@link BlockingQueue} implementation */ + @SuppressWarnings("unchecked") public static BlockingQueue createQueue(Class itemClass) { - // if we run in java >=7 its the best to just use the LinkedTransferQueue which - // comes with java bundled. See #273 - if (DetectionUtil.javaVersion() >= 7) { - return new java.util.concurrent.LinkedTransferQueue(); - } - try { + // if we run in java >=7 its the best to just use the LinkedTransferQueue which + // comes with java bundled. See #273 + if (DetectionUtil.javaVersion() >= 7) { + // Use reflect as android otherwise will panic out. See #458 + return (BlockingQueue) QueueFactory.class.getClassLoader() + .loadClass("java.util.concurrent.LinkedTransferQueue").newInstance(); + } if (useUnsafe) { return new LinkedTransferQueue(); } @@ -75,13 +77,16 @@ public final class QueueFactory { * @return queue the {@link BlockingQueue} implementation */ public static BlockingQueue createQueue(Collection collection, Class itemClass) { - // if we run in java >=7 its the best to just use the LinkedTransferQueue which - // comes with java bundled. See #273 - if (DetectionUtil.javaVersion() >= 7) { - return new java.util.concurrent.LinkedTransferQueue(); - } - try { + // if we run in java >=7 its the best to just use the LinkedTransferQueue which + // comes with java bundled. See #273 + if (DetectionUtil.javaVersion() >= 7) { + // Use reflect as android otherwise will panic out. See #458 + @SuppressWarnings("unchecked") + Class> clazz = (Class>) QueueFactory.class.getClassLoader() + .loadClass("java.util.concurrent.LinkedTransferQueue"); + return clazz.getConstructor(Collection.class).newInstance(collection); + } if (useUnsafe) { return new LinkedTransferQueue(collection); } @@ -99,4 +104,5 @@ public final class QueueFactory { return new LegacyLinkedTransferQueue(collection); } + }