private static Constraint loadConstraint ( Annotation context ) { Constraint constraint = null ; final ServiceLoader < Constraint > constraints = ServiceLoader . load ( Constraint . class ) ; for ( Constraint aConstraint : constraints ) { try { aConstraint . getClass ( ) . getDeclaredMethod ( "check" , context . annotationType ( ) ) ; constraint = aConstraint ; break ; } catch ( NoSuchMethodException e ) { // Look for next implementation if method not found with required signature. } } if ( constraint == null ) { throw new IllegalStateException ( "Couldn't found any implementation of " + Constraint . class . getName ( ) ) ; } return constraint ; }
we have only one implementation on classpath .