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
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
import static org.codehaus.groovy.ast.tools.GeneralUtils.thisPropX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
import static org.codehaus.groovy.ast.tools.GenericsUtils.findActualTypeByGenericsPlaceholderName;
import static org.codehaus.groovy.ast.tools.GenericsUtils.makeDeclaringAndActualGenericsTypeMap;
import static org.codehaus.groovy.ast.tools.GenericsUtils.makeDeclaringAndActualGenericsTypeMapOfExactType;
import static org.codehaus.groovy.ast.tools.GenericsUtils.toGenericTypesString;
import static org.codehaus.groovy.ast.tools.WideningCategories.isBigDecCategory;
import static org.codehaus.groovy.ast.tools.WideningCategories.isBigIntCategory;
Expand Down Expand Up @@ -608,7 +608,7 @@ public void visitVariableExpression(final VariableExpression vexp) {
FieldNode fieldNode = (FieldNode) accessedVariable;
ClassNode actualType = findActualTypeByGenericsPlaceholderName(
fieldNode.getOriginType().getUnresolvedName(),
makeDeclaringAndActualGenericsTypeMap(fieldNode.getDeclaringClass(), typeCheckingContext.getEnclosingClassNode())
makeDeclaringAndActualGenericsTypeMapOfExactType(fieldNode.getDeclaringClass(), typeCheckingContext.getEnclosingClassNode())
);
if (actualType != null) {
storeType(vexp, actualType);
Expand Down
39 changes: 23 additions & 16 deletions src/test/groovy/bugs/Groovy9204.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
*/
package groovy.bugs

import groovy.test.NotYetImplemented
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit
import org.junit.Test

final class Groovy9204 {

@Test @NotYetImplemented
@Test
void testGenerics() {
def config = new CompilerConfiguration(
targetDirectory: File.createTempDir(),
Expand All @@ -36,29 +35,37 @@ final class Groovy9204 {
try {
def a = new File(parentDir, 'A.java')
a.write '''
public class A {
public String meth() {
return "hello";
}
}

abstract class One<T extends A> {
class One<T extends java.util.List> {
protected T field;
}

abstract class Two<T extends A> extends One<T> {
class Two<T extends java.util.List> extends One<T> {
}

abstract class Three extends Two<A> {
class Three extends Two<java.util.List> {
}

class Four extends Two<java.util.LinkedList> {
}

'''
def b = new File(parentDir, 'B.groovy')
b.write '''
@groovy.transform.CompileStatic
class B extends Three {
class ArrayListTest extends Three {
def test() {
field = new ArrayList()
field.add("hello")
field[0]
}
}

@groovy.transform.CompileStatic
class LinkedListTest extends Four {
def test() {
field.meth() // typeof(field) should be A
// ^^^^ Cannot find matching method java.lang.Object#meth()
field = new LinkedList()
field.addFirst("hello")
field[0]
}
}
'''
Expand All @@ -68,8 +75,8 @@ final class Groovy9204 {
cu.addSources(a, b)
cu.compile()

Class clazz = loader.loadClass('B')
assert clazz.newInstance().test() == 'hello'
assert loader.loadClass('LinkedListTest').getConstructor().newInstance().test() == 'hello'
assert loader.loadClass('ArrayListTest').getConstructor().newInstance().test() == 'hello'
} finally {
parentDir.deleteDir()
config.targetDirectory.deleteDir()
Expand Down