You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2021/07/23 06:43:07 UTC
[sling-org-apache-sling-feature-extension-apiregions] branch master
updated: SLING-10647 : Support validation mode for deprecated api
This is an automated email from the ASF dual-hosted git repository.
cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-extension-apiregions.git
The following commit(s) were added to refs/heads/master by this push:
new 0b8138e SLING-10647 : Support validation mode for deprecated api
0b8138e is described below
commit 0b8138e8e61930fdb5fd257cbb9bd2e5c61025a7
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Fri Jul 23 08:42:54 2021 +0200
SLING-10647 : Support validation mode for deprecated api
---
.../apiregions/analyser/CheckDeprecatedApi.java | 8 +++++-
.../extension/apiregions/api/ApiExport.java | 31 +++++++++++++++++++---
.../extension/apiregions/api/DeprecationInfo.java | 29 ++++++++++++++++++--
.../apiregions/api/DeprecationValidationMode.java | 27 +++++++++++++++++++
.../extension/apiregions/api/package-info.java | 2 +-
.../extension/apiregions/api/ApiExportTest.java | 12 +++++++++
6 files changed, 102 insertions(+), 7 deletions(-)
diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckDeprecatedApi.java b/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckDeprecatedApi.java
index 2361c4e..f98bd6d 100644
--- a/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckDeprecatedApi.java
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckDeprecatedApi.java
@@ -31,6 +31,7 @@ import org.apache.sling.feature.extension.apiregions.api.ApiExport;
import org.apache.sling.feature.extension.apiregions.api.ApiRegion;
import org.apache.sling.feature.extension.apiregions.api.ApiRegions;
import org.apache.sling.feature.extension.apiregions.api.DeprecationInfo;
+import org.apache.sling.feature.extension.apiregions.api.DeprecationValidationMode;
import org.apache.sling.feature.scanner.BundleDescriptor;
import org.apache.sling.feature.scanner.PackageInfo;
import org.osgi.framework.Version;
@@ -126,7 +127,12 @@ public class CheckDeprecatedApi implements AnalyserTask{
if ( deprecationInfo.getSince() != null ) {
msg = msg.concat(" Deprecated since ").concat(deprecationInfo.getSince());
}
- boolean isError = strict;
+ boolean isError;
+ if ( deprecationInfo.getMode() != null ) {
+ isError = deprecationInfo.getMode() == DeprecationValidationMode.STRICT;
+ } else {
+ isError = strict;
+ }
if ( deprecationInfo.isForRemoval() ) {
boolean printRemoval = true;
if ( checkDate != null ) {
diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/api/ApiExport.java b/src/main/java/org/apache/sling/feature/extension/apiregions/api/ApiExport.java
index bf91b9e..0eab0a5 100644
--- a/src/main/java/org/apache/sling/feature/extension/apiregions/api/ApiExport.java
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/api/ApiExport.java
@@ -45,6 +45,8 @@ public class ApiExport implements Comparable<ApiExport> {
private static final String FOR_REMOVAL_KEY = "for-removal";
+ private static final String MODE_KEY = "mode";
+
private static final String MEMBERS_KEY = "members";
private static final String NAME_KEY = "name";
@@ -213,6 +215,13 @@ public class ApiExport implements Comparable<ApiExport> {
final DeprecationInfo info = new DeprecationInfo(depObj.getString(MSG_KEY));
info.setSince(depObj.getString(SINCE_KEY, null));
info.setForRemoval(depObj.getString(FOR_REMOVAL_KEY, null));
+ if ( depObj.getString(MODE_KEY, null) != null ) {
+ try {
+ info.setMode(DeprecationValidationMode.valueOf(depObj.getString(MODE_KEY)));
+ } catch ( final IllegalArgumentException iae) {
+ throw new IOException(iae);
+ }
+ }
this.getDeprecation().setPackageInfo(info);
} else {
if ( depObj.containsKey(SINCE_KEY) ) {
@@ -221,6 +230,9 @@ public class ApiExport implements Comparable<ApiExport> {
if ( depObj.containsKey(FOR_REMOVAL_KEY) ) {
throw new IOException("Export " + this.getName() + " has wrong for-removal in " + DEPRECATED_KEY);
}
+ if ( depObj.containsKey(MODE_KEY) ) {
+ throw new IOException("Export " + this.getName() + " has wrong mode in " + DEPRECATED_KEY);
+ }
final JsonValue val = depObj.get(MEMBERS_KEY);
if ( val.getValueType() != ValueType.OBJECT) {
throw new IOException("Export " + this.getName() + " has wrong type for " + MEMBERS_KEY + " : " + val.getValueType().name());
@@ -237,7 +249,14 @@ public class ApiExport implements Comparable<ApiExport> {
final DeprecationInfo info = new DeprecationInfo(memberObj.getString(MSG_KEY));
info.setSince(memberObj.getString(SINCE_KEY, null));
info.setForRemoval(depObj.getString(FOR_REMOVAL_KEY, null));
- this.getDeprecation().addMemberInfo(memberProp.getKey(), info);
+ if ( depObj.getString(MODE_KEY, null) != null ) {
+ try {
+ info.setMode(DeprecationValidationMode.valueOf(depObj.getString(MODE_KEY)));
+ } catch ( final IllegalArgumentException iae) {
+ throw new IOException(iae);
+ }
+ }
+ this.getDeprecation().addMemberInfo(memberProp.getKey(), info);
} else {
throw new IOException("Export " + this.getName() + " has wrong type for member in " + MEMBERS_KEY + " : " + memberProp.getValue().getValueType().name());
}
@@ -255,7 +274,7 @@ public class ApiExport implements Comparable<ApiExport> {
JsonValue deprecationToJSON() {
final Deprecation dep = this.getDeprecation();
if ( dep.getPackageInfo() != null ) {
- if ( dep.getPackageInfo().getSince() == null && dep.getPackageInfo().getForRemoval() == null ) {
+ if ( dep.getPackageInfo().getSince() == null && dep.getPackageInfo().getForRemoval() == null && dep.getPackageInfo().getMode() == null) {
return Json.createValue(dep.getPackageInfo().getMessage());
} else {
final JsonObjectBuilder depBuilder = Json.createObjectBuilder();
@@ -266,13 +285,16 @@ public class ApiExport implements Comparable<ApiExport> {
if ( dep.getPackageInfo().getForRemoval() != null ) {
depBuilder.add(FOR_REMOVAL_KEY, dep.getPackageInfo().getForRemoval());
}
+ if ( dep.getPackageInfo().getMode() != null ) {
+ depBuilder.add(MODE_KEY, dep.getPackageInfo().getMode().name());
+ }
return depBuilder.build();
}
} else if ( !dep.getMemberInfos().isEmpty() ) {
final JsonObjectBuilder depBuilder = Json.createObjectBuilder();
final JsonObjectBuilder membersBuilder = Json.createObjectBuilder();
for(final Map.Entry<String, DeprecationInfo> memberEntry : dep.getMemberInfos().entrySet()) {
- if ( memberEntry.getValue().getSince() == null && memberEntry.getValue().getForRemoval() == null ) {
+ if ( memberEntry.getValue().getSince() == null && memberEntry.getValue().getForRemoval() == null && memberEntry.getValue().getMode() == null ) {
membersBuilder.add(memberEntry.getKey(), memberEntry.getValue().getMessage());
} else {
final JsonObjectBuilder mBuilder = Json.createObjectBuilder();
@@ -283,6 +305,9 @@ public class ApiExport implements Comparable<ApiExport> {
if ( memberEntry.getValue().getForRemoval() != null ) {
mBuilder.add(FOR_REMOVAL_KEY, memberEntry.getValue().getForRemoval());
}
+ if ( memberEntry.getValue().getMode() != null ) {
+ mBuilder.add(MODE_KEY, memberEntry.getValue().getMode().name());
+ }
membersBuilder.add(memberEntry.getKey(), mBuilder);
}
}
diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/api/DeprecationInfo.java b/src/main/java/org/apache/sling/feature/extension/apiregions/api/DeprecationInfo.java
index 740109c..adf389d 100644
--- a/src/main/java/org/apache/sling/feature/extension/apiregions/api/DeprecationInfo.java
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/api/DeprecationInfo.java
@@ -37,6 +37,12 @@ public class DeprecationInfo {
private String forRemoval;
/**
+ * Optional validation mode
+ * @since 1.4.0
+ */
+ private DeprecationValidationMode mode;
+
+ /**
* Create a new info
* @param msg The msg
* @throws IllegalArgumentException if msg is {@code null}
@@ -131,9 +137,27 @@ public class DeprecationInfo {
return null;
}
+ /**
+ * Get the optional validation mode.
+ * @return The mode or {@code null}
+ * @since 1.4.0
+ */
+ public DeprecationValidationMode getMode() {
+ return mode;
+ }
+
+ /**
+ * Set the validation mode.
+ * @param value The new mode
+ * @since 1.4.0
+ */
+ public void setMode(final DeprecationValidationMode value) {
+ this.mode = value;
+ }
+
@Override
public int hashCode() {
- return Objects.hash(message, since, forRemoval);
+ return Objects.hash(message, since, forRemoval, mode);
}
@Override
@@ -148,6 +172,7 @@ public class DeprecationInfo {
return false;
}
DeprecationInfo other = (DeprecationInfo) obj;
- return Objects.equals(message, other.message) && Objects.equals(since, other.since) && Objects.equals(forRemoval, other.forRemoval);
+ return Objects.equals(message, other.message) && Objects.equals(since, other.since) && Objects.equals(forRemoval, other.forRemoval)
+ && Objects.equals(mode, other.mode);
}
}
diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/api/DeprecationValidationMode.java b/src/main/java/org/apache/sling/feature/extension/apiregions/api/DeprecationValidationMode.java
new file mode 100644
index 0000000..b404daf
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/api/DeprecationValidationMode.java
@@ -0,0 +1,27 @@
+/*
+ * 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.sling.feature.extension.apiregions.api;
+
+public enum DeprecationValidationMode {
+
+ /** Default mode - if api is used, issue a warning. */
+ LENIENT,
+
+ /** If api is used, issue an error. */
+ STRICT
+
+}
diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/api/package-info.java b/src/main/java/org/apache/sling/feature/extension/apiregions/api/package-info.java
index 4e32aaf..2a621b7 100644
--- a/src/main/java/org/apache/sling/feature/extension/apiregions/api/package-info.java
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/api/package-info.java
@@ -17,7 +17,7 @@
* under the License.
*/
-@org.osgi.annotation.versioning.Version("1.3.0")
+@org.osgi.annotation.versioning.Version("1.4.0")
package org.apache.sling.feature.extension.apiregions.api;
diff --git a/src/test/java/org/apache/sling/feature/extension/apiregions/api/ApiExportTest.java b/src/test/java/org/apache/sling/feature/extension/apiregions/api/ApiExportTest.java
index 60cc3d9..bb8c2fc 100644
--- a/src/test/java/org/apache/sling/feature/extension/apiregions/api/ApiExportTest.java
+++ b/src/test/java/org/apache/sling/feature/extension/apiregions/api/ApiExportTest.java
@@ -155,4 +155,16 @@ public class ApiExportTest {
assertEquals(jv, exp.deprecationToJSON());
}
+
+ @Test
+ public void testMode() throws Exception {
+ final JsonValue jv = getJson("{\"msg\":\"" + MSG + "\",\"mode\":\"" + DeprecationValidationMode.STRICT.name() + "\"}");
+
+ final ApiExport exp = new ApiExport(PCK);
+ exp.parseDeprecation(jv);
+
+ assertEquals(DeprecationValidationMode.STRICT, exp.getDeprecation().getPackageInfo().getMode());
+
+ assertEquals(jv, exp.deprecationToJSON());
+ }
}