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

Bug with @TypeChecked and generic properties of Java classes used in binary expressions

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.5
    • 2.4.6
    • Compiler
    • None

    Description

      I have the following two Java Classes:

      import java.math.BigDecimal;
      
      public abstract class Attribute<T> {
      
          private BigDecimal numericValue;
      
          public abstract T getValue();
      
          public abstract void setValue(T value);
      
          public BigDecimal getNumericValue() {
              return numericValue;
          }
      
          public void setNumericValue(BigDecimal numericValue) {
              this.numericValue = numericValue;
          }
      }
      
      import java.math.BigDecimal;
      
      public class LongAttribute extends Attribute<Long> {
          public Long getValue() {
              return getNumericValue() == null ? null : getNumericValue().longValue();
          }
          public void setValue(Long value) {
              setNumericValue(BigDecimal.valueOf(value));
          }
      }
      

      And a Groovy test class with @TypeChecked annotation:

      import groovy.transform.TypeChecked
      
      @TypeChecked
      class Scratch {
          public static void main(String[] args) {
              LongAttribute attr = new LongAttribute();
              attr.value = 0L
              println "getter: " + attr.value
              println "smaller as 0? " + (attr.value < 0)
          }
      }
      

      Compiling the class leads to the error message:

      Error:(10, 36) Groovyc: [Static type checking] - Cannot find matching method java.lang.Object#compareTo(int). Please check if the declared type is right and if the method exists.
      

      Removing the @TypeChecked annotation or moving the Java classes into the Groovy class as static inner classes suppresses the compilation error.

      Attachments

        Activity

          People

            paulk Paul King
            m.ewert Marc Ewert
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: