Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7204

Static type checking and compilation fail when multiple generics in use

    XMLWordPrintableJSON

    Details

      Description

      Consider the following interfaces:

      CrudRepository.java
      package f;
      
      import java.io.Serializable;
      
      public interface CrudRepository<T, S extends Serializable> {
      
      	void delete(S arg);
      	void delete(T arg);
      }
      
      MyRepository.java
      package f;
      
      public interface MyRepository extends CrudRepository<String, Long> {
      }
      

      The following implementation class:

      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");
      	}
      }
      

      And the following Groovy class:

      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')
      	}	
      }
      

      Static type checking returns the following error:

      MyClass.groovy: 15: [Static type checking] - Cannot call f.MyRepository#delete(S) with arguments [java.lang.String]
      

      The same applies if you use @CompileStatic instead of @TypeChecked.

      Note that if, In the previous code, you change the method main by replacing:

      MyRepository r = factory()
      

      with:

      MyRepository r = new MyRepositoryImpl()
      

      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.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                daniel_sun Daniel Sun
                Reporter:
                mauromol Mauro Molinari
              • Votes:
                2 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: