Search in sources :

Example 6 with ClassRef

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.");
}
Also used : ClassRef(io.sundr.model.ClassRef) HashMap(java.util.HashMap) AnnotationRefBuilder(io.sundr.model.AnnotationRefBuilder) TypeRef(io.sundr.model.TypeRef) TypeRefTypeVisitor(io.sundr.adapter.apt.visitors.TypeRefTypeVisitor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with ClassRef

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;
}
Also used : ClassRef(io.sundr.model.ClassRef) NoType(javax.lang.model.type.NoType) TypeRef(io.sundr.model.TypeRef) TypeElement(javax.lang.model.element.TypeElement) VoidRef(io.sundr.model.VoidRef) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ClassRefBuilder(io.sundr.model.ClassRefBuilder) TypeRefTypeVisitor(io.sundr.adapter.apt.visitors.TypeRefTypeVisitor)

Example 8 with ClassRef

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;
}
Also used : RefModel(io.swagger.models.RefModel) ClassRef(io.sundr.model.ClassRef) ComposedModel(io.swagger.models.ComposedModel) ArrayList(java.util.ArrayList) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) Model(io.swagger.models.Model) Method(io.sundr.model.Method) Property(io.sundr.model.Property) TypeDefBuilder(io.sundr.model.TypeDefBuilder)

Example 9 with ClassRef

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 "";
}
Also used : ClassRef(io.sundr.model.ClassRef) TypeDef(io.sundr.model.TypeDef) TypeRef(io.sundr.model.TypeRef)

Example 10 with ClassRef

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);
}
Also used : DefinitionRepository(io.sundr.model.repo.DefinitionRepository) TypeDef(io.sundr.model.TypeDef) ClassRef(io.sundr.model.ClassRef)

Aggregations

ClassRef (io.sundr.model.ClassRef)68 TypeDef (io.sundr.model.TypeDef)41 TypeRef (io.sundr.model.TypeRef)35 RichTypeDef (io.sundr.model.RichTypeDef)22 ClassRefBuilder (io.sundr.model.ClassRefBuilder)21 ArrayList (java.util.ArrayList)21 Property (io.sundr.model.Property)19 Method (io.sundr.model.Method)18 TypeDefBuilder (io.sundr.model.TypeDefBuilder)18 Test (org.junit.Test)17 List (java.util.List)12 AnnotationRef (io.sundr.model.AnnotationRef)11 MethodBuilder (io.sundr.model.MethodBuilder)11 Collectors (java.util.stream.Collectors)11 TypedVisitor (io.sundr.builder.TypedVisitor)10 TypeParamDef (io.sundr.model.TypeParamDef)10 LinkedHashSet (java.util.LinkedHashSet)10 PropertyBuilder (io.sundr.model.PropertyBuilder)9 Map (java.util.Map)9 AttributeKey (io.sundr.model.AttributeKey)8