Search in sources :

Example 1 with FieldInfo

use of io.github.classgraph.FieldInfo in project conquery by bakdata.

the class GroupHandler method handleClass.

private void handleClass(String name, ClassInfo c) throws IOException {
    Introspection source = Introspection.from(root, c);
    try (Closeable details = details(name, c, source)) {
        if (c.getFieldInfo().stream().anyMatch(this::isJSONSettableField)) {
            out.line("Supported Fields:");
            out.tableHeader("", "Field", "Type", "Default", "Example", "Description");
            for (FieldInfo field : c.getFieldInfo().stream().sorted().collect(Collectors.toList())) {
                handleField(c, field);
            }
        } else {
            out.paragraph("No fields can be set for this type.");
        }
    }
}
Also used : Closeable(java.io.Closeable) Introspection(com.bakdata.conquery.introspection.Introspection) FieldInfo(io.github.classgraph.FieldInfo)

Example 2 with FieldInfo

use of io.github.classgraph.FieldInfo in project AdvantageKit by Mechanical-Advantage.

the class Sigcheck method compareClasses.

private static boolean compareClasses(ClassInfo original, ClassInfo patch) {
    List<String> failures = new ArrayList<>();
    // Ensure patch class has same fields as original
    for (FieldInfo originalField : original.getFieldInfo()) {
        if (!originalField.isPublic())
            // Skip non-public fields (not part of the API)
            continue;
        // Check that the patch class has the field
        FieldInfo patchField = patch.getFieldInfo(originalField.getName());
        if (patchField != null) {
            List<String> fieldFailures = new ArrayList<>();
            // Field is present, compare
            if (!originalField.getModifiersStr().equals(patchField.getModifiersStr())) {
                fieldFailures.add("\t  -> Expected modifiers '" + originalField.getModifiersStr() + "' but got '" + patchField.getModifiersStr() + "'");
            }
            if (!originalField.getTypeSignatureOrTypeDescriptorStr().equals(patchField.getTypeSignatureOrTypeDescriptorStr())) {
                // Intentionally not using getTypeSignatureOrTypeDescriptorStr as this method
                // doesn't return nice names for primitives
                fieldFailures.add("\t  -> Expected type '" + originalField.getTypeSignatureOrTypeDescriptor() + "' but got '" + patchField.getTypeSignatureOrTypeDescriptor() + "'");
            }
            if (!fieldFailures.isEmpty()) {
                failures.add("\t-> For field '" + originalField.getName() + "'");
                failures.addAll(fieldFailures);
            }
        } else {
            failures.add("\t-> Missing field '" + originalField + "'");
        }
    }
    // Ensure patch class has same methods as original
    for (MethodInfo originalMethod : original.getMethodAndConstructorInfo()) {
        if (!originalMethod.isPublic())
            // Skip non-public methods (not part of the API)
            continue;
        // Check that the patch class has the method
        MethodInfo patchMethod = patch.getMethodInfo(originalMethod.getName()).stream().filter(originalMethod::equals).findFirst().orElse(null);
        if (patchMethod == null) {
            failures.add("\t-> Missing method '" + originalMethod + "'");
            // The method is either outright missing or incorrectly defined
            // Reverse lookup in the original class to see if there are any methods defined
            // in the patch that aren't defined in the original
            List<String> mismatchedMethods = new ArrayList<>();
            for (MethodInfo m : patch.getMethodInfo(originalMethod.getName())) {
                if (!original.getMethodAndConstructorInfo().contains(m)) {
                    mismatchedMethods.add("\t    -> '" + m + "'");
                }
            }
            if (!mismatchedMethods.isEmpty()) {
                failures.add("\t  -> Found " + mismatchedMethods.size() + (mismatchedMethods.size() == 1 ? " method" : " methods") + " with same name in patch that " + (mismatchedMethods.size() == 1 ? "does" : "do") + " not match any original method signatures");
                failures.addAll(mismatchedMethods);
            }
        }
    }
    if (!failures.isEmpty()) {
        System.out.println("[FAIL] For class '" + original.getName() + "'");
        failures.forEach(System.out::println);
        return false;
    } else {
        System.out.println("[PASS] Class '" + original.getName() + "'");
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) MethodInfo(io.github.classgraph.MethodInfo) FieldInfo(io.github.classgraph.FieldInfo)

Example 3 with FieldInfo

use of io.github.classgraph.FieldInfo in project classgraph by classgraph.

the class RecordTest method recordJar.

/**
 * Test records (JDK 14+).
 *
 * @throws Exception
 *             the exception
 */
@Test
public void recordJar() throws Exception {
    try (ScanResult scanResult = new ClassGraph().overrideClassLoaders(new URLClassLoader(new URL[] { jarURL })).enableAllInfo().scan()) {
        final ClassInfoList classInfoList = scanResult.getAllRecords();
        assertThat(classInfoList).isNotEmpty();
        final ClassInfo classInfo = classInfoList.get(0);
        final FieldInfo fieldInfo = classInfo.getFieldInfo("x");
        assertThat(fieldInfo).isNotNull();
    }
}
Also used : ScanResult(io.github.classgraph.ScanResult) ClassInfoList(io.github.classgraph.ClassInfoList) ClassGraph(io.github.classgraph.ClassGraph) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL) FieldInfo(io.github.classgraph.FieldInfo) ClassInfo(io.github.classgraph.ClassInfo) Test(org.junit.jupiter.api.Test)

Example 4 with FieldInfo

use of io.github.classgraph.FieldInfo in project classgraph by classgraph.

the class Issue100Test method issue100Test.

/**
 * Issue 100 test.
 */
@Test
public void issue100Test() {
    final ClassLoader classLoader = Issue100Test.class.getClassLoader();
    final String aJarName = "issue100-has-field-a.zip";
    final URL aJarURL = classLoader.getResource(aJarName);
    final String bJarName = "issue100-has-field-b.zip";
    final URL bJarURL = classLoader.getResource(bJarName);
    final URLClassLoader overrideClassLoader = new URLClassLoader(new URL[] { aJarURL, bJarURL });
    // Class issue100.Test with field "a" should mask class of same name with field "b", because "...a.jar" is
    // earlier in classpath than "...b.jar"
    final ArrayList<String> fieldNames1 = new ArrayList<>();
    try (ScanResult scanResult = new ClassGraph().overrideClassLoaders(overrideClassLoader).acceptPackages("issue100").rejectJars(bJarName).enableFieldInfo().scan()) {
        for (final ClassInfo ci : scanResult.getAllClasses()) {
            for (final FieldInfo f : ci.getFieldInfo()) {
                fieldNames1.add(f.getName());
            }
        }
    }
    assertThat(fieldNames1).containsOnly("a");
    // However, if "...b.jar" is specifically accepted, "...a.jar" should not be visible. Originally, the
    // version of the class in "...a.jar" was supposed to mask the same class in "...b.jar" (#100). However,
    // this resulted in a slowdown in scan time (#117). Since classloading behavior is undefined if you override
    // the classpath (or in this case, the classloaders), we should only see field "b" in "...b.jar" (which is
    // what we actually see through scanning the accepted jar, "bJarName").
    final ArrayList<String> fieldNames2 = new ArrayList<>();
    try (ScanResult scanResult = new ClassGraph().overrideClassLoaders(overrideClassLoader).acceptPackages("issue100").acceptJars(bJarName).enableFieldInfo().scan()) {
        for (final ClassInfo ci : scanResult.getAllClasses()) {
            for (final FieldInfo f : ci.getFieldInfo()) {
                fieldNames2.add(f.getName());
            }
        }
    }
    assertThat(fieldNames2).containsOnly("b");
}
Also used : ScanResult(io.github.classgraph.ScanResult) URLClassLoader(java.net.URLClassLoader) ClassGraph(io.github.classgraph.ClassGraph) ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL) FieldInfo(io.github.classgraph.FieldInfo) ClassInfo(io.github.classgraph.ClassInfo) Test(org.junit.jupiter.api.Test)

Example 5 with FieldInfo

use of io.github.classgraph.FieldInfo in project classgraph by classgraph.

the class ClassGraphTest method scanStaticFinalFieldName.

/**
 * Scan static final field name.
 */
@Test
public void scanStaticFinalFieldName() {
    try (ScanResult scanResult = new ClassGraph().acceptPackages(ACCEPT_PACKAGE).enableStaticFinalFieldConstantInitializerValues().scan()) {
        int numInitializers = 0;
        for (final FieldInfo fieldInfo : scanResult.getClassInfo(StaticField.class.getName()).getFieldInfo()) {
            if (fieldInfo.getConstantInitializerValue() != null) {
                numInitializers++;
            }
        }
        assertThat(numInitializers).isEqualTo(0);
    }
}
Also used : ScanResult(io.github.classgraph.ScanResult) ClassGraph(io.github.classgraph.ClassGraph) FieldInfo(io.github.classgraph.FieldInfo) Test(org.junit.jupiter.api.Test)

Aggregations

FieldInfo (io.github.classgraph.FieldInfo)7 ClassGraph (io.github.classgraph.ClassGraph)5 ScanResult (io.github.classgraph.ScanResult)5 Test (org.junit.jupiter.api.Test)5 ClassInfo (io.github.classgraph.ClassInfo)3 URL (java.net.URL)2 URLClassLoader (java.net.URLClassLoader)2 ArrayList (java.util.ArrayList)2 Introspection (com.bakdata.conquery.introspection.Introspection)1 ClassInfoList (io.github.classgraph.ClassInfoList)1 MethodInfo (io.github.classgraph.MethodInfo)1 ByteArrayConsumerThrowsIOException (io.github.classgraph.ResourceList.ByteArrayConsumerThrowsIOException)1 StaticField (io.github.classgraph.test.accepted.StaticField)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1