Skip to content

Commit 95284b0

Browse files
committed
Fix for #1077: T[] and Object[] cannot accept primitive arrays
1 parent 5c18605 commit 95284b0

File tree

2 files changed

+32
-3
lines changed
  • base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/core/util
  • base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search

2 files changed

+32
-3
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/DGMInferencingTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,31 @@ public void testDGM45a() {
587587
}
588588
}
589589

590+
@Test
591+
public void testDGM45b() {
592+
String contents =
593+
//@formatter:off
594+
"String[] array = []\n" +
595+
"array.sort { a, b ->\n" +
596+
" a.trim() <=> b.trim()\n" +
597+
"}\n";
598+
//@formatter:on
599+
600+
assertDeclType(contents, "sort", "org.codehaus.groovy.runtime.DefaultGroovyMethods");
601+
}
602+
603+
@Test // https://github.com/groovy/groovy-eclipse/issues/1077
604+
public void testDGM45c() {
605+
String contents =
606+
//@formatter:off
607+
"char[] array = []\n" +
608+
"array.sort()\n";
609+
//@formatter:on
610+
611+
int offset = contents.indexOf("sort");
612+
assertUnknownConfidence(contents, offset, offset + 4);
613+
}
614+
590615
@Test
591616
public void testDGM46() {
592617
String contents =

base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/core/util/GroovyUtils.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2019 the original author or authors.
2+
* Copyright 2009-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -397,8 +397,12 @@ private static boolean isAssignable(boolean array, ClassNode source, ClassNode t
397397
} else*/ if (target.isInterface()) {
398398
result = GeneralUtils.isOrImplements(source, target);
399399
} else if (array) {
400-
// Object or Object[] is universal receiver for an array
401-
result = ClassHelper.OBJECT_TYPE.equals(target) || source.isDerivedFrom(target);
400+
if (target.isGenericsPlaceHolder() || target.equals(ClassHelper.OBJECT_TYPE)) {
401+
// T[] or Object[] cannot accept a primitive array
402+
result = !ClassHelper.isPrimitiveType(source);
403+
} else {
404+
result = source.isDerivedFrom(target);
405+
}
402406
} else {
403407
result = getWrapperTypeIfPrimitive(source).isDerivedFrom(getWrapperTypeIfPrimitive(target));
404408
}

0 commit comments

Comments
 (0)