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;
}
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);
}
}
}
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);
}
}
}
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;
}
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();
}
Aggregations