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

[Static type checking] - Inconvertible types: cannot cast java.io.Writer to SomeInterface

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 2.1.2
    • Static Type Checker
    • Java 1.7.0_17

    Description

      Similar to GROOVY-5870, but that has been marked fixed.

      Compiling this code:

      import groovy.transform.CompileStatic;
      
      import java.io.IOException;
      
      class CastingBugTest {
      
          
          public void testCastingToInterface() {
              Writer myWriter=MyTester.createWriter()
              MyTester.sayHelloIfSomeInterface(myWriter)
          }
      }
      
      @CompileStatic
      class MyTester {
          static Writer createWriter() {
              return new MyWriter()
          }
          
          static void sayHelloIfSomeInterface(Writer writer) {
              if(writer instanceof SomeInterface) {
                  ((SomeInterface)writer).sayHello()
              }
          }
      }
      
      @CompileStatic
      interface SomeInterface {
          public void sayHello();
      }
      
      @CompileStatic
      class MyWriter extends Writer implements SomeInterface {
          public void sayHello() {
              println "Hello"
              
          }
      
          @Override
          public void write(char[] cbuf, int off, int len) throws IOException {
              
          }
      
          @Override
          public void flush() throws IOException {
              
          }
      
          @Override
          public void close() throws IOException {
              
          }
      }
      

      Causes this error:

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      CastingBugTest.groovy: 22: [Static type checking] - Inconvertible types: cannot cast java.io.Writer to SomeInterface
       @ line 22, column 13.
                     ((SomeInterface)writer).sayHello()
                     ^
      
      1 error
      

      Attachments

        1. CastingBugTest.groovy
          1.0 kB
          Lari Hotari

        Activity

          People

            melix Cédric Champeau
            lhotari Lari Hotari
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: