use of io.sundr.model.Method 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.Method 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;
}
use of io.sundr.model.Method in project sundrio by sundrio.
the class MethodDirective method render.
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException {
String block = "";
Method method = null;
Boolean isInterface = false;
int level = 1;
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
if (node.jjtGetChild(i) != null) {
if (!(node.jjtGetChild(i) instanceof ASTBlock)) {
// reading and casting inline parameters
if (i == 0) {
method = (Method) node.jjtGetChild(i).value(context);
} else if (i == 1) {
isInterface = (Boolean) node.jjtGetChild(i).value(context);
} else if (i == 2) {
level = (Integer) node.jjtGetChild(i).value(context);
} else {
break;
}
} else {
// reading block content and rendering it
StringWriter blockContent = new StringWriter();
node.jjtGetChild(i).render(context, blockContent);
block = blockContent.toString();
break;
}
}
}
boolean hasBody = !method.isAbstract() && (!isInterface || method.isDefaultMethod());
writeMethod(writer, method, block, hasBody, level);
return true;
}
use of io.sundr.model.Method in project sundrio by sundrio.
the class BuilderUtils method getInlineableConstructors.
public static Set<Method> getInlineableConstructors(Property property) {
Set<Method> result = new HashSet<Method>();
TypeRef typeRef = property.getTypeRef();
TypeRef unwrapped = TypeAs.combine(TypeAs.UNWRAP_COLLECTION_OF, TypeAs.UNWRAP_ARRAY_OF, TypeAs.UNWRAP_OPTIONAL_OF).apply(typeRef);
if (unwrapped instanceof ClassRef) {
ClassRef classRef = (ClassRef) unwrapped;
// We need to handle `new String(String str)` as a special case of Inlineable constructor and deprecate Inlineables of it before we acutally remove it, so here goes...
if (STRING_REF.equals(typeRef)) {
result.add(new MethodBuilder().withName("String").addNewArgument().withName("s").withTypeRef(classRef).endArgument().build());
return result;
}
// We only want to inline non java types
String pkg = Nameable.getPackageName(((ClassRef) unwrapped).getFullyQualifiedName());
if (!Stream.of(NON_INLINABLE_PACKAGES).filter(s -> pkg.startsWith(s)).findAny().isPresent()) {
for (Method candidate : GetDefinition.of((ClassRef) unwrapped).getConstructors()) {
if (isInlineable(candidate)) {
result.add(candidate);
}
}
}
}
return result;
}
use of io.sundr.model.Method in project sundrio by sundrio.
the class ToPojo method readObjectArrayValue.
/**
* Returns the string representation of the code that reads an object array property.
*
* @param ref The reference.
* @param source The type of the reference.
* @param property The property to read.
* @return The code.
*/
private static String readObjectArrayValue(String ref, TypeDef source, Property property) {
StringBuilder sb = new StringBuilder();
Method getter = getterOf(source, property);
TypeRef getterTypeRef = getter.getReturnType();
TypeRef propertyTypeRef = property.getTypeRef();
if (propertyTypeRef instanceof ClassRef && getterTypeRef instanceof ClassRef) {
String nextRef = variables.pop();
try {
TypeDef propertyType = GetDefinition.of((ClassRef) propertyTypeRef);
TypeDef getterType = GetDefinition.of((ClassRef) getterTypeRef);
if (Types.STRING.equals(getterType)) {
sb.append(ref + " instanceof Map ? toStringArray(((Map)" + ref + ").get(\"" + getter.getName() + "\")) : toStringArray(" + ref + ")");
} else {
sb.append(indent(ref)).append("Arrays.stream(");
sb.append("(Map[])(" + ref + " instanceof Map ? ((Map)" + ref + ").getOrDefault(\"" + getter.getName() + "\" , new Map[0]) : new Map[0]))");
sb.append(".map(").append(nextRef).append(" ->").append(convertMap(nextRef, getterType, propertyType)).append(")");
sb.append(".toArray(size-> new " + propertyType.getFullyQualifiedName() + "[size])");
}
} finally {
variables.push(nextRef);
}
return sb.toString();
}
throw new IllegalArgumentException("Expected an object property and a matching object getter!!");
}
Aggregations