use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class ClazzAsTest method testToFluent.
@Test
public void testToFluent() {
TypeDef type = new TypeDefBuilder().withName("MyClass").withPackageName(getClass().getPackage().getName()).withParameters().build();
Method constructor = new MethodBuilder().withReturnType(type.toReference()).withAnnotations(Constants.BUILDABLE_ANNOTATION).build();
type = new TypeDefBuilder(type).withConstructors(constructor).build();
TypeDef result = ClazzAs.FLUENT_IMPL.apply(TypeArguments.apply(type));
System.out.println(result);
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class TypeElementToTypeDef method apply.
@Override
public TypeDef apply(TypeElement classElement) {
// Check SuperClass
Kind kind = Kind.CLASS;
Element enclosing = classElement.getEnclosingElement();
if (enclosing != null && ANY.equals(enclosing.getSimpleName().toString())) {
throw new SundrException("Failed to read class element:" + classElement.getQualifiedName().toString() + ". " + Messages.POTENTIAL_UNRESOLVED_SYMBOL);
}
TypeMirror superClass = classElement.getSuperclass();
TypeRef superClassType = TypeDef.OBJECT_REF;
if (superClass == null) {
// ignore
} else if (superClass instanceof NoType) {
// ignore
} else if (superClass.toString().equals(TypeDef.OBJECT.getFullyQualifiedName())) {
// ignore
} else {
superClassType = referenceAdapterFunction.apply(superClass);
}
List<TypeParamDef> genericTypes = new ArrayList<TypeParamDef>();
List<ClassRef> interfaces = new ArrayList<ClassRef>();
if (classElement.getKind() == ElementKind.INTERFACE) {
kind = Kind.INTERFACE;
} else if (classElement.getKind() == ElementKind.CLASS) {
kind = Kind.CLASS;
} else if (classElement.getKind() == ElementKind.ANNOTATION_TYPE) {
kind = Kind.ANNOTATION;
} else if (classElement.getKind() == ElementKind.ENUM) {
kind = Kind.ENUM;
}
String comments = AptContext.getContext().getElements().getDocComment(classElement);
List<String> commentList = Strings.isNullOrEmpty(comments) ? new ArrayList<>() : Arrays.stream(comments.split(NEWLINE_PATTERN)).map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toList());
for (TypeMirror interfaceTypeMirrror : classElement.getInterfaces()) {
TypeRef interfaceType = referenceAdapterFunction.apply(interfaceTypeMirrror);
if (interfaceType instanceof ClassRef) {
interfaces.add((ClassRef) interfaceType);
} else {
throw new IllegalStateException("Interface: [" + interfaceType + "] not mapped to a class ref.");
}
}
for (TypeParameterElement typeParameter : classElement.getTypeParameters()) {
TypeParamDef genericType = typeParamAdapterFunction.apply(typeParameter);
genericTypes.add(genericType);
}
TypeDef baseType = new TypeDefBuilder().withComments(commentList).withKind(kind).withModifiers(Types.modifiersToInt(classElement.getModifiers())).withPackageName(getPackageName(classElement)).withName(getClassName(classElement)).withParameters(genericTypes).withExtendsList(superClassType instanceof ClassRef ? (ClassRef) superClassType : null).withImplementsList(interfaces).withOuterTypeName(classElement.getEnclosingElement() instanceof TypeElement ? classElement.getEnclosingElement().toString() : null).build();
// We will register the base type first and will replace it with the full blown version later.
context.getDefinitionRepository().registerIfAbsent(baseType);
List<TypeDef> innerTypes = new ArrayList<TypeDef>();
for (TypeElement innerElement : ElementFilter.typesIn(classElement.getEnclosedElements())) {
TypeDef innerType = context.getDefinitionRepository().register(apply(innerElement));
if (innerType == null) {
throw new IllegalStateException("Inner type for:" + innerElement + " is null");
}
innerType = new TypeDefBuilder(innerType).withOuterTypeName(baseType.getFullyQualifiedName()).build();
context.getDefinitionRepository().register(innerType);
innerTypes.add(innerType);
}
TypeDefBuilder builder = new TypeDefBuilder(baseType).withInnerTypes(innerTypes);
for (ExecutableElement constructor : ElementFilter.constructorsIn(classElement.getEnclosedElements())) {
builder.addToConstructors(methodAdapterFunction.apply(constructor));
}
// Populate Fields
for (VariableElement variableElement : ElementFilter.fieldsIn(classElement.getEnclosedElements())) {
builder.addToProperties(propertyAdapterFunction.apply(variableElement));
}
Set<ExecutableElement> allMethods = new LinkedHashSet<ExecutableElement>();
allMethods.addAll(ElementFilter.methodsIn(classElement.getEnclosedElements()));
allMethods.addAll(getInheritedMethods(classElement));
for (ExecutableElement method : allMethods) {
builder.addToMethods(methodAdapterFunction.apply(method));
}
for (AnnotationMirror annotationMirror : classElement.getAnnotationMirrors()) {
builder.addToAnnotations(annotationAdapterFunction.apply(annotationMirror));
}
// Let's register the full blown definition
TypeDef result = context.getDefinitionRepository().register(builder.build());
// Also register other types
if (context.isDeep()) {
Set<TypeElement> references = new HashSet<>(context.getReferences());
references.stream().filter(t -> !t.equals(classElement)).filter(t -> !t.toString().startsWith("sun.") && !t.toString().startsWith("com.sun.")).forEach(t -> {
String fqcn = t.toString();
TypeDef existing = context.getDefinitionRepository().getDefinition(fqcn);
if (existing == null) {
context.getDefinitionRepository().registerIfAbsent(fqcn, () -> apply(t));
}
context.getReferences().remove(t);
});
}
return result;
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class SourcesTest method shouldAdaptList.
@Test
public void shouldAdaptList() throws Exception {
TypeDef typeDef = Sources.readTypeDefFromResource("java/util/List.java", context);
assertNotNull(typeDef);
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class AbstractAdapterTest method testWithSimpleClass.
@Test
public void testWithSimpleClass() {
T input = getInput(SimpleClass.class);
TypeDef typeDef = Adapters.adaptType(input, getContext());
assertNotNull(typeDef);
assertEquals("SimpleClass", typeDef.getName());
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class AbstractAdapterTest method testClassWithAnnotationParams.
@Test
public void testClassWithAnnotationParams() {
T input = getInput(ClassWithAnnotation.class);
TypeDef typeDef = Adapters.adaptType(input, getContext());
final List<Property> properties = typeDef.getProperties();
final Property foo = properties.stream().filter(p -> p.getName().equals("foo")).findFirst().orElseThrow(RuntimeException::new);
List<AnnotationRef> annotations = foo.getAnnotations();
assertEquals(1, annotations.size());
AnnotationRef annotationRef = annotations.get(0);
assertTrue(annotationRef.toString().contains("SimpleAnnotation"));
assertEquals("foo", annotationRef.getParameters().get("name"));
final Method bar = typeDef.getMethods().stream().filter(p -> p.getName().equals("bar")).findFirst().orElseThrow(RuntimeException::new);
annotations = bar.getAnnotations();
assertEquals(1, annotations.size());
annotationRef = annotations.get(0);
assertEquals("bar", annotationRef.getParameters().get("name"));
Object params = annotationRef.getParameters().get("values");
assertArrayEquals(new int[] { 1, 2, 3, 5, 7 }, (int[]) annotationRef.getParameters().get("values"));
}
Aggregations