Java Code Examples for com.google.common.reflect.ClassPath#getTopLevelClassesRecursive()

The following examples show how to use com.google.common.reflect.ClassPath#getTopLevelClassesRecursive() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CheckUtil.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Gets all checkstyle's modules.
 * @return the set of checkstyle's module classes.
 * @throws IOException if the attempt to read class path resources failed.
 * @see #isCheckstyleModule(Class)
 */
public static Set<Class<?>> getCheckstyleModules() throws IOException {
    final Set<Class<?>> checkstyleModules = new HashSet<>();

    final ClassLoader loader = Thread.currentThread()
            .getContextClassLoader();
    final ClassPath classpath = ClassPath.from(loader);
    final String packageName = "com.puppycrawl.tools.checkstyle";
    final ImmutableSet<ClassPath.ClassInfo> checkstyleClasses = classpath
            .getTopLevelClassesRecursive(packageName);

    for (ClassPath.ClassInfo clazz : checkstyleClasses) {
        final Class<?> loadedClass = clazz.load();
        if (isCheckstyleModule(loadedClass)) {
            checkstyleModules.add(loadedClass);
        }
    }
    return checkstyleModules;
}
 
Example 2
Source File: DocumentationGenerator.java    From cukes with Apache License 2.0 6 votes vote down vote up
private static Map<CukesComponent, Multimap<StepType, StepDefinition>> collectSteps() throws IOException, ClassNotFoundException {
    Map<CukesComponent, Multimap<StepType, StepDefinition>> steps = createStepsStubs();
    ClassPath classPath = ClassPath.from(DocumentationGenerator.class.getClassLoader());
    ImmutableSet<ClassPath.ClassInfo> classes = classPath.getTopLevelClassesRecursive("lv.ctco.cukes");
    for (ClassPath.ClassInfo classInfo : classes) {
        String className = classInfo.getName();
        Class<?> aClass = Class.forName(className);
        Method[] methods = aClass.getMethods();
        for (Method method : methods) {
            StepType type = StepType.getTypeForMethod(method);
            if (type != null) {
                CukesComponent component = CukesComponent.findByClassName(className);
                steps.get(component).put(type, new StepDefinition(type.getPattern(method), type.getDescription(method)));
            }
        }
    }
    return steps;
}
 
Example 3
Source File: DefaultWidgetTypesRegistry.java    From Orienteer with Apache License 2.0 6 votes vote down vote up
public IWidgetTypesRegistry register(String packageName, ClassLoader classLoader) {
	ClassPath classPath;
	try {
		classPath = ClassPath.from(classLoader);
	} catch (IOException e) {
		throw new WicketRuntimeException("Can't scan classpath", e);
	}
	ImmutableSet<ClassInfo> classesInPackage = classPath.getTopLevelClassesRecursive(packageName);
	for (ClassInfo classInfo : classesInPackage) {
		Class<?> clazz = classInfo.load();
		Widget widgetDescription = clazz.getAnnotation(Widget.class);
		if (widgetDescription != null) {
			if (!AbstractWidget.class.isAssignableFrom(clazz))
				throw new WicketRuntimeException("@" + Widget.class.getSimpleName() + " should be only on widgets");
			Class<? extends AbstractWidget<Object>> widgetClass = (Class<? extends AbstractWidget<Object>>) clazz;
			register(widgetClass);
		}
	}
	return this;
}
 
Example 4
Source File: AbstractScanner.java    From minnal with Apache License 2.0 6 votes vote down vote up
public void scan(Listener<Class<?>> listener) {
	try {
		ClassPath path = ClassPath.from(classLoader);
		for (String packageName : packages) {
			for (ClassInfo classInfo : path.getTopLevelClassesRecursive(packageName)) {
				Class<?> clazz = classLoader.loadClass(classInfo.getName());
				if (match(clazz)) {
					listener.handle(clazz);
				}
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
		// TODO Handle exception
	}
}
 
Example 5
Source File: DispatcherManager.java    From skywalking with Apache License 2.0 5 votes vote down vote up
/**
 * Scan all classes under `org.apache.skywalking` package,
 * <p>
 * If it implement {@link org.apache.skywalking.oap.server.core.analysis.SourceDispatcher}, then, it will be added
 * into this DispatcherManager based on the Source definition.
 */
public void scan() throws IOException, IllegalAccessException, InstantiationException {
    ClassPath classpath = ClassPath.from(this.getClass().getClassLoader());
    ImmutableSet<ClassPath.ClassInfo> classes = classpath.getTopLevelClassesRecursive("org.apache.skywalking");
    for (ClassPath.ClassInfo classInfo : classes) {
        Class<?> aClass = classInfo.load();

        addIfAsSourceDispatcher(aClass);
    }
}
 
Example 6
Source File: MeterSystem.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public MeterSystem(final ModuleManager manager) {
    this.manager = manager;
    classPool = ClassPool.getDefault();

    ClassPath classpath = null;
    try {
        classpath = ClassPath.from(MeterSystem.class.getClassLoader());
    } catch (IOException e) {
        throw new UnexpectedException("Load class path failure.");
    }
    ImmutableSet<ClassPath.ClassInfo> classes = classpath.getTopLevelClassesRecursive("org.apache.skywalking");
    for (ClassPath.ClassInfo classInfo : classes) {
        Class<?> functionClass = classInfo.load();

        if (functionClass.isAnnotationPresent(MeterFunction.class)) {
            MeterFunction metricsFunction = functionClass.getAnnotation(MeterFunction.class);
            if (!AcceptableValue.class.isAssignableFrom(functionClass)) {
                throw new IllegalArgumentException(
                    "Function " + functionClass.getCanonicalName() + " doesn't implement AcceptableValue.");
            }
            functionRegister.put(
                metricsFunction.functionName(),
                (Class<? extends MeterFunction>) functionClass
            );
        }
    }
}
 
Example 7
Source File: MetricsHolder.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public static void init() throws IOException {
    ClassPath classpath = ClassPath.from(MetricsHolder.class.getClassLoader());
    ImmutableSet<ClassPath.ClassInfo> classes = classpath.getTopLevelClassesRecursive("org.apache.skywalking");
    for (ClassPath.ClassInfo classInfo : classes) {
        Class<?> aClass = classInfo.load();

        if (aClass.isAnnotationPresent(MetricsFunction.class)) {
            MetricsFunction metricsFunction = aClass.getAnnotation(MetricsFunction.class);
            REGISTER.put(
                metricsFunction.functionName(),
                (Class<? extends Metrics>) aClass
            );
        }
    }
}
 
Example 8
Source File: OrienteerWebApplication.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
private void mountOrUnmountPackage(String packageName, ClassLoader classLoader, boolean mount) {
	ClassPath classPath;
	try {
		classPath = ClassPath.from(classLoader);
	} catch (IOException e) {
		throw new WicketRuntimeException("Can't scan classpath", e);
	}
	
	for(ClassInfo classInfo : classPath.getTopLevelClassesRecursive(packageName)) {
		Class<?> clazz = classInfo.load();
		MountPath mountPath = clazz.getAnnotation(MountPath.class);
		if(mountPath!=null) {
			if(IRequestablePage.class.isAssignableFrom(clazz)) { 
				Class<? extends IRequestablePage> pageClass = (Class<? extends IRequestablePage>) clazz;
				forEachOnMountPath(mountPath, path -> {
									if(mount) {
										if ("/".equals(path)) {
											mount(new HomePageMapper(pageClass));
										}
										mount(new MountedMapper(path, pageClass));
									} else {
										unmount(path);
									}
								});
			} else if(IResource.class.isAssignableFrom(clazz)) {
				if(mount) {
					String resourceKey = clazz.getName();
					getSharedResources().add(resourceKey, (IResource) getServiceInstance(clazz));
					SharedResourceReference reference = new SharedResourceReference(resourceKey);
					forEachOnMountPath(mountPath, path -> mountResource(path, reference));
				} else {
					forEachOnMountPath(mountPath, this::unmount);
				}
			} else {
				throw new WicketRuntimeException("@"+MountPath.class.getSimpleName()+" should be only on pages or resources");
			}
		}
	}
}
 
Example 9
Source File: ClasspathWorkflowRepository.java    From copper-engine with Apache License 2.0 5 votes vote down vote up
static Set<Class<?>> findWorkflowClasses(final List<String> wfPackages, final ClassLoader cl) throws Exception {
    final ClassPath cp = ClassPath.from(cl);
    final Set<Class<?>> set = new HashSet<Class<?>>();
    for (String wfPackage : wfPackages) {
        final ImmutableSet<com.google.common.reflect.ClassPath.ClassInfo> x = cp.getTopLevelClassesRecursive(wfPackage);
        for (com.google.common.reflect.ClassPath.ClassInfo ci : x) {
            final Class<?> c = cl.loadClass(ci.getName());
            set.add(c);
            set.addAll(Arrays.asList(c.getDeclaredClasses()));
            loadAnonymousInnerClasses(cl, set, c);
        }
    }
    return set;
}