use of io.sundr.model.ClassRef in project kubernetes-client by fabric8io.
the class AnnotatedPropertyPathDetector method visit.
@Override
public void visit(TypeDefBuilder builder) {
TypeDef type = builder.build();
final List<Property> properties = type.getProperties();
for (Property p : properties) {
if (parents.contains(p)) {
continue;
}
List<Property> newParents = new ArrayList<>(parents);
boolean match = p.getAnnotations().stream().anyMatch(a -> a.getClassRef().getName().equals(annotationName));
if (match) {
newParents.add(p);
reference.set(Optional.of(newParents.stream().map(Property::getName).collect(Collectors.joining(DOT, prefix, ""))));
return;
}
}
properties.stream().filter(p -> p.getTypeRef() instanceof ClassRef).forEach(p -> {
if (!parents.contains(p)) {
ClassRef classRef = (ClassRef) p.getTypeRef();
TypeDef propertyType = Types.typeDefFrom(classRef);
if (!propertyType.isEnum()) {
List<Property> newParents = new ArrayList<>(parents);
newParents.add(p);
new TypeDefBuilder(propertyType).accept(new AnnotatedPropertyPathDetector(prefix, annotationName, newParents, reference)).build();
}
}
});
}
use of io.sundr.model.ClassRef in project kubernetes-client by fabric8io.
the class TypesTest method projectSuperClassesShouldProduceProperlyTypedClasses.
@Test
void projectSuperClassesShouldProduceProperlyTypedClasses() {
List<ClassRef> superClasses = Types.typeDefFrom(Basic.class).getExtendsList();
assertEquals(2, superClasses.size());
Optional<ClassRef> crOpt = superClasses.stream().filter(c -> c.getName().contains("CustomResource")).findFirst();
assertTrue(crOpt.isPresent());
ClassRef crDef = crOpt.get();
List<TypeRef> arguments = crDef.getArguments();
assertEquals(2, arguments.size());
assertTrue(arguments.get(0).toString().contains(BasicSpec.class.getSimpleName()));
assertTrue(arguments.get(1).toString().contains(BasicStatus.class.getSimpleName()));
}
use of io.sundr.model.ClassRef in project kubernetes-client by fabric8io.
the class TypesTest method shouldFindInheritedStatusProperty.
@Test
void shouldFindInheritedStatusProperty() {
final TypeDef def = Types.typeDefFrom(Child.class);
final Optional<Property> p = Types.findStatusProperty(def);
assertTrue(p.isPresent());
final Property property = p.get();
final TypeRef typeRef = property.getTypeRef();
assertTrue(typeRef instanceof ClassRef);
final ClassRef classRef = (ClassRef) typeRef;
final SpecAndStatus specAndStatus = Types.resolveSpecAndStatusTypes(def);
assertEquals(specAndStatus.getStatusClassName(), classRef.getFullyQualifiedName());
}
use of io.sundr.model.ClassRef in project sundrio by sundrio.
the class GetDefinition method apply.
@Override
public TypeDef apply(ClassRef t) {
String fullyQualifiedName = t.getFullyQualifiedName();
TypeDef def = DefinitionRepository.getRepository().getDefinition(fullyQualifiedName);
if (def != null) {
return def;
}
Predicate<String> isLowerCase = w -> Character.isUpperCase(w.charAt(0));
Predicate<String> inPackage = until(isLowerCase);
Predicate<String> outOfPackage = after(isLowerCase);
String packageName = Arrays.stream(fullyQualifiedName.split("\\.")).filter(inPackage).collect(Collectors.joining("."));
String className = Arrays.stream(fullyQualifiedName.split("\\.")).filter(outOfPackage).collect(Collectors.joining("."));
String ownerClassName = className.contains(".") ? className.substring(0, className.indexOf(".")) : null;
if (ownerClassName != null) {
className = className.substring(ownerClassName.length() + 1);
return new TypeDefBuilder().withName(className).withPackageName(packageName).withOuterTypeName(packageName + "." + ownerClassName).build();
}
return new TypeDefBuilder().withName(className).withPackageName(packageName).build();
}
use of io.sundr.model.ClassRef in project sundrio by sundrio.
the class ClassToTypeDef method apply.
@Override
public TypeDef apply(Class item) {
if (Object.class.equals(item)) {
return TypeDef.OBJECT;
}
Kind kind = classToKind.apply(item);
List<ClassRef> extendsList = new ArrayList<>();
List<ClassRef> implementsList = new ArrayList<>();
List<Property> properties = new ArrayList<>();
List<Method> methods = new ArrayList<>();
List<Method> constructors = new ArrayList<>();
List<TypeParamDef> parameters = new ArrayList<>();
if (item.getSuperclass() != null) {
extendsList.add((ClassRef) typeToTypeRef.apply(item.getGenericSuperclass()));
references.add(item.getSuperclass());
}
for (Class interfaceClass : item.getInterfaces()) {
references.add(interfaceClass);
}
for (Type interfaceClass : item.getGenericInterfaces()) {
TypeRef ref = typeToTypeRef.apply(interfaceClass);
if (ref instanceof ClassRef) {
implementsList.add((ClassRef) ref);
}
}
constructors.addAll(getConstructors(item, references));
methods.addAll(getMethods(item, references));
properties.addAll(getProperties(item, references));
for (TypeVariable typeVariable : item.getTypeParameters()) {
List<ClassRef> bounds = new ArrayList<>();
for (Type boundType : typeVariable.getBounds()) {
TypeRef typeRef = typeToTypeRef.apply(boundType);
if (typeRef instanceof ClassRef) {
bounds.add((ClassRef) typeRef);
}
}
parameters.add(new TypeParamDefBuilder().withName(typeVariable.getName()).withBounds(bounds).build());
}
String outerFQCN = item.getDeclaringClass() != null ? item.getDeclaringClass().getName() : null;
TypeDef result = context.getDefinitionRepository().register(new TypeDefBuilder().withKind(kind).withOuterTypeName(outerFQCN).withName(item.getSimpleName()).withPackageName(item.getPackage() != null ? item.getPackage().getName() : null).withModifiers(item.getModifiers()).withParameters(parameters).withConstructors(constructors).withMethods(methods).withProperties(properties).withExtendsList(extendsList).withImplementsList(implementsList).build());
Set<Class> copy = new HashSet<>(references);
copy.stream().peek(c -> references.remove(c)).filter(c -> !c.equals(item)).filter(c -> !c.getName().startsWith("sun.") && !c.getName().toString().startsWith("com.sun.")).forEach(c -> {
String referenceFQCN = c.getName().replaceAll(Pattern.quote("$"), ".");
context.getDefinitionRepository().registerIfAbsent(referenceFQCN, () -> apply(c));
});
return result;
}
Aggregations