Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions internal/codenav/implementations.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func methodsOverrideMatch(base, sub *Member) bool {
if base.Kind != kMethod || sub.Kind != kMethod || base.Name != sub.Name {
return false
}
if base.Arity != sub.Arity || base.Varargs != sub.Varargs {
if base.Arity != sub.Arity {
return false
}
if base.Params == sub.Params || base.Params == "" || sub.Params == "" {
Expand All @@ -438,8 +438,11 @@ func methodsOverrideMatch(base, sub *Member) bool {
if bParts[i] == sParts[i] {
continue
}
// Allow single-identifier generic type parameters (e.g. T -> String) in subclasses.
if isGenericTypeParam(bParts[i]) || isGenericTypeParam(sParts[i]) {
// Allow a type variable to stand for a substituted type, also as an array component.
bType, sType := bParts[i], sParts[i]
bElem, sElem := strings.TrimRight(bType, "[]"), strings.TrimRight(sType, "[]")
bDims, sDims := len(bType)-len(bElem), len(sType)-len(sElem)
if bDims <= sDims && isGenericTypeParam(bElem) || sDims <= bDims && isGenericTypeParam(sElem) {
continue
}
return false
Expand Down
34 changes: 34 additions & 0 deletions internal/codenav/implementations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,40 @@ class Caller {
}
}

func TestArraySignatureImplementations(t *testing.T) {
const base = `package demo;

interface Buffer<T> {
void write(String text);
void write(String... texts);
void put(T[] values);
}
`
const sub = `package demo;

class TextBuffer implements Buffer<String> {
public void write(String text) {}
public void write(String[] texts) {}
public void put(String[] values) {}
}
`
const basePath, subPath = "demo/Buffer.java", "demo/TextBuffer.java"
ix := buildNavIndex(map[string]string{basePath: base, subPath: sub})
for _, tt := range []struct{ baseMethod, subMethod string }{
{"write(String text)", "write(String text)"},
{"write(String... texts)", "write(String[] texts)"},
{"put(T[] values)", "put(String[] values)"},
} {
t.Run(tt.baseMethod, func(t *testing.T) {
got := ix.Implementations(basePath, []byte(base), indexOf(base, tt.baseMethod))
want := declAtSrc(t, subPath, sub, tt.subMethod)
if len(got) != 1 || got[0].Path != want.Path || got[0].Line != want.Line || got[0].Column != want.Column {
t.Fatalf("implementations = %+v, want only %+v", got, want)
}
})
}
}

func TestCyclicInheritanceGuardDoesNotLoop(t *testing.T) {
aSrc := "package cyc; class A extends B { void m() {} }\n"
bSrc := "package cyc; class B extends A { void m() {} }\n"
Expand Down
81 changes: 81 additions & 0 deletions internal/codenav/jdtls_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,87 @@ func TestOverloadsWithTheSameArity(t *testing.T) {
}
}

func TestArrayOverloads(t *testing.T) {
const path = "src/p/Use.java"
const src = `package p;
import java.nio.charset.Charset;
import static java.nio.charset.StandardCharsets.UTF_8;

class Buffer {
void write(int value) {}
void write(byte[] bytes) {}
void write(byte rows[][]) {}
void collect(Object value) {}
void collect(byte... values) {}
void select(Object value) {}
void select(CharSequence[] values) {}
void read(byte[] values, String tag) {}
void read(java.io.InputStream stream, Object tag) {}
}

class Key {
byte[] getBytes(Charset charset) { return new byte[0]; }
byte getLegacy()[] { return new byte[0]; }
byte bytes[] = new byte[0];
}

class Result<T> {
T getData() { return null; }
}

class Use {
void run(Buffer buffer, Key key, Result<byte[]> result, byte... rest) {
byte rows[][] = new byte[1][1];
buffer.write(key.getBytes(UTF_8));
buffer.write(42);
buffer.write(rows);
buffer.write(rows[0]);
buffer.write(rows[0][0]);
buffer.write(new byte[]{1});
buffer.write((byte[]) null);
buffer.write((key.getLegacy()));
buffer.write(key.bytes);
buffer.write(rest);
buffer.write(rest[0]);
buffer.collect(new byte[1]);
buffer.collect((byte) 1);
buffer.collect((byte) 1, (byte) 2);
buffer.select(new String[1]);
buffer.read(result.getData(), "data");
}
}
`
ix := NewIndex()
ix.Put(ParseFile(path, []byte(src)))
for _, test := range []struct{ call, decl string }{
{"write(key.getBytes(UTF_8))", "write(byte[] bytes)"},
{"write(42)", "write(int value)"},
{"write(rows)", "write(byte rows[][])"},
{"write(rows[0])", "write(byte[] bytes)"},
{"write(rows[0][0])", "write(int value)"},
{"write(new byte[]{1})", "write(byte[] bytes)"},
{"write((byte[]) null)", "write(byte[] bytes)"},
{"write((key.getLegacy()))", "write(byte[] bytes)"},
{"write(key.bytes)", "write(byte[] bytes)"},
{"write(rest)", "write(byte[] bytes)"},
{"write(rest[0])", "write(int value)"},
{"collect(new byte[1])", "collect(byte... values)"},
{"collect((byte) 1)", "collect(Object value)"},
{"collect((byte) 1, (byte) 2)", "collect(byte... values)"},
{"select(new String[1])", "select(CharSequence[] values)"},
{`read(result.getData(), "data")`, "read(byte[] values, String tag)"},
} {
t.Run(test.call, func(t *testing.T) {
off := offsetOf(t, src, test.call, 0, 0)
got, ok := ix.Resolve(path, []byte(src), off)
want := declAtSrc(t, path, src, test.decl)
if !ok || got.Path != want.Path || got.Line != want.Line || got.Column != want.Column {
t.Fatalf("got %v (resolved %v), want %v", got, ok, want)
}
})
}
}

var jdtlsCaseProject = map[string]string{
"src/q/Manager.java": `package q;

Expand Down
Loading
Loading