private static void findClasses(List<String> classes, File dir, String prefix) throws IOException { for (File subDir : dir.listFiles(DIR_FILTER)) { findClasses(classes, subDir, prefix); } for (File classFile : dir.listFiles(CLASS_FILE_FILTER)) { try (InputStream is = new FileInputStream(classFile)) { if (hasMainMethod(is)) { classes.add(convertToClass(classFile.getAbsolutePath(), prefix)); } } } }
public static Set<String> getResources(String resource, List<ClassLoader> additionalClassLoaders) throws IOException { ClassLoader[] classLoaders = mergeClassLoaders(additionalClassLoaders); Set<String> ret = new HashSet<>(); for (ClassLoader cl : classLoaders) { Enumeration<URL> urlEnum = cl.getResources(resource); ret.addAll(extractUrlAsStringsFromEnumeration(urlEnum)); } return ret; }
private static ClassLoader[] mergeClassLoaders(List<ClassLoader> additionalClassLoaders) { ClassLoader[] classLoaders; if (additionalClassLoaders != null && !additionalClassLoaders.isEmpty()) { classLoaders = ArrayUtils.addAll(getClassLoaders(), additionalClassLoaders.toArray(new ClassLoader[additionalClassLoaders.size()])); } else { classLoaders = getClassLoaders(); } return classLoaders; }
try( URLClassLoader projectClassLoader = ClassUtil.createProjectClassLoader(getContext().getProject().getCompileClasspathElements(), log)) {
public static Set<String> getResources(String resource) throws IOException { return getResources(resource, null); }
String getMainClass() throws MojoExecutionException { if (mainClass != null) { return mainClass; } // Try to detect a single main class from target/classes try { List<String> foundMainClasses = ClassUtil.findMainClasses(classesDir); if (foundMainClasses.size() == 0) { return mainClass = null; } else if (foundMainClasses.size() == 1) { return mainClass = foundMainClasses.get(0); } else { log.warn("Found more than one main class : %s. Ignoring ....", foundMainClasses); return mainClass = null; } } catch (IOException e) { throw new IllegalStateException("Can not examine main classes: " + e, e); } } }
public static <T> Class<T> classForName(String className, List<ClassLoader> additionalClassLoaders) { ClassLoader[] classLoaders = mergeClassLoaders(additionalClassLoaders); Set<ClassLoader> tried = new HashSet<>(); for (ClassLoader loader : classLoaders) { // Go up the classloader stack to eventually find the server class. Sometimes the WebAppClassLoader // hide the server classes loaded by the parent class loader. while (loader != null) { try { if (!tried.contains(loader)) { return (Class<T>) Class.forName(className, true, loader); } } catch (ClassNotFoundException ignored) {} tried.add(loader); loader = loader.getParent(); } } return null; }
/** * Find all classes below a certain directory which contain * main() classes * * @param rootDir the directory to start from * @return List of classes with "public void static main(String[] args)" methods. Can be empty, but not null. * @exception IOException if something goes wrong */ public static List<String> findMainClasses(File rootDir) throws IOException { List<String> ret = new ArrayList<>(); if (!rootDir.exists()) { return ret; } if (!rootDir.isDirectory()) { throw new IllegalArgumentException(String.format("Path %s is not a directory",rootDir.getPath())); } findClasses(ret, rootDir, rootDir.getAbsolutePath() + "/"); return ret; }
Class<T> clazz = ClassUtil.classForName(entry.getClassName(), additionalClassLoaders); if (clazz == null) { throw new ClassNotFoundException("Class " + entry.getClassName() + " could not be found");
public EnricherManager(ResourceConfig resourceConfig, EnricherContext enricherContext, Optional<List<String>> extraClasspathElements) { PluginServiceFactory<EnricherContext> pluginFactory = new PluginServiceFactory<>(enricherContext); extraClasspathElements.ifPresent( cpElements -> pluginFactory.addAdditionalClassLoader(ClassUtil.createProjectClassLoader(cpElements, enricherContext.getLog()))); this.log = enricherContext.getLog(); this.defaultEnricherConfig = enricherContext.getConfiguration().getProcessorConfig().orElse(ProcessorConfig.EMPTY); this.enrichers = pluginFactory.createServiceObjects("META-INF/fabric8-enricher-default", "META-INF/fabric8/enricher-default", "META-INF/fabric8-enricher", "META-INF/fabric8/enricher"); logEnrichers(filterEnrichers(defaultEnricherConfig, enrichers)); }
private <T> void readServiceDefinitions(Map<ServiceEntry, T> extractorMap, String defPath) { try { for (String url : ClassUtil.getResources(defPath, additionalClassLoaders)) { readServiceDefinitionFromUrl(extractorMap, url); } } catch (IOException e) { throw new IllegalStateException("Cannot load service from " + defPath + ": " + e, e); } }
String getMainClass() throws MojoExecutionException { if (mainClass != null) { return mainClass; } // Try to detect a single main class from target/classes try { List<String> foundMainClasses = ClassUtil.findMainClasses(classesDir); if (foundMainClasses.size() == 0) { return mainClass = null; } else if (foundMainClasses.size() == 1) { return mainClass = foundMainClasses.get(0); } else { log.warn("Found more than one main class : %s. Ignoring ....", foundMainClasses); return mainClass = null; } } catch (IOException e) { throw new IllegalStateException("Can not examine main classes: " + e, e); } } }
new PluginServiceFactory<>(watcherCtx, ClassUtil.createProjectClassLoader(watcherCtx.getProject().getCompileClasspathElements(), watcherCtx.getLogger())) : new PluginServiceFactory<>(watcherCtx);
/** * Read all profiles found in the classpath. * * @param name name of the profile to lookup * @param ext to use (e.g. 'default' for checking 'profile-default.yml'. Can also be null or empty. * @return all profiles with this name stored in files with this extension * * @throws IOException if reading of a profile fails */ public static List<Profile> readAllFromClasspath(String name, String ext) throws IOException { List<Profile > ret = new ArrayList<>(); for (String location : getMetaInfProfilePaths(ext)) { for (String url : ClassUtil.getResources(location)) { for (Profile profile : fromYaml(new URL(url).openStream())) { if (name.equals(profile.getName())) { ret.add(profile); } } } } return ret; }
try { pluginFactory = genCtx.isUseProjectClasspath() ? new PluginServiceFactory<GeneratorContext>(genCtx, ClassUtil.createProjectClassLoader(genCtx.getProject().getCompileClasspathElements(), genCtx.getLogger())) : new PluginServiceFactory<GeneratorContext>(genCtx); } catch (DependencyResolutionRequiredException e) {