Allon Mureinik has uploaded a new change for review. Change subject: core: Disable member init for GWT-compiled code ......................................................................
core: Disable member init for GWT-compiled code Following the upgrade of GWT(P), if an entity's constructor is not explicitly referenced in the UI code the inline member initializations are not taken into account in GWT's serialization policy [1]. This patch is the culmination of a series that suggests taking the safe route - instead of having the code (silently!) break if a call to the constructor is removed from the UI code, it simply disallows having such iniine initializations. The previous patches in this set remove such initializers for various parts of the common and compat modules (broken up into smaller units for relative ease of review). This patch implements a checkstyle check to fail the build in compile time if such a dangerous initializer is introduced. [1] http://lists.ovirt.org/pipermail/engine-devel/2013-July/005256.html Change-Id: Iffd520ecb627e87254c4d325c85088179b0ef744 Signed-off-by: Allon Mureinik <amure...@redhat.com> --- M backend/manager/modules/common/pom.xml M backend/manager/modules/compat/pom.xml M build-tools-root/checkstyles/src/main/resources/checkstyle.xml A build-tools-root/ovirt-checkstyle-extension/src/main/java/checks/NoMemberInitializationCheck.java 4 files changed, 71 insertions(+), 0 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/40/18140/1 diff --git a/backend/manager/modules/common/pom.xml b/backend/manager/modules/common/pom.xml index 071cf5b..0a45d19 100644 --- a/backend/manager/modules/common/pom.xml +++ b/backend/manager/modules/common/pom.xml @@ -67,6 +67,7 @@ <artifactId>maven-checkstyle-plugin</artifactId> <configuration> <propertyExpansion>disallowFinals=true</propertyExpansion> + <propertyExpansion>disallowMemberInit=true</propertyExpansion> </configuration> </plugin> <plugin> diff --git a/backend/manager/modules/compat/pom.xml b/backend/manager/modules/compat/pom.xml index 19f455b..179ca02 100644 --- a/backend/manager/modules/compat/pom.xml +++ b/backend/manager/modules/compat/pom.xml @@ -36,6 +36,7 @@ <artifactId>maven-checkstyle-plugin</artifactId> <configuration> <propertyExpansion>disallowFinals=true</propertyExpansion> + <propertyExpansion>disallowMemberInit=true</propertyExpansion> </configuration> </plugin> diff --git a/build-tools-root/checkstyles/src/main/resources/checkstyle.xml b/build-tools-root/checkstyles/src/main/resources/checkstyle.xml index a4026fc..77fc4a6 100644 --- a/build-tools-root/checkstyles/src/main/resources/checkstyle.xml +++ b/build-tools-root/checkstyles/src/main/resources/checkstyle.xml @@ -25,5 +25,8 @@ <module name="checks.NoFinalMemberCheck"> <property name="run" value="${disallowFinals}" default="false"/> </module> + <module name="checks.NoMemberInitializationCheck"> + <property name="run" value="${disallowMemberInit}" default="false"/> + </module> </module> </module> diff --git a/build-tools-root/ovirt-checkstyle-extension/src/main/java/checks/NoMemberInitializationCheck.java b/build-tools-root/ovirt-checkstyle-extension/src/main/java/checks/NoMemberInitializationCheck.java new file mode 100644 index 0000000..1ec92c3 --- /dev/null +++ b/build-tools-root/ovirt-checkstyle-extension/src/main/java/checks/NoMemberInitializationCheck.java @@ -0,0 +1,66 @@ +package checks; + +import com.puppycrawl.tools.checkstyle.api.Check; +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.ScopeUtils; +import com.puppycrawl.tools.checkstyle.api.TokenTypes; + +/** + * <p> + * Checks if any class or object member is explicitly initialized instead of being initialized in the class' + * constructor. + * </p> + * <p> + * Rationale: Such calls are not interpreted by GWT's compiler, and if these objects are shared to the frontend, these + * members may not be initialized. + * </p> + * <p> + * This code was adapted from {@link com.puppycrawl.tools.checkstyle.checks.coding.ExplicitInitializationCheck}, and + * should be removed once such a check is integrated into the checkstyle project. + * </p> + */ +public class NoMemberInitializationCheck extends Check { + private boolean run = true; + + @Override + public final int[] getDefaultTokens() { + return new int[] { TokenTypes.VARIABLE_DEF }; + } + + @Override + public final int[] getRequiredTokens() { + return getDefaultTokens(); + } + + @Override + public void visitToken(DetailAST aAST) { + if (!run) { + return; + } + + // do not check local variables and + // fields declared in interface/annotations + if (ScopeUtils.isLocalVariableDef(aAST) || ScopeUtils.inInterfaceOrAnnotationBlock(aAST)) { + return; + } + + final DetailAST assign = aAST.findFirstToken(TokenTypes.ASSIGN); + if (assign == null) { + // no assign - no check + return; + } + + final DetailAST modifiers = aAST.findFirstToken(TokenTypes.MODIFIERS); + if ((modifiers != null) && modifiers.branchContains(TokenTypes.FINAL)) { + // do not check final variables + return; + } + + final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT); + log(ident, "Initialization of members is not allowed", ident.getText()); + } + + public void setRun(boolean run) { + this.run = run; + } +} -- To view, visit http://gerrit.ovirt.org/18140 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Iffd520ecb627e87254c4d325c85088179b0ef744 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Allon Mureinik <amure...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches