1515 */
1616package com .vaadin .flow .component .internal ;
1717
18+ import java .io .Serializable ;
1819import java .util .Collections ;
1920import java .util .List ;
2021import java .util .Map ;
3536 **/
3637public class ComponentTracker {
3738
38- private static Map <Component , StackTraceElement > createLocation = Collections
39+ private static Map <Component , Location > createLocation = Collections
3940 .synchronizedMap (new WeakHashMap <>());
40- private static Map <Component , StackTraceElement > attachLocation = Collections
41+ private static Map <Component , Location > attachLocation = Collections
4142 .synchronizedMap (new WeakHashMap <>());
4243
4344 private static Boolean productionMode = null ;
@@ -47,15 +48,20 @@ public class ComponentTracker {
4748 "com.vaadin.flow.spring." , "java." , "jdk." ,
4849 "org.springframework.beans." , };
4950
51+ /**
52+ * Represents a location in the source code.
53+ */
54+ public record Location (String className , String filename , int lineNumber ) implements Serializable {
55+ }
56+
5057 /**
5158 * Finds the location where the given component instance was created.
5259 *
5360 * @param component
5461 * the component to find
55- * @return an element from the stack trace describing the relevant location
56- * where the component was created
62+ * @return the location where the component was created
5763 */
58- public static StackTraceElement findCreate (Component component ) {
64+ public static Location findCreate (Component component ) {
5965 return createLocation .get (component );
6066 }
6167
@@ -72,10 +78,10 @@ public static void trackCreate(Component component) {
7278 return ;
7379 }
7480 StackTraceElement [] stack = Thread .currentThread ().getStackTrace ();
75- StackTraceElement location = findRelevantElement (component .getClass (),
76- stack , null );
81+ Location location = findRelevantLocation (component .getClass (), stack ,
82+ null );
7783 if (isNavigatorCreate (location )) {
78- location = findRelevantElement (null , stack , null );
84+ location = findRelevantLocation (null , stack , null );
7985 }
8086 createLocation .put (component , location );
8187 }
@@ -86,10 +92,9 @@ public static void trackCreate(Component component) {
8692 *
8793 * @param component
8894 * the component to find
89- * @return an element from the stack trace describing the relevant location
90- * where the component was attached
95+ * @return the location where the component was attached
9196 */
92- public static StackTraceElement findAttach (Component component ) {
97+ public static Location findAttach (Component component ) {
9398 return attachLocation .get (component );
9499 }
95100
@@ -109,8 +114,8 @@ public static void trackAttach(Component component) {
109114
110115 // In most cases the interesting attach call is found in the same class
111116 // where the component was created and not in a generic layout class
112- StackTraceElement location = findRelevantElement (component .getClass (),
113- stack , findCreate (component ));
117+ Location location = findRelevantLocation (component .getClass (), stack ,
118+ findCreate (component ));
114119 if (isNavigatorCreate (location )) {
115120 // For routes, we can just show the init location as we have nothing
116121 // better
@@ -119,14 +124,14 @@ public static void trackAttach(Component component) {
119124 attachLocation .put (component , location );
120125 }
121126
122- private static boolean isNavigatorCreate (StackTraceElement location ) {
123- return location .getClassName ()
127+ private static boolean isNavigatorCreate (Location location ) {
128+ return location .className ()
124129 .equals (AbstractNavigationStateRenderer .class .getName ());
125130 }
126131
127- private static StackTraceElement findRelevantElement (
132+ private static Location findRelevantLocation (
128133 Class <? extends Component > excludeClass , StackTraceElement [] stack ,
129- StackTraceElement preferredClass ) {
134+ Location preferredClass ) {
130135 List <StackTraceElement > candidates = Stream .of (stack )
131136 .filter (e -> excludeClass == null
132137 || !e .getClassName ().equals (excludeClass .getName ()))
@@ -141,13 +146,13 @@ private static StackTraceElement findRelevantElement(
141146 if (preferredClass != null ) {
142147 Optional <StackTraceElement > preferredCandidate = candidates .stream ()
143148 .filter (e -> e .getClassName ()
144- .equals (preferredClass .getClassName ()))
149+ .equals (preferredClass .className ()))
145150 .findFirst ();
146151 if (preferredCandidate .isPresent ()) {
147- return preferredCandidate .get ();
152+ return toLocation ( preferredCandidate .get () );
148153 }
149154 }
150- return candidates .stream ().findFirst ().orElse (null );
155+ return toLocation ( candidates .stream ().findFirst ().orElse (null ) );
151156 }
152157
153158 /**
@@ -185,4 +190,15 @@ private static boolean isProductionMode() {
185190 return productionMode ;
186191 }
187192
193+ private static Location toLocation (StackTraceElement stackTraceElement ) {
194+ if (stackTraceElement == null ) {
195+ return null ;
196+ }
197+
198+ String className = stackTraceElement .getClassName ();
199+ String fileName = stackTraceElement .getFileName ();
200+ int lineNumber = stackTraceElement .getLineNumber ();
201+ return new Location (className , fileName , lineNumber );
202+ }
203+
188204}
0 commit comments