Permalink
Browse files

Made is isInstantiable smarter by checking inner classes properly

  • Loading branch information...
1 parent fa4841b commit bab2b13c438e4383faf15c66fa079229ad23dbc2 aslak.hellesoy@gmail.com committed Feb 2, 2012
@@ -15,7 +15,8 @@
}
public static boolean isInstantiable(Class<?> clazz) {
- return Modifier.isPublic(clazz.getModifiers()) && !Modifier.isAbstract(clazz.getModifiers());
+ boolean isNonStaticInnerClass = !Modifier.isStatic(clazz.getModifiers()) && clazz.getEnclosingClass() != null;
+ return Modifier.isPublic(clazz.getModifiers()) && !Modifier.isAbstract(clazz.getModifiers()) && !isNonStaticInnerClass;
}
public static boolean hasConstructor(Class<?> clazz, Class[] paramTypes) {
@@ -2,9 +2,12 @@
import org.junit.Test;
+import static cucumber.runtime.Utils.isInstantiable;
import static cucumber.runtime.Utils.packageName;
import static cucumber.runtime.Utils.packagePath;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
public class UtilsTest {
@Test
@@ -18,4 +21,20 @@ public void finds_path_for_class_in_toplevel_package() {
assertEquals("", packageName("TopLevelClass"));
assertEquals("", packagePath(packageName("TopLevelClass")));
}
+
+ @Test
+ public void public_non_static_inner_classes_are_not_instantiable() {
+ assertFalse(isInstantiable(NonStaticInnerClass.class));
+ }
+
+ @Test
+ public void public_static_inner_classes_are_instantiable() {
+ assertTrue(isInstantiable(StaticInnerClass.class));
+ }
+
+ public class NonStaticInnerClass {
+ }
+
+ public static class StaticInnerClass {
+ }
}

0 comments on commit bab2b13

Please sign in to comment.