use of io.sundr.model.ClassRef in project sundrio by sundrio.
the class Generify method findTypeArguments.
private static Set<TypeRef> findTypeArguments(Set<ClassRef> interfaces) {
if (interfaces.size() < 2) {
return Collections.<TypeRef>emptySet();
}
// 1st pass find all generics
Set<TypeRef> allGenerics = new LinkedHashSet<TypeRef>();
for (ClassRef clazz : interfaces) {
allGenerics.addAll(clazz.getArguments());
}
// 2nd pass collect common generics
Set<TypeRef> common = new LinkedHashSet<TypeRef>(allGenerics);
for (ClassRef clazz : interfaces) {
Set<TypeRef> ownGenerics = new LinkedHashSet<TypeRef>();
ownGenerics.addAll(clazz.getArguments());
common.remove(clazz);
common.retainAll(ownGenerics);
}
Set<TypeRef> result = new LinkedHashSet<TypeRef>();
for (TypeRef type : common) {
Boolean isGeneric = type.getAttributes().containsKey(IS_GENERIC) ? (Boolean) type.getAttributes().get(IS_GENERIC) : false;
if (!isGeneric) {
result.add(type);
}
}
return result;
}
use of io.sundr.model.ClassRef in project sundrio by sundrio.
the class ClazzAs method staticAdapterBody.
private static String staticAdapterBody(TypeDef pojo, TypeDef pojoBuilder, TypeDef source, boolean returnBuilder) {
StringBuilder sb = new StringBuilder();
sb.append("return new ").append(pojoBuilder.getName()).append("()");
for (Method m : source.getMethods()) {
String trimmedName = Strings.deCapitalizeFirst(m.getName().replaceAll("^get", "").replaceAll("^is", ""));
if (m.getReturnType() instanceof ClassRef) {
ClassRef ref = (ClassRef) m.getReturnType();
Boolean hasSuperClass = pojo.getProperties().stream().filter(p -> p.getTypeRef() instanceof ClassRef && p.getName().equals(Getter.propertyNameSafe(m))).map(p -> GetDefinition.of((ClassRef) p.getTypeRef())).flatMap(c -> c.getExtendsList().stream()).count() > 0;
if (GetDefinition.of(ref).isAnnotation()) {
TypeDef generatedType = Types.allProperties(pojo).stream().filter(p -> p.getName().equals(m.getName())).map(p -> p.getTypeRef()).filter(r -> r instanceof ClassRef).map(r -> (ClassRef) r).map(c -> GetDefinition.of(c)).findFirst().orElse(null);
if (generatedType != null) {
Method ctor = BuilderUtils.findBuildableConstructor(generatedType);
if (m.getReturnType().getDimensions() > 0) {
sb.append(".addAllTo").append(Strings.capitalizeFirst(trimmedName)).append("(").append("Arrays.asList(").append("instance.").append(m.getName()).append("())").append(".stream().map(i ->").append("new ").append(generatedType.getName()).append("(").append(ctor.getArguments().stream().map(p -> Getter.find(GetDefinition.of((ClassRef) m.getReturnType()), p, true)).map(g -> g.getName()).map(s -> "i." + s + "()").collect(Collectors.joining(", "))).append(")").append(")").append(".collect(Collectors.toList()))");
} else {
sb.append(".with").append(Strings.capitalizeFirst(trimmedName)).append("(").append("new ").append(generatedType.getName()).append("(").append(ctor.getArguments().stream().map(p -> Getter.find(GetDefinition.of((ClassRef) m.getReturnType()), p, true)).map(g -> g.getName()).map(s -> "instance." + m.getName() + "()." + s + "()").collect(Collectors.joining(", "))).append(")").append(")");
}
continue;
}
}
}
String withMethod = "with" + (Strings.capitalizeFirst(trimmedName));
if (Types.hasProperty(pojo, trimmedName)) {
sb.append(".").append(withMethod).append("(").append("instance.").append(m.getName()).append("())");
}
}
if (returnBuilder) {
sb.append(".build();");
} else {
sb.append(";");
}
return sb.toString();
}
use of io.sundr.model.ClassRef in project sundrio by sundrio.
the class TypeToTypeRef method apply.
@Override
public TypeRef apply(Type type) {
if (type instanceof VoidType) {
return new VoidRef();
} else if (type instanceof WildcardType) {
return new WildcardRef();
} else if (type instanceof ReferenceType) {
ReferenceType referenceType = (ReferenceType) type;
int dimensions = referenceType.getArrayCount();
TypeRef typeRef = apply(referenceType.getType());
if (dimensions == 0) {
return typeRef;
} else if (typeRef instanceof ClassRef) {
return new ClassRefBuilder((ClassRef) typeRef).withDimensions(dimensions).build();
} else if (typeRef instanceof PrimitiveRef) {
return new PrimitiveRefBuilder((PrimitiveRef) typeRef).withDimensions(dimensions).build();
} else if (typeRef instanceof TypeParamRef) {
return new TypeParamRefBuilder((TypeParamRef) typeRef).withDimensions(dimensions).build();
}
} else if (type instanceof PrimitiveType) {
PrimitiveType primitiveType = (PrimitiveType) type;
return new PrimitiveRefBuilder().withName(primitiveType.getType().name()).build();
} else if (type instanceof ClassOrInterfaceType) {
return classOrInterfaceToTypeRef.apply((ClassOrInterfaceType) type);
}
throw new IllegalArgumentException("Can't handle type:[" + type + "].");
}
use of io.sundr.model.ClassRef in project sundrio by sundrio.
the class SimpleClassTest method testBuilder.
@Test
public void testBuilder() {
TypeDef builder = ClazzAs.BUILDER.apply(simpleClassDef);
System.out.println(builder);
assertEquals(Kind.CLASS, builder.getKind());
assertEquals("SimpleClassBuilder", builder.getName());
assertEquals(1, builder.getExtendsList().size());
ClassRef superClass = builder.getImplementsList().iterator().next();
assertEquals("VisitableBuilder", superClass.getName());
assertEquals(2, superClass.getArguments().size());
Iterator<TypeRef> argIterator = superClass.getArguments().iterator();
TypeRef ref = argIterator.next();
assertEquals("testpackage.SimpleClass", ref.toString());
assertEquals("testpackage.SimpleClass", ref.render());
ref = argIterator.next();
assertEquals("testpackage.SimpleClassBuilder", ref.toString());
assertEquals("testpackage.SimpleClassBuilder", ref.render());
}
use of io.sundr.model.ClassRef in project sundrio by sundrio.
the class SimpleClassTest method testFluent.
@Test
public void testFluent() {
TypeDef fluent = ClazzAs.FLUENT_INTERFACE.apply(simpleClassDef);
System.out.println(fluent);
assertEquals(Kind.INTERFACE, fluent.getKind());
assertEquals("SimpleClassFluent", fluent.getName());
assertEquals(1, fluent.getExtendsList().size());
ClassRef superClass = fluent.getExtendsList().iterator().next();
assertEquals("Fluent", superClass.getName());
assertEquals(1, superClass.getArguments().size());
assertEquals("A", superClass.getArguments().iterator().next().toString());
}
Aggregations