From 53db9e82888a344ce5ebea566734cf7b8f89f0bc Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Thu, 14 Oct 2021 11:14:17 +0200 Subject: [PATCH 1/7] #1981: Avoid OSGi bundle's dependency on sun.misc package --- gson/bnd.bnd | 1 + .../com/google/gson/regression/OSGiTest.java | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 gson/src/test/java/com/google/gson/regression/OSGiTest.java diff --git a/gson/bnd.bnd b/gson/bnd.bnd index 57a8657fd8..6ce7ad1faf 100644 --- a/gson/bnd.bnd +++ b/gson/bnd.bnd @@ -5,6 +5,7 @@ Bundle-Vendor: Google Gson Project Bundle-ContactAddress: ${project.parent.url} Bundle-RequiredExecutionEnvironment: JavaSE-1.6, JavaSE-1.7, JavaSE-1.8 Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.6))" +Import-Package: com.google.gson.*,!sun.misc -removeheaders: Private-Package diff --git a/gson/src/test/java/com/google/gson/regression/OSGiTest.java b/gson/src/test/java/com/google/gson/regression/OSGiTest.java new file mode 100644 index 0000000000..5a8fbe05c6 --- /dev/null +++ b/gson/src/test/java/com/google/gson/regression/OSGiTest.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2016 Google Inc. + * + * Licensed 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 com.google.gson.regression; + +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.jar.Manifest; + +import junit.framework.TestCase; + +public class OSGiTest extends TestCase { + public void testSunMiscImportPackage() throws Exception { + Manifest mf = findManifest("com.google.gson"); + String importPkg = mf.getMainAttributes().getValue("Import-Package"); + assertNotNull("Import-Package statement is currently there", importPkg); + assertEquals("There should be no sun.misc dependency, but was: " + importPkg, -1, importPkg.indexOf("sun.misc")); + } + + private Manifest findManifest(String pkg) throws IOException { + Enumeration en = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF"); + List urls = new ArrayList(); + while (en.hasMoreElements()) { + URL u = en.nextElement(); + Manifest mf = new Manifest(u.openStream()); + if (pkg.equals(mf.getMainAttributes().getValue("Bundle-SymbolicName"))) { + return mf; + } + urls.add(u); + } + fail("Cannot find com.google.gson OSGi bundle manifest among: " + urls); + return null; + } +} From 91a780a3ffc368bfec15eb56623a516863df604e Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Fri, 22 Oct 2021 01:15:30 +0200 Subject: [PATCH 2/7] Specify optional dependency on sun.misc.Unsafe --- gson/bnd.bnd | 4 ++++ gson/src/main/java/module-info.java | 3 +++ 2 files changed, 7 insertions(+) diff --git a/gson/bnd.bnd b/gson/bnd.bnd index 57a8657fd8..07746f0a48 100644 --- a/gson/bnd.bnd +++ b/gson/bnd.bnd @@ -6,6 +6,10 @@ Bundle-ContactAddress: ${project.parent.url} Bundle-RequiredExecutionEnvironment: JavaSE-1.6, JavaSE-1.7, JavaSE-1.8 Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.6))" +# Optional dependency for JDK's sun.misc.Unsafe +# https://bnd.bndtools.org/chapters/920-faq.html#remove-unwanted-imports- +Import-Package: sun.misc;resolution:=optional, * + -removeheaders: Private-Package -exportcontents:\ diff --git a/gson/src/main/java/module-info.java b/gson/src/main/java/module-info.java index 38c26e569c..0134c9dc19 100644 --- a/gson/src/main/java/module-info.java +++ b/gson/src/main/java/module-info.java @@ -10,4 +10,7 @@ // Optional dependency on java.sql requires static java.sql; + + // Optional dependency on jdk.unsupported for JDK's sun.misc.Unsafe + requires static jdk.unsupported; } From 857a561e13892ac410134537b45ee2807cb63b92 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Fri, 22 Oct 2021 06:13:54 +0200 Subject: [PATCH 3/7] Adjusting the test to sun.misc import being optional --- gson/bnd.bnd | 1 - .../com/google/gson/regression/OSGiTest.java | 16 ++++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/gson/bnd.bnd b/gson/bnd.bnd index 001d10099e..07746f0a48 100644 --- a/gson/bnd.bnd +++ b/gson/bnd.bnd @@ -5,7 +5,6 @@ Bundle-Vendor: Google Gson Project Bundle-ContactAddress: ${project.parent.url} Bundle-RequiredExecutionEnvironment: JavaSE-1.6, JavaSE-1.7, JavaSE-1.8 Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.6))" -Import-Package: com.google.gson.*,!sun.misc # Optional dependency for JDK's sun.misc.Unsafe # https://bnd.bndtools.org/chapters/920-faq.html#remove-unwanted-imports- diff --git a/gson/src/test/java/com/google/gson/regression/OSGiTest.java b/gson/src/test/java/com/google/gson/regression/OSGiTest.java index 5a8fbe05c6..95000c1938 100644 --- a/gson/src/test/java/com/google/gson/regression/OSGiTest.java +++ b/gson/src/test/java/com/google/gson/regression/OSGiTest.java @@ -25,11 +25,23 @@ import junit.framework.TestCase; public class OSGiTest extends TestCase { + public void testComGoogleGsonAnnotationsPackage() throws Exception { + Manifest mf = findManifest("com.google.gson"); + String importPkg = mf.getMainAttributes().getValue("Import-Package"); + assertNotNull("Import-Package statement is there", importPkg); + assertNotSame("There should be com.google.gson.annotations dependency, but was: " + importPkg, -1, importPkg.indexOf("com.google.gson.annotations")); + } public void testSunMiscImportPackage() throws Exception { Manifest mf = findManifest("com.google.gson"); String importPkg = mf.getMainAttributes().getValue("Import-Package"); - assertNotNull("Import-Package statement is currently there", importPkg); - assertEquals("There should be no sun.misc dependency, but was: " + importPkg, -1, importPkg.indexOf("sun.misc")); + assertNotNull("Import-Package statement is there", importPkg); + for (String dep : importPkg.split(",")) { + if (dep.contains("sun.misc")) { + assertNotSame("sun.misc import is optional", -1, dep.indexOf("resolution:=optional")); + return; + } + } + fail("There should be sun.misc dependency, but was: " + importPkg); } private Manifest findManifest(String pkg) throws IOException { From d0835fcd588aef58b5c32e028009fb278f07cacd Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Fri, 22 Oct 2021 06:20:29 +0200 Subject: [PATCH 4/7] Using Collections.list and for loop --- gson/src/test/java/com/google/gson/regression/OSGiTest.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/gson/src/test/java/com/google/gson/regression/OSGiTest.java b/gson/src/test/java/com/google/gson/regression/OSGiTest.java index 95000c1938..79f0876edc 100644 --- a/gson/src/test/java/com/google/gson/regression/OSGiTest.java +++ b/gson/src/test/java/com/google/gson/regression/OSGiTest.java @@ -18,7 +18,7 @@ import java.io.IOException; import java.net.URL; import java.util.ArrayList; -import java.util.Enumeration; +import java.util.Collections; import java.util.List; import java.util.jar.Manifest; @@ -45,10 +45,8 @@ public void testSunMiscImportPackage() throws Exception { } private Manifest findManifest(String pkg) throws IOException { - Enumeration en = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF"); List urls = new ArrayList(); - while (en.hasMoreElements()) { - URL u = en.nextElement(); + for (URL u : Collections.list(getClass().getClassLoader().getResources("META-INF/MANIFEST.MF"))) { Manifest mf = new Manifest(u.openStream()); if (pkg.equals(mf.getMainAttributes().getValue("Bundle-SymbolicName"))) { return mf; From 3dc8d3c35de25eeec1af031d0382af2bcd1ff20c Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Fri, 22 Oct 2021 11:32:58 +0200 Subject: [PATCH 5/7] Let the fail message include name of package Co-authored-by: Marcono1234 --- gson/src/test/java/com/google/gson/regression/OSGiTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gson/src/test/java/com/google/gson/regression/OSGiTest.java b/gson/src/test/java/com/google/gson/regression/OSGiTest.java index 79f0876edc..6346a30da8 100644 --- a/gson/src/test/java/com/google/gson/regression/OSGiTest.java +++ b/gson/src/test/java/com/google/gson/regression/OSGiTest.java @@ -53,7 +53,7 @@ private Manifest findManifest(String pkg) throws IOException { } urls.add(u); } - fail("Cannot find com.google.gson OSGi bundle manifest among: " + urls); + fail("Cannot find " + pkg + " OSGi bundle manifest among: " + urls); return null; } } From 306e29520061de288f5ec05119ae31008ff4e060 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Fri, 22 Oct 2021 11:36:50 +0200 Subject: [PATCH 6/7] Closing the input stream --- gson/src/test/java/com/google/gson/regression/OSGiTest.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gson/src/test/java/com/google/gson/regression/OSGiTest.java b/gson/src/test/java/com/google/gson/regression/OSGiTest.java index 6346a30da8..82d5234675 100644 --- a/gson/src/test/java/com/google/gson/regression/OSGiTest.java +++ b/gson/src/test/java/com/google/gson/regression/OSGiTest.java @@ -15,6 +15,7 @@ */ package com.google.gson.regression; +import java.io.InputStream; import java.io.IOException; import java.net.URL; import java.util.ArrayList; @@ -31,6 +32,7 @@ public void testComGoogleGsonAnnotationsPackage() throws Exception { assertNotNull("Import-Package statement is there", importPkg); assertNotSame("There should be com.google.gson.annotations dependency, but was: " + importPkg, -1, importPkg.indexOf("com.google.gson.annotations")); } + public void testSunMiscImportPackage() throws Exception { Manifest mf = findManifest("com.google.gson"); String importPkg = mf.getMainAttributes().getValue("Import-Package"); @@ -47,7 +49,9 @@ public void testSunMiscImportPackage() throws Exception { private Manifest findManifest(String pkg) throws IOException { List urls = new ArrayList(); for (URL u : Collections.list(getClass().getClassLoader().getResources("META-INF/MANIFEST.MF"))) { - Manifest mf = new Manifest(u.openStream()); + InputStream is = u.openStream(); + Manifest mf = new Manifest(is); + is.close(); if (pkg.equals(mf.getMainAttributes().getValue("Bundle-SymbolicName"))) { return mf; } From 3c1c5ff7f9447c55df4fa99f29c44557ebd69689 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Fri, 22 Oct 2021 11:42:03 +0200 Subject: [PATCH 7/7] Dedicated assertSubstring method --- .../java/com/google/gson/regression/OSGiTest.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/gson/src/test/java/com/google/gson/regression/OSGiTest.java b/gson/src/test/java/com/google/gson/regression/OSGiTest.java index 82d5234675..64a7e371ab 100644 --- a/gson/src/test/java/com/google/gson/regression/OSGiTest.java +++ b/gson/src/test/java/com/google/gson/regression/OSGiTest.java @@ -30,7 +30,7 @@ public void testComGoogleGsonAnnotationsPackage() throws Exception { Manifest mf = findManifest("com.google.gson"); String importPkg = mf.getMainAttributes().getValue("Import-Package"); assertNotNull("Import-Package statement is there", importPkg); - assertNotSame("There should be com.google.gson.annotations dependency, but was: " + importPkg, -1, importPkg.indexOf("com.google.gson.annotations")); + assertSubstring("There should be com.google.gson.annotations dependency", importPkg, "com.google.gson.annotations"); } public void testSunMiscImportPackage() throws Exception { @@ -39,7 +39,7 @@ public void testSunMiscImportPackage() throws Exception { assertNotNull("Import-Package statement is there", importPkg); for (String dep : importPkg.split(",")) { if (dep.contains("sun.misc")) { - assertNotSame("sun.misc import is optional", -1, dep.indexOf("resolution:=optional")); + assertSubstring("sun.misc import is optional", dep, "resolution:=optional"); return; } } @@ -60,4 +60,11 @@ private Manifest findManifest(String pkg) throws IOException { fail("Cannot find " + pkg + " OSGi bundle manifest among: " + urls); return null; } + + private static void assertSubstring(String msg, String wholeText, String subString) { + if (wholeText.contains(subString)) { + return; + } + fail(msg + ". Expecting " + subString + " but was: " + wholeText); + } }