public static Method getSAMMethod ( Class < ? > c ) { // SAM = single public abstract method // if the class is not abstract there is no abstract method if ( ! Modifier . isAbstract ( c . getModifiers ( ) ) ) return null ; if ( c . isInterface ( ) ) { Method [ ] methods = c . getMethods ( ) ; // res stores the first found abstract method Method res = null ; for ( Method mi : methods ) { // ignore methods, that are not abstract and from Object if ( ! Modifier . isAbstract ( mi . getModifiers ( ) ) ) continue ; // ignore trait methods which have a default implementation if ( mi . getAnnotation ( Traits . Implemented . class ) != null ) continue ; try { Object . class . getMethod ( mi . getName ( ) , mi . getParameterTypes ( ) ) ; continue ; } catch ( NoSuchMethodException e ) { /*ignore*/ } // we have two methods, so no SAM if ( res != null ) return null ; res = mi ; } return res ; } else { LinkedList < Method > methods = new LinkedList ( ) ; getAbstractMethods ( c , methods ) ; if ( methods . isEmpty ( ) ) return null ; ListIterator < Method > it = methods . listIterator ( ) ; while ( it . hasNext ( ) ) { Method m = it . next ( ) ; if ( hasUsableImplementation ( c , m ) ) it . remove ( ) ; } return getSingleNonDuplicateMethod ( methods ) ; } }
returns the abstract method from a SAM type if it is a SAM type .