use of io.sundr.model.Kind 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;
}
use of io.sundr.model.Kind 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;
}
use of io.sundr.model.Kind in project sundrio by sundrio.
the class TypeDeclarationToTypeDef method apply.
@Override
public TypeDef apply(TypeDeclaration type) {
if (type instanceof ClassOrInterfaceDeclaration) {
ClassOrInterfaceDeclaration decl = (ClassOrInterfaceDeclaration) type;
Kind kind = decl.isInterface() ? Kind.INTERFACE : Kind.CLASS;
List<TypeParamDef> parameters = new ArrayList<TypeParamDef>();
List<ClassRef> extendsList = new ArrayList<ClassRef>();
List<ClassRef> implementsList = new ArrayList<ClassRef>();
List<Property> properties = new ArrayList<Property>();
List<Method> methods = new ArrayList<Method>();
List<Method> constructors = new ArrayList<Method>();
List<AnnotationRef> annotations = new ArrayList<AnnotationRef>();
for (AnnotationExpr annotationExpr : decl.getAnnotations()) {
annotations.add(ANNOTATIONREF.apply(annotationExpr));
}
for (TypeParameter typeParameter : decl.getTypeParameters()) {
parameters.add(typeParameterToTypeParamDef.apply(typeParameter));
}
for (ClassOrInterfaceType classOrInterfaceType : decl.getExtends()) {
extendsList.add((ClassRef) classOrInterfaceToTypeRef.apply(classOrInterfaceType));
}
for (ClassOrInterfaceType classOrInterfaceType : decl.getImplements()) {
implementsList.add((ClassRef) classOrInterfaceToTypeRef.apply(classOrInterfaceType));
}
for (BodyDeclaration bodyDeclaration : decl.getMembers()) {
if (bodyDeclaration instanceof FieldDeclaration) {
FieldDeclaration fieldDeclaration = (FieldDeclaration) bodyDeclaration;
for (VariableDeclarator var : fieldDeclaration.getVariables()) {
TypeRef fieldDeclRef = typeToTypeRef.apply(fieldDeclaration.getType());
TypeRef typeRef = checkAgainstTypeParamRef(fieldDeclRef, parameters);
properties.add(new PropertyBuilder().withName(var.getId().getName()).withTypeRef(typeRef).withModifiers(fieldDeclaration.getModifiers()).addToAttributes(Attributeable.INIT, var.getInit() != null ? var.getInit().toStringWithoutComments() : null).build());
}
} else if (bodyDeclaration instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
List<Property> arguments = new ArrayList<Property>();
List<ClassRef> exceptions = new ArrayList<ClassRef>();
List<AnnotationRef> methodAnnotations = new ArrayList<AnnotationRef>();
for (AnnotationExpr annotationExpr : methodDeclaration.getAnnotations()) {
methodAnnotations.add(ANNOTATIONREF.apply(annotationExpr));
}
for (ReferenceType referenceType : methodDeclaration.getThrows()) {
TypeRef exceptionRef = typeToTypeRef.apply(referenceType.getType());
if (exceptionRef instanceof ClassRef) {
exceptions.add((ClassRef) exceptionRef);
}
}
Boolean preferVarArg = false;
for (Parameter parameter : methodDeclaration.getParameters()) {
List<AnnotationRef> paramAnnotations = new ArrayList<AnnotationRef>();
for (AnnotationExpr annotationExpr : parameter.getAnnotations()) {
paramAnnotations.add(ANNOTATIONREF.apply(annotationExpr));
}
TypeRef typeRef = typeToTypeRef.apply(parameter.getType());
if (parameter.isVarArgs()) {
preferVarArg = true;
typeRef = typeRef.withDimensions(typeRef.getDimensions() + 1);
}
arguments.add(new PropertyBuilder().withName(parameter.getId().getName()).withTypeRef(typeRef).withModifiers(parameter.getModifiers()).withAnnotations(paramAnnotations).build());
}
List<TypeParamDef> typeParamDefs = new ArrayList<TypeParamDef>();
for (TypeParameter typeParameter : methodDeclaration.getTypeParameters()) {
typeParamDefs.add(typeParameterToTypeParamDef.apply(typeParameter));
}
TypeRef returnType = checkAgainstTypeParamRef(typeToTypeRef.apply(methodDeclaration.getType()), parameters);
methods.add(new MethodBuilder().withName(methodDeclaration.getName()).withDefaultMethod(methodDeclaration.isDefault()).withModifiers(methodDeclaration.getModifiers()).withParameters(typeParamDefs).withVarArgPreferred(preferVarArg).withReturnType(returnType).withExceptions(exceptions).withArguments(arguments).withAnnotations(methodAnnotations).withBlock(BLOCK.apply(methodDeclaration.getBody())).build());
} else if (bodyDeclaration instanceof ConstructorDeclaration) {
ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration) bodyDeclaration;
List<Property> arguments = new ArrayList<Property>();
List<ClassRef> exceptions = new ArrayList<ClassRef>();
List<AnnotationRef> ctorAnnotations = new ArrayList<AnnotationRef>();
for (AnnotationExpr annotationExpr : constructorDeclaration.getAnnotations()) {
ctorAnnotations.add(ANNOTATIONREF.apply(annotationExpr));
}
for (ReferenceType referenceType : constructorDeclaration.getThrows()) {
TypeRef exceptionRef = typeToTypeRef.apply(referenceType.getType());
exceptions.add((ClassRef) exceptionRef);
}
for (Parameter parameter : constructorDeclaration.getParameters()) {
List<AnnotationRef> ctorParamAnnotations = new ArrayList<AnnotationRef>();
for (AnnotationExpr annotationExpr : parameter.getAnnotations()) {
ctorParamAnnotations.add(ANNOTATIONREF.apply(annotationExpr));
}
TypeRef typeRef = checkAgainstTypeParamRef(typeToTypeRef.apply(parameter.getType()), parameters);
arguments.add(new PropertyBuilder().withName(parameter.getId().getName()).withTypeRef(typeRef).withModifiers(parameter.getModifiers()).withAnnotations(ctorParamAnnotations).build());
}
constructors.add(new MethodBuilder().withModifiers(constructorDeclaration.getModifiers()).withExceptions(exceptions).withArguments(arguments).withAnnotations(ctorAnnotations).withBlock(BLOCK.apply(constructorDeclaration.getBlock())).build());
}
}
return context.getDefinitionRepository().register(new TypeDefBuilder().withKind(kind).withPackageName(PACKAGENAME.apply(type)).withName(decl.getName()).withModifiers(type.getModifiers()).withParameters(parameters).withExtendsList(extendsList).withImplementsList(implementsList).withProperties(properties).withMethods(methods).withConstructors(constructors).withAnnotations(annotations).addToAttributes(TypeDef.ALSO_IMPORT, IMPORTS.apply(type)).build());
}
if (type instanceof AnnotationDeclaration) {
AnnotationDeclaration decl = (AnnotationDeclaration) type;
Kind kind = Kind.ANNOTATION;
List<Method> methods = new ArrayList<Method>();
for (BodyDeclaration bodyDeclaration : decl.getMembers()) {
if (bodyDeclaration instanceof AnnotationMemberDeclaration) {
Map<AttributeKey, Object> attributes = new HashMap<>();
AnnotationMemberDeclaration annotationMemberDeclaration = (AnnotationMemberDeclaration) bodyDeclaration;
if (annotationMemberDeclaration.getDefaultValue() != null) {
attributes.put(Attributeable.DEFAULT_VALUE, annotationMemberDeclaration.getDefaultValue().toString());
}
TypeRef returnType = typeToTypeRef.apply(annotationMemberDeclaration.getType());
methods.add(new MethodBuilder().withName(annotationMemberDeclaration.getName()).withModifiers(annotationMemberDeclaration.getModifiers()).withReturnType(returnType).withAttributes(attributes).build());
}
}
List<AnnotationRef> annotations = new ArrayList<AnnotationRef>();
for (AnnotationExpr annotationExpr : decl.getAnnotations()) {
annotations.add(ANNOTATIONREF.apply(annotationExpr));
}
return context.getDefinitionRepository().register(new TypeDefBuilder().withKind(kind).withPackageName(PACKAGENAME.apply(type)).withName(decl.getName()).withModifiers(type.getModifiers()).withMethods(methods).withAnnotations(annotations).addToAttributes(TypeDef.ALSO_IMPORT, IMPORTS.apply(type)).build());
}
throw new IllegalArgumentException("Unsupported TypeDeclaration:[" + type + "].");
}
Aggregations