Cleanup in ReflectiveEnvironment: more comments & logging.
authorJan Vrany <jan.vrany@fit.cvut.cz>
Sun, 03 Aug 2014 23:28:19 +0100
changeset 3189 304d3543242f
parent 3185 9003a9074b2c
child 3191 5260fddf10f6
Cleanup in ReflectiveEnvironment: more comments & logging. Added more comments here and there. Use java.util.logging when .jar could not be opened.
tools/java/src/stx/libjava/tools/environment/ReflectiveEnvironment.java
--- a/tools/java/src/stx/libjava/tools/environment/ReflectiveEnvironment.java	Wed Jul 30 16:34:59 2014 +0100
+++ b/tools/java/src/stx/libjava/tools/environment/ReflectiveEnvironment.java	Sun Aug 03 23:28:19 2014 +0100
@@ -12,6 +12,8 @@
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Vector;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipException;
 import java.util.zip.ZipFile;
@@ -23,7 +25,8 @@
 import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
 
 public class ReflectiveEnvironment implements INameEnvironment {
-	
+    
+	private static final Logger LOGGER = Logger.getLogger(ReflectiveEnvironment.class.getName());
 	private static final boolean DEBUG_isPackage = false;
 	private static Field FIELD_ClassLoader_Classes;
 	
@@ -110,9 +113,6 @@
 			return ctype;
 		} catch (ClassNotFoundException cnfe) {
 			return null;
-		} catch (RuntimeException e) {
-			e.printStackTrace();
-			return null;
 		}		
 	}
 	
@@ -204,7 +204,14 @@
 		if (isPackage(packageName, loader.getParent())) {
 		    return true;
 		}
-		// Check loaded classes...
+		
+		/*
+		 * Another performance optimization: consult the list of classes already loaded by
+		 * given classloader. That list is not readable by standard ClassLoader API though this
+		 * hack. On OpenJDK-based environments, there's a field {@link ClassLoader#classes}. 
+		 * However, we may or may not have an access to it. If not, just continue and do slow
+		 * search through .jars and .class directories.
+		 */
 		if (FIELD_ClassLoader_Classes != null) {
 			int packageNameLen = packageName.length();
 			Vector<Class<?>> classes;
@@ -219,6 +226,9 @@
     						(className.charAt(packageNameLen) == '.') &&
     						(className.startsWith(packageName))) {
     						return true;
+    					} else if ((classNameLen == packageNameLen) &&
+    					            className.equals(packageName)) {
+    					    return false;
     					}
     				}
 				}
@@ -269,7 +279,7 @@
                             zip = new ZipFile(file);
                             if (DEBUG_isPackage) System.err.println("checking jar: " + file.getName());                        
                             /*
-                             * try to short-circuit: look for .class file assuming
+                             * Try to short-circuit: look for .class file assuming
                              * packageName is actually class name. If found, return
                              * false (it is a class, not a package)
                              */
@@ -277,9 +287,9 @@
                                 return false;
                             } else {
                                 zips[i] = zip;
-                            }														
-    					} catch (ZipException e) {						
-    					} catch (IOException e) {					
+                            }														    					    					    
+    					} catch (IOException e) {
+    					    LOGGER.log(Level.INFO, "Failed to open .jar file", e);
     					}
     				}
     			}