Search in sources :

Example 1 with TypeSignature

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

the class GroupHandler method handleField.

private void handleField(ClassInfo currentType, FieldInfo field) throws IOException {
    if (!isJSONSettableField(field)) {
        return;
    }
    Introspection introspec = Introspection.from(root, field.getClassInfo()).findField(field);
    String name = field.getName();
    TypeSignature typeSignature = field.getTypeSignatureOrTypeDescriptor();
    Ctx ctx = new Ctx().withField(field);
    String type;
    if (ID_REF.stream().anyMatch(field::hasAnnotation)) {
        type = ID_OF + printType(ctx.withIdOf(true), typeSignature);
    } else if (ID_REF_COL.stream().anyMatch(field::hasAnnotation)) {
        type = LIST_OF + ID_OF + StringUtils.removeStart(printType(ctx.withIdOf(true), typeSignature), LIST_OF);
    } else {
        type = printType(ctx, typeSignature);
    }
    out.table(editLink(introspec), name, type, findDefault(currentType, field), introspec.getExample(), introspec.getDescription());
}
Also used : BaseTypeSignature(io.github.classgraph.BaseTypeSignature) TypeSignature(io.github.classgraph.TypeSignature) ArrayTypeSignature(io.github.classgraph.ArrayTypeSignature) ClassRefTypeSignature(io.github.classgraph.ClassRefTypeSignature) Introspection(com.bakdata.conquery.introspection.Introspection)

Example 2 with TypeSignature

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

the class MethodInfoTest method testMethodInfoLoadMethodForArrayArg.

/**
 * MethodInfo.loadClassAndGetMethod for arrays argument (#344)
 */
@Test
public void testMethodInfoLoadMethodForArrayArg() {
    try (ScanResult scanResult = new ClassGraph().acceptPackages(MethodInfoTest.class.getPackage().getName()).enableClassInfo().enableMethodInfo().enableAnnotationInfo().scan()) {
        final MethodInfo mi = scanResult.getClassInfo(MethodInfoTest.class.getName()).getMethodInfo().getSingleMethod("publicMethodWithArgs");
        assertThat(mi).isNotNull();
        assertThatCode(() -> {
            mi.loadClassAndGetMethod();
        }).doesNotThrowAnyException();
        assertThat(mi.loadClassAndGetMethod()).isNotNull();
        // Extract array-typed params from method params
        final List<ArrayClassInfo> arrayClassInfoList = new ArrayList<>();
        for (final MethodParameterInfo mpi : mi.getParameterInfo()) {
            final TypeSignature paramTypeSig = mpi.getTypeSignatureOrTypeDescriptor();
            if (paramTypeSig instanceof ArrayTypeSignature) {
                arrayClassInfoList.add(((ArrayTypeSignature) paramTypeSig).getArrayClassInfo());
            }
        }
        assertThat(arrayClassInfoList.toString()).isEqualTo("[class float[], class byte[][], " + "class " + X.class.getName() + "[][][], " + "class java.lang.String[][]]");
        final ArrayClassInfo p1 = arrayClassInfoList.get(1);
        assertThat(p1.loadElementClass()).isEqualTo(byte.class);
        assertThat(p1.loadClass()).isEqualTo(byte[][].class);
        assertThat(p1.getElementClassInfo()).isNull();
        assertThat(p1.getNumDimensions()).isEqualTo(2);
        final ArrayClassInfo p2 = arrayClassInfoList.get(2);
        assertThat(p2.loadElementClass()).isEqualTo(X.class);
        assertThat(p2.getElementClassInfo().getName()).isEqualTo(X.class.getName());
        assertThat(p2.loadClass()).isEqualTo(X[][][].class);
        assertThat(p2.getElementClassInfo().getMethodInfo().get(0).getName()).isEqualTo("xMethod");
        assertThat(p2.getNumDimensions()).isEqualTo(3);
        final ArrayClassInfo p3 = arrayClassInfoList.get(3);
        assertThat(p3.loadElementClass()).isEqualTo(String.class);
        assertThat(p3.loadClass()).isEqualTo(String[][].class);
        assertThat(p3.getElementClassInfo()).isNull();
        assertThat(p3.getNumDimensions()).isEqualTo(2);
    }
}
Also used : ArrayClassInfo(io.github.classgraph.ArrayClassInfo) ScanResult(io.github.classgraph.ScanResult) TypeSignature(io.github.classgraph.TypeSignature) ArrayTypeSignature(io.github.classgraph.ArrayTypeSignature) ClassGraph(io.github.classgraph.ClassGraph) ArrayList(java.util.ArrayList) MethodInfo(io.github.classgraph.MethodInfo) MethodParameterInfo(io.github.classgraph.MethodParameterInfo) ArrayTypeSignature(io.github.classgraph.ArrayTypeSignature) Test(org.junit.jupiter.api.Test)

Example 3 with TypeSignature

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

the class Issue140Test method issue140Test.

/**
 * Issue 140 test.
 */
@Test
public void issue140Test() {
    try (ScanResult scanResult = new ClassGraph().acceptPackages(Issue140Test.class.getPackage().getName()).enableFieldInfo().scan()) {
        final ClassInfo ci = scanResult.getClassInfo(Issue140Test.class.getName());
        assertThat(ci).isNotNull();
        final FieldInfoList allFieldInfo = ci.getFieldInfo();
        assertThat(allFieldInfo.size()).isEqualTo(2);
        final TypeSignature type0 = allFieldInfo.get(0).getTypeSignatureOrTypeDescriptor();
        assertThat(type0).isInstanceOf(BaseTypeSignature.class);
        assertThat(((BaseTypeSignature) type0).getType()).isEqualTo(int.class);
        final TypeSignature type1 = allFieldInfo.get(1).getTypeSignatureOrTypeDescriptor();
        assertThat(type1).isInstanceOf(ArrayTypeSignature.class);
        assertThat(((ArrayTypeSignature) type1).getNumDimensions()).isEqualTo(1);
        final TypeSignature elementTypeSignature = ((ArrayTypeSignature) type1).getElementTypeSignature();
        assertThat(elementTypeSignature).isInstanceOf(ClassRefTypeSignature.class);
        final ClassRefTypeSignature classRefTypeSignature = (ClassRefTypeSignature) elementTypeSignature;
        assertThat(classRefTypeSignature.getBaseClassName()).isEqualTo(String.class.getName());
        assertThat(classRefTypeSignature.loadClass()).isEqualTo(String.class);
    }
}
Also used : ScanResult(io.github.classgraph.ScanResult) FieldInfoList(io.github.classgraph.FieldInfoList) TypeSignature(io.github.classgraph.TypeSignature) BaseTypeSignature(io.github.classgraph.BaseTypeSignature) ArrayTypeSignature(io.github.classgraph.ArrayTypeSignature) ClassRefTypeSignature(io.github.classgraph.ClassRefTypeSignature) BaseTypeSignature(io.github.classgraph.BaseTypeSignature) ClassGraph(io.github.classgraph.ClassGraph) ClassRefTypeSignature(io.github.classgraph.ClassRefTypeSignature) ArrayTypeSignature(io.github.classgraph.ArrayTypeSignature) ClassInfo(io.github.classgraph.ClassInfo) Test(org.junit.jupiter.api.Test)

Example 4 with TypeSignature

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

the class GroupHandler method printType.

private String printType(Ctx ctx, TypeSignature type) {
    if (type instanceof ArrayTypeSignature) {
        return LIST_OF + printType(ctx, ((ArrayTypeSignature) type).getElementTypeSignature());
    }
    if (type instanceof BaseTypeSignature) {
        return code(type.toString());
    }
    if (type instanceof ClassRefTypeSignature) {
        ClassRefTypeSignature classRef = (ClassRefTypeSignature) type;
        Class<?> cl = classRef.loadClass();
        // ID
        if (IId.class.isAssignableFrom(cl)) {
            String name = cl.getSimpleName();
            return ID_OF + code(name.substring(0, name.length() - 2));
        }
        // File
        if (File.class.isAssignableFrom(cl)) {
            // we could check if dir or file here
            return code("File");
        }
        // List
        if (List.class.isAssignableFrom(cl)) {
            TypeArgument param = classRef.getTypeArguments().get(0);
            return LIST_OF + printType(ctx.withGeneric(true), param);
        }
        // Map
        if (BiMap.class.isAssignableFrom(cl)) {
            return "bijective map from " + printType(ctx.withGeneric(true), classRef.getTypeArguments().get(0)) + " to " + printType(ctx.withGeneric(true), classRef.getTypeArguments().get(1));
        }
        if (ClassToInstanceMap.class.isAssignableFrom(cl)) {
            return "ClassToInstanceMap maps from base class " + printType(ctx.withGeneric(true), classRef.getTypeArguments().get(0)) + " to instances of subtypes";
        }
        if (Map.class.isAssignableFrom(cl)) {
            return "map from " + printType(ctx.withGeneric(true), classRef.getTypeArguments().get(0)) + " to " + printType(ctx.withGeneric(true), classRef.getTypeArguments().get(1));
        }
        // String
        if (String.class.isAssignableFrom(cl)) {
            return code("String");
        }
        // another BaseClass
        if (content.keySet().stream().map(Base::getBaseClass).anyMatch(c -> c.equals(cl))) {
            return "[" + type.toStringWithSimpleNames() + "](" + anchor(baseTitle(cl)) + ")";
        }
        // another contentClass
        Optional<Pair<CPSType, ClassInfo>> match = content.values().stream().filter(p -> p.getRight().loadClass().equals(cl)).collect(MoreCollectors.toOptional());
        if (match.isPresent()) {
            return "[" + match.get().getLeft().id() + "](" + anchor(match.get().getLeft().id()) + ")";
        }
        if (content.keySet().stream().map(Base::getBaseClass).anyMatch(c -> c.equals(cl))) {
            return "[" + type.toStringWithSimpleNames() + "](" + anchor(baseTitle(cl)) + ")";
        }
        // another class in the group
        if (group.getOtherClasses().contains(cl)) {
            return "[" + cl.getSimpleName() + "](" + anchor(typeTitle(cl)) + ")";
        }
        // a marker interface
        if (group.getMarkerInterfaces().contains(cl)) {
            return "[" + cl.getSimpleName() + "](" + anchor(markerTitle(cl)) + ")";
        }
        // ENUM
        if (Enum.class.isAssignableFrom(cl)) {
            return "one of " + Arrays.stream(cl.getEnumConstants()).map(Enum.class::cast).map(Enum::name).collect(Collectors.joining(", "));
        }
        if (Primitives.isWrapperType(cl)) {
            return "`" + Primitives.unwrap(cl).getSimpleName() + "`" + (ctx.isIdOf() ? "" : " or `null`");
        }
        // default for hidden types
        if (group.getHides().contains(cl)) {
            return code(type.toStringWithSimpleNames());
        }
    }
    if (!ctx.isIdOf()) {
        log.warn("Unhandled type {}", type);
    }
    return code(type.toStringWithSimpleNames());
}
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) BaseTypeSignature(io.github.classgraph.BaseTypeSignature) ClassRefTypeSignature(io.github.classgraph.ClassRefTypeSignature) TypeArgument(io.github.classgraph.TypeArgument) ArrayTypeSignature(io.github.classgraph.ArrayTypeSignature) Pair(org.apache.commons.lang3.tuple.Pair)

Example 5 with TypeSignature

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

the class NativeUtil method getSimpleType.

Class<?> getSimpleType(Function<String, Class<?>> classResolver, Supplier<TypeSignature> typeSignatureSupplier, Supplier<TypeSignature> typeDescriptorSupplier) {
    TypeSignature typeSignature = typeSignatureSupplier.get();
    if (typeSignature == null) {
        // not a generic type
        TypeSignature typeDescriptor = typeDescriptorSupplier.get();
        return getSimpleType(classResolver, typeDescriptor);
    }
    if (typeSignature instanceof ClassRefTypeSignature) {
        ClassRefTypeSignature refType = (ClassRefTypeSignature) typeSignature;
        List<TypeArgument> typeArguments = refType.getTypeArguments();
        if (typeArguments.size() == 1) {
            TypeArgument typeArgument = typeArguments.get(0);
            ReferenceTypeSignature ref = typeArgument.getTypeSignature();
            return getSimpleType(classResolver, ref);
        }
    }
    return getSimpleType(classResolver, typeSignature);
}
Also used : ReferenceTypeSignature(io.github.classgraph.ReferenceTypeSignature) BaseTypeSignature(io.github.classgraph.BaseTypeSignature) ClassRefTypeSignature(io.github.classgraph.ClassRefTypeSignature) ReferenceTypeSignature(io.github.classgraph.ReferenceTypeSignature) TypeSignature(io.github.classgraph.TypeSignature) ClassRefTypeSignature(io.github.classgraph.ClassRefTypeSignature) TypeArgument(io.github.classgraph.TypeArgument)

Aggregations

TypeSignature (io.github.classgraph.TypeSignature)6 ArrayTypeSignature (io.github.classgraph.ArrayTypeSignature)4 BaseTypeSignature (io.github.classgraph.BaseTypeSignature)4 ClassRefTypeSignature (io.github.classgraph.ClassRefTypeSignature)4 ScanResult (io.github.classgraph.ScanResult)3 Introspection (com.bakdata.conquery.introspection.Introspection)2 ClassGraph (io.github.classgraph.ClassGraph)2 ClassInfo (io.github.classgraph.ClassInfo)2 MethodInfo (io.github.classgraph.MethodInfo)2 MethodParameterInfo (io.github.classgraph.MethodParameterInfo)2 TypeArgument (io.github.classgraph.TypeArgument)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 Constants (com.bakdata.conquery.Constants)1 CPSType (com.bakdata.conquery.io.cps.CPSType)1 Jackson (com.bakdata.conquery.io.jackson.Jackson)1 Base (com.bakdata.conquery.model.Base)1 Group (com.bakdata.conquery.model.Group)1 IId (com.bakdata.conquery.models.identifiable.ids.IId)1 PrettyPrinter (com.bakdata.conquery.util.PrettyPrinter)1