use of io.sundr.model.TypeDefBuilder 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.TypeDefBuilder in project sundrio by sundrio.
the class TemplateTransformationProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
Elements elements = processingEnv.getElementUtils();
Types types = processingEnv.getTypeUtils();
Filer filer = processingEnv.getFiler();
AptContext aptContext = AptContext.create(elements, types, DefinitionRepository.getRepository());
Map<TemplateTransformation, Map<String, TypeDef>> annotatedTypes = new HashMap<>();
for (TypeElement typeElement : annotations) {
for (Element element : env.getElementsAnnotatedWith(typeElement)) {
TemplateTransformations transformations = element.getAnnotation(TemplateTransformations.class);
TemplateTransformation transformation = element.getAnnotation(TemplateTransformation.class);
List<TemplateTransformation> all = new ArrayList<>();
if (transformation != null) {
all.add(transformation);
}
if (transformations != null) {
for (TemplateTransformation t : transformations.value()) {
all.add(t);
}
}
TypeDef def = new TypeDefBuilder(Adapters.adaptType(Apt.getClassElement(element), aptContext)).build();
for (TemplateTransformation t : all) {
if (!annotatedTypes.containsKey(t)) {
annotatedTypes.put(t, new HashMap<>());
}
// If there are no selectors processes the annotated class
if (transformations == null) {
annotatedTypes.get(t).put(def.getFullyQualifiedName(), def);
} else if (transformations.annotations().length > 0) {
for (AnnotationSelector selector : transformations.annotations()) {
selectAnnotated(env, types, selector, annotatedTypes.get(t));
}
} else if (transformations.packages().length > 0) {
for (PackageSelector selector : transformations.packages()) {
selectPackages(elements, selector, annotatedTypes.get(t));
}
} else if (transformations.resources().length > 0) {
for (ResourceSelector selector : transformations.resources()) {
selectFromResource(elements, filer, selector, annotatedTypes.get(t));
}
} else {
annotatedTypes.get(t).put(def.getFullyQualifiedName(), def);
}
}
}
for (Map.Entry<TemplateTransformation, Map<String, TypeDef>> entry : annotatedTypes.entrySet()) {
TemplateTransformation transformation = entry.getKey();
Map<String, TypeDef> annotated = entry.getValue();
try {
if (transformation.gather()) {
URL templateUrl = readTemplateURL(filer, null, transformation.value());
TemplateRenderer<Map> renderer = TemplateRenderers.getTemplateRenderer(Map.class, templateUrl).orElseThrow(() -> new IllegalStateException("No template renderer found for:" + templateUrl));
CodeGenerator.newGenerator(Map.class).withRenderer(renderer).withOutput(new GenericAptOutput<Map>(filer, renderer, transformation.outputPath())).skipping(i -> false).generate(annotated);
} else {
for (TypeDef typeDef : annotated.values()) {
URL templateUrl = readTemplateURL(filer, typeDef.getPackageName(), transformation.value());
TemplateRenderer<TypeDef> renderer = TemplateRenderers.getTemplateRenderer(TypeDef.class, templateUrl).orElseThrow(() -> new IllegalStateException("No template renderer found for:" + templateUrl));
Function<TypeDef, String> identifier = t -> io.sundr.model.utils.Types.parseFullyQualifiedName(renderer.render(t));
CodeGenerator.newGenerator(TypeDef.class).withRenderer(renderer).withIdentifier(identifier).withOutput(new TypeDefAptOutput(filer, renderer)).skipping(t -> TypeLookup.lookup(identifier.apply(typeDef), AptContext.getContext()).isPresent()).generate(typeDef);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
return false;
}
use of io.sundr.model.TypeDefBuilder 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.TypeDefBuilder in project sundrio by sundrio.
the class BuildableProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
Elements elements = processingEnv.getElementUtils();
Types types = processingEnv.getTypeUtils();
Filer filer = processingEnv.getFiler();
BuilderContext ctx = null;
// First pass register all buildables
Set<TypeDef> buildables = new HashSet<>();
for (TypeElement typeElement : annotations) {
for (Element element : env.getElementsAnnotatedWith(typeElement)) {
Buildable buildable = element.getAnnotation(Buildable.class);
if (buildable == null) {
continue;
}
AptContext aptContext = AptContext.create(elements, types, DefinitionRepository.getRepository());
ctx = BuilderContextManager.create(elements, types, buildable.validationEnabled(), buildable.generateBuilderPackage(), buildable.builderPackage());
TypeDef b = new TypeDefBuilder(Adapters.adaptType(Apt.getClassElement(element), aptContext)).addToAttributes(BUILDABLE, buildable).addToAttributes(EDITABLE_ENABLED, buildable.editableEnabled()).addToAttributes(VALIDATION_ENABLED, buildable.validationEnabled()).accept(new Visitor<PropertyBuilder>() {
@Override
public void visit(PropertyBuilder builder) {
builder.addToAttributes(LAZY_COLLECTIONS_INIT_ENABLED, buildable.lazyCollectionInitEnabled());
builder.addToAttributes(LAZY_MAP_INIT_ENABLED, buildable.lazyMapInitEnabled());
}
}).build();
ctx.getDefinitionRepository().register(b);
ctx.getBuildableRepository().register(b);
buildables.add(b);
for (TypeElement ref : BuilderUtils.getBuildableReferences(ctx, buildable)) {
TypeDef r = new TypeDefBuilder(Adapters.adaptType(Apt.getClassElement(ref), aptContext)).addToAttributes(BUILDABLE, buildable).addToAttributes(EDITABLE_ENABLED, buildable.editableEnabled()).addToAttributes(VALIDATION_ENABLED, buildable.validationEnabled()).accept(new Visitor<PropertyBuilder>() {
@Override
public void visit(PropertyBuilder builder) {
builder.addToAttributes(LAZY_COLLECTIONS_INIT_ENABLED, buildable.lazyCollectionInitEnabled());
builder.addToAttributes(LAZY_MAP_INIT_ENABLED, buildable.lazyMapInitEnabled());
}
}).build();
ctx.getDefinitionRepository().register(r);
ctx.getBuildableRepository().register(r);
buildables.add(r);
}
}
}
if (ctx == null) {
return true;
}
generateLocalDependenciesIfNeeded();
ctx.getDefinitionRepository().updateReferenceMap();
generateBuildables(ctx, buildables);
generatePojos(ctx, buildables);
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, String.format("%-120s", "100%: Builder generation complete."));
return false;
}
use of io.sundr.model.TypeDefBuilder in project sundrio by sundrio.
the class ExternalBuildableProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
Elements elements = processingEnv.getElementUtils();
Types types = processingEnv.getTypeUtils();
Filer filer = processingEnv.getFiler();
BuilderContext ctx = null;
Set<TypeDef> buildables = new HashSet<>();
// First pass register all externals
for (TypeElement annotation : annotations) {
for (Element element : env.getElementsAnnotatedWith(annotation)) {
final ExternalBuildables generated = element.getAnnotation(ExternalBuildables.class);
if (generated == null) {
continue;
}
ctx = BuilderContextManager.create(elements, types, generated.validationEnabled(), generated.generateBuilderPackage(), generated.builderPackage());
for (String name : generated.value()) {
PackageElement packageElement = elements.getPackageElement(name);
List<TypeElement> typeElements = new ArrayList<>();
if (packageElement != null) {
for (Element e : packageElement.getEnclosedElements()) {
if (e instanceof TypeElement) {
typeElements.add((TypeElement) e);
}
}
} else {
TypeElement e = elements.getTypeElement(name);
if (e != null) {
typeElements.add(e);
}
}
for (TypeElement typeElement : typeElements) {
final boolean isLazyCollectionInitEnabled = generated.lazyCollectionInitEnabled();
final boolean isLazyMapInitEnabled = generated.lazyMapInitEnabled();
final boolean includeInterfaces = generated.includeInterfaces();
final boolean includeAbstractClasses = generated.includeAbstractClasses();
AptContext aptContext = AptContext.create(ctx.getElements(), ctx.getTypes(), ctx.getDefinitionRepository());
TypeDef original = Adapters.adaptType(typeElement, aptContext);
String fqcn = original.getFullyQualifiedName();
boolean isBuildable = original.getKind() != Kind.ENUM && (includeAbstractClasses || !original.isAbstract()) && (includeInterfaces || original.getKind() != Kind.INTERFACE) && isIncluded(fqcn, generated.includes()) && !isExcluded(fqcn, generated.excludes());
TypeDef b = new TypeDefBuilder(original).accept(new Visitor<PropertyBuilder>() {
@Override
public void visit(PropertyBuilder builder) {
if (isBuildable) {
builder.addToAttributes(EXTERNAL_BUILDABLE, generated);
builder.addToAttributes(EDITABLE_ENABLED, generated.editableEnabled());
builder.addToAttributes(VALIDATION_ENABLED, generated.validationEnabled());
builder.addToAttributes(LAZY_COLLECTIONS_INIT_ENABLED, isLazyCollectionInitEnabled);
builder.addToAttributes(LAZY_MAP_INIT_ENABLED, isLazyMapInitEnabled);
}
}
}).build();
if (b.getKind() == Kind.ENUM) {
continue;
}
if (b.isAbstract() && !includeAbstractClasses) {
continue;
}
if (b.getKind() == Kind.INTERFACE && !includeInterfaces) {
continue;
}
if (!isIncluded(b.getFullyQualifiedName(), generated.includes())) {
continue;
}
if (isExcluded(b.getFullyQualifiedName(), generated.excludes())) {
continue;
}
ctx.getDefinitionRepository().register(b);
ctx.getBuildableRepository().register(b);
buildables.add(b);
}
}
for (TypeElement ref : BuilderUtils.getBuildableReferences(ctx, generated)) {
final boolean isLazyCollectionInitEnabled = generated.lazyCollectionInitEnabled();
final boolean isLazyMapInitEnabled = generated.lazyMapInitEnabled();
final boolean includeInterfaces = generated.includeInterfaces();
final boolean includeAbstractClasses = generated.includeAbstractClasses();
AptContext aptContext = AptContext.create(ctx.getElements(), ctx.getTypes(), ctx.getDefinitionRepository());
TypeDef original = Adapters.adaptType(Apt.getClassElement(ref), aptContext);
String fqcn = original.getFullyQualifiedName();
boolean isBuildable = original.getKind() != Kind.ENUM && !original.isAbstract() && isIncluded(fqcn, generated.includes()) && !isExcluded(fqcn, generated.excludes());
TypeDef r = new TypeDefBuilder(original).accept(new Visitor<PropertyBuilder>() {
@Override
public void visit(PropertyBuilder builder) {
if (isBuildable) {
builder.addToAttributes(EXTERNAL_BUILDABLE, generated);
builder.addToAttributes(EDITABLE_ENABLED, generated.editableEnabled());
builder.addToAttributes(VALIDATION_ENABLED, generated.validationEnabled());
builder.addToAttributes(LAZY_COLLECTIONS_INIT_ENABLED, isLazyCollectionInitEnabled);
builder.addToAttributes(LAZY_MAP_INIT_ENABLED, isLazyMapInitEnabled);
}
}
}).build();
if (r.getKind() == Kind.ENUM || r.isAbstract()) {
continue;
}
if (r.getKind() == Kind.ENUM) {
continue;
}
if (r.isAbstract() && !includeAbstractClasses) {
continue;
}
if (r.getKind() == Kind.INTERFACE && !includeInterfaces) {
continue;
}
if (!isIncluded(r.getFullyQualifiedName(), generated.includes())) {
continue;
}
if (isExcluded(r.getFullyQualifiedName(), generated.excludes())) {
continue;
}
ctx.getDefinitionRepository().register(r);
ctx.getBuildableRepository().register(r);
buildables.add(r);
}
}
}
if (ctx == null) {
return true;
}
generateLocalDependenciesIfNeeded();
ctx.getDefinitionRepository().updateReferenceMap();
generateBuildables(ctx, buildables);
generatePojos(ctx, buildables);
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, String.format("%-120s", "100%: Builder generation complete."));
return true;
}
Aggregations