本文整理匯總了Java中com.google.common.reflect.ClassPath類的典型用法代碼示例。如果您正苦於以下問題:Java ClassPath類的具體用法?Java ClassPath怎麽用?Java ClassPath使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ClassPath類屬於com.google.common.reflect包,在下文中一共展示了ClassPath類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: loadClassesInPackage
import com.google.common.reflect.ClassPath; //導入依賴的package包/類
private List<Class<?>> loadClassesInPackage() throws IOException {
List<Class<?>> classes = Lists.newArrayList();
String packageName = getClass().getPackage().getName();
for (ClassPath.ClassInfo classInfo
: ClassPath.from(getClass().getClassLoader()).getTopLevelClasses(packageName)) {
Class<?> cls;
try {
cls = classInfo.load();
} catch (NoClassDefFoundError e) {
// In case there were linking problems, this is probably not a class we care to test anyway.
logger.log(Level.SEVERE, "Cannot load class " + classInfo + ", skipping...", e);
continue;
}
if (!cls.isInterface()) {
classes.add(cls);
}
}
return classes;
}
示例2: 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();
}
}
示例3: hasAccessibleConstructor
import com.google.common.reflect.ClassPath; //導入依賴的package包/類
boolean hasAccessibleConstructor(ClassPath.ClassInfo info, String fromPackage) {
Class<?> load = info.load();
boolean isPublicClass = Modifier.isPublic(load.getModifiers()),
isSamePackage = fromPackage.equals(info.getPackageName());
for (Constructor<?> candidate : load.getDeclaredConstructors()) {
int modifiers = candidate.getModifiers();
if (isPublicClass && Modifier.isPublic(modifiers)) return true;
else if (isSamePackage
&& !Modifier.isPrivate(modifiers)
&& !Modifier.isProtected(modifiers)) return true;
}
return false;
}
示例4: 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);
}
}
示例5: setClassesInProject
import com.google.common.reflect.ClassPath; //導入依賴的package包/類
private void setClassesInProject(URLClassLoader urlClassLoader) {
classesInProject = new HashSet<>();
Set<String> topLevelPackages = getTopLevelPackages();
if (topLevelPackages == null)
return;
for (String packageInProject : topLevelPackages) {
try {
Set<ClassPath.ClassInfo> classesInCurrentPackage = ClassPath.from(urlClassLoader).getTopLevelClassesRecursive(packageInProject);
for (ClassPath.ClassInfo classInfo : classesInCurrentPackage) {
classesInProject.add(classInfo.load());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
示例6: getClasses
import com.google.common.reflect.ClassPath; //導入依賴的package包/類
@API public static List<Class<?>> getClasses(String path)
{
try
{
ClassPath classPath = ClassPath.from(ClassUtil.class.getClassLoader());
Set<ClassInfo> classInfo = classPath.getTopLevelClassesRecursive(path);
Iterator<ClassInfo> iterator = classInfo.iterator();
List<Class<?>> classes = new ArrayList<>();
while(iterator.hasNext())
{
ClassInfo ci = iterator.next();
Optional<Class<?>> classOptional = getClass(ci.getName());
classOptional.ifPresent(classes::add);
}
return classes;
}
catch(IOException e)
{
throw new UncheckedIOException(e);
}
}
示例7: unpackDummyResources
import com.google.common.reflect.ClassPath; //導入依賴的package包/類
public static void unpackDummyResources(String prefix, Path output) throws IOException {
ClassPath classPath = ClassPath.from(TestData.class.getClassLoader());
Map<String, URL> files = classPath.getResources().stream()
.filter(info -> info.getResourceName().startsWith(prefix))
.collect(Collectors.toMap(
info -> info.getResourceName().substring(prefix.length()),
ClassPath.ResourceInfo::url)
);
files.forEach((name, url) -> {
Path file = output.resolve(name);
try (InputStream is = url.openStream()) {
Files.copy(is, file);
} catch (IOException e) {
throw new RuntimeException(String.format("name: %s, url: %s", name, url), e);
}
});
}
示例8: 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;
}
示例9: get
import com.google.common.reflect.ClassPath; //導入依賴的package包/類
/**
* @param basePackage base package for the factory to avoid scanning the whole classpath
* @return list of factories
*/
@SuppressWarnings("unchecked")
public List<Class<? extends FactoryBase>> get(String basePackage) {
List<Class<? extends FactoryBase>> result = new ArrayList<>();
try {
for (ClassPath.ClassInfo classInfo : ClassPath.from(ClasspathBasedFactoryProvider.class.getClassLoader()).getAllClasses()) {
if (classInfo.getName().startsWith(basePackage)) {
Class<?> clazz = classInfo.load();
if (FactoryBase.class.isAssignableFrom(clazz) && clazz != FactoryBase.class) {
result.add((Class<FactoryBase>) clazz);
}
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return result;
}
示例10: 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);
}
}
}
示例11: 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;
}
示例12: getAllCommandClasses
import com.google.common.reflect.ClassPath; //導入依賴的package包/類
/**
* Gets the set of all non-abstract classes implementing the {@link Command} interface (abstract
* class and interface subtypes of Command aren't expected to have cli commands). Note that this
* also filters out HelpCommand, which has special handling in {@link RegistryCli} and isn't in
* the command map.
*
* @throws IOException if reading the classpath resources fails.
*/
@SuppressWarnings("unchecked")
private ImmutableSet<Class<? extends Command>> getAllCommandClasses() throws IOException {
ImmutableSet.Builder<Class<? extends Command>> builder = new ImmutableSet.Builder<>();
for (ClassInfo classInfo : ClassPath
.from(getClass().getClassLoader())
.getTopLevelClassesRecursive(getPackageName(getClass()))) {
Class<?> clazz = classInfo.load();
if (Command.class.isAssignableFrom(clazz)
&& !Modifier.isAbstract(clazz.getModifiers())
&& !Modifier.isInterface(clazz.getModifiers())
&& !clazz.equals(HelpCommand.class)) {
builder.add((Class<? extends Command>) clazz);
}
}
return builder.build();
}
示例13: 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;
}
示例14: loadClassesInPackage
import com.google.common.reflect.ClassPath; //導入依賴的package包/類
private List<Class<?>> loadClassesInPackage() throws IOException {
List<Class<?>> classes = Lists.newArrayList();
String packageName = getClass().getPackage().getName();
for (ClassPath.ClassInfo classInfo
: ClassPath.from(getClass().getClassLoader()).getTopLevelClasses(packageName)) {
Class<?> cls;
try {
cls = classInfo.load();
} catch (NoClassDefFoundError e) {
// In case there were linking problems, this is probably not a class we care to test anyway.
logger.log(Level.SEVERE, "Cannot load class " + classInfo + ", skipping...", e);
continue;
}
if (!cls.isInterface()) {
classes.add(cls);
}
}
return classes;
}
示例15: serializableClassesMustDefineSerialVersionUID
import com.google.common.reflect.ClassPath; //導入依賴的package包/類
@Test
public void serializableClassesMustDefineSerialVersionUID() throws IOException {
List<Class<?>> serializableClassesWithoutSerialVersionUID = ClassPath
.from(SerializableClassesTest.class.getClassLoader())
.getTopLevelClassesRecursive("no.digipost")
.stream().map(ClassInfo::load)
.flatMap(c -> Stream.concat(Stream.of(c), Stream.of(c.getDeclaredClasses())))
.filter(c -> !c.getName().contains("Test"))
.filter(c -> !Enum.class.isAssignableFrom(c))
.filter(Serializable.class::isAssignableFrom)
.filter(c -> {
try {
c.getDeclaredField("serialVersionUID");
return false;
} catch (NoSuchFieldException e) {
return true;
}
}).collect(toList());
assertThat(serializableClassesWithoutSerialVersionUID, empty());
}