Support simple generics

This commit is contained in:
Andrea Cavalli 2022-02-28 13:29:58 +01:00
parent a45f1908f1
commit bda42291c7
2 changed files with 31 additions and 6 deletions

View File

@ -1,7 +1,32 @@
package it.cavallium.data.generator;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
public class CustomTypesConfiguration {
public String javaClass;
private String javaClass;
public String serializer;
public void setJavaClass(String javaClass) {
this.javaClass = javaClass;
}
public TypeName getJavaClassType() {
int indexOfGeneric;
if ((indexOfGeneric = javaClass.indexOf("<")) == -1) {
return ClassName.bestGuess(javaClass);
} else {
var rawTypesArray = javaClass.substring(indexOfGeneric + 1, javaClass.length() - 1).split(",");
var genericsResult = new TypeName[rawTypesArray.length];
int i = 0;
for (String rawType : rawTypesArray) {
genericsResult[i] = ClassName.bestGuess(rawType);
i++;
}
var base = ClassName.bestGuess(javaClass.substring(0, indexOfGeneric));
return ParameterizedTypeName.get(base, genericsResult);
}
}
}

View File

@ -724,14 +724,14 @@ public class SourcesGenerator {
.build()
);
typeMustGenerateSerializer.put(key, false);
typeTypes.put(key, ClassName.bestGuess(customTypeConfiguration.javaClass));
typeTypes.put(key, customTypeConfiguration.getJavaClassType());
typeFamily.put(key, Family.OTHER);
if (nextVersionCustomTypeConfiguration.isPresent()) {
nextVersionTypeTypes.put(key, ClassName.bestGuess(nextVersionCustomTypeConfiguration.get().javaClass));
nextVersionTypeTypes.put(key, nextVersionCustomTypeConfiguration.get().getJavaClassType());
nextVersionTypeFamily.put(key, Family.OTHER);
}
var arrayClassName = ClassName.bestGuess(customTypeConfiguration.javaClass);
var arrayClassName = customTypeConfiguration.getJavaClassType();
var neededTypes = registerNeededTypes(versionConfiguration,
Family.OTHER,
key,
@ -2581,8 +2581,8 @@ public class SourcesGenerator {
HashMap<String, Family> typeFamily,
@Nullable HashMap<String, TypeName> nextVersionTypeTypes,
@Nullable HashMap<String, Family> nextVersionTypeFamily,
Supplier<ClassName> arrayClassName,
Supplier<ClassName> nextArrayClassName) {
Supplier<TypeName> arrayClassName,
Supplier<TypeName> nextArrayClassName) {
// Check if the nullable type is needed
boolean nullableTypeNeeded = versionConfiguration.classes
.values()