generics in Groovy

后端 未结 2 1114
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 11:39

The following Groovy code prints \"it works\"

def printIt(Class clazz) {
  println \"it works\"
}

printIt(String.class)
<         


        
2条回答
  •  难免孤独
    2021-01-18 12:25

    Since you know it's supposed to be an Exception, this works in Java (or Groovy):

    // true if the class is a subclass of Exception
    Exception.class.isAssignableFrom(clazz);
    

    That in no way uses the generic information, but that wouldn't be available in Java either.

提交回复
热议问题