diff --git a/common/src/main/java/io/netty/util/internal/TypeParameterMatcher.java b/common/src/main/java/io/netty/util/internal/TypeParameterMatcher.java index 635516e26c..bb0ef298db 100644 --- a/common/src/main/java/io/netty/util/internal/TypeParameterMatcher.java +++ b/common/src/main/java/io/netty/util/internal/TypeParameterMatcher.java @@ -27,6 +27,7 @@ import java.util.Map; public abstract class TypeParameterMatcher { private static final TypeParameterMatcher NOOP = new NoOpTypeParameterMatcher(); + private static final Object TEST_OBJECT = new Object(); private static final ThreadLocal, Map>> typeMap = new ThreadLocal, Map>>() { @@ -56,13 +57,20 @@ public abstract class TypeParameterMatcher { } else if (PlatformDependent.hasJavassist()) { try { matcher = JavassistTypeParameterMatcherGenerator.generate(messageType); + matcher.match(TEST_OBJECT); + } catch (IllegalAccessError e) { + // Happens if messageType is not public. + matcher = null; } catch (Exception e) { // Will not usually happen, but just in case. - matcher = new ReflectiveMatcher(messageType); + matcher = null; } - } else { + } + + if (matcher == null) { matcher = new ReflectiveMatcher(messageType); } + map.put(typeParamName, matcher); } diff --git a/common/src/test/java/io/netty/util/internal/TypeParameterMatcherTest.java b/common/src/test/java/io/netty/util/internal/TypeParameterMatcherTest.java index eba3ca5a97..d6ab86ca1f 100644 --- a/common/src/test/java/io/netty/util/internal/TypeParameterMatcherTest.java +++ b/common/src/test/java/io/netty/util/internal/TypeParameterMatcherTest.java @@ -23,12 +23,8 @@ import static org.junit.Assert.*; public class TypeParameterMatcherTest { @Test - public void testSimple() throws Exception { - Object o = new TypeQ(); - - TypeParameterMatcher m; - - m = TypeParameterMatcher.find(o, TypeX.class, "A"); + public void testConcreteClass() throws Exception { + TypeParameterMatcher m = TypeParameterMatcher.find(new TypeQ(), TypeX.class, "A"); assertFalse(m.match(new Object())); assertFalse(m.match(new A())); assertFalse(m.match(new AA())); @@ -38,14 +34,16 @@ public class TypeParameterMatcherTest { assertFalse(m.match(new BBB())); assertFalse(m.match(new C())); assertFalse(m.match(new CC())); + } - try { - TypeParameterMatcher.find(o, TypeX.class, "B"); - } catch (IllegalStateException e) { - // expected - } + @Test(expected = IllegalStateException.class) + public void testUnsolvedParameter() throws Exception { + TypeParameterMatcher.find(new TypeQ(), TypeX.class, "B"); + } - m = TypeParameterMatcher.find(new TypeQ() { }, TypeX.class, "B"); + @Test + public void testAnonymousClass() throws Exception { + TypeParameterMatcher m = TypeParameterMatcher.find(new TypeQ() { }, TypeX.class, "B"); assertFalse(m.match(new Object())); assertFalse(m.match(new A())); assertFalse(m.match(new AA())); @@ -55,8 +53,11 @@ public class TypeParameterMatcherTest { assertTrue(m.match(new BBB())); assertFalse(m.match(new C())); assertFalse(m.match(new CC())); + } - m = TypeParameterMatcher.find(o, TypeX.class, "C"); + @Test + public void testAbstractClass() throws Exception { + TypeParameterMatcher m = TypeParameterMatcher.find(new TypeQ(), TypeX.class, "C"); assertFalse(m.match(new Object())); assertFalse(m.match(new A())); assertFalse(m.match(new AA())); @@ -76,7 +77,7 @@ public class TypeParameterMatcherTest { public static class TypeY extends TypeX { } - public static class TypeZ extends TypeY { } + public static abstract class TypeZ extends TypeY { } public static class TypeQ extends TypeZ { } @@ -93,4 +94,15 @@ public class TypeParameterMatcherTest { @SuppressWarnings("ClassMayBeInterface") public static class C { } public static class CC extends C { } + + @Test + public void testInaccessibleClass() throws Exception { + TypeParameterMatcher m = TypeParameterMatcher.find(new U() { }, U.class, "E"); + assertFalse(m.match(new Object())); + assertTrue(m.match(new T())); + } + + @SuppressWarnings("ClassMayBeInterface") + private static class T { } + private static class U { E a; } }