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);
}
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);
}
}
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);
}
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;
}
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);
}
Aggregations