Search in sources :

Example 1 with SourceType

use of cz.habarta.typescript.generator.parser.SourceType in project typescript-generator by vojtechhabarta.

the class JaxrsApplicationTest method testReturnedTypesFromResource.

@Test
public void testReturnedTypesFromResource() {
    JaxrsApplicationParser jaxrsApplicationParser = createJaxrsApplicationParser(TestUtils.settings());
    final JaxrsApplicationParser.Result result = jaxrsApplicationParser.tryParse(new SourceType<>(TestResource1.class));
    Assertions.assertNotNull(result);
    List<Type> types = getTypes(result.discoveredTypes);
    final List<Type> expectedTypes = Arrays.asList(A.class, new TypeReference<List<B>>() {
    }.getType(), C.class, new TypeReference<List<D>>() {
    }.getType(), List.class, E.class, new TypeReference<List<F>>() {
    }.getType(), G.class, new TypeReference<Map<String, H>>() {
    }.getType(), I.class, JGenericArrayType.of(J[].class), // types handled by DefaultTypeProcessor
    String.class, Boolean.class, Character.class, Number.class, Integer.class, int.class, void.class);
    assertHasSameItems(expectedTypes, types);
}
Also used : JGenericArrayType(cz.habarta.typescript.generator.type.JGenericArrayType) MediaType(jakarta.ws.rs.core.MediaType) Type(java.lang.reflect.Type) SourceType(cz.habarta.typescript.generator.parser.SourceType) JaxrsApplicationParser(cz.habarta.typescript.generator.parser.JaxrsApplicationParser) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.jupiter.api.Test)

Example 2 with SourceType

use of cz.habarta.typescript.generator.parser.SourceType in project typescript-generator by vojtechhabarta.

the class Input method from.

public static Input from(Parameters parameters) {
    final ClassLoader originalContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        if (parameters.classLoader != null) {
            Thread.currentThread().setContextClassLoader(parameters.classLoader);
        }
        try (final ClasspathScanner classpathScanner = new ClasspathScanner(parameters.classLoader, parameters.scanningAcceptedPackages, parameters.debug)) {
            final List<SourceType<Type>> types = new ArrayList<>();
            if (parameters.classNames != null) {
                types.addAll(fromClassNames(parameters.classNames));
            }
            if (parameters.classNamePatterns != null) {
                types.addAll(fromClassNamePatterns(classpathScanner.getScanResult(), parameters.classNamePatterns));
            }
            if (parameters.classesImplementingInterfaces != null) {
                final ScanResult scanResult = classpathScanner.getScanResult();
                final List<SourceType<Type>> c = fromClassNames(parameters.classesImplementingInterfaces.stream().flatMap(interf -> scanResult.getClassesImplementing(interf).getNames().stream()).distinct().collect(Collectors.toList()));
                types.addAll(c);
            }
            if (parameters.classesExtendingClasses != null) {
                final ScanResult scanResult = classpathScanner.getScanResult();
                final List<SourceType<Type>> c = fromClassNames(parameters.classesExtendingClasses.stream().flatMap(superclass -> scanResult.getSubclasses(superclass).getNames().stream()).distinct().collect(Collectors.toList()));
                types.addAll(c);
            }
            if (parameters.classesWithAnnotations != null) {
                final ScanResult scanResult = classpathScanner.getScanResult();
                types.addAll(fromClassNames(parameters.classesWithAnnotations.stream().flatMap(annotation -> scanResult.getClassesWithAnnotation(annotation).getNames().stream()).distinct().collect(Collectors.toList())));
            }
            if (parameters.jaxrsApplicationClassName != null) {
                types.addAll(fromClassNames(Arrays.asList(parameters.jaxrsApplicationClassName)));
            }
            if (parameters.automaticJaxrsApplication) {
                types.addAll(JaxrsApplicationScanner.scanAutomaticJaxrsApplication(classpathScanner.getScanResult(), parameters.isClassNameExcluded));
            }
            if (types.isEmpty()) {
                final String errorMessage = "No input classes found.";
                TypeScriptGenerator.getLogger().error(errorMessage);
                throw new RuntimeException(errorMessage);
            }
            return new Input(types);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(originalContextClassLoader);
    }
}
Also used : ClassGraph(io.github.classgraph.ClassGraph) Arrays(java.util.Arrays) Date(java.util.Date) Predicate(java.util.function.Predicate) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Objects(java.util.Objects) URLClassLoader(java.net.URLClassLoader) List(java.util.List) Type(java.lang.reflect.Type) Utils(cz.habarta.typescript.generator.util.Utils) Pattern(java.util.regex.Pattern) SourceType(cz.habarta.typescript.generator.parser.SourceType) ScanResult(io.github.classgraph.ScanResult) Collections(java.util.Collections) ScanResult(io.github.classgraph.ScanResult) SourceType(cz.habarta.typescript.generator.parser.SourceType) ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader)

Example 3 with SourceType

use of cz.habarta.typescript.generator.parser.SourceType in project typescript-generator by vojtechhabarta.

the class Input method from.

public static Input from(Type... types) {
    Objects.requireNonNull(types, "types");
    final List<SourceType<Type>> sourceTypes = new ArrayList<>();
    for (Type type : types) {
        sourceTypes.add(new SourceType<>(type));
    }
    return new Input(sourceTypes);
}
Also used : Type(java.lang.reflect.Type) SourceType(cz.habarta.typescript.generator.parser.SourceType) SourceType(cz.habarta.typescript.generator.parser.SourceType) ArrayList(java.util.ArrayList)

Example 4 with SourceType

use of cz.habarta.typescript.generator.parser.SourceType in project typescript-generator by vojtechhabarta.

the class SpringApplicationParser method tryParse.

@Override
public JaxrsApplicationParser.Result tryParse(SourceType<?> sourceType) {
    if (!(sourceType.type instanceof Class<?>)) {
        return null;
    }
    final Class<?> cls = (Class<?>) sourceType.type;
    // application
    final SpringBootApplication app = AnnotationUtils.findAnnotation(cls, SpringBootApplication.class);
    if (app != null) {
        if (settings.scanSpringApplication) {
            TypeScriptGenerator.getLogger().verbose("Scanning Spring application: " + cls.getName());
            final ClassLoader originalContextClassLoader = Thread.currentThread().getContextClassLoader();
            try {
                Thread.currentThread().setContextClassLoader(settings.classLoader);
                final SpringApplicationHelper springApplicationHelper = new SpringApplicationHelper(settings.classLoader, cls);
                final List<Class<?>> restControllers = springApplicationHelper.findRestControllers();
                return new JaxrsApplicationParser.Result(restControllers.stream().map(controller -> new SourceType<Type>(controller, cls, "<scanned>")).collect(Collectors.toList()));
            } finally {
                Thread.currentThread().setContextClassLoader(originalContextClassLoader);
            }
        } else {
            return null;
        }
    }
    // controller
    final Component component = AnnotationUtils.findAnnotation(cls, Component.class);
    if (component != null) {
        TypeScriptGenerator.getLogger().verbose("Parsing Spring component: " + cls.getName());
        final JaxrsApplicationParser.Result result = new JaxrsApplicationParser.Result();
        final RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(cls, RequestMapping.class);
        final String path = requestMapping != null && requestMapping.path() != null && requestMapping.path().length != 0 ? requestMapping.path()[0] : null;
        final JaxrsApplicationParser.ResourceContext context = new JaxrsApplicationParser.ResourceContext(cls, path);
        parseController(result, context, cls);
        return result;
    }
    return null;
}
Also used : RequestMapping(org.springframework.web.bind.annotation.RequestMapping) SpringBootApplication(org.springframework.boot.autoconfigure.SpringBootApplication) Type(java.lang.reflect.Type) RestApplicationType(cz.habarta.typescript.generator.parser.RestApplicationType) SourceType(cz.habarta.typescript.generator.parser.SourceType) ParameterizedType(java.lang.reflect.ParameterizedType) TsType(cz.habarta.typescript.generator.TsType) JaxrsApplicationParser(cz.habarta.typescript.generator.parser.JaxrsApplicationParser) Component(org.springframework.stereotype.Component)

Example 5 with SourceType

use of cz.habarta.typescript.generator.parser.SourceType in project typescript-generator by vojtechhabarta.

the class JaxrsApplicationTest method testWithParsingWithDefaultApplication.

@Test
public void testWithParsingWithDefaultApplication() {
    final List<SourceType<Type>> sourceTypes = JaxrsApplicationScanner.scanAutomaticJaxrsApplication(new ClassGraph().enableAllInfo().scan(), null);
    testWithParsing(sourceTypes, false);
}
Also used : SourceType(cz.habarta.typescript.generator.parser.SourceType) ClassGraph(io.github.classgraph.ClassGraph) Test(org.junit.jupiter.api.Test)

Aggregations

SourceType (cz.habarta.typescript.generator.parser.SourceType)5 Type (java.lang.reflect.Type)4 JaxrsApplicationParser (cz.habarta.typescript.generator.parser.JaxrsApplicationParser)2 ClassGraph (io.github.classgraph.ClassGraph)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 TsType (cz.habarta.typescript.generator.TsType)1 RestApplicationType (cz.habarta.typescript.generator.parser.RestApplicationType)1 JGenericArrayType (cz.habarta.typescript.generator.type.JGenericArrayType)1 Utils (cz.habarta.typescript.generator.util.Utils)1 ScanResult (io.github.classgraph.ScanResult)1 MediaType (jakarta.ws.rs.core.MediaType)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 URLClassLoader (java.net.URLClassLoader)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Date (java.util.Date)1 List (java.util.List)1 Objects (java.util.Objects)1