Skip to content

Commit

Permalink
extract type correctly for default and protected classes (fixes #99)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertlatacz committed Nov 27, 2016
1 parent 6ea89c0 commit ddef650
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 7 deletions.
7 changes: 3 additions & 4 deletions src/javarepl/Utils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package javarepl;

import com.googlecode.totallylazy.*;
import com.googlecode.totallylazy.numbers.Numbers;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -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 {
Expand All @@ -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()
Expand All @@ -50,7 +49,7 @@ public static Type extractType(Type type) {
}
}

if (isPrivate(clazz.getModifiers()))
if (!isPublic(clazz.getModifiers()))
return extractType(clazz.getGenericSuperclass());

return clazz;
Expand Down
12 changes: 9 additions & 3 deletions test/javarepl/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<E>"));
assertThat(extractType(anonymousInnerRunnable().getClass()), hasFormOf("interface java.lang.Runnable"));
assertThat(extractType(anonymousInnerArrayList().getClass()), hasFormOf("java.util.ArrayList<java.lang.Object>"));
assertThat(extractType(anonymousInnerGenericTestClass().getClass()), hasFormOf("javarepl.testclasses.GenericTestClass<javarepl.testclasses.BaseTestInterface>"));
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
Expand Down Expand Up @@ -88,4 +93,5 @@ public void run() {
}
};
}

}
4 changes: 4 additions & 0 deletions test/javarepl/testclasses/BaseTestInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package javarepl.testclasses;

public interface BaseTestInterface {
}
28 changes: 28 additions & 0 deletions test/javarepl/testclasses/Constructors.java
Original file line number Diff line number Diff line change
@@ -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<BaseTestInterface>() {
};
}


private static class PrivateAccessTestClass extends PublicBaseTestClass implements BaseTestInterface{}
}
4 changes: 4 additions & 0 deletions test/javarepl/testclasses/DefaultAccessTestClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package javarepl.testclasses;

class DefaultAccessTestClass extends PublicBaseTestClass implements BaseTestInterface {
}
5 changes: 5 additions & 0 deletions test/javarepl/testclasses/GenericTestClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package javarepl.testclasses;


public class GenericTestClass<T extends BaseTestInterface> extends PublicBaseTestClass {
}
4 changes: 4 additions & 0 deletions test/javarepl/testclasses/ProtectedAccessTestClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package javarepl.testclasses;

public class ProtectedAccessTestClass extends PublicBaseTestClass implements BaseTestInterface{
}
4 changes: 4 additions & 0 deletions test/javarepl/testclasses/PublicBaseTestClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package javarepl.testclasses;

public class PublicBaseTestClass{
}

0 comments on commit ddef650

Please sign in to comment.