nashorn/samples/find_nonfinals.js
changeset 30984 844283fdfa54
parent 26235 62342aaa8ff9
--- a/nashorn/samples/find_nonfinals.js	Wed Jun 03 09:36:58 2015 +0200
+++ b/nashorn/samples/find_nonfinals.js	Wed Jun 03 18:44:12 2015 +0530
@@ -39,6 +39,7 @@
 
 var Class = Java.type("java.lang.Class");
 var System = Java.type("java.lang.System");
+var Thread = Java.type("java.lang.Thread");
 var File = Java.type("java.io.File");
 var JarFile = Java.type("java.util.jar.JarFile");
 var Modifier = Java.type("java.lang.reflect.Modifier");
@@ -58,6 +59,10 @@
 function analyzeClass(cls) {
     var methods = cls.getDeclaredMethods();
     for each (var method in methods) {
+        var methodModifiers = method.modifiers;
+        if (Modifier.isAbstract(methodModifiers) || Modifier.isNative(methodModifiers)) {
+            continue;
+        }
         // this requires -parameters option when compiling java sources
         var params = method.parameters;
         for each (var p in params) {
@@ -73,6 +78,8 @@
 }
 
 var jarFile = findNashorn();
+var ctxtLoader = Thread.currentThread().contextClassLoader;
+
 // load each class and use reflection to analyze each Class
 new JarFile(jarFile).stream().forEach(
     function(entry) {
@@ -80,8 +87,15 @@
         if (name.endsWith(".class")) {
             var clsName = name.substring(0, name.lastIndexOf('.class'));
             clsName = clsName.replace(/\//g, '.');
-            var cls = Class.forName(clsName);
-            analyzeClass(cls);
+            try {
+                // don't initialize to avoid for possible initialization errors 
+                var cls = Class.forName(clsName, false, ctxtLoader);
+                analyzeClass(cls);
+            } catch (e) {
+                // print exception and continue analysis for other classes
+                print("Failed to analyze " + clsName);
+                e.printStackTrace();
+            }
         }
     }
 )