use of io.sundr.model.Property 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;
}
use of io.sundr.model.Property in project sundrio by sundrio.
the class ClassToTypeDef method getProperties.
private Set<Property> getProperties(Class item, Set<Class> references) {
Set<Property> properties = new HashSet<Property>();
for (Field field : item.getDeclaredFields()) {
List<AnnotationRef> annotationRefs = new ArrayList<AnnotationRef>();
processAnnotatedElement(field, annotationRefs);
if (field.getGenericType() instanceof Class) {
references.add((Class) field.getGenericType());
}
// If property contains generic bounds, we need to process them too.
if (field.getGenericType() instanceof ParameterizedType) {
ParameterizedType p = (ParameterizedType) field.getGenericType();
references.addAll(Stream.of(p.getActualTypeArguments()).filter(t -> t instanceof Class).map(t -> (Class) t).filter(c -> !item.equals(c)).collect(Collectors.toList()));
}
properties.add(new PropertyBuilder().withName(field.getName()).withModifiers(field.getModifiers()).withAnnotations(annotationRefs).withTypeRef(typeToTypeRef.apply(field.getGenericType())).build());
}
return properties;
}
use of io.sundr.model.Property in project sundrio by sundrio.
the class SundrioGenerator method createModel.
public TypeDef createModel(String name, Model model, AbstractJavaCodegen config, Map<String, Model> allDefinitions) {
String prefix = name.contains(DOT) ? name.substring(0, name.lastIndexOf(DOT)) : EMPTY;
String packageName = prefix.isEmpty() ? config.modelPackage() : config.modelPackage() + DOT + prefix;
String className = prefix.isEmpty() ? name : name.substring(name.lastIndexOf(DOT) + 1);
ClassRef superClass = null;
List<ClassRef> interfaces = new ArrayList<>();
List<Property> fields = new ArrayList<>();
List<Method> methods = new ArrayList<>();
if (model instanceof ComposedModel) {
ComposedModel composed = (ComposedModel) model;
// interfaces (intermediate models)
if (composed.getInterfaces() != null) {
for (RefModel _interface : composed.getInterfaces()) {
Model interfaceModel = null;
if (allDefinitions != null) {
interfaceModel = allDefinitions.get(_interface.getSimpleRef());
}
}
return new TypeDefBuilder().withKind(Kind.CLASS).withPackageName(packageName).withName(className).withImplementsList(interfaces).withExtendsList(superClass).withProperties(fields).withMethods(methods).build();
}
}
return null;
}
use of io.sundr.model.Property in project sundrio by sundrio.
the class FieldDirective method render.
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException {
String block = "";
Property field = null;
// reading params
if (node.jjtGetChild(0) != null) {
field = (Property) node.jjtGetChild(0).value(context);
}
writeField(writer, field, block);
return true;
}
use of io.sundr.model.Property in project sundrio by sundrio.
the class BuilderUtils method toHashCode.
public static List<Statement> toHashCode(Collection<Property> properties) {
List<Statement> statements = new ArrayList<>();
statements.add(new StringStatement("return java.util.Objects.hash(" + Stream.concat(properties.stream().map(Property::getName), Stream.of("super.hashCode()")).collect(Collectors.joining(", ")) + ");"));
return statements;
}
Aggregations