ForbiddenApis: Adjust for deprecations in JDK 21+

Fix failures reported when raising the language level to 23
new URL() is deprecated
new Locale() is deprecated
still allow ThreadDeath and Thread.getId() for now to keep support for JDK 8

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1925199 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2025-04-21 12:07:21 +00:00
parent db61f5b4be
commit 0036655df4
11 changed files with 46 additions and 29 deletions

View File

@ -24,6 +24,8 @@ import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
@ -127,11 +129,11 @@ public class TimeStampSimpleHttpClient implements TimeStampHttpClient {
proxy = Proxy.NO_PROXY;
} else {
try {
URL pUrl = new URL(proxyUrl);
URL pUrl = new URI(proxyUrl).toURL();
String host = pUrl.getHost();
int port = pUrl.getPort();
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByName(host), (port == -1 ? 80 : port)));
} catch (IOException ignored) {
} catch (IOException | URISyntaxException ignored) {
}
}
}
@ -205,7 +207,12 @@ public class TimeStampSimpleHttpClient implements TimeStampHttpClient {
}
protected TimeStampHttpClientResponse handleRedirect(String url, MethodHandler handler, boolean followRedirect) throws IOException {
HttpURLConnection huc = (HttpURLConnection)new URL(url).openConnection(proxy);
final HttpURLConnection huc;
try {
huc = (HttpURLConnection)new URI(url).toURL().openConnection(proxy);
} catch (URISyntaxException e) {
throw new IOException(e);
}
if (ignoreHttpsCertificates) {
recklessConnection(huc);
}

View File

@ -41,6 +41,8 @@ import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.KeyStoreException;
@ -597,8 +599,8 @@ class TestSignatureInfo {
public static String getAccessError(String destinationUrl, boolean fireRequest, int timeout) {
URL url;
try {
url = new URL(destinationUrl);
} catch (MalformedURLException e) {
url = new URI(destinationUrl).toURL();
} catch (MalformedURLException | URISyntaxException e) {
throw new IllegalArgumentException("Invalid destination URL", e);
}

View File

@ -33,6 +33,7 @@ import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.util.SuppressForbidden;
import org.apache.poi.util.TempFile;
import org.apache.poi.util.TempFileCreationStrategy;
import org.apache.poi.xssf.streaming.SXSSFSheet;
@ -51,6 +52,7 @@ class TestTempFileThreaded {
// the actual thread-safe temp-file strategy
private static TempFileCreationStrategy createTempFileCreationStrategy(File poiTempFileDirectory) {
return new TempFileCreationStrategy() {
@SuppressForbidden("Thread.getId() is deprecated and replaced with threadId() in JDK 19+")
@Override
public File createTempFile(String prefix, String suffix) throws IOException {
long threadId = Thread.currentThread().getId();

View File

@ -29,6 +29,8 @@ import java.awt.geom.Dimension2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
@ -271,9 +273,9 @@ public final class TestPictures {
@Test
@Disabled("requires an internet connection to a 3rd party site")
// As of 2017-06-20, the file still exists at the specified URL and the test passes.
void testZeroPictureLength() throws IOException {
void testZeroPictureLength() throws IOException, URISyntaxException {
// take the data from www instead of test directory
URL url = new URL("http://www.cs.sfu.ca/~anoop/courses/CMPT-882-Fall-2002/chris.ppt");
URL url = new URI("http://www.cs.sfu.ca/~anoop/courses/CMPT-882-Fall-2002/chris.ppt").toURL();
HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(url.openStream());
/* Assume that the file could retrieved...
InputStream is;

View File

@ -26,6 +26,8 @@ import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
@ -2316,12 +2318,12 @@ final class TestBugs extends BaseTestBugzillaIssues {
}
@Test
void test46515() throws IOException {
void test46515() throws IOException, URISyntaxException {
try (Workbook wb = openSampleWorkbook("46515.xls")) {
// Get structure from webservice
String urlString = "https://poi.apache.org/components/spreadsheet/images/calendar.jpg";
URL structURL = new URL(urlString);
URL structURL = new URI(urlString).toURL();
BufferedImage bimage;
try {
bimage = ImageIO.read(structURL);

View File

@ -26,6 +26,8 @@ import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
@ -171,7 +173,7 @@ public final class ExcelFileFormatDocFunctionExtractor {
String returnClass, String paramClasses, String volatileFlagStr) {
boolean isVolatile = volatileFlagStr.length() > 0;
Integer funcIxKey = Integer.valueOf(funcIx);
Integer funcIxKey = funcIx;
if(!_groupFunctionIndexes.add(funcIxKey)) {
throw new RuntimeException("Duplicate function index (" + funcIx + ")");
}
@ -209,7 +211,7 @@ public final class ExcelFileFormatDocFunctionExtractor {
throw new RuntimeException("changing function '"
+ funcName + "' definition without foot-note");
}
_allFunctionsByIndex.remove(Integer.valueOf(fdPrev.getIndex()));
_allFunctionsByIndex.remove(fdPrev.getIndex());
}
}
@ -326,7 +328,7 @@ public final class ExcelFileFormatDocFunctionExtractor {
processTableRow(cellData, noteFlags);
} else if(matchesRelPath(TABLE_CELL_RELPATH_NAMES)) {
_rowData.add(_textNodeBuffer.toString().trim());
_rowNoteFlags.add(Boolean.valueOf(_cellHasNote));
_rowNoteFlags.add(_cellHasNote);
_textNodeBuffer.setLength(0);
}
}
@ -350,7 +352,7 @@ public final class ExcelFileFormatDocFunctionExtractor {
}
int funcIx = parseInt(funcIxStr);
boolean hasFootnote = noteFlags[i + 1].booleanValue();
boolean hasFootnote = noteFlags[i + 1];
String funcName = cellData[i + 1];
int minParams = parseInt(cellData[i + 2]);
int maxParams = parseInt(cellData[i + 3]);
@ -577,8 +579,8 @@ public final class ExcelFileFormatDocFunctionExtractor {
private static File downloadSourceFile() {
URL url;
try {
url = new URL("http://sc.openoffice.org/" + SOURCE_DOC_FILE_NAME);
} catch (MalformedURLException e) {
url = new URI("http://sc.openoffice.org/" + SOURCE_DOC_FILE_NAME).toURL();
} catch (MalformedURLException | URISyntaxException e) {
throw new RuntimeException(e);
}

View File

@ -69,7 +69,7 @@ final class TestNumericFunction {
void testDOLLAR() {
Locale defaultLocale = LocaleUtil.getUserLocale();
try {
LocaleUtil.setUserLocale(new Locale("en", "US"));
LocaleUtil.setUserLocale(new Locale.Builder().setLanguage("en").setRegion("US").build());
HSSFWorkbook wb = new HSSFWorkbook();
HSSFCell cell = wb.createSheet().createRow(0).createCell(0);
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
@ -89,7 +89,7 @@ final class TestNumericFunction {
void testDOLLARIreland() {
Locale defaultLocale = LocaleUtil.getUserLocale();
try {
LocaleUtil.setUserLocale(new Locale("en", "IE"));
LocaleUtil.setUserLocale(new Locale.Builder().setLanguage("en").setRegion("IE").build());
HSSFWorkbook wb = new HSSFWorkbook();
HSSFCell cell = wb.createSheet().createRow(0).createCell(0);
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
@ -104,7 +104,7 @@ final class TestNumericFunction {
void testDOLLARSpain() {
Locale defaultLocale = LocaleUtil.getUserLocale();
try {
LocaleUtil.setUserLocale(new Locale("es", "ES"));
LocaleUtil.setUserLocale(new Locale.Builder().setLanguage("es").setRegion("ES").build());
HSSFWorkbook wb = new HSSFWorkbook();
HSSFCell cell = wb.createSheet().createRow(0).createCell(0);
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
@ -119,7 +119,7 @@ final class TestNumericFunction {
void testDOLLARJapan() {
Locale defaultLocale = LocaleUtil.getUserLocale();
try {
LocaleUtil.setUserLocale(new Locale("ja", "JP"));
LocaleUtil.setUserLocale(new Locale.Builder().setLanguage("ja").setRegion("JP").build());
HSSFWorkbook wb = new HSSFWorkbook();
HSSFCell cell = wb.createSheet().createRow(0).createCell(0);
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
@ -135,7 +135,7 @@ final class TestNumericFunction {
void testDOLLARDenmark() {
Locale defaultLocale = LocaleUtil.getUserLocale();
try {
LocaleUtil.setUserLocale(new Locale("da", "DK"));
LocaleUtil.setUserLocale(new Locale.Builder().setLanguage("da").setRegion("DK").build());
HSSFWorkbook wb = new HSSFWorkbook();
HSSFCell cell = wb.createSheet().createRow(0).createCell(0);
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);

View File

@ -937,7 +937,7 @@ class TestDataFormatter {
// 2017-12-01 09:54:33 which is 42747.412892397523 as double
DataFormatter dfDE = new DataFormatter(Locale.GERMANY);
DataFormatter dfZH = new DataFormatter(Locale.PRC);
DataFormatter dfIE = new DataFormatter(new Locale("GA", "IE"));
DataFormatter dfIE = new DataFormatter(new Locale.Builder().setLanguage("GA").setRegion("IE").build());
double date = 42747.412892397523;
String format = "dd MMMM yyyy HH:mm:ss";
assertEquals("12 Januar 2017 09:54:33", dfDE.formatRawCellContents(date, -1, format));

View File

@ -103,15 +103,15 @@ class TestExcelStyleDateFormatter {
public static Stream<Arguments> initializeLocales() throws ParseException {
Object[][] locExps = {
{ Locale.GERMAN, "JFMAMJJASOND" },
{ new Locale("de", "AT"), "JFMAMJJASOND" },
{ new Locale.Builder().setLanguage("de").setRegion("AT").build(), "JFMAMJJASOND" },
{ Locale.UK, "JFMAMJJASOND" },
{ new Locale("en", "IN"), "JFMAMJJASOND" },
{ new Locale("in", "ID"), "JFMAMJJASOND" },
{ new Locale.Builder().setLanguage("en").setRegion("IN").build(), "JFMAMJJASOND" },
{ new Locale.Builder().setLanguage("in").setRegion("ID").build(), "JFMAMJJASOND" },
{ Locale.FRENCH, "jfmamjjasond" },
{ new Locale("ru", "RU"), "\u044f\u0444\u043c\u0430\u043c\u0438\u0438\u0430\u0441\u043e\u043d\u0434" },
{ new Locale.Builder().setLanguage("ru").setRegion("RU").build(), "\u044f\u0444\u043c\u0430\u043c\u0438\u0438\u0430\u0441\u043e\u043d\u0434" },
{ Locale.CHINESE, localeIndex(Locale.CHINESE) == 0 ? "\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u4e03\u516b\u4e5d\u5341\u5341\u5341" : "123456789111" },
{ new Locale("tr", "TR"), "\u004f\u015e\u004d\u004e\u004d\u0048\u0054\u0041\u0045\u0045\u004b\u0041" },
{ new Locale("hu", "HU"), "\u006a\u0066\u006d\u00e1\u006d\u006a\u006a\u0061\u0073\u006f\u006e\u0064" }
{ new Locale.Builder().setLanguage("tr").setRegion("TR").build(), "\u004f\u015e\u004d\u004e\u004d\u0048\u0054\u0041\u0045\u0045\u004b\u0041" },
{ new Locale.Builder().setLanguage("hu").setRegion("HU").build(), "\u006a\u0066\u006d\u00e1\u006d\u006a\u006a\u0061\u0073\u006f\u006e\u0064" }
};
String[] dates = {

View File

@ -130,7 +130,7 @@ final class TestDateFormatConverter {
void testJDK8EmptyLocale() {
// JDK 8 seems to add an empty locale-string to the list returned via DateFormat.getAvailableLocales()
// therefore we now cater for this special locale as well
String prefix = getPrefixForLocale(new Locale(""));
String prefix = getPrefixForLocale(new Locale.Builder().build());
assertEquals("", prefix);
}

View File

@ -42,7 +42,7 @@ class ExceptionUtilTest {
assertTrue(ExceptionUtil.isFatal(new VirtualMachineError(){}));
}
@SuppressForbidden("Test with ThreadDeath on purpose here")
@Test
void testThreadDeath() {
assertTrue(ExceptionUtil.isFatal(new ThreadDeath()));