Skip to content

Commit

Permalink
Qute: fix template global class generation in the dev mode
Browse files Browse the repository at this point in the history
- if a non-application template global class is present we have to
reflect this fact when assigning the priority for an application
template global resolver; otherwise a conflict may occur
  • Loading branch information
mkouba committed Jan 22, 2025
1 parent 625a0e2 commit b694435
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2094,7 +2094,13 @@ public Function<FieldInfo, String> apply(ClassInfo clazz) {
}

if (!templateGlobals.isEmpty()) {
TemplateGlobalGenerator globalGenerator = new TemplateGlobalGenerator(classOutput, GLOBAL_NAMESPACE, -1000, index);
Set<String> generatedGlobals = new HashSet<>();
// The initial priority is increased during live reload so that priorities of non-application globals
// do not conflict with priorities of application globals
int initialPriority = -1000 + existingValueResolvers.globals.size();

TemplateGlobalGenerator globalGenerator = new TemplateGlobalGenerator(classOutput, GLOBAL_NAMESPACE,
initialPriority, index);

Map<DotName, Map<String, AnnotationTarget>> classToTargets = new HashMap<>();
Map<DotName, List<TemplateGlobalBuildItem>> classToGlobals = templateGlobals.stream()
Expand All @@ -2105,12 +2111,19 @@ public Function<FieldInfo, String> apply(ClassInfo clazz) {
}

for (Entry<DotName, Map<String, AnnotationTarget>> e : classToTargets.entrySet()) {
globalGenerator.generate(index.getClassByName(e.getKey()), e.getValue());
String generatedClass = existingValueResolvers.getGeneratedGlobalClass(e.getKey());
if (generatedClass != null) {
generatedGlobals.add(generatedClass);
} else {
generatedClass = globalGenerator.generate(index.getClassByName(e.getKey()), e.getValue());
existingValueResolvers.addGlobal(e.getKey(), generatedClass, applicationClassPredicate);
}
}
generatedGlobals.addAll(globalGenerator.getGeneratedTypes());

for (String generatedType : globalGenerator.getGeneratedTypes()) {
globalProviders.produce(new TemplateGlobalProviderBuildItem(generatedType));
reflectiveClass.produce(ReflectiveClassBuildItem.builder(generatedType).build());
for (String globalType : generatedGlobals) {
globalProviders.produce(new TemplateGlobalProviderBuildItem(globalType));
reflectiveClass.produce(ReflectiveClassBuildItem.builder(globalType).build());
}
}
}
Expand All @@ -2122,12 +2135,18 @@ public Function<FieldInfo, String> apply(ClassInfo clazz) {
static class ExistingValueResolvers {

final Map<String, String> identifiersToGeneratedClass = new HashMap<>();
// class declaring globals -> generated type
final Map<String, String> globals = new HashMap<>();

boolean contains(MethodInfo extensionMethod) {
return identifiersToGeneratedClass
.containsKey(toKey(extensionMethod));
}

String getGeneratedGlobalClass(DotName declaringClassName) {
return globals.get(declaringClassName.toString());
}

String getGeneratedClass(MethodInfo extensionMethod) {
return identifiersToGeneratedClass.get(toKey(extensionMethod));
}
Expand All @@ -2138,6 +2157,12 @@ void add(MethodInfo extensionMethod, String className, Predicate<DotName> applic
}
}

void addGlobal(DotName declaringClassName, String generatedClassName, Predicate<DotName> applicationClassPredicate) {
if (!applicationClassPredicate.test(declaringClassName)) {
globals.put(declaringClassName.toString(), generatedClassName);
}
}

private String toKey(MethodInfo extensionMethod) {
return extensionMethod.declaringClass().toString() + "#" + extensionMethod.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ public class TemplateGlobalGenerator extends AbstractGenerator {
private final String namespace;
private int priority;

public TemplateGlobalGenerator(ClassOutput classOutput, String namespace, int priority, IndexView index) {
public TemplateGlobalGenerator(ClassOutput classOutput, String namespace, int initialPriority, IndexView index) {
super(index, classOutput);
this.namespace = namespace;
this.priority = priority;
this.priority = initialPriority;
}

public void generate(ClassInfo declaringClass, Map<String, AnnotationTarget> targets) {
public String generate(ClassInfo declaringClass, Map<String, AnnotationTarget> targets) {

String baseName;
if (declaringClass.enclosingClass() != null) {
Expand All @@ -65,7 +65,8 @@ public void generate(ClassInfo declaringClass, Map<String, AnnotationTarget> tar
}
String targetPackage = packageName(declaringClass.name());
String generatedName = generatedNameFromTarget(targetPackage, baseName, SUFFIX);
generatedTypes.add(generatedName.replace('/', '.'));
String generatedClassName = generatedName.replace('/', '.');
generatedTypes.add(generatedClassName);

ClassCreator provider = ClassCreator.builder().classOutput(classOutput).className(generatedName)
.interfaces(TemplateGlobalProvider.class).build();
Expand Down Expand Up @@ -141,6 +142,7 @@ public void accept(BytecodeCreator bc) {
resolve.returnValue(resolve.invokeStaticMethod(Descriptors.RESULTS_NOT_FOUND_EC, evalContext));

provider.close();
return generatedClassName;
}

public Set<String> getGeneratedTypes() {
Expand Down

0 comments on commit b694435

Please # to comment.