From ddef65001e6e944c19a705da204c18ed80aecbf7 Mon Sep 17 00:00:00 2001 From: albertlatacz Date: Sun, 27 Nov 2016 14:04:45 +0000 Subject: [PATCH] extract type correctly for default and protected classes (fixes #99) --- src/javarepl/Utils.java | 7 ++--- test/javarepl/UtilsTest.java | 12 ++++++-- .../testclasses/BaseTestInterface.java | 4 +++ test/javarepl/testclasses/Constructors.java | 28 +++++++++++++++++++ .../testclasses/DefaultAccessTestClass.java | 4 +++ .../testclasses/GenericTestClass.java | 5 ++++ .../testclasses/ProtectedAccessTestClass.java | 4 +++ .../testclasses/PublicBaseTestClass.java | 4 +++ 8 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 test/javarepl/testclasses/BaseTestInterface.java create mode 100644 test/javarepl/testclasses/Constructors.java create mode 100644 test/javarepl/testclasses/DefaultAccessTestClass.java create mode 100644 test/javarepl/testclasses/GenericTestClass.java create mode 100644 test/javarepl/testclasses/ProtectedAccessTestClass.java create mode 100644 test/javarepl/testclasses/PublicBaseTestClass.java diff --git a/src/javarepl/Utils.java b/src/javarepl/Utils.java index 68e3c89..04d8236 100644 --- a/src/javarepl/Utils.java +++ b/src/javarepl/Utils.java @@ -1,7 +1,6 @@ package javarepl; import com.googlecode.totallylazy.*; -import com.googlecode.totallylazy.numbers.Numbers; import java.io.File; import java.io.FileInputStream; @@ -25,7 +24,7 @@ import static com.googlecode.totallylazy.Strings.replace; import static com.googlecode.totallylazy.URLs.url; import static java.lang.String.format; -import static java.lang.reflect.Modifier.isPrivate; +import static java.lang.reflect.Modifier.isPublic; import static java.net.URLDecoder.decode; public class Utils { @@ -40,7 +39,7 @@ public static String randomIdentifier(String prefix) { public static Type extractType(Type type) { if (type instanceof Class) { Class clazz = (Class) type; - if (clazz.isAnonymousClass() || clazz.isSynthetic()) { + if (clazz.isAnonymousClass() || clazz.isSynthetic() || clazz.isMemberClass()) { if (clazz.getGenericSuperclass().equals(Object.class)) { return extractType(sequence(clazz.getGenericInterfaces()) .headOption() @@ -50,7 +49,7 @@ public static Type extractType(Type type) { } } - if (isPrivate(clazz.getModifiers())) + if (!isPublic(clazz.getModifiers())) return extractType(clazz.getGenericSuperclass()); return clazz; diff --git a/test/javarepl/UtilsTest.java b/test/javarepl/UtilsTest.java index 7ff985b..d5e2e87 100644 --- a/test/javarepl/UtilsTest.java +++ b/test/javarepl/UtilsTest.java @@ -13,9 +13,11 @@ import static com.googlecode.totallylazy.URLs.url; import static java.util.Arrays.asList; import static javarepl.Utils.*; +import static javarepl.testclasses.Constructors.*; import static junit.framework.Assert.assertTrue; import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; public class UtilsTest { @Test @@ -33,8 +35,11 @@ public void checksJavaVersionCorrectly() { public void extractsTypeFromClass() { assertThat(extractType("".getClass()), hasFormOf("class java.lang.String")); assertThat(extractType(asList(1, 2, 3).getClass()), hasFormOf("java.util.AbstractList")); - assertThat(extractType(anonymousInnerRunnable().getClass()), hasFormOf("interface java.lang.Runnable")); - assertThat(extractType(anonymousInnerArrayList().getClass()), hasFormOf("java.util.ArrayList")); + assertThat(extractType(anonymousInnerGenericTestClass().getClass()), hasFormOf("javarepl.testclasses.GenericTestClass")); + assertThat(extractType(anonymousInnerBaseTestInterface().getClass()), hasFormOf("interface javarepl.testclasses.BaseTestInterface")); + assertThat(extractType(protectedAccessTestClass().getClass()), hasFormOf("class javarepl.testclasses.ProtectedAccessTestClass")); + assertThat(extractType(privateAccessTestClass().getClass()), hasFormOf("class javarepl.testclasses.PublicBaseTestClass")); + assertThat(extractType(defaultAccessTestClass().getClass()), hasFormOf("class javarepl.testclasses.PublicBaseTestClass")); } @Test @@ -88,4 +93,5 @@ public void run() { } }; } + } diff --git a/test/javarepl/testclasses/BaseTestInterface.java b/test/javarepl/testclasses/BaseTestInterface.java new file mode 100644 index 0000000..4fe0baf --- /dev/null +++ b/test/javarepl/testclasses/BaseTestInterface.java @@ -0,0 +1,4 @@ +package javarepl.testclasses; + +public interface BaseTestInterface { +} diff --git a/test/javarepl/testclasses/Constructors.java b/test/javarepl/testclasses/Constructors.java new file mode 100644 index 0000000..55860e6 --- /dev/null +++ b/test/javarepl/testclasses/Constructors.java @@ -0,0 +1,28 @@ +package javarepl.testclasses; + +public class Constructors { + public static Object defaultAccessTestClass() { + return new DefaultAccessTestClass(); + } + + public static Object protectedAccessTestClass() { + return new ProtectedAccessTestClass(); + } + + public static Object privateAccessTestClass() { + return new PrivateAccessTestClass(); + } + + public static Object anonymousInnerBaseTestInterface() { + return new BaseTestInterface() { + }; + } + + public static Object anonymousInnerGenericTestClass() { + return new GenericTestClass() { + }; + } + + + private static class PrivateAccessTestClass extends PublicBaseTestClass implements BaseTestInterface{} +} diff --git a/test/javarepl/testclasses/DefaultAccessTestClass.java b/test/javarepl/testclasses/DefaultAccessTestClass.java new file mode 100644 index 0000000..4dbb9e1 --- /dev/null +++ b/test/javarepl/testclasses/DefaultAccessTestClass.java @@ -0,0 +1,4 @@ +package javarepl.testclasses; + +class DefaultAccessTestClass extends PublicBaseTestClass implements BaseTestInterface { +} diff --git a/test/javarepl/testclasses/GenericTestClass.java b/test/javarepl/testclasses/GenericTestClass.java new file mode 100644 index 0000000..eaf6b6d --- /dev/null +++ b/test/javarepl/testclasses/GenericTestClass.java @@ -0,0 +1,5 @@ +package javarepl.testclasses; + + +public class GenericTestClass extends PublicBaseTestClass { +} diff --git a/test/javarepl/testclasses/ProtectedAccessTestClass.java b/test/javarepl/testclasses/ProtectedAccessTestClass.java new file mode 100644 index 0000000..1290fa1 --- /dev/null +++ b/test/javarepl/testclasses/ProtectedAccessTestClass.java @@ -0,0 +1,4 @@ +package javarepl.testclasses; + +public class ProtectedAccessTestClass extends PublicBaseTestClass implements BaseTestInterface{ +} diff --git a/test/javarepl/testclasses/PublicBaseTestClass.java b/test/javarepl/testclasses/PublicBaseTestClass.java new file mode 100644 index 0000000..68c2db9 --- /dev/null +++ b/test/javarepl/testclasses/PublicBaseTestClass.java @@ -0,0 +1,4 @@ +package javarepl.testclasses; + +public class PublicBaseTestClass{ +}