本文整理匯總了Java中com.google.common.reflect.ClassPath.from方法的典型用法代碼示例。如果您正苦於以下問題:Java ClassPath.from方法的具體用法?Java ClassPath.from怎麽用?Java ClassPath.from使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.common.reflect.ClassPath
的用法示例。
在下文中一共展示了ClassPath.from方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getAnnotatedClasses
import com.google.common.reflect.ClassPath; //導入方法依賴的package包/類
public static Set<Class<?>> getAnnotatedClasses(final Class[] annotations, final String... packages) {
Set<String> annotationsToScan = Stream.of(annotations).map(Class::getSimpleName).collect(toSet());
ClassLoader cl = AnnotationScanner.class.getClassLoader();
try {
ClassPath cp = ClassPath.from(cl);
return Stream.of(packages)
.flatMap(packageName -> cp.getTopLevelClassesRecursive(packageName).stream())
.filter(isClassAnnotatedByScannedAnnotations(annotationsToScan))
.map(ClassPath.ClassInfo::load)
.collect(toSet());
} catch (IOException e) {
log.error("Failed to get annotated classes", e);
return Collections.emptySet();
}
}
示例2: getClasses
import com.google.common.reflect.ClassPath; //導入方法依賴的package包/類
public <T> List<Class<? extends T>> getClasses(Class<T> superType) throws IOException {
classPath = ClassPath.from(this.getClass().getClassLoader());
ImmutableSet<ClassPath.ClassInfo> classInfos = classPath.getTopLevelClasses(packageName);
List<Class<? extends T>> classes = new ArrayList<>();
for (ClassPath.ClassInfo classInfo : classInfos) {
if (classInfo.load().isAnnotationPresent(annotationType)) {
try {
classes.add(classInfo.load().asSubclass(superType));
} catch (ClassCastException ex) {
//Checks that the class is castable before casring it.
}
}
}
return classes;
}
示例3: findMessageClasses
import com.google.common.reflect.ClassPath; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
private Iterable<Class<? extends AbstractMessage>> findMessageClasses() {
try {
List<Class<? extends AbstractMessage>> result = new ArrayList<>();
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
ClassPath classpath = ClassPath.from(classloader);
ImmutableSet<ClassInfo> xx =
classpath.getTopLevelClassesRecursive("net.wizardsoflua.testenv.net");
Iterable<ClassInfo> yy = Iterables.filter(xx, input -> {
Class<?> cls = input.load();
return AbstractMessage.class.isAssignableFrom(cls)
&& !Modifier.isAbstract(cls.getModifiers());
});
for (ClassInfo classInfo : yy) {
result.add((Class<? extends AbstractMessage>) classInfo.load());
}
return result;
} catch (IOException e) {
throw new UndeclaredThrowableException(e);
}
}
示例4: loadAllProvider
import com.google.common.reflect.ClassPath; //導入方法依賴的package包/類
@Unsafe(Unsafe.ASM_API)
private static void loadAllProvider() throws Exception {
ClassPath path = ClassPath.from(AlchemyTransformerManager.class.getClassLoader());
for (ClassInfo info : path.getAllClasses())
if (info.getName().startsWith(MOD_PACKAGE)) {
ClassReader reader = new ClassReader(info.url().openStream());
ClassNode node = new ClassNode(ASM5);
reader.accept(node, 0);
if (checkSideOnly(node)) {
loadPatch(node);
loadField(node);
loadProxy(node);
loadHook(node);
loadTransform(node, info);
}
}
}
示例5: getCheckstyleModulesRecursive
import com.google.common.reflect.ClassPath; //導入方法依賴的package包/類
/**
* Gets checkstyle's modules in the given package recursively.
* @param packageName the package name to use
* @param loader the class loader used to load Checkstyle package name
* @return the set of checkstyle's module classes
* @throws IOException if the attempt to read class path resources failed
* @see ModuleReflectionUtils#isCheckstyleModule(Class)
*/
private static Set<Class<?>> getCheckstyleModulesRecursive(
String packageName, ClassLoader loader) throws IOException {
final ClassPath classPath = ClassPath.from(loader);
final Set<Class<?>> result = new HashSet<Class<?>>();
for (ClassInfo clsInfo : classPath.getTopLevelClassesRecursive(packageName)) {
final Class<?> cls = clsInfo.load();
if (ModuleReflectionUtils.isCheckstyleModule(cls)
&& !cls.getName().endsWith("Stub")
&& !cls.getCanonicalName()
.startsWith("com.puppycrawl.tools.checkstyle.internal.testmodules")
&& !cls.getCanonicalName()
.startsWith("com.puppycrawl.tools.checkstyle.packageobjectfactory")) {
result.add(cls);
}
}
return result;
}
示例6: getAllModels
import com.google.common.reflect.ClassPath; //導入方法依賴的package包/類
public Collection<Class> getAllModels() throws IOException {
Class<Charge> chargeClass = Charge.class;
ClassPath classPath = ClassPath.from(chargeClass.getClassLoader());
ImmutableSet<ClassPath.ClassInfo> topLevelClasses = classPath.getTopLevelClasses(chargeClass.getPackage().getName());
List<Class> classList = Lists.newArrayListWithExpectedSize(topLevelClasses.size());
for (ClassPath.ClassInfo classInfo : topLevelClasses) {
Class c = classInfo.load();
// Skip things that aren't APIResources
if (!APIResource.class.isAssignableFrom(c)) {
continue;
}
// Skip the APIResource itself
if (APIResource.class == c) {
continue;
}
classList.add(classInfo.load());
}
return classList;
}
示例7: allClasses
import com.google.common.reflect.ClassPath; //導入方法依賴的package包/類
private Set<ClassInfo> allClasses() {
ClassPath cpScanner;
try {
cpScanner = ClassPath.from(ClasspathConstantScanner.class.getClassLoader());
} catch (IOException e) {
LOGGER.warn("Cannot scan classes. No Constants will be returned.");
return Collections.emptySet();
}
return cpScanner.getTopLevelClasses().stream().filter(ci -> {
if (basePackages.isEmpty()) {
return true;
} else {
return basePackages.stream().anyMatch(p -> ci.getPackageName().startsWith(p));
}
}).collect(Collectors.toSet());
}
示例8: prepareRaml
import com.google.common.reflect.ClassPath; //導入方法依賴的package包/類
/**
* Main entrypoint for raml generation
* @throws MojoExecutionException Kaboom.
* @throws MojoFailureException Kaboom.
* @throws IOException Kaboom.
*/
protected void prepareRaml() throws MojoExecutionException, MojoFailureException, IOException {
ClassLoaderUtils.addLocationsToClassLoader(project);
List<String> targetPacks = ClassLoaderUtils.loadPackages(project);
if (dependencyPackagesList != null && !dependencyPackagesList.isEmpty()) {
targetPacks.addAll(dependencyPackagesList);
}
ClassPath classPath = ClassPath.from(Thread.currentThread().getContextClassLoader());
for (String pack : targetPacks) {
scanPack(pack, classPath);
}
for (ClassPath.ResourceInfo resourceInfo : classPath.getResources()) {
if (resourceInfo.getResourceName().endsWith(documentationSuffix)) {
try {
documents.add(new ApiDocumentMetadata(resourceInfo, documentationSuffix));
this.getLog().info("Adding Documentation File " + resourceInfo.getResourceName());
} catch (Throwable ex) {
this.getLog().warn("Skipping Resource: Unable to load" + resourceInfo.getResourceName(), ex);
}
}
}
ClassLoaderUtils.restoreOriginalClassLoader();
}
示例9: collectSteps
import com.google.common.reflect.ClassPath; //導入方法依賴的package包/類
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;
}
示例10: list
import com.google.common.reflect.ClassPath; //導入方法依賴的package包/類
static ImmutableList<TestResource> list() {
if (cachedTestResources != null) {
return cachedTestResources;
}
try {
ImmutableList.Builder<TestResource> result = ImmutableList.builder();
ClassPath classPath = ClassPath.from(TestResource.class.getClassLoader());
for (ClassPath.ResourceInfo resource : classPath.getResources()) {
String resourceName = resource.getResourceName();
if (resourceName.endsWith(DOC_COMMENT_EXTENSION)) {
String resourceNameWithoutExtension = resourceName.substring(0,
resourceName.length() - DOC_COMMENT_EXTENSION.length());
result.add(new TestResource(resourceNameWithoutExtension));
}
}
cachedTestResources = result.build();
return cachedTestResources;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例11: createBasicOperations
import com.google.common.reflect.ClassPath; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
private List<OperationMetaData> createBasicOperations() {
try {
ClassPath cp = ClassPath.from(getClass().getClassLoader());
return cp.getAllClasses().stream()
.filter(ci -> ci.getName().startsWith("edu.wpi.grip.core.operations"))
.map(ClassPath.ClassInfo::load)
.filter(Operation.class::isAssignableFrom)
.map(c -> (Class<? extends Operation>) c)
.filter(c -> c.isAnnotationPresent(Description.class))
.map(c -> new OperationMetaData(descriptionFor(c), () -> injector.getInstance(c)))
.collect(Collectors.toList());
} catch (IOException e) {
logger.log(Level.WARNING, "Could not discover operations", e);
return ImmutableList.of();
}
}
示例12: findPublishables
import com.google.common.reflect.ClassPath; //導入方法依賴的package包/類
/**
* Finds all subclasses of {@link Publishable} in {@code edu.wpi.grip.core.operation}.
*/
@SuppressWarnings("unchecked")
private List<Class<Publishable>> findPublishables() {
if (publishableTypes == null) {
// Only need to search once
try {
ClassPath cp = ClassPath.from(getClass().getClassLoader());
publishableTypes = cp.getAllClasses().stream()
// only look in our namespace (don't want to wade through tens of thousands of classes)
.filter(ci -> ci.getName().startsWith("edu.wpi.grip.core.operation"))
.map(ClassPath.ClassInfo::load)
.filter(Publishable.class::isAssignableFrom)
// only accept concrete top-level subclasses
.filter(c -> !c.isAnonymousClass() && !c.isInterface() && !c.isLocalClass()
&& !c.isMemberClass())
.filter(c -> Modifier.isPublic(c.getModifiers()))
.map(c -> (Class<Publishable>) c)
.collect(Collectors.toList());
} catch (IOException e) {
logger.log(Level.WARNING, "Could not find the publishable types.", e);
publishableTypes = ImmutableList.of();
}
}
return publishableTypes;
}
示例13: tesstByteBuf
import com.google.common.reflect.ClassPath; //導入方法依賴的package包/類
public void tesstByteBuf() throws InterruptedException, IOException {
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
ClassPath from = ClassPath.from(systemClassLoader);
StdInstantiatorStrategy instantiator = new StdInstantiatorStrategy();
Class<Request> requestClazz = Request.class;
for (ClassPath.ClassInfo clazz : from.getAllClasses()) {
Class<?> load;
try {
load = clazz.load();
} catch (NoClassDefFoundError e) {
continue;
}
for (Class<?> aClass : load.getInterfaces()) {
if (aClass.equals(requestClazz)) {
Object o = instantiator.newInstantiatorOf(aClass).newInstance();
testByteBuf((Request) o);
}
}
}
}
示例14: getRecognizedClasses
import com.google.common.reflect.ClassPath; //導入方法依賴的package包/類
private Set<ClassPath.ClassInfo> getRecognizedClasses(ClassLoader classLoader, List<String> packages) {
Set<ClassPath.ClassInfo> result = new HashSet<>();
try {
ClassPath classPath = ClassPath.from(classLoader);
for (ClassPath.ClassInfo classInfo : classPath.getAllClasses()) {
String packageName = classInfo.getPackageName();
for (String aPackage : packages) {
if (packageName.startsWith(aPackage)) {
result.add(classInfo);
}
}
}
} catch (IOException e) {
logger.severe("Cannot create ClassPath: " + e);
}
return result;
}
示例15: scan
import com.google.common.reflect.ClassPath; //導入方法依賴的package包/類
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
}
}