Search in sources :

Example 1 with MethodInfo

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

the class GroupHandler method handle.

public void handle() throws IOException {
    out.heading(group.getName());
    out.paragraph("This is an automatically created documentation. It is not 100% accurate since the generator does not handle every edge case.");
    out.paragraph("Instead of a list ConQuery also always accepts a single element.");
    if (group.getDescription() != null) {
        out.paragraph(group.getDescription());
    }
    for (Base base : group.getBases()) {
        content.putAll(base, scan.getAllClasses().stream().filter(c -> c.hasAnnotation(CPS_TYPE)).flatMap(c -> Arrays.stream(c.loadClass().getAnnotationsByType(CPSType.class)).map(anno -> Pair.of(anno, c))).filter(p -> p.getLeft().base().equals(base.getBaseClass())).collect(Collectors.toList()));
    }
    for (Class<?> resource : group.getResources()) {
        collectEndpoints(resource);
    }
    if (!endpoints.isEmpty()) {
        out.heading("REST endpoints");
        for (Pair<String, MethodInfo> endpoint : endpoints.stream().sorted(Comparator.comparing(Pair::getLeft)).collect(Collectors.toList())) {
            handleEndpoint(endpoint.getLeft(), endpoint.getRight());
        }
    }
    for (Base base : group.getBases()) {
        handleBase(base);
    }
    out.subHeading("Other Types");
    for (Class<?> t : group.getOtherClasses().stream().sorted(Comparator.comparing(Class::getSimpleName)).collect(Collectors.toList())) {
        handleClass(typeTitle(t), scan.getClassInfo(t.getName()));
    }
    if (!group.getMarkerInterfaces().isEmpty()) {
        out.subHeading("Marker Interfaces");
        for (Class<?> t : group.getMarkerInterfaces().stream().sorted(Comparator.comparing(Class::getSimpleName)).collect(Collectors.toList())) {
            handleMarkerInterface(markerTitle(t), scan.getClassInfo(t.getName()));
        }
    }
}
Also used : Arrays(java.util.Arrays) BaseTypeSignature(io.github.classgraph.BaseTypeSignature) RequiredArgsConstructor(lombok.RequiredArgsConstructor) MethodInfo(io.github.classgraph.MethodInfo) IId(com.bakdata.conquery.models.identifiable.ids.IId) VariableDefaultValue(com.bakdata.conquery.util.VariableDefaultValue) StringUtils(org.apache.commons.lang3.StringUtils) HashMultimap(com.google.common.collect.HashMultimap) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) UriBuilder(javax.ws.rs.core.UriBuilder) JsonNode(com.fasterxml.jackson.databind.JsonNode) Path(java.nio.file.Path) BiMap(com.google.common.collect.BiMap) CPSType(com.bakdata.conquery.io.cps.CPSType) MoreCollectors(com.google.common.collect.MoreCollectors) Set(java.util.Set) PrettyPrinter(com.bakdata.conquery.util.PrettyPrinter) MethodParameterInfo(io.github.classgraph.MethodParameterInfo) Collectors(java.util.stream.Collectors) Base(com.bakdata.conquery.model.Base) List(java.util.List) TypeSignature(io.github.classgraph.TypeSignature) Slf4j(lombok.extern.slf4j.Slf4j) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) Optional(java.util.Optional) Jackson(com.bakdata.conquery.io.jackson.Jackson) ArrayTypeSignature(io.github.classgraph.ArrayTypeSignature) FilenameUtils(org.apache.commons.io.FilenameUtils) TypeParameter(io.github.classgraph.TypeParameter) Constants(com.bakdata.conquery.Constants) Multimap(com.google.common.collect.Multimap) ClassRefTypeSignature(io.github.classgraph.ClassRefTypeSignature) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) ClassToInstanceMap(com.google.common.collect.ClassToInstanceMap) Group(com.bakdata.conquery.model.Group) Introspection(com.bakdata.conquery.introspection.Introspection) ScanResult(io.github.classgraph.ScanResult) FieldInfo(io.github.classgraph.FieldInfo) TypeVariableSignature(io.github.classgraph.TypeVariableSignature) ClassInfo(io.github.classgraph.ClassInfo) IOException(java.io.IOException) TypeArgument(io.github.classgraph.TypeArgument) File(java.io.File) Primitives(com.google.common.primitives.Primitives) Closeable(java.io.Closeable) Comparator(java.util.Comparator) MethodInfo(io.github.classgraph.MethodInfo) Base(com.bakdata.conquery.model.Base) Pair(org.apache.commons.lang3.tuple.Pair)

Example 2 with MethodInfo

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

the class GroupHandler method collectEndpoints.

private void collectEndpoints(Class<?> resource) throws IOException {
    ClassInfo info = scan.getClassInfo(resource.getName());
    for (MethodInfo method : info.getMethodInfo()) {
        if (getRestMethod(method) == null) {
            continue;
        }
        UriBuilder builder = UriBuilder.fromResource(resource);
        if (method.hasAnnotation(PATH)) {
            builder = builder.path(resource, method.getName());
        }
        endpoints.add(Pair.of(builder.toTemplate(), method));
    }
}
Also used : MethodInfo(io.github.classgraph.MethodInfo) UriBuilder(javax.ws.rs.core.UriBuilder) ClassInfo(io.github.classgraph.ClassInfo)

Example 3 with MethodInfo

use of io.github.classgraph.MethodInfo in project helidon by oracle.

the class JaxRsMethodAnalyzer method find.

Set<Class<?>> find() {
    ClassInfoList classes = context.scan().getClassesWithMethodAnnotation(HTTP_METHOD_ANNOTATION);
    for (ClassInfo aClass : classes) {
        MethodInfoList methods = aClass.getMethodInfo();
        for (MethodInfo method : methods) {
            if (method.hasAnnotation(HTTP_METHOD_ANNOTATION)) {
                add(method.getTypeSignatureOrTypeDescriptor().getResultType());
                MethodParameterInfo[] parameterInfo = method.getParameterInfo();
                for (MethodParameterInfo param : parameterInfo) {
                    if (param.getAnnotationInfo().isEmpty()) {
                        add(param.getTypeSignatureOrTypeDescriptor());
                    }
                }
            }
        }
    }
    Set<String> result = Set.copyOf(classesToAdd);
    classesToAdd.clear();
    return result.stream().map(nativeUtil.classMapper("jaxrs-result-or-param")).filter(Objects::nonNull).filter(nativeUtil.inclusionFilter("jaxrs-result-or-param")).collect(Collectors.toSet());
}
Also used : ClassInfoList(io.github.classgraph.ClassInfoList) Objects(java.util.Objects) MethodInfoList(io.github.classgraph.MethodInfoList) MethodInfo(io.github.classgraph.MethodInfo) MethodParameterInfo(io.github.classgraph.MethodParameterInfo) ClassInfo(io.github.classgraph.ClassInfo)

Example 4 with MethodInfo

use of io.github.classgraph.MethodInfo 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 5 with MethodInfo

use of io.github.classgraph.MethodInfo in project domui by fjalvingh.

the class PageUrlMapping method scan.

public void scan() {
    ScanResult r = m_application.getClasspathScanResult();
    for (ClassInfo classInfo : r.getClassesWithAnnotation(UIPage.class.getName())) {
        AnnotationInfo anninfo = classInfo.getAnnotationInfo(UIPage.class.getName());
        String pattern = (String) anninfo.getParameterValues().getValue("value");
        LOG.info("Page " + classInfo.getName() + " url " + pattern);
        // -- Find all methods annotated with UIUrlParameter
        Map<String, String> pageParams = new HashMap<>();
        for (MethodInfo methodInfo : classInfo.getMethodInfo()) {
            AnnotationInfo mai = methodInfo.getAnnotationInfo(UIUrlParameter.class.getName());
            if (null != mai) {
                String typeStr = methodInfo.getTypeDescriptor().getResultType().toString();
                String pname = (String) mai.getParameterValues().getValue("name");
                if (pname == null || pname.isEmpty()) {
                    pname = methodInfo.getName();
                    if (pname.startsWith("is")) {
                        pname = pname.substring(2);
                    } else if (pname.startsWith("get")) {
                        pname = pname.substring(3);
                    }
                    pname = Introspector.decapitalize(pname);
                }
                pageParams.put(pname, typeStr);
            }
        }
        appendPage(classInfo.getName(), pattern, pageParams);
    }
}
Also used : ScanResult(io.github.classgraph.ScanResult) UIPage(to.etc.domui.annotations.UIPage) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MethodInfo(io.github.classgraph.MethodInfo) UIUrlParameter(to.etc.domui.annotations.UIUrlParameter) ClassInfo(io.github.classgraph.ClassInfo) AnnotationInfo(io.github.classgraph.AnnotationInfo)

Aggregations

MethodInfo (io.github.classgraph.MethodInfo)30 Test (org.junit.jupiter.api.Test)22 ScanResult (io.github.classgraph.ScanResult)20 ClassInfo (io.github.classgraph.ClassInfo)18 ClassGraph (io.github.classgraph.ClassGraph)17 ArrayList (java.util.ArrayList)15 URL (java.net.URL)9 URLClassLoader (java.net.URLClassLoader)9 ClassInfoList (io.github.classgraph.ClassInfoList)5 AnnotationInfo (io.github.classgraph.AnnotationInfo)3 MethodParameterInfo (io.github.classgraph.MethodParameterInfo)3 ArrayTypeSignature (io.github.classgraph.ArrayTypeSignature)2 ClassRefTypeSignature (io.github.classgraph.ClassRefTypeSignature)2 FieldInfo (io.github.classgraph.FieldInfo)2 TypeSignature (io.github.classgraph.TypeSignature)2 Method (java.lang.reflect.Method)2 UriBuilder (javax.ws.rs.core.UriBuilder)2 Constants (com.bakdata.conquery.Constants)1 Introspection (com.bakdata.conquery.introspection.Introspection)1 CPSType (com.bakdata.conquery.io.cps.CPSType)1