Search in sources :

Example 11 with ClassGraph

use of io.github.classgraph.ClassGraph in project querydsl by querydsl.

the class SerializationTest method expressions.

@Test
public void expressions() throws Exception {
    Map<Class<?>, Object> args = new HashMap<>();
    args.put(Object.class, "obj");
    args.put(BeanPath.class, new EntityPathBase<Object>(Object.class, "obj"));
    args.put(Class.class, Integer.class);
    args.put(Class[].class, new Class<?>[] { Object.class, Object.class });
    args.put(java.util.Date.class, new java.util.Date(0));
    args.put(java.sql.Date.class, new java.sql.Date(0));
    args.put(java.sql.Time.class, new java.sql.Time(0));
    args.put(java.sql.Timestamp.class, new java.sql.Timestamp(0));
    args.put(Expression.class, Expressions.enumPath(Gender.class, "e"));
    args.put(Expression[].class, new Expression<?>[] { Expressions.enumPath(Gender.class, "e"), Expressions.stringPath("s") });
    args.put(FactoryExpression.class, Projections.tuple(Expressions.stringPath("str")));
    args.put(GroupExpression.class, GroupBy.avg(Expressions.numberPath(Integer.class, "num")));
    args.put(Number.class, 1);
    args.put(Operator.class, Ops.AND);
    args.put(Path.class, Expressions.stringPath("str"));
    args.put(PathBuilderValidator.class, PathBuilderValidator.DEFAULT);
    args.put(PathMetadata.class, PathMetadataFactory.forVariable("obj"));
    args.put(PathInits.class, PathInits.DEFAULT);
    args.put(Predicate.class, Expressions.path(Object.class, "obj").isNull());
    args.put(QueryMetadata.class, new DefaultQueryMetadata());
    args.put(String.class, "obj");
    ClassGraph reflections = new ClassGraph().enableClassInfo();
    List<Class<?>> types = reflections.scan().getSubclasses(Expression.class.getName()).loadClasses();
    for (Class<?> type : types) {
        if (!type.isInterface() && !type.isMemberClass() && !Modifier.isAbstract(type.getModifiers())) {
            for (Constructor<?> c : type.getConstructors()) {
                Object[] parameters = new Object[c.getParameterTypes().length];
                for (int i = 0; i < c.getParameterTypes().length; i++) {
                    parameters[i] = Objects.requireNonNull(args.get(c.getParameterTypes()[i]), c.getParameterTypes()[i].getName());
                }
                c.setAccessible(true);
                Object o = c.newInstance(parameters);
                assertEquals(o, Serialization.serialize(o));
            }
        }
    }
}
Also used : ClassGraph(io.github.classgraph.ClassGraph) java.util(java.util) DefaultQueryMetadata(com.querydsl.core.DefaultQueryMetadata) GroupExpression(com.querydsl.core.group.GroupExpression) Test(org.junit.Test)

Example 12 with ClassGraph

use of io.github.classgraph.ClassGraph in project querydsl by querydsl.

the class CycleClassInitDependencyTest method overrideClassLoader.

@BeforeClass
public static void overrideClassLoader() {
    loader = Thread.currentThread().getContextClassLoader();
    Collection<URL> urls = new ClassGraph().getClasspathURLs();
    ClassLoader cl = URLClassLoader.newInstance(urls.toArray(new URL[0]), null);
    Thread.currentThread().setContextClassLoader(cl);
}
Also used : ClassGraph(io.github.classgraph.ClassGraph) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL) BeforeClass(org.junit.BeforeClass)

Example 13 with ClassGraph

use of io.github.classgraph.ClassGraph in project morphia by mongodb.

the class Mapper method getClasses.

private List<Class> getClasses(ClassLoader loader, String packageName, boolean mapSubPackages) throws ClassNotFoundException {
    final Set<Class> classes = new HashSet<>();
    ClassGraph classGraph = new ClassGraph().addClassLoader(loader).enableAllInfo();
    if (mapSubPackages) {
        classGraph.acceptPackages(packageName);
        classGraph.acceptPackages(packageName + ".*");
    } else {
        classGraph.acceptPackagesNonRecursive(packageName);
    }
    try (ScanResult scanResult = classGraph.scan()) {
        for (ClassInfo classInfo : scanResult.getAllClasses()) {
            classes.add(Class.forName(classInfo.getName(), true, loader));
        }
    }
    return new ArrayList<>(classes);
}
Also used : ScanResult(io.github.classgraph.ScanResult) ClassGraph(io.github.classgraph.ClassGraph) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClassInfo(io.github.classgraph.ClassInfo)

Example 14 with ClassGraph

use of io.github.classgraph.ClassGraph in project beam by apache.

the class ClasspathScanningResourcesDetectorTest method shouldDetectResourcesInOrderTheyAppearInURLClassLoader.

@Test
public void shouldDetectResourcesInOrderTheyAppearInURLClassLoader() throws Exception {
    File file1 = createTestTmpJarFile("test1");
    File file2 = createTestTmpJarFile("test2");
    ClassLoader classLoader = new URLClassLoader(new URL[] { file1.toURI().toURL(), file2.toURI().toURL() });
    ClasspathScanningResourcesDetector detector = new ClasspathScanningResourcesDetector(new ClassGraph());
    List<String> result = detector.detect(classLoader);
    assertThat(result, containsInRelativeOrder(containsString(file1.getAbsolutePath()), containsString(file2.getAbsolutePath())));
}
Also used : URLClassLoader(java.net.URLClassLoader) ClassGraph(io.github.classgraph.ClassGraph) URLClassLoader(java.net.URLClassLoader) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 15 with ClassGraph

use of io.github.classgraph.ClassGraph in project beam by apache.

the class ClasspathScanningResourcesDetectorTest method shouldNotDetectOrdinaryFiles.

@Test
public void shouldNotDetectOrdinaryFiles() throws Exception {
    File textFile = tmpFolder.newFile("ordinaryTextFile.txt");
    ClassLoader classLoader = new URLClassLoader(new URL[] { textFile.toURI().toURL() });
    ClasspathScanningResourcesDetector detector = new ClasspathScanningResourcesDetector(new ClassGraph());
    List<String> result = detector.detect(classLoader);
    assertThat(result, not(hasItem(containsString(textFile.getAbsolutePath()))));
}
Also used : URLClassLoader(java.net.URLClassLoader) ClassGraph(io.github.classgraph.ClassGraph) URLClassLoader(java.net.URLClassLoader) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) Test(org.junit.Test)

Aggregations

ClassGraph (io.github.classgraph.ClassGraph)21 ScanResult (io.github.classgraph.ScanResult)8 Test (org.junit.Test)8 URLClassLoader (java.net.URLClassLoader)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 File (java.io.File)5 URL (java.net.URL)5 ArrayList (java.util.ArrayList)4 ClassInfo (io.github.classgraph.ClassInfo)3 HashSet (java.util.HashSet)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 URI (java.net.URI)2 StringJoiner (java.util.StringJoiner)2 Nonnull (javax.annotation.Nonnull)2 ClassLoaderUtil (com.hazelcast.internal.nio.ClassLoaderUtil)1 ExceptionUtil.sneakyThrow (com.hazelcast.internal.util.ExceptionUtil.sneakyThrow)1 DefaultQueryMetadata (com.querydsl.core.DefaultQueryMetadata)1 GroupExpression (com.querydsl.core.group.GroupExpression)1 DiagnosticSource (com.sun.tools.javac.util.DiagnosticSource)1 DiagnosticPosition (com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition)1