Understand the technical principles of Vaadin development mode detector
The technical principles of VAADIN development mode detector
VAADIN development mode detector is a tool for detecting the development mode of VAADIN applications.It can help developers identify potential problems in the code and find out places to be improved.The following will introduce the technical principles of the VAADIN development mode detector and how to use the Java code example to demonstrate.
Technical principle:
The technical principle of VAADIN development mode detector is based on code analysis of the code of VAADIN applications.It uses a series of rules to detect possible problems and modes.These rules can be defined by coding agreement, best practice and experience.
The workflow of the VAADIN development mode detector is as follows:
1. By parsing the source code of the VAADIN application, build a code model.
2. The code model will be passed to the detector so that the application can be detected according to the selected rules.
3. Check the code model according to the rules and generate problems.
You can use the Vaadin Flow server framework that comes with Vaadin to implement the VAADIN development mode detector.Below is a VAADIN development mode detector implemented by Java code examples to detect whether a parameter constructor is used in the UI class.
import com.vaadin.flow.component.UI;
import com.vaadin.flow.router.Route;
@Route("")
public class MainView extends UI {
public MainView() {
}
}
In the above example, we created a Vaadin UI class called MainView and marked it as a routing.According to the best practice of VADIN, it should be avoided in the UI class.Therefore, we can use the VAADIN development mode detector to detect this problem.
The following is an example of the rules that detect the non -constructor function:
import com.github.mvafeeis.vadiin.rules.AbstractVaadinRule;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.router.Route;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtConstructor;
public class NoArgConstructorRule extends AbstractVaadinRule {
private static final Logger LOGGER = LoggerFactory.getLogger(NoArgConstructorRule.class);
@Override
public void visitCtClass(CtClass<?> ctClass) {
if (isUI(ctClass) && hasNoArgConstructor(ctClass)) {
LOGGER.warn("Avoid using no-arg constructor in UI class: " + ctClass.getSimpleName());
}
}
private boolean isUI(CtClass<?> ctClass) {
return ctClass.getAnnotation(Route.class) != null || ctClass.getSuperclass() == UI.class;
}
private boolean hasNoArgConstructor(CtClass<?> ctClass) {
for (CtConstructor<?> constructor : ctClass.getConstructors()) {
if (constructor.getSignature().isEmpty()) {
return true;
}
}
return false;
}
}
In the above rule examples, we define a `noargConStructorrule` class, which inherits from the` AbstractVaadinrule ".We rewritten the `visitctClass` method to check whether the UI class has a parameter constructor function in this method.If you have a non -parameter constructor, a warning is generated.
To use the VAADIN development mode detector, you need to add this rule example to the detector and then run the detector.This will detect whether a parameter constructor is used in your VAADIN application.
In summary, the VAADIN development mode detector is a tool to detect the VAADIN application development mode.Through static analysis source code, and using a series of rules, it can identify potential problems and improved places.The author can use Java to write rules and add it to the detector to implement the custom detection function.