private boolean inSamePackage ( Class < ? > c1 , Class < ? > c2 ) { String nameC1 = c1 . getName ( ) ; String nameC2 = c2 . getName ( ) ; int indexDotC1 = nameC1 . lastIndexOf ( ' ' ) ; int indexDotC2 = nameC2 . lastIndexOf ( ' ' ) ; if ( indexDotC1 != indexDotC2 ) { return false ; // cannot be in the same package if indices are not the same } if ( indexDotC1 == - 1 ) { return true ; // both of them are in default package } return nameC1 . regionMatches ( 0 , nameC2 , 0 , indexDotC1 ) ; }
Checks if two classes belong to the same package .