From 21afecd03e705b3ee625da4ee6e4b043fd10b2de Mon Sep 17 00:00:00 2001 From: Artur Date: Fri, 2 Jun 2023 08:55:20 +0300 Subject: [PATCH] fix: Scan method annotations (#16937) Fixes #16925 --- .../scanner/FrontendClassVisitor.java | 7 ++ .../scanner/ScannerDependenciesTest.java | 73 +++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/flow-server/src/main/java/com/vaadin/flow/server/frontend/scanner/FrontendClassVisitor.java b/flow-server/src/main/java/com/vaadin/flow/server/frontend/scanner/FrontendClassVisitor.java index 90ad9110109..fad79417a9a 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/frontend/scanner/FrontendClassVisitor.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/frontend/scanner/FrontendClassVisitor.java @@ -65,6 +65,13 @@ public FrontendMethodVisitor() { super(Opcodes.ASM9); } + @Override + public AnnotationVisitor visitAnnotation(String descriptor, + boolean visible) { + addSignatureToClasses(classInfo.children, descriptor); + return annotationVisitor; + } + // We are interested in the new instances created inside the method @Override public void visitTypeInsn(int opcode, String type) { diff --git a/flow-server/src/test/java/com/vaadin/flow/server/frontend/scanner/ScannerDependenciesTest.java b/flow-server/src/test/java/com/vaadin/flow/server/frontend/scanner/ScannerDependenciesTest.java index 88599ffbb97..ec2f8fe0afe 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/frontend/scanner/ScannerDependenciesTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/frontend/scanner/ScannerDependenciesTest.java @@ -1,16 +1,25 @@ package com.vaadin.flow.server.frontend.scanner; +import java.io.InputStream; +import java.lang.reflect.AnnotatedElement; +import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.function.Supplier; import org.junit.Test; +import org.objectweb.asm.ClassReader; +import com.vaadin.flow.component.Component; import com.vaadin.flow.component.dependency.JsModule; import com.vaadin.flow.router.Route; import com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder; +import com.vaadin.flow.server.frontend.scanner.ScannerDependenciesTest.UISearchField.SearchFieldComponentDefinitionCreator; +import com.vaadin.flow.server.frontend.scanner.ScannerDependenciesTest.UISearchField.UISearchLayout; +import com.vaadin.flow.server.frontend.scanner.ScannerDependenciesTest.UISearchField.UISearchLayout.SearchLayoutComponentDefinitionCreator; import com.vaadin.flow.server.frontend.scanner.ScannerTestComponents.BridgeClass; import com.vaadin.flow.server.frontend.scanner.ScannerTestComponents.Component0; import com.vaadin.flow.server.frontend.scanner.ScannerTestComponents.Component1; @@ -242,4 +251,68 @@ public void should_visitServices() { DepsTests.assertImportsExcludingUI(deps.getModules(), "dynamic-component.js", "dynamic-layout.js"); } + + @Test + public void should_visitMethodAnnotations() { + FrontendDependencies deps = getFrontendDependencies( + MethodAnnotationRoute.class); + DepsTests.assertImportsExcludingUI(deps.getModules(), + "./search-layout.js", "./search-field.js"); + } + + @Route + public static class MethodAnnotationRoute { + private MyPmo myPmo; + } + + @UISearchLayout + public static class MyPmo { + + @UISearchField() + public String getText() { + return "value to be displayed"; + } + } + + public interface ComponentDefinitionCreator { + + } + + public @interface LinkTo { + Class value(); + } + + @LinkTo(SearchFieldComponentDefinitionCreator.class) + public @interface UISearchField { + class SearchFieldComponentDefinitionCreator + implements ComponentDefinitionCreator { + public Supplier create(UISearchField annotation, + AnnotatedElement annotatedElement) { + return () -> new SearchField(); + } + } + + @LinkTo(SearchLayoutComponentDefinitionCreator.class) + public @interface UISearchLayout { + class SearchLayoutComponentDefinitionCreator + implements ComponentDefinitionCreator { + public Supplier create(UISearchLayout annotation, + AnnotatedElement annotatedElement) { + return () -> new SearchLayout(); + } + } + } + + @JsModule("./search-layout.js") + public static class SearchLayout extends Component { + + } + + @JsModule("./search-field.js") + public static class SearchField extends Component { + + } + + } + }