Skip to content

Commit b694435

Browse files
committed
Qute: fix template global class generation in the dev mode
- 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
1 parent 625a0e2 commit b694435

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/QuteProcessor.java

+30-5
Original file line numberDiff line numberDiff line change
@@ -2094,7 +2094,13 @@ public Function<FieldInfo, String> apply(ClassInfo clazz) {
20942094
}
20952095

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

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

21072113
for (Entry<DotName, Map<String, AnnotationTarget>> e : classToTargets.entrySet()) {
2108-
globalGenerator.generate(index.getClassByName(e.getKey()), e.getValue());
2114+
String generatedClass = existingValueResolvers.getGeneratedGlobalClass(e.getKey());
2115+
if (generatedClass != null) {
2116+
generatedGlobals.add(generatedClass);
2117+
} else {
2118+
generatedClass = globalGenerator.generate(index.getClassByName(e.getKey()), e.getValue());
2119+
existingValueResolvers.addGlobal(e.getKey(), generatedClass, applicationClassPredicate);
2120+
}
21092121
}
2122+
generatedGlobals.addAll(globalGenerator.getGeneratedTypes());
21102123

2111-
for (String generatedType : globalGenerator.getGeneratedTypes()) {
2112-
globalProviders.produce(new TemplateGlobalProviderBuildItem(generatedType));
2113-
reflectiveClass.produce(ReflectiveClassBuildItem.builder(generatedType).build());
2124+
for (String globalType : generatedGlobals) {
2125+
globalProviders.produce(new TemplateGlobalProviderBuildItem(globalType));
2126+
reflectiveClass.produce(ReflectiveClassBuildItem.builder(globalType).build());
21142127
}
21152128
}
21162129
}
@@ -2122,12 +2135,18 @@ public Function<FieldInfo, String> apply(ClassInfo clazz) {
21222135
static class ExistingValueResolvers {
21232136

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

21262141
boolean contains(MethodInfo extensionMethod) {
21272142
return identifiersToGeneratedClass
21282143
.containsKey(toKey(extensionMethod));
21292144
}
21302145

2146+
String getGeneratedGlobalClass(DotName declaringClassName) {
2147+
return globals.get(declaringClassName.toString());
2148+
}
2149+
21312150
String getGeneratedClass(MethodInfo extensionMethod) {
21322151
return identifiersToGeneratedClass.get(toKey(extensionMethod));
21332152
}
@@ -2138,6 +2157,12 @@ void add(MethodInfo extensionMethod, String className, Predicate<DotName> applic
21382157
}
21392158
}
21402159

2160+
void addGlobal(DotName declaringClassName, String generatedClassName, Predicate<DotName> applicationClassPredicate) {
2161+
if (!applicationClassPredicate.test(declaringClassName)) {
2162+
globals.put(declaringClassName.toString(), generatedClassName);
2163+
}
2164+
}
2165+
21412166
private String toKey(MethodInfo extensionMethod) {
21422167
return extensionMethod.declaringClass().toString() + "#" + extensionMethod.toString();
21432168
}

independent-projects/qute/generator/src/main/java/io/quarkus/qute/generator/TemplateGlobalGenerator.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ public class TemplateGlobalGenerator extends AbstractGenerator {
4848
private final String namespace;
4949
private int priority;
5050

51-
public TemplateGlobalGenerator(ClassOutput classOutput, String namespace, int priority, IndexView index) {
51+
public TemplateGlobalGenerator(ClassOutput classOutput, String namespace, int initialPriority, IndexView index) {
5252
super(index, classOutput);
5353
this.namespace = namespace;
54-
this.priority = priority;
54+
this.priority = initialPriority;
5555
}
5656

57-
public void generate(ClassInfo declaringClass, Map<String, AnnotationTarget> targets) {
57+
public String generate(ClassInfo declaringClass, Map<String, AnnotationTarget> targets) {
5858

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

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

143144
provider.close();
145+
return generatedClassName;
144146
}
145147

146148
public Set<String> getGeneratedTypes() {

0 commit comments

Comments
 (0)