diff --git a/src/javarepl/completion/InstanceMemberCompleter.java b/src/javarepl/completion/InstanceMemberCompleter.java index 1d76ca2..869e120 100644 --- a/src/javarepl/completion/InstanceMemberCompleter.java +++ b/src/javarepl/completion/InstanceMemberCompleter.java @@ -3,6 +3,7 @@ import com.googlecode.totallylazy.Option; import com.googlecode.totallylazy.Sequence; import com.googlecode.totallylazy.Sequences; +import com.googlecode.totallylazy.reflection.Types; import javarepl.Evaluator; import javarepl.reflection.ClassReflection; import javarepl.reflection.MemberReflection; @@ -33,16 +34,17 @@ public CompletionResult call(String expression) throws Exception { final Boolean canComplete = packagePart.matches("[a-zA-Z0-9\\$_\\\\.\\(\\[\\]]*") && packagePart.contains("."); final int beginIndex = packagePart.lastIndexOf('.') + 1; - Option aClass = canComplete - ? evaluator.typeOfExpression(packagePart.substring(0, beginIndex - 1)) - : none(Type.class); + Option> aClass = canComplete + ? evaluator.typeOfExpression(packagePart.substring(0, beginIndex - 1)).map(Types::classOf) + : none(); if (aClass.isDefined()) { ClassReflection classReflection = reflectionOf(aClass.get()); Sequence join = Sequences.empty(MemberReflection.class) .join(classReflection.declaredFields()) - .join(classReflection.declaredMethods()); + .join(classReflection.declaredMethods()) + .unique(); Sequence candidates = join .filter(isPublic().and(not(isStatic()))) diff --git a/test/javarepl/completion/InstanceMemberCompleterTest.java b/test/javarepl/completion/InstanceMemberCompleterTest.java index aeb0200..7061664 100644 --- a/test/javarepl/completion/InstanceMemberCompleterTest.java +++ b/test/javarepl/completion/InstanceMemberCompleterTest.java @@ -1,20 +1,25 @@ package javarepl.completion; +import javarepl.completion.helpers.SimpleCompletionInstance; import org.junit.Test; +import static com.googlecode.totallylazy.Sequences.sequence; import static javarepl.completion.CompleterTestHelper.*; import static org.junit.Assert.assertThat; public class InstanceMemberCompleterTest { @Test - public void shouldCompleteStaticMembers() { - InstanceMemberCompleter completer = new InstanceMemberCompleter(evaluator()); - String expression = TestOuterClass.class.getCanonicalName() + ".staticField."; + public void shouldCompleteInstance() { + String expression = "ci.over"; + String instanceClassName = SimpleCompletionInstance.class.getCanonicalName(); + InstanceMemberCompleter completer = new InstanceMemberCompleter(evaluator("ci = new "+instanceClassName+"()")); -// CompletionResult result = completer.apply(expression); -// assertThat(result, -// completesTo(candidates("staticField", "staticMethod("), position(expression.length()))); + CompletionResult result = completer.apply(expression); + + assertThat(result, + completesTo(candidates(new CompletionCandidate("overloadedMethod(", sequence("int overloadedMethod()", "int overloadedMethod(int)", "int overloadedMethod(int, int)"))), + position(3))); assertThat(completer.apply("invalid"), completesTo(candidatesValues(), position(0))); } diff --git a/test/javarepl/completion/helpers/SimpleCompletionInstance.java b/test/javarepl/completion/helpers/SimpleCompletionInstance.java new file mode 100644 index 0000000..fce8e63 --- /dev/null +++ b/test/javarepl/completion/helpers/SimpleCompletionInstance.java @@ -0,0 +1,32 @@ +package javarepl.completion.helpers; + + +import java.util.Date; + + +public class SimpleCompletionInstance { + public void voidMethod() { + } + + + public int primitiveTypeReturningMethod() { + return 0; + } + + public Date complexTypeReturningMethod() { + return new Date(); + } + + public int overloadedMethod() { + return 0; + } + + public int overloadedMethod(int param) { + return param; + } + + public int overloadedMethod(int param1, int param2) { + return param1 + param2; + } + +}