Search in sources :

Example 6 with TypeRef

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

the class TypeRefTypeVisitor method visitDeclared.

public TypeRef visitDeclared(DeclaredType t, Integer dimension) {
    List<TypeRef> arguments = new ArrayList<TypeRef>();
    for (TypeMirror typeMirror : t.getTypeArguments()) {
        TypeRef arg = typeMirror.accept(this, dimension);
        if (arg != null) {
            arguments.add(arg);
        }
    }
    TypeElement element = (TypeElement) t.asElement();
    // TODO: need a cleaner way to get this registered.
    if (!context.getDefinitionRepository().hasDefinition(element.toString())) {
        context.getReferences().add(element);
    }
    String fqcn = element.toString();
    return new ClassRefBuilder().withFullyQualifiedName(fqcn).withDimensions(dimension).withArguments(arguments).build();
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeRef(io.sundr.model.TypeRef) TypeElement(javax.lang.model.element.TypeElement) ClassRefBuilder(io.sundr.model.ClassRefBuilder) ArrayList(java.util.ArrayList)

Example 7 with TypeRef

use of io.sundr.model.TypeRef 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 8 with TypeRef

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

the class AnnotationMirrorToAnnotationRef method checkEntry.

/**
 * Checks if there is error extracting the specified annotation parameter
 *
 * @param entry the entry that represents the annotation param.
 */
private void checkEntry(Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry) {
    ExecutableElement key = entry.getKey();
    Object value = entry.getValue().getValue();
    if (ERROR.equals(value)) {
        TypeRef returnType = referenceAdapterFunction.apply(key.getReturnType());
        if (returnType.equals(Types.CLASS)) {
            throw new SundrException("Failed to extract class parameter from annotation. " + Messages.POTENTIAL_UNRESOLVED_SYMBOL);
        }
    }
}
Also used : TypeRef(io.sundr.model.TypeRef) ExecutableElement(javax.lang.model.element.ExecutableElement) SundrException(io.sundr.SundrException)

Example 9 with TypeRef

use of io.sundr.model.TypeRef 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 10 with TypeRef

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

the class AbstractAdapterTest method testWithPrimitiveArray.

@Test
public void testWithPrimitiveArray() {
    T input = getInput(ClassWithPrimitiveArray.class);
    TypeDef typeDef = Adapters.adaptType(input, getContext());
    assertNotNull(typeDef);
    assertEquals("ClassWithPrimitiveArray", typeDef.getName());
    assertEquals(1, typeDef.getProperties().size());
    TypeRef typeRef = typeDef.getProperties().iterator().next().getTypeRef();
    assertTrue(typeRef instanceof PrimitiveRef);
    assertEquals(1, ((PrimitiveRef) typeRef).getDimensions());
}
Also used : RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) TypeRef(io.sundr.model.TypeRef) PrimitiveRef(io.sundr.model.PrimitiveRef) Test(org.junit.Test)

Aggregations

TypeRef (io.sundr.model.TypeRef)45 ClassRef (io.sundr.model.ClassRef)33 TypeDef (io.sundr.model.TypeDef)23 ArrayList (java.util.ArrayList)16 ClassRefBuilder (io.sundr.model.ClassRefBuilder)14 Method (io.sundr.model.Method)13 RichTypeDef (io.sundr.model.RichTypeDef)13 TypeDefBuilder (io.sundr.model.TypeDefBuilder)12 Property (io.sundr.model.Property)10 MethodBuilder (io.sundr.model.MethodBuilder)9 PropertyBuilder (io.sundr.model.PropertyBuilder)9 TypeParamRef (io.sundr.model.TypeParamRef)9 TypedVisitor (io.sundr.builder.TypedVisitor)8 AnnotationRef (io.sundr.model.AnnotationRef)8 List (java.util.List)8 TypeParamDef (io.sundr.model.TypeParamDef)7 HashMap (java.util.HashMap)7 AttributeKey (io.sundr.model.AttributeKey)6 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6