You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Daniel Sun (JIRA)" <ji...@apache.org> on 2018/05/07 02:06:00 UTC

[jira] [Comment Edited] (GROOVY-7204) Static type checking and compilation fail when multiple generics in use

    [ https://issues.apache.org/jira/browse/GROOVY-7204?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16465359#comment-16465359 ] 

Daniel Sun edited comment on GROOVY-7204 at 5/7/18 2:05 AM:
------------------------------------------------------------

Here is the simplified version:

 
{code:java}
import java.io.Serializable;

class Repository<T, S extends Serializable> {
    void delete(T arg) {}
    void delete(S arg) {} // compilation passes if the code is commented
}

@groovy.transform.TypeChecked
def test() {
    Repository<String, Long> r = new Repository<String, Long>()
    r.delete('foo')
}

test()
{code}


was (Author: daniel_sun):
Here is the simplified version:

 
{code:java}
import java.io.Serializable;

class Repository<T, S extends Serializable> {
    void delete(T arg) {}
    void delete(S arg) {} // compilation passes if the code commented
}

@groovy.transform.TypeChecked
def test() {
    Repository<String, Long> r = new Repository<String, Long>()
    r.delete('foo')
}

test()
{code}

> Static type checking and compilation fail when multiple generics in use
> -----------------------------------------------------------------------
>
>                 Key: GROOVY-7204
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7204
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation, Static Type Checker
>    Affects Versions: 2.3.8
>            Reporter: Mauro Molinari
>            Priority: Critical
>
> Consider the following interfaces:
> {code:title=CrudRepository.java}
> package f;
> import java.io.Serializable;
> public interface CrudRepository<T, S extends Serializable> {
> 	void delete(S arg);
> 	void delete(T arg);
> }
> {code}
> {code:title=MyRepository.java}
> package f;
> public interface MyRepository extends CrudRepository<String, Long> {
> }
> {code}
> The following implementation class:
> {code:title=MyRepositoryImpl.java}
> package f;
> public class MyRepositoryImpl implements MyRepository {
> 	@Override
> 	public void delete(String arg) {
> 		System.out.println("String");
> 	}
> 	@Override
> 	public void delete(Long arg) {
> 		System.out.println("Long");
> 	}
> }
> {code}
> And the following Groovy class:
> {code:title=MyClass.groovy}
> package f
> import groovy.transform.CompileStatic;
> import groovy.transform.TypeChecked;
> @TypeChecked
> class MyClass {
> 	static MyRepository factory() {
> 		return new MyRepositoryImpl()
> 	}
> 	
> 	static void main(String[] args) {
> 		MyRepository r = factory()
> 		r.delete('foo')
> 	}	
> }
> {code}
> Static type checking returns the following error:
> {noformat}
> MyClass.groovy: 15: [Static type checking] - Cannot call f.MyRepository#delete(S) with arguments [java.lang.String]
> {noformat}
> The same applies if you use {{@CompileStatic}} instead of {{@TypeChecked}}.
> Note that if, In the previous code, you change the method {{main}} by replacing:
> {code}
> MyRepository r = factory()
> {code}
> with: 
> {code}
> MyRepository r = new MyRepositoryImpl()
> {code}
> compilation succeeds. However in real code this might not be possible (the {{MyRepository}} instance may be injected and auto-generated, think of Spring Data for instance).
> The only workaround is (yet again...) to disable static type checking and static compilation.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)