-
Notifications
You must be signed in to change notification settings - Fork 332
Add annotation @BehaviorChangeSince for behavior-change flags #1170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f744263
aa23522
674467f
5a9ea4a
2c95c2b
30a2c2d
7cc7e7b
10eef5b
55b33e8
9f376b8
c4299e3
2a059d7
49a955d
8c163b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -160,6 +160,46 @@ tasks.withType(Jar::class).configureEach { | |
| } | ||
| } | ||
|
|
||
| class StaleBehaviorChangeConfigurationChecker(private val appVersion: String) : FormatterFunc { | ||
|
|
||
| // Helper function for spotless to check for stale BehaviorChangeConfigurations | ||
| fun isStaleVersion( | ||
| annotatedVersion: String, | ||
| expiresVersion: String?, | ||
| polarisVersion: String, | ||
| ): Boolean { | ||
| if (expiresVersion == null) { | ||
| val (majorA, minorA, _) = annotatedVersion.split(".").map { it.toInt() } | ||
| return isStaleVersion(annotatedVersion, "${majorA + 1}.${minorA}.0", polarisVersion) || | ||
| isStaleVersion(annotatedVersion, "${majorA}.${minorA + 2}.0", polarisVersion); | ||
| } else { | ||
| val (majorA, minorA, patchA) = expiresVersion.split(".").map { it.toInt() } | ||
| val (majorB, minorB, patchB) = polarisVersion.split(".").map { it.toInt() } | ||
| return (majorA > majorB || | ||
| (majorA == majorB && minorA > minorB) || | ||
| (majorA == majorB && minorA == minorB && patchA > patchB)) | ||
| } | ||
| } | ||
|
|
||
| override fun apply(input: String): String { | ||
| val versionRegex = | ||
| """@BehaviorChangeScope\s*\(\s*since\s*=\s*"(\d+\.\d+\.\d+)"(?:\s*,\s*expires\s*=\s*"(\d+\.\d+\.\d+)")?\s*\)""" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will it work if the annotation is split across multiple lines?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It more or less should work because of all the |
||
| .toRegex() | ||
| val matcher = versionRegex.findAll(input) | ||
| matcher.forEach { matchResult -> | ||
| val sinceVersion = matchResult.groupValues[1] | ||
| val expiresVersion = matchResult.groupValues.getOrNull(2) | ||
| if (isStaleVersion(sinceVersion, expiresVersion, appVersion)) { | ||
| throw GradleException( | ||
| "Outdated behavior change annotation detected. Consider removing the configuration, " + | ||
| " promoting it to a feature configuration, or disabling this check for the configuration." | ||
| ) | ||
| } | ||
| } | ||
| return input | ||
| } | ||
| } | ||
|
|
||
| spotless { | ||
| java { | ||
| target("src/main/java/**/*.java", "src/testFixtures/java/**/*.java", "src/test/java/**/*.java") | ||
|
|
@@ -178,6 +218,16 @@ spotless { | |
| } | ||
| }, | ||
| ) | ||
| val polarisVersion = | ||
| File(rootProject.projectDir, "version.txt").readText().trim().substringBefore("-") | ||
| custom( | ||
| "checkStaleBehaviorChangeFlags", | ||
| object : Serializable, FormatterFunc { | ||
| override fun apply(text: String): String { | ||
| return StaleBehaviorChangeConfigurationChecker(polarisVersion).apply(text) | ||
| } | ||
| }, | ||
| ) | ||
| toggleOffOn() | ||
| } | ||
| kotlinGradle { | ||
|
|
@@ -193,6 +243,23 @@ spotless { | |
| // getting the license-header delimiter right is a bit tricky. | ||
| // licenseHeaderFile(rootProject.file("codestyle/copyright-header.xml"), '<^[!?].*$') | ||
| } | ||
| format("enforceBehaviorChangeFlagLocations") { | ||
| target("**/*.java") | ||
| targetExclude("**/BehaviorChangeConfiguration.java", "**/PolarisConfigurationStoreTest.java") | ||
| custom( | ||
| "enforceBehaviorChangeFlags", | ||
| object : Serializable, FormatterFunc { | ||
| override fun apply(text: String): String { | ||
| if (".buildBehaviorChangeConfiguration()" in text) { | ||
| throw GradleException( | ||
| "Usage of buildBehaviorChangeConfiguration() is restricted to BehaviorChangeConfiguration.java" | ||
| ) | ||
| } | ||
| return text | ||
| } | ||
| }, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| dependencies { errorprone(versionCatalogs.named("libs").findLibrary("errorprone").get()) } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.polaris.core.config; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * An annotation that should be applied to all BehaviorChangeConfiguration instances. Spotless will | ||
| * enforce that these flags are not around too long. When the flag expires, the linter will prompt | ||
| * the person attempting to do the release to take 1 of 3 actions: | ||
| * | ||
| * <pre> | ||
| * <ul> | ||
| * <li>Remove the flag</li> | ||
| * <li>Promote the flag to a feature flag</li> | ||
| * <li>Disable the linter for this flag for an "extension"</li> | ||
| * </ul> | ||
| * </pre> | ||
| */ | ||
| @Target(ElementType.FIELD) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface BehaviorChange { | ||
| static final String AUTOMATIC_EXPIRES = "auto"; | ||
|
|
||
| String since(); | ||
|
|
||
| String expires() default AUTOMATIC_EXPIRES; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.polaris.core.config; | ||
|
|
||
| import java.util.Set; | ||
| import javax.annotation.processing.AbstractProcessor; | ||
| import javax.annotation.processing.RoundEnvironment; | ||
| import javax.annotation.processing.SupportedAnnotationTypes; | ||
| import javax.annotation.processing.SupportedSourceVersion; | ||
| import javax.lang.model.SourceVersion; | ||
| import javax.lang.model.element.Element; | ||
| import javax.lang.model.element.ElementKind; | ||
| import javax.lang.model.element.TypeElement; | ||
| import javax.lang.model.element.VariableElement; | ||
| import javax.lang.model.type.TypeMirror; | ||
| import javax.lang.model.util.Elements; | ||
| import javax.tools.Diagnostic; | ||
|
|
||
| @SupportedAnnotationTypes("BehaviorChange") | ||
| @SupportedSourceVersion(SourceVersion.RELEASE_8) | ||
| public class BehaviorChangeProcessor extends AbstractProcessor { | ||
|
|
||
| @Override | ||
| public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to move version/expiry checks here? It think it would be nicer from the overall design POV. Build scripts could probably inject "current version" into system properties. WDYT?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really prefer to do things like this in a linter. We can always do it in both, too.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. having both looks awkward to me... I'd prefer to move the logic and keep only one of them (up to you which one). |
||
|
|
||
| Elements elementUtils = processingEnv.getElementUtils(); | ||
|
|
||
| for (Element element : roundEnv.getRootElements()) { | ||
| if (element.getKind() == ElementKind.CLASS) { | ||
| TypeElement typeElement = (TypeElement) element; | ||
| for (Element enclosedElement : typeElement.getEnclosedElements()) { | ||
| if (enclosedElement.getKind() == ElementKind.FIELD) { | ||
| VariableElement field = (VariableElement) enclosedElement; | ||
| TypeMirror fieldType = field.asType(); | ||
|
|
||
| if (isBehaviorChangeConfiguration(fieldType, elementUtils)) { | ||
| if (field.getAnnotation(BehaviorChange.class) == null) { | ||
| processingEnv | ||
| .getMessager() | ||
| .printMessage( | ||
| Diagnostic.Kind.ERROR, | ||
| "Field " | ||
| + field.getSimpleName() | ||
| + " of type BehaviorChangeConfiguration<?> must be annotated with @BehaviorChange", | ||
| field); | ||
| } else { | ||
| processingEnv | ||
| .getMessager() | ||
| .printMessage( | ||
| Diagnostic.Kind.NOTE, | ||
| "Field " | ||
| + field.getSimpleName() | ||
| + " is correctly annotated with @BehaviorChange", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth logging?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's not, but I noticed the processor isn't working so I added this to debug -- see my comment here. If you have any tips on fixing this, that would be awesome. I think the annotation processor is a good suggestion |
||
| field); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private boolean isBehaviorChangeConfiguration(TypeMirror typeMirror, Elements elementUtils) { | ||
| TypeElement typeElement = | ||
| (TypeElement) elementUtils.getTypeElement("BehaviorChangeConfiguration"); | ||
| if (typeElement == null) { | ||
| return false; | ||
| } | ||
| TypeMirror behaviorChangeConfigurationType = typeElement.asType(); | ||
| return processingEnv.getTypeUtils().isAssignable(typeMirror, behaviorChangeConfigurationType); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| org.apache.polaris.core.config.BehaviorChangeProcessor |
Uh oh!
There was an error while loading. Please reload this page.