Skip to content

Commit 1a72444

Browse files
committed
feat: add InstrumentationViewInitializer
1 parent c7658a9 commit 1a72444

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*-
2+
* #%L
3+
* Json Migration Helper
4+
* %%
5+
* Copyright (C) 2025 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.jsonmigration;
21+
22+
import com.vaadin.flow.component.Component;
23+
import com.vaadin.flow.router.RouteConfiguration;
24+
import com.vaadin.flow.server.VaadinServiceInitListener;
25+
import com.vaadin.flow.server.Version;
26+
27+
/**
28+
* Abstract base class for Vaadin service initializers that register instrumented views. Subclasses
29+
* should implement {@link #serviceInit(com.vaadin.flow.server.ServiceInitEvent)} and call
30+
* {@link #registerInstrumentedRoute(Class)} to register views with instrumented routes.
31+
*
32+
* @author Javier Godoy / Flowing Code
33+
*/
34+
@SuppressWarnings("serial")
35+
public abstract class InstrumentationViewInitializer implements VaadinServiceInitListener {
36+
37+
/**
38+
* Registers an instrumented route for the given navigation target. The navigation target must be
39+
* annotated with {@link InstrumentedRoute} to specify the route path. This method calls
40+
* {@link JsonMigration#instrumentClass(Class)} to get the instrumented class and registers it as
41+
* a Vaadin view with the route derived from the annotation.
42+
*
43+
* @param navigationTarget the component class to instrument and register, must be annotated with
44+
* {@link InstrumentedRoute}
45+
* @throws IllegalArgumentException if the navigationTarget is not annotated with
46+
* {@link InstrumentedRoute}
47+
*/
48+
protected final void registerInstrumentedRoute(Class<? extends Component> navigationTarget) {
49+
InstrumentedRoute annotation = navigationTarget.getAnnotation(InstrumentedRoute.class);
50+
if (annotation == null) {
51+
throw new IllegalArgumentException(
52+
navigationTarget.getName() + " must be annotated with @"
53+
+ InstrumentedRoute.class.getSimpleName());
54+
}
55+
56+
String route = annotation.value();
57+
if (Version.getMajorVersion() > 24) {
58+
navigationTarget = JsonMigration.instrumentClass(navigationTarget);
59+
}
60+
RouteConfiguration.forApplicationScope().setRoute(route, navigationTarget);
61+
}
62+
63+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*-
2+
* #%L
3+
* Json Migration Helper
4+
* %%
5+
* Copyright (C) 2025 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.jsonmigration;
21+
22+
import com.vaadin.flow.component.Component;
23+
import java.lang.annotation.Documented;
24+
import java.lang.annotation.ElementType;
25+
import java.lang.annotation.Retention;
26+
import java.lang.annotation.RetentionPolicy;
27+
import java.lang.annotation.Target;
28+
29+
/**
30+
* Annotation to mark a {@link Component} class for instrumented route registration.
31+
*
32+
* @author Javier Godoy / Flowing Code
33+
*/
34+
@Retention(RetentionPolicy.RUNTIME)
35+
@Target(ElementType.TYPE)
36+
@Documented
37+
public @interface InstrumentedRoute {
38+
39+
/**
40+
* The route path for this component.
41+
*
42+
* @return the route path
43+
*/
44+
String value();
45+
46+
}

0 commit comments

Comments
 (0)