use of io.sundr.model.ClassRef in project sundrio by sundrio.
the class AnnotationMirrorToAnnotationRef method apply.
@Override
public AnnotationRef apply(AnnotationMirror item) {
TypeRef annotationType = item.getAnnotationType().accept(new TypeRefTypeVisitor(context), 0);
Map<String, Object> parameters = new HashMap<String, Object>();
if (annotationType instanceof ClassRef) {
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : item.getElementValues().entrySet()) {
checkEntry(entry);
String key = entry.getKey().toString().replace(EMPTY_PARENTHESIS, EMPTY);
Object value = mapAnnotationValue(entry.getValue().getValue());
parameters.put(key, value);
}
return new AnnotationRefBuilder().withClassRef((ClassRef) annotationType).withParameters(parameters).build();
}
throw new IllegalStateException("Annotation type: [" + annotationType + "] is not a class reference.");
}
use of io.sundr.model.ClassRef in project sundrio by sundrio.
the class TypeMirrorToTypeRef method apply.
@Override
public TypeRef apply(TypeMirror item) {
if (item instanceof NoType) {
return new VoidRef();
}
if (item == null) {
throw new IllegalArgumentException("TypeMirror cannot be null.");
}
Element element = AptContext.getContext().getTypes().asElement(item);
TypeRef typeRef = item.accept(new TypeRefTypeVisitor(context), 0);
if (typeRef instanceof ClassRef && element instanceof TypeElement) {
TypeElement typeElement = (TypeElement) element;
String fqcn = typeElement.toString();
context.getReferences().add((TypeElement) element);
return new ClassRefBuilder((ClassRef) typeRef).withNewFullyQualifiedName(fqcn).build();
}
return typeRef;
}
use of io.sundr.model.ClassRef 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.ClassRef in project sundrio by sundrio.
the class BuilderUtils method fullyQualifiedNameDiff.
public static String fullyQualifiedNameDiff(TypeRef typeRef, TypeDef originType) {
Map<String, String> map = DefinitionRepository.getRepository().getReferenceMap();
String currentPackage = originType != null ? originType.getPackageName() : null;
if (typeRef instanceof ClassRef) {
TypeRef unwrapped = TypeAs.combine(UNWRAP_COLLECTION_OF, UNWRAP_ARRAY_OF, UNWRAP_OPTIONAL_OF, UNWRAP_MAP_VALUE_OF).apply(typeRef);
if (unwrapped instanceof ClassRef) {
ClassRef classRef = (ClassRef) unwrapped;
String candidateFqn = classRef.getFullyQualifiedName().replace(classRef.getPackageName(), currentPackage);
// If classRef is inside the current package.
if (candidateFqn.equals(classRef.getFullyQualifiedName())) {
return "";
}
// If candidate is imported and different that the actual name, do a diff
if (originType.getImports().contains(candidateFqn) && !classRef.getFullyQualifiedName().equals(candidateFqn)) {
return capitalizeFirst(Types.fullyQualifiedNameDiff(candidateFqn, classRef.getFullyQualifiedName()));
}
// If not then we compare against what has been found in the map.
String fqcn = map.get(classRef.getName());
TypeDef mainDef = fqcn != null ? DefinitionRepository.getRepository().getDefinition(fqcn) : null;
boolean mainBuildable = mainDef != null ? isBuildable(mainDef) : false;
if (fqcn == null) {
System.out.println("Warning: Expected to find class with name:" + classRef.getName());
} else if (!classRef.getFullyQualifiedName().equals(fqcn) && mainBuildable) {
return capitalizeFirst(Types.fullyQualifiedNameDiff(fqcn, classRef.getFullyQualifiedName()));
}
}
}
return "";
}
use of io.sundr.model.ClassRef in project sundrio by sundrio.
the class BuilderUtils method hasDefaultConstructor.
/**
* Checks if there is a default constructor available.
*
* @param item The clazz to check.
* @return True if default constructor is found, false otherwise.
*/
public static boolean hasDefaultConstructor(TypeRef item) {
DefinitionRepository repository = DefinitionRepository.getRepository();
TypeDef def = repository.getDefinition(item);
if (def == null && item instanceof ClassRef) {
def = GetDefinition.of((ClassRef) item);
}
return hasDefaultConstructor(def);
}
Aggregations