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

The following examples show how to use com.google.common.reflect.ClassPath#getResources() . 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: ClasspathResourceRepository.java    From jweb-cms with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public List<Resource> list(String directory) {
    try {
        ClassPath classpath = ClassPath.from(Thread.currentThread().getContextClassLoader());
        List<Resource> resources = Lists.newArrayList();
        for (ClassPath.ResourceInfo resourceInfo : classpath.getResources()) {
            if (resourceInfo.getResourceName().startsWith(basePath)) {
                String name = resourceInfo.getResourceName();
                String path = name.substring(basePath.length());
                if (path.startsWith(directory)) {
                    resources.add(new ClasspathResource(path, name));
                }
            }
        }
        return resources;
    } catch (IOException e) {
        throw new ResourceException(e);
    }
}
 
Example 2
Source File: GenrulePremiumListTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Test
public void testParse_allPremiumLists() throws Exception {
  ClassPath classpath = ClassPath.from(getClass().getClassLoader());
  int numParsed = 0;
  for (ResourceInfo resource : classpath.getResources()) {
    if (resource.getResourceName().startsWith(LISTS_DIRECTORY)
        && resource.getResourceName().endsWith(".txt")) {
      testParseOfPremiumListFile(resource.getResourceName());
      numParsed++;
    }
  }
  assertWithMessage("No premium lists found").that(numParsed).isAtLeast(1);
}
 
Example 3
Source File: GenruleReservedListTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Test
public void testParse_allReservedLists() throws Exception {
  ClassPath classpath = ClassPath.from(getClass().getClassLoader());
  int numParsed = 0;
  for (ResourceInfo resource : classpath.getResources()) {
    if (resource.getResourceName().startsWith(LISTS_DIRECTORY)
        && resource.getResourceName().endsWith(".txt")) {
      testParseOfReservedListFile(resource.getResourceName());
      numParsed++;
    }
  }
  assertWithMessage("No reserved lists found").that(numParsed).isAtLeast(1);
}
 
Example 4
Source File: ClassPathResource.java    From wisdom with Apache License 2.0 5 votes vote down vote up
/**
 * Adds all the resources found in the given classpath to the given jar. Are excluded resources matching the
 * 'doNotCopy' pattern.
 *
 * @param jar       the jar in which the resources are added
 * @param classpath the classpath
 * @param doNotCopy the do not copy pattern
 */
public static void build(Jar jar, ClassPath classpath, Pattern doNotCopy) {
    ImmutableSet<ClassPath.ResourceInfo> resources = classpath.getResources();
    for (ClassPath.ResourceInfo resource : resources) {
        if (doNotCopy != null && doNotCopy.matcher(resource.getResourceName()).matches()) {
            continue;
        }
        jar.putResource(resource.getResourceName(), new ClassPathResource(resource));
    }
}
 
Example 5
Source File: ClassPathResourceTest.java    From wisdom with Apache License 2.0 5 votes vote down vote up
private static ClassPath.ResourceInfo getResourceForName(String name) throws IOException {
    ClassPath cp = ClassPath.from(ClassPathResourceTest.class.getClassLoader());
    ImmutableSet<ClassPath.ResourceInfo> resources = cp.getResources();
    for (ClassPath.ResourceInfo info : resources) {
        if (info.getResourceName().equals(name)) {
            return info;
        }
    }
    return null;
}