protected static final void initDatabase( Class<?>[] classes, Map<String, String> options, Map<String, Field> optionFields) { if (outputDB == null) { return; } // --Init Database outputDB.connect(); DBResultLogger logger = new DBResultLogger(outputDB, runName); Execution.logger = logger; outputDB.beginTransaction(); // --Add Options for (String key : optionFields.keySet()) { Field f = optionFields.get(key.toLowerCase()); // (try to save the declared option) String value = options.get(key); if (value == null) { // (if no declared option, get field value) try { boolean accessSave = true; if (!f.isAccessible()) { accessSave = false; f.setAccessible(true); } Object v = f.get(null); if (v == null) { value = "<null>"; } else if (v.getClass().isArray()) { value = Arrays.toString((Object[]) v); } else { value = v.toString(); } if (!accessSave) { f.setAccessible(false); } } catch (IllegalArgumentException e) { throw log.fail(e); } catch (IllegalAccessException e) { throw log.fail(e); } } logger.logOption(key, value, f.getDeclaringClass().getName() + "." + f.getName()); } // --Commit outputDB.endTransaction(); }
@SuppressWarnings("rawtypes") private static final Class filePathToClass(String cpEntry, String path) { if (path.length() <= cpEntry.length()) { throw new IllegalArgumentException("Illegal path: cp=" + cpEntry + " path=" + path); } if (path.charAt(cpEntry.length()) != '/') { throw new IllegalArgumentException("Illegal path: cp=" + cpEntry + " path=" + path); } path = path.substring(cpEntry.length() + 1); path = path.replaceAll("/", ".").substring(0, path.length() - 6); try { return Class.forName(path, false, ClassLoader.getSystemClassLoader()); } catch (ClassNotFoundException e) { throw log.fail("Could not load class at path: " + path); } catch (NoClassDefFoundError ex) { log.debug(LOG_TAG, "Class at path " + path + " is unloadable"); return null; } }
private static final void ensureScalaPath(Map<String, String> options, String[] cp) { // (check if it's in the classpath) try { Class.forName("scala.None", false, ClassLoader.getSystemClassLoader()); } catch (ClassNotFoundException e) { // (case: scala library not in the classpath) if (options.containsKey(SCALA_PATH)) { // (case: scala_path option set) try { String path = options.get(SCALA_PATH); if (!(new File(path).exists())) { System.err.println("The library strongly integrates with the Scala runtime, "); System.err.println("however it could not find the Scala library (scala-library.jar) "); System.err.println( "at the path given by the command line option '" + SCALA_PATH + "': " + path); log.exit(ExitCode.BAD_OPTION); } URL url = new File(path).toURI().toURL(); URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class<URLClassLoader> sysclass = URLClassLoader.class; Method method = sysclass.getDeclaredMethod("addURL", url.getClass()); boolean savedAccessible = method.isAccessible(); method.setAccessible(true); method.invoke(sysloader, new Object[] {url}); method.setAccessible(savedAccessible); Class.forName("scala.None", false, ClassLoader.getSystemClassLoader()); } catch (Exception ex) { throw log.fail(ex); } // end try catch } else { // (case: we cannot find the scala library at all) System.err.println("The library strongly integrates with the Scala runtime, "); System.err.println( "however it could not find the Scala library (scala-library.jar) in the classpath, "); System.err.println("and the '" + SCALA_PATH + "' command line argument is not set."); log.exit(ExitCode.BAD_OPTION); } } options.remove(SCALA_PATH); }
private static final Class<?>[] getVisibleClasses(Map<String, String> options) { // --Variables List<Class<?>> classes = new ArrayList<Class<?>>(); // (get classpath) String pathSep = System.getProperty("path.separator"); String[] cp = System.getProperties().getProperty("java.class.path", null).split(pathSep); // --Configuration ensureScalaPath(options, cp); // --Fill Options // (get classes) for (String entry : cp) { // (should skip?) if (entry.equals(".") || entry.trim().length() == 0) { continue; } boolean isIgnored = false; for (String pattern : ignoredClasspath) { if (entry.matches(pattern)) { log.debug(LOG_TAG, "Ignoring options in classpath element: " + entry); isIgnored = true; break; } } if (isIgnored) { continue; } // (no, don't skip) File f = new File(entry); if (f.isDirectory()) { // --Case: Files LazyFileIterator iter = new LazyFileIterator(f, ".*class$"); while (iter.hasNext()) { // (get the associated class) Class<?> clazz = filePathToClass(entry, iter.next().getPath()); if (clazz != null) { // (add the class if it's valid) classes.add(clazz); } } } else if (!isIgnored(entry)) { // --Case: Jar try { JarFile jar = new JarFile(f); Enumeration<JarEntry> e = jar.entries(); while (e.hasMoreElements()) { // (for each jar file element) JarEntry jarEntry = e.nextElement(); String clazz = jarEntry.getName(); if (clazz.matches(".*class$")) { // (if it's a class) clazz = clazz.substring(0, clazz.length() - 6).replaceAll("/", "."); // (add it) try { classes.add(Class.forName(clazz, false, ClassLoader.getSystemClassLoader())); } catch (ClassNotFoundException ex) { throw Log.internal("Could not load class in jar: " + f + " at path: " + clazz); } catch (NoClassDefFoundError ex) { log.debug(LOG_TAG, "Could not scan class: " + clazz + " (in jar: " + f + ")"); } } } } catch (IOException e) { throw log.fail("Could not open jar file: " + f + "(are you sure the file exists?)"); } } else { // case: ignored jar } } return classes.toArray(new Class<?>[classes.size()]); }