Skip to content

Commit 157b49e

Browse files
authored
fix: Do not use stack trace elements in public API (#15637)
Enables updating the location from code later on if needed
1 parent 6739ea5 commit 157b49e

2 files changed

Lines changed: 51 additions & 35 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/component/internal/ComponentTracker.java‎

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.vaadin.flow.component.internal;
1717

18+
import java.io.Serializable;
1819
import java.util.Collections;
1920
import java.util.List;
2021
import java.util.Map;
@@ -35,9 +36,9 @@
3536
**/
3637
public 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
}

‎flow-server/src/test/java/com/vaadin/flow/ComponentTrackerTest.java‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ public void createLocationTracked() {
7171
Component c2;
7272
c2 = new Component1();
7373

74-
StackTraceElement c1Location = ComponentTracker.findCreate(c1);
75-
Assert.assertEquals(70, c1Location.getLineNumber());
76-
Assert.assertEquals(getClass().getName(), c1Location.getClassName());
74+
ComponentTracker.Location c1Location = ComponentTracker.findCreate(c1);
75+
Assert.assertEquals(70, c1Location.lineNumber());
76+
Assert.assertEquals(getClass().getName(), c1Location.className());
7777

78-
StackTraceElement c2Location = ComponentTracker.findCreate(c2);
79-
Assert.assertEquals(72, c2Location.getLineNumber());
80-
Assert.assertEquals(getClass().getName(), c2Location.getClassName());
78+
ComponentTracker.Location c2Location = ComponentTracker.findCreate(c2);
79+
Assert.assertEquals(72, c2Location.lineNumber());
80+
Assert.assertEquals(getClass().getName(), c2Location.className());
8181
}
8282

8383
@Test
@@ -88,24 +88,24 @@ public void attachLocationTracked() {
8888

8989
Layout layout = new Layout(c1);
9090

91-
StackTraceElement c1Location = ComponentTracker.findAttach(c1);
92-
Assert.assertEquals(89, c1Location.getLineNumber());
93-
Assert.assertEquals(getClass().getName(), c1Location.getClassName());
91+
ComponentTracker.Location c1Location = ComponentTracker.findAttach(c1);
92+
Assert.assertEquals(89, c1Location.lineNumber());
93+
Assert.assertEquals(getClass().getName(), c1Location.className());
9494

9595
layout.add(c2);
9696

97-
StackTraceElement c2Location = ComponentTracker.findAttach(c2);
98-
Assert.assertEquals(95, c2Location.getLineNumber());
99-
Assert.assertEquals(getClass().getName(), c2Location.getClassName());
97+
ComponentTracker.Location c2Location = ComponentTracker.findAttach(c2);
98+
Assert.assertEquals(95, c2Location.lineNumber());
99+
Assert.assertEquals(getClass().getName(), c2Location.className());
100100

101101
// Last attach is tracked
102102
layout.add(c3);
103103
layout.remove(c3);
104104
layout.add(c3);
105105

106-
StackTraceElement c3Location = ComponentTracker.findAttach(c3);
107-
Assert.assertEquals(104, c3Location.getLineNumber());
108-
Assert.assertEquals(getClass().getName(), c3Location.getClassName());
106+
ComponentTracker.Location c3Location = ComponentTracker.findAttach(c3);
107+
Assert.assertEquals(104, c3Location.lineNumber());
108+
Assert.assertEquals(getClass().getName(), c3Location.className());
109109
}
110110

111111
@Test

0 commit comments

Comments
 (0)