public static boolean isSamePackage(Class<?> class1,Class<?> class2){
  assert (!class1.isArray() && !class2.isArray());
  if (class1 == class2)   return true;
  if (class1.getClassLoader() != class2.getClassLoader())   return false;
  String name1=class1.getName(), name2=class2.getName();
  int dot=name1.lastIndexOf('.');
  if (dot != name2.lastIndexOf('.'))   return false;
  for (int i=0; i < dot; i++) {
    if (name1.charAt(i) != name2.charAt(i))     return false;
  }
  return true;
}

public static boolean isSamePackage ( Class < ? > class1 , Class < ? > class2 ) { assert ( ! class1 . isArray ( ) && ! class2 . isArray ( ) ) ; if ( class1 == class2 ) return true ; if ( class1 . getClassLoader ( ) != class2 . getClassLoader ( ) ) return false ; String name1 = class1 . getName ( ) , name2 = class2 . getName ( ) ; int dot = name1 . lastIndexOf ( '.' ) ; if ( dot != name2 . lastIndexOf ( '.' ) ) return false ; for ( int i = 0 ; i < dot ; i ++ ) { if ( name1 . charAt ( i ) != name2 . charAt ( i ) ) return false ; } return true ; }
test if two classes have the same class loader and package qualifier .
