use of io.sundr.model.Method in project sundrio by sundrio.
the class ToPojo method readPrimitiveArrayProperty.
/**
* Returns the string representation of the code that reads a primitive array property.
*
* @param ref The reference.
* @param source The type of the reference.
* @param property The property to read.
* @return The code.
*/
private static String readPrimitiveArrayProperty(String ref, TypeDef source, Property property) {
StringBuilder sb = new StringBuilder();
Method getter = getterOf(source, property);
sb.append("Arrays.asList(").append(ref).append(".").append(getter.getName()).append("())").append(".stream()").append(".collect(Collectors.toList())).toArray(new " + getter.getReturnType().toString() + "[])");
return sb.toString();
}
use of io.sundr.model.Method in project sundrio by sundrio.
the class ToPojo method readProperty.
/**
* Returns the string representation of the code that given a reference of the specified type, reads the specified property.
*
* @param ref The reference.
* @param source The type of the reference.
* @param property The property to read.
* @return The code.
*/
private static String readProperty(String ref, TypeDef source, Property property) {
TypeRef propertyTypeRef = property.getTypeRef();
Method getter = getterOf(source, property);
if (getter == null) {
return "null";
}
TypeRef getterTypeRef = getter.getReturnType();
if (propertyTypeRef.getDimensions() == getterTypeRef.getDimensions() && Assignable.isAssignable(propertyTypeRef).from(getterTypeRef)) {
return readObjectProperty(ref, source, property);
}
if (property.getTypeRef().getDimensions() > 0) {
return readArrayProperty(ref, source, property);
}
if (property.getTypeRef() instanceof ClassRef && GetDefinition.of((ClassRef) getterTypeRef).isAnnotation()) {
return readAnnotationProperty(ref + "." + getter.getName() + "()", GetDefinition.of((ClassRef) getterTypeRef), property);
}
return readObjectProperty(ref, source, property);
}
use of io.sundr.model.Method in project sundrio by sundrio.
the class ToPojo method readMapValue.
/**
* Returns the string representation of the code that given a reference of the specified type, reads the specified property.
*
* @param ref The reference.
* @param source The type of the reference.
* @param property The property to read.
* @return The code.
*/
private static String readMapValue(String ref, TypeDef source, Property property) {
TypeRef propertyTypeRef = property.getTypeRef();
Method getter = getterOf(source, property);
if (getter == null) {
return "null";
}
TypeRef getterTypeRef = getter.getReturnType();
if (propertyTypeRef.getDimensions() == getterTypeRef.getDimensions() && Assignable.isAssignable(propertyTypeRef).from(getterTypeRef)) {
return readObjectValue(ref, source, property);
}
if (property.getTypeRef().getDimensions() > 0) {
return readArrayValue(ref, source, property);
}
if (getterTypeRef instanceof ClassRef) {
TypeDef getterTypeDef = GetDefinition.of((ClassRef) getterTypeRef);
if (getterTypeDef.isEnum()) {
return readEnumValue(ref, source, property);
}
if (getterTypeDef.isAnnotation()) {
return readAnnotationValue("((Map)(" + ref + " instanceof Map ? ((Map)" + ref + ").get(\"" + getterOf(source, property).getName() + "\") : " + getDefaultValue(property) + "))", GetDefinition.of((ClassRef) getterTypeRef), property);
}
}
return readObjectValue(ref, source, property);
}
use of io.sundr.model.Method 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.Method 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;
}
Aggregations