Skip to content

Commit

Permalink
Reverting unwanted commit
Browse files Browse the repository at this point in the history
  • Loading branch information
albertlatacz committed Nov 28, 2016
1 parent 59db590 commit 3b000d5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 64 deletions.
52 changes: 9 additions & 43 deletions src/javarepl/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.googlecode.totallylazy.Sequence;
import com.googlecode.totallylazy.Sequences;
import com.googlecode.totallylazy.functions.Function1;
import com.googlecode.totallylazy.reflection.Types;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -19,16 +18,13 @@
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;

import static com.googlecode.totallylazy.Characters.characters;
import static com.googlecode.totallylazy.Files.*;
import static com.googlecode.totallylazy.Randoms.takeFromValues;
import static com.googlecode.totallylazy.Sequences.memorise;
import static com.googlecode.totallylazy.Sequences.one;
import static com.googlecode.totallylazy.Sequences.sequence;
import static com.googlecode.totallylazy.Sequences.*;
import static com.googlecode.totallylazy.Strings.replace;
import static com.googlecode.totallylazy.io.URLs.url;
import static com.googlecode.totallylazy.predicates.Predicates.*;
import static com.googlecode.totallylazy.reflection.Types.classOf;
import static com.googlecode.totallylazy.predicates.Predicates.is;
import static com.googlecode.totallylazy.predicates.Predicates.where;
import static java.lang.String.format;
import static java.lang.reflect.Modifier.isPublic;
import static java.net.URLDecoder.decode;
Expand All @@ -42,58 +38,28 @@ public static String randomIdentifier(String prefix) {
return prefix + "$" + takeFromValues(characters("abcdefghijklmnopqrstuvwxyz1234567890")).take(20).toString("");
}

public static Type extractType1(Type type) {
public static Type extractType(Type type) {
if (type instanceof Class) {
Class clazz = classOf(type);
Class clazz = (Class) type;
if (clazz.isAnonymousClass() || clazz.isSynthetic() || clazz.isMemberClass()) {
if (clazz.getGenericSuperclass().equals(Object.class)) {
return extractType1(sequence(clazz.getGenericInterfaces())
return extractType(sequence(clazz.getGenericInterfaces())
.headOption()
.getOrElse(Object.class));
} else {
return extractType1(clazz.getGenericSuperclass());
return extractType(clazz.getGenericSuperclass());
}
}

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

return clazz;
}

return type;
}


public static Sequence<Type> foo(Sequence<Type> typeSeq) {

Sequence<Class> classes = typeSeq.flatMap(Types::classOption).filter(not(equalTo(Object.class))).unsafeCast();

if (classes.isEmpty())
return sequence();

return classes.safeCast(Type.class).join(classes
.flatMap(c ->
foo(one(c.getGenericSuperclass()))
.join(foo(sequence(c.getGenericInterfaces())))
)).unique();


}


public static Type extractType(Type type) {

Sequence<Class> foos = foo(one(type)).flatMap(Types::classOption).filter(x -> isPublic(x.getModifiers())).unsafeCast();



System.out.println(foos.toString("\n"));


return foos.headOption().getOrElse(Object.class);
}

public static Throwable unwrapException(Throwable e) {
if (e instanceof InvocationTargetException)
return unwrapException(((InvocationTargetException) e).getTargetException());
Expand Down Expand Up @@ -195,7 +161,7 @@ public static Sequence<String> entries(File file) {
.map(ZipEntry::getName);
} catch (Exception e) {
System.err.println("Couldn't load entries from jar " + file.toURI() + ". " + e.getLocalizedMessage());
return Sequences.empty();
return empty();
}
}
}
Expand Down
21 changes: 2 additions & 19 deletions test/javarepl/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;

import static com.googlecode.totallylazy.Sequences.empty;
import static com.googlecode.totallylazy.Sequences.sequence;
Expand Down Expand Up @@ -35,30 +34,14 @@ public void checksJavaVersionCorrectly() {
@Test
public void extractsTypeFromClass() {
assertThat(extractType("".getClass()), hasFormOf("class java.lang.String"));
assertThat(extractType(asList(1, 2, 3).getClass()), hasFormOf("class java.util.AbstractList"));
assertThat(extractType(anonymousInnerGenericTestClass().getClass()), hasFormOf("class javarepl.testclasses.GenericTestClass"));
assertThat(extractType(asList(1, 2, 3).getClass()), hasFormOf("java.util.AbstractList<E>"));
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
public void extractsTypeFromClassNew() {
// assertThat(extractType(Arrays.asList(1,2,3).stream().parallel().getClass()), hasFormOf("class javarepl.testclasses.PublicBaseTestClass"));
// assertThat(extractTypeNew(Arrays.asList(1,2,3).stream().parallel().getClass()), hasFormOf("class javarepl.testclasses.PublicBaseTestClass"));
// assertThat(extractTypeNew(privateAccessTestClass().getClass()), hasFormOf("class javarepl.testclasses.PublicBaseTestClass"));
//
//
// assertThat(extractTypeNew("".getClass()), hasFormOf("class java.lang.String"));
// assertThat(extractTypeNew(asList(1, 2, 3).getClass()), hasFormOf("class java.util.AbstractList"));
// assertThat(extractTypeNew(anonymousInnerGenericTestClass().getClass()), hasFormOf("class javarepl.testclasses.GenericTestClass"));
// assertThat(extractTypeNew(anonymousInnerBaseTestInterface().getClass()), hasFormOf("interface javarepl.testclasses.BaseTestInterface"));
// assertThat(extractTypeNew(protectedAccessTestClass().getClass()), hasFormOf("class javarepl.testclasses.ProtectedAccessTestClass"));
// assertThat(extractTypeNew(privateAccessTestClass().getClass()), hasFormOf("class javarepl.testclasses.PublicBaseTestClass"));
// assertThat(extractTypeNew(defaultAccessTestClass().getClass()), hasFormOf("class javarepl.testclasses.PublicBaseTestClass"));
}

@Test
public void returnsResolvedUrl() {
assertThat(resolveURL("file:/aaa/bbb").toString(), equalTo("file:/aaa/bbb"));
Expand Down
4 changes: 2 additions & 2 deletions test/javarepl/rendering/TypeRendererTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class TypeRendererTest {
@Test
public void supportsGenericTypesRendering() {
assertThat(renderType(new ArrayList<String>() {
}.getClass()), is("java.util.ArrayList"));
assertThat(renderType(asList(1, 2, 3).getClass()), is("java.util.AbstractList"));
}.getClass()), is("java.util.ArrayList<java.lang.String>"));
assertThat(renderType(asList(1, 2, 3).getClass()), is("java.util.AbstractList<Object>"));
}
}

0 comments on commit 3b000d5

Please sign in to comment.