List of all default methods

Remi Forax forax at univ-mlv.fr
Thu Nov 21 03:59:52 PST 2013


On 11/21/2013 09:48 AM, Jochen Theodorou wrote:
> Hi all,
>
> we already found we have some trouble with List#sort(Comparable) in
> Groovy, but to be able to identify further problems it would be very
> helpful to have a list of all default methods, that have been added. Is
> there one somewhere?
>
> bye Jochen
>

Not a beautiful code, it takes rt.jar as argument and do the job :)

public class AllDefaultMethods {
   private static Class<?> loadClassFromFileName(String name) {
     String className = name.substring(0, name.length() - 
".class".length());
     try {
       return Class.forName(className.replace('/', '.'));
     } catch (Throwable t) {
       // skip unknown class, or LinkageError
       return null;
     }
   }

   public static void main(String[] args) throws IOException {
     try(JarFile jarFile = new JarFile(args[0])) {
       jarFile.stream()
              .map(JarEntry::getName)
              .filter(name -> name.endsWith(".class"))
              .map(AllDefaultMethods::loadClassFromFileName)
              .filter(Objects::nonNull)
              .flatMap(type -> Arrays.stream(type.getDeclaredMethods()))
              .filter(Method::isDefault)
              .forEach(System.out::println);
     }
   }
}

cheers,
Rémi



More information about the lambda-dev mailing list