From ba8bdd1cb37c4011a793a4c52b3f83b5a4491a53 Mon Sep 17 00:00:00 2001 From: Tomas Langer Date: Fri, 15 Nov 2024 14:06:05 +0100 Subject: [PATCH] Only generate scope constants for custom scopes. (#9504) --- .../inject/codegen/InjectionExtension.java | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/service/inject/codegen/src/main/java/io/helidon/service/inject/codegen/InjectionExtension.java b/service/inject/codegen/src/main/java/io/helidon/service/inject/codegen/InjectionExtension.java index 28fb4954854..2cef3307b2a 100644 --- a/service/inject/codegen/src/main/java/io/helidon/service/inject/codegen/InjectionExtension.java +++ b/service/inject/codegen/src/main/java/io/helidon/service/inject/codegen/InjectionExtension.java @@ -2270,17 +2270,31 @@ private void qualifiersMethod(ClassModel.Builder classModel, DescribedService se private void scopeMethod(ClassModel.Builder classModel, DescribedService service) { // TypeName scope() - classModel.addField(scopesField -> scopesField - .isStatic(true) - .isFinal(true) - .name("SCOPE") - .type(TypeNames.TYPE_NAME) - .addContentCreate(service.scope())); + TypeName scope = service.scope(); - classModel.addMethod(scopeMethod -> scopeMethod.name("scope") - .addAnnotation(Annotations.OVERRIDE) - .returnType(TypeNames.TYPE_NAME) - .addContentLine("return SCOPE;")); + if (scope.packageName().equals(INJECTION_SINGLETON.packageName()) + && scope.enclosingNames().size() == 1 && scope.enclosingNames().getFirst().equals("Injection")) { + // only method + classModel.addMethod(scopeMethod -> scopeMethod.name("scope") + .addAnnotation(Annotations.OVERRIDE) + .returnType(TypeNames.TYPE_NAME) + .addContent("return ") + .addContent(scope) + .addContentLine(".TYPE;")); + } else { + // field and method + classModel.addField(scopesField -> scopesField + .isStatic(true) + .isFinal(true) + .name("SCOPE") + .type(TypeNames.TYPE_NAME) + .addContentCreate(scope)); + + classModel.addMethod(scopeMethod -> scopeMethod.name("scope") + .addAnnotation(Annotations.OVERRIDE) + .returnType(TypeNames.TYPE_NAME) + .addContentLine("return SCOPE;")); + } } private void weightMethod(ClassModel.Builder classModel, DescribedService service) {