Search in sources :

Example 1 with TypeParamDef

use of io.sundr.model.TypeParamDef in project sundrio by sundrio.

the class ClassToTypeDef method apply.

@Override
public TypeDef apply(Class item) {
    if (Object.class.equals(item)) {
        return TypeDef.OBJECT;
    }
    Kind kind = classToKind.apply(item);
    List<ClassRef> extendsList = new ArrayList<>();
    List<ClassRef> implementsList = new ArrayList<>();
    List<Property> properties = new ArrayList<>();
    List<Method> methods = new ArrayList<>();
    List<Method> constructors = new ArrayList<>();
    List<TypeParamDef> parameters = new ArrayList<>();
    if (item.getSuperclass() != null) {
        extendsList.add((ClassRef) typeToTypeRef.apply(item.getGenericSuperclass()));
        references.add(item.getSuperclass());
    }
    for (Class interfaceClass : item.getInterfaces()) {
        references.add(interfaceClass);
    }
    for (Type interfaceClass : item.getGenericInterfaces()) {
        TypeRef ref = typeToTypeRef.apply(interfaceClass);
        if (ref instanceof ClassRef) {
            implementsList.add((ClassRef) ref);
        }
    }
    constructors.addAll(getConstructors(item, references));
    methods.addAll(getMethods(item, references));
    properties.addAll(getProperties(item, references));
    for (TypeVariable typeVariable : item.getTypeParameters()) {
        List<ClassRef> bounds = new ArrayList<>();
        for (Type boundType : typeVariable.getBounds()) {
            TypeRef typeRef = typeToTypeRef.apply(boundType);
            if (typeRef instanceof ClassRef) {
                bounds.add((ClassRef) typeRef);
            }
        }
        parameters.add(new TypeParamDefBuilder().withName(typeVariable.getName()).withBounds(bounds).build());
    }
    String outerFQCN = item.getDeclaringClass() != null ? item.getDeclaringClass().getName() : null;
    TypeDef result = context.getDefinitionRepository().register(new TypeDefBuilder().withKind(kind).withOuterTypeName(outerFQCN).withName(item.getSimpleName()).withPackageName(item.getPackage() != null ? item.getPackage().getName() : null).withModifiers(item.getModifiers()).withParameters(parameters).withConstructors(constructors).withMethods(methods).withProperties(properties).withExtendsList(extendsList).withImplementsList(implementsList).build());
    Set<Class> copy = new HashSet<>(references);
    copy.stream().peek(c -> references.remove(c)).filter(c -> !c.equals(item)).filter(c -> !c.getName().startsWith("sun.") && !c.getName().toString().startsWith("com.sun.")).forEach(c -> {
        String referenceFQCN = c.getName().replaceAll(Pattern.quote("$"), ".");
        context.getDefinitionRepository().registerIfAbsent(referenceFQCN, () -> apply(c));
    });
    return result;
}
Also used : TypeParamDefBuilder(io.sundr.model.TypeParamDefBuilder) AnnotationRefBuilder(io.sundr.model.AnnotationRefBuilder) HashMap(java.util.HashMap) TypeDefBuilder(io.sundr.model.TypeDefBuilder) Function(java.util.function.Function) AdapterContext(io.sundr.adapter.api.AdapterContext) Attributeable(io.sundr.model.Attributeable) ArrayList(java.util.ArrayList) AttributeKey(io.sundr.model.AttributeKey) ClassRef(io.sundr.model.ClassRef) HashSet(java.util.HashSet) Map(java.util.Map) TypeVariable(java.lang.reflect.TypeVariable) Set(java.util.Set) Method(io.sundr.model.Method) TypeRef(io.sundr.model.TypeRef) Field(java.lang.reflect.Field) AnnotationRef(io.sundr.model.AnnotationRef) Kind(io.sundr.model.Kind) Collectors(java.util.stream.Collectors) Property(io.sundr.model.Property) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) Stream(java.util.stream.Stream) MethodBuilder(io.sundr.model.MethodBuilder) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) TypeDef(io.sundr.model.TypeDef) PropertyBuilder(io.sundr.model.PropertyBuilder) Pattern(java.util.regex.Pattern) TypeParamDef(io.sundr.model.TypeParamDef) AnnotatedElement(java.lang.reflect.AnnotatedElement) TypeParamDef(io.sundr.model.TypeParamDef) ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) ArrayList(java.util.ArrayList) Method(io.sundr.model.Method) TypeDefBuilder(io.sundr.model.TypeDefBuilder) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeDef(io.sundr.model.TypeDef) TypeVariable(java.lang.reflect.TypeVariable) Kind(io.sundr.model.Kind) TypeParamDefBuilder(io.sundr.model.TypeParamDefBuilder) Property(io.sundr.model.Property) HashSet(java.util.HashSet)

Example 2 with TypeParamDef

use of io.sundr.model.TypeParamDef in project sundrio by sundrio.

the class ClassToTypeDef method processMethod.

private void processMethod(Set<Class> references, java.lang.reflect.Executable method, List<AnnotationRef> annotationRefs, List<ClassRef> exceptionRefs, List<Property> arguments, List<TypeParamDef> parameters) {
    processAnnotatedElement(method, annotationRefs);
    for (Class exceptionType : method.getExceptionTypes()) {
        exceptionRefs.add((ClassRef) typeToTypeRef.apply(exceptionType));
    }
    for (int i = 1; i <= method.getGenericParameterTypes().length; i++) {
        Type argumentType = method.getGenericParameterTypes()[i - 1];
        arguments.add(new PropertyBuilder().withName(ARGUMENT_PREFIX + i).withTypeRef(typeToTypeRef.apply(argumentType)).build());
        if (argumentType instanceof Class) {
            references.add((Class) argumentType);
        }
    }
    for (Type type : method.getGenericParameterTypes()) {
        TypeParamDef typeParamDef = typeToTypeParamDef.apply(type);
        if (typeParamDef != null) {
            parameters.add(typeParamDef);
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeParamDef(io.sundr.model.TypeParamDef) PropertyBuilder(io.sundr.model.PropertyBuilder)

Example 3 with TypeParamDef

use of io.sundr.model.TypeParamDef in project sundrio by sundrio.

the class ClassTo method processMethod.

private static void processMethod(Set<Class> references, java.lang.reflect.Executable method, List<AnnotationRef> annotationRefs, List<ClassRef> exceptionRefs, List<Property> arguments, List<TypeParamDef> parameters) {
    processAnnotatedElement(method, annotationRefs);
    for (Class exceptionType : method.getExceptionTypes()) {
        exceptionRefs.add((ClassRef) TYPEREF.apply(exceptionType));
    }
    for (int i = 1; i <= method.getGenericParameterTypes().length; i++) {
        Type argumentType = method.getGenericParameterTypes()[i - 1];
        arguments.add(new PropertyBuilder().withName(ARGUMENT_PREFIX + i).withTypeRef(TYPEREF.apply(argumentType)).build());
        if (argumentType instanceof Class) {
            references.add((Class) argumentType);
        }
    }
    for (Type type : method.getGenericParameterTypes()) {
        TypeParamDef typeParamDef = TYPEPARAMDEF.apply(type);
        if (typeParamDef != null) {
            parameters.add(typeParamDef);
        }
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeParamDef(io.sundr.model.TypeParamDef) PropertyBuilder(io.sundr.model.PropertyBuilder)

Example 4 with TypeParamDef

use of io.sundr.model.TypeParamDef in project sundrio by sundrio.

the class Combine method extractParameters.

private static final Set<TypeParamDef> extractParameters(ClassRef classRef) {
    final Set<TypeParamDef> result = new LinkedHashSet<TypeParamDef>();
    final Set<TypeParamRef> refs = new LinkedHashSet<TypeParamRef>();
    ClassRef ignored = new ClassRefBuilder(classRef).accept(new TypeParamDefColletor(result)).accept(new TypeParamRefColletor(refs)).build();
    for (TypeParamRef typeParamRef : refs) {
        result.add(new TypeParamDefBuilder().withName(typeParamRef.getName()).withAttributes(typeParamRef.getAttributes()).build());
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TypeParamRef(io.sundr.model.TypeParamRef) TypeParamDef(io.sundr.model.TypeParamDef) TypeParamRefColletor(io.sundr.dsl.internal.visitors.TypeParamRefColletor) ClassRef(io.sundr.model.ClassRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) TypeParamDefBuilder(io.sundr.model.TypeParamDefBuilder) TypeParamDefColletor(io.sundr.dsl.internal.visitors.TypeParamDefColletor)

Example 5 with TypeParamDef

use of io.sundr.model.TypeParamDef in project sundrio by sundrio.

the class TypeDefUtils method executableToInterface.

/**
 * Convert an {@link javax.lang.model.element.ExecutableElement} to a {@link io.sundr.model.TypeDef}
 *
 * @param context The context of the operation.
 * @param executableElement The target element.
 * @return An instance of {@link io.sundr.model.TypeDef} that describes the interface.
 */
public static TypeDef executableToInterface(DslContext context, ExecutableElement executableElement) {
    // Do generate the interface
    Boolean multiple = executableElement.getAnnotation(Multiple.class) != null;
    Boolean isEntryPoint = executableElement.getAnnotation(EntryPoint.class) != null;
    Boolean isTerminal = executableElement.getAnnotation(Terminal.class) != null || !isVoid(executableElement);
    Set<String> classes = new HashSet<String>();
    Set<String> keywords = new HashSet<String>();
    Set<String> methods = new HashSet<String>();
    TransitionFilter filter = executableElement.getAnnotation(Or.class) != null ? new OrTransitionFilter(context.getToRequiresAll().apply(executableElement), context.getToRequiresAny().apply(executableElement), context.getToRequiresOnly().apply(executableElement), context.getToRequiresNoneOf().apply(executableElement)) : new AndTransitionFilter(context.getToRequiresAll().apply(executableElement), context.getToRequiresAny().apply(executableElement), context.getToRequiresOnly().apply(executableElement), context.getToRequiresNoneOf().apply(executableElement));
    for (String clazz : context.getToClasses().apply(executableElement)) {
        classes.add(clazz);
    }
    for (String keyword : context.getToKeywords().apply(executableElement)) {
        keywords.add(keyword);
    }
    // Let's add the name of the method as a keyword to make things simpler
    methods.add(executableElement.getSimpleName().toString());
    TypeRef returnType;
    if (isTerminal(executableElement)) {
        returnType = isVoid(executableElement) ? VOID_REF : Adapters.adaptReference(executableElement.getReturnType(), context.getAptContext());
    } else {
        returnType = TRANSPARENT_REF;
    }
    InterfaceName targetInterfaceName = executableElement.getAnnotation(InterfaceName.class);
    MethodName tagetMethodName = executableElement.getAnnotation(MethodName.class);
    Begin begin = executableElement.getAnnotation(Begin.class);
    End end = executableElement.getAnnotation(End.class);
    if (begin != null) {
        keywords.add(begin.value());
    }
    if (end != null) {
        keywords.add(end.value());
    }
    String methodName = tagetMethodName != null ? tagetMethodName.value() : executableElement.getSimpleName().toString();
    String beginScope = begin != null ? begin.value() : null;
    String endScope = end != null ? end.value() : null;
    TypeParamDef paremeterType = Generics.MAP.apply(returnType);
    Method sourceMethod = Adapters.adaptMethod(executableElement, context.getAptContext());
    List<AnnotationRef> annotations = new ArrayList<AnnotationRef>();
    for (AnnotationRef candidate : sourceMethod.getAnnotations()) {
        if (!candidate.getClassRef().getFullyQualifiedName().startsWith("io.sundr")) {
            annotations.add(candidate);
        }
    }
    Method targetMethod = new MethodBuilder(sourceMethod).withAnnotations(annotations).withModifiers(Types.modifiersToInt(Modifier.PUBLIC)).withReturnType(paremeterType.toReference()).withName(methodName).build();
    String interfaceName = targetInterfaceName != null ? targetInterfaceName.value() : toInterfaceName(targetMethod.getName());
    return new TypeDefBuilder().withPackageName(Apt.getPackageElement(executableElement).toString()).withName(interfaceName).withParameters(paremeterType).withKind(Kind.INTERFACE).withModifiers(Types.modifiersToInt(Modifier.PUBLIC)).addToAttributes(ORIGINAL_RETURN_TYPE, returnType).addToAttributes(IS_ENTRYPOINT, isEntryPoint).addToAttributes(IS_TERMINAL, isTerminal).addToAttributes(IS_GENERIC, Boolean.FALSE).addToAttributes(CLASSES, classes).addToAttributes(KEYWORDS, keywords).addToAttributes(METHODS, methods).addToAttributes(BEGIN_SCOPE, beginScope).addToAttributes(END_SCOPE, endScope).addToAttributes(FILTER, filter).addToAttributes(CARDINALITY_MULTIPLE, multiple).addToAttributes(METHOD_NAME, methodName).addToMethods(targetMethod).build();
}
Also used : AndTransitionFilter(io.sundr.dsl.internal.element.functions.filter.AndTransitionFilter) TypeParamDef(io.sundr.model.TypeParamDef) Multiple(io.sundr.dsl.annotations.Multiple) TypeRef(io.sundr.model.TypeRef) ArrayList(java.util.ArrayList) EntryPoint(io.sundr.dsl.annotations.EntryPoint) Method(io.sundr.model.Method) TypeDefBuilder(io.sundr.model.TypeDefBuilder) MethodBuilder(io.sundr.model.MethodBuilder) OrTransitionFilter(io.sundr.dsl.internal.element.functions.filter.OrTransitionFilter) AndTransitionFilter(io.sundr.dsl.internal.element.functions.filter.AndTransitionFilter) TransitionFilter(io.sundr.dsl.internal.element.functions.filter.TransitionFilter) OrTransitionFilter(io.sundr.dsl.internal.element.functions.filter.OrTransitionFilter) Begin(io.sundr.dsl.annotations.Begin) InterfaceName(io.sundr.dsl.annotations.InterfaceName) End(io.sundr.dsl.annotations.End) MethodName(io.sundr.dsl.annotations.MethodName) AnnotationRef(io.sundr.model.AnnotationRef) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

TypeParamDef (io.sundr.model.TypeParamDef)13 TypeDefBuilder (io.sundr.model.TypeDefBuilder)7 AnnotationRef (io.sundr.model.AnnotationRef)6 ClassRef (io.sundr.model.ClassRef)6 Method (io.sundr.model.Method)6 TypeDef (io.sundr.model.TypeDef)6 TypeRef (io.sundr.model.TypeRef)6 ArrayList (java.util.ArrayList)6 MethodBuilder (io.sundr.model.MethodBuilder)5 Property (io.sundr.model.Property)5 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)5 AttributeKey (io.sundr.model.AttributeKey)4 PropertyBuilder (io.sundr.model.PropertyBuilder)4 Kind (io.sundr.model.Kind)3 TypeParamDefBuilder (io.sundr.model.TypeParamDefBuilder)3 ParameterizedType (java.lang.reflect.ParameterizedType)3 Type (java.lang.reflect.Type)3 TypeParamDefColletor (io.sundr.dsl.internal.visitors.TypeParamDefColletor)2