From d26f11865e11a209a7441139d41b5038f5dc5ad1 Mon Sep 17 00:00:00 2001 From: Tomas Langer Date: Fri, 15 Nov 2024 11:25:17 +0100 Subject: [PATCH] Only generate scope constants for custom scopes. --- .../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) {