Skip to content

Commit d4d0df4

Browse files
authored
Java 21: fix deprecation warnings caused by Java update (#2922)
See #2890
1 parent 4b768ae commit d4d0df4

File tree

10 files changed

+46
-23
lines changed

10 files changed

+46
-23
lines changed

io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/odoo/OdooUtils.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.io.OutputStreamWriter;
77
import java.net.HttpURLConnection;
88
import java.net.MalformedURLException;
9-
import java.net.URL;
9+
import java.net.URI;
1010
import java.time.ZoneId;
1111
import java.time.ZonedDateTime;
1212
import java.time.format.DateTimeFormatter;
@@ -101,7 +101,9 @@ public static SuccessResponseAndHeaders sendJsonrpcRequest(String url, String co
101101
HttpURLConnection connection = null;
102102
try {
103103
// Open connection to Odoo
104-
connection = (HttpURLConnection) new URL(url).openConnection();
104+
connection = (HttpURLConnection) URI.create(url) //
105+
.toURL() //
106+
.openConnection(); //
105107
connection.setConnectTimeout(5000);// 5 secs
106108
connection.setReadTimeout(timeout);// 5 secs
107109
connection.setRequestProperty("Accept-Charset", "US-ASCII");
@@ -276,8 +278,9 @@ private static Object executeKw(Credentials creds, String model, String action,
276278
private static Object executeKw(Credentials creds, String model, String action, Object[] arg, Map<String, ?> kw)
277279
throws MalformedURLException, XMLRPCException {
278280
var params = new Object[] { creds.getDatabase(), creds.getUid(), creds.getPassword(), model, action, arg, kw };
279-
var client = new XMLRPCClient(new URL(String.format("%s/xmlrpc/2/object", creds.getUrl())),
280-
XMLRPCClient.FLAGS_NIL);
281+
var uri = URI.create(String.format("%s/xmlrpc/2/object", creds.getUrl()));
282+
var client = new XMLRPCClient(uri.toURL(), XMLRPCClient.FLAGS_NIL);
283+
281284
client.setTimeout(60 /* seconds */);
282285
return client.call("execute_kw", params);
283286
}
@@ -557,9 +560,9 @@ protected static byte[] getOdooReport(Credentials credentials, String report, in
557560

558561
HttpURLConnection connection = null;
559562
try {
560-
connection = (HttpURLConnection) new URL(
561-
credentials.getUrl() + "/report/pdf/" + report + "/" + id + "?session_id=" + session)
562-
.openConnection();
563+
connection = (HttpURLConnection) URI
564+
.create(credentials.getUrl() + "/report/pdf/" + report + "/" + id + "?session_id=" + session)
565+
.toURL().openConnection();
563566
connection.setConnectTimeout(5000);
564567
connection.setReadTimeout(5000);
565568
connection.setRequestMethod("GET");

io.openems.common/test/io/openems/common/jsonrpc/response/QueryHistoricTimeseriesExportXlsxResponseTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private byte[] generateXlsxFile() throws OpenemsNamedException, IOException {
7070
) {
7171
var ws = workbook.newWorksheet("Export");
7272

73-
Locale currentLocale = new Locale("en", "EN");
73+
Locale currentLocale = Locale.of("en", "EN");
7474

7575
var translationBundle = ResourceBundle.getBundle("io.openems.common.jsonrpc.response.translation",
7676
currentLocale);

io.openems.edge.bridge.onewire/src/com/dalsemi/onewire/adapter/NetAdapterSim.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,11 @@ public NetAdapterSim(String execCmd, String logFilename, boolean multiThread) th
230230
public NetAdapterSim(String execCmd, String logFilename, int listenPort, boolean multiThread) throws IOException {
231231
// save references to file and command
232232
this.execCommand = execCmd;
233-
this.process = Runtime.getRuntime().exec(execCmd);
233+
234+
// Use ProcessBuilder instead of Runtime.getRuntime().exec()
235+
ProcessBuilder processBuilder = new ProcessBuilder(execCmd.split(" "));
236+
processBuilder.redirectErrorStream(true); // Redirect error stream to standard output
237+
this.process = processBuilder.start();
234238
this.processOutput = new BufferedReader(new InputStreamReader(this.process.getInputStream()));
235239
this.processError = new BufferedReader(new InputStreamReader(this.process.getErrorStream()));
236240
this.processInput = new OutputStreamWriter(this.process.getOutputStream());
@@ -323,7 +327,11 @@ public NetAdapterSim(String execCmd, String logFilename, ServerSocket serverSock
323327
throws IOException {
324328
// save references to file and command
325329
this.execCommand = execCmd;
326-
this.process = Runtime.getRuntime().exec(execCmd);
330+
331+
// Use ProcessBuilder instead of Runtime.getRuntime().exec()
332+
ProcessBuilder processBuilder = new ProcessBuilder(execCmd.split(" "));
333+
processBuilder.redirectErrorStream(true); // Redirect error stream to standard output
334+
this.process = processBuilder.start();
327335
this.processOutput = new BufferedReader(new InputStreamReader(this.process.getInputStream()));
328336
this.processError = new BufferedReader(new InputStreamReader(this.process.getErrorStream()));
329337
this.processInput = new OutputStreamWriter(this.process.getOutputStream());

io.openems.edge.controller.api.rest/test/io/openems/edge/controller/api/rest/readwrite/ControllerApiRestReadWriteImplTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import java.io.IOException;
1414
import java.io.InputStreamReader;
1515
import java.net.HttpURLConnection;
16-
import java.net.URL;
16+
import java.net.URI;
1717
import java.util.Base64;
1818

1919
import org.junit.Test;
@@ -156,7 +156,8 @@ private static JsonElement sendPostRequest(int port, String password, String end
156156
private static JsonElement sendRequest(int port, String requestMethod, String password, String endpoint,
157157
JsonObject request) throws OpenemsNamedException {
158158
try {
159-
var url = new URL("http://127.0.0.1:" + port + endpoint);
159+
var uri = URI.create("http://127.0.0.1:" + port + endpoint);
160+
var url = uri.toURL();
160161
var con = (HttpURLConnection) url.openConnection();
161162
con.setRequestProperty("Authorization",
162163
"Basic " + new String(Base64.getEncoder().encode(("x:" + password).getBytes())));

io.openems.edge.core/src/io/openems/edge/core/host/HostImpl.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,13 @@ protected void logError(Logger log, String message) {
319319
* @throws IOException on error
320320
*/
321321
private static String execReadToString(String execCommand) throws IOException {
322-
try (var s = new Scanner(Runtime.getRuntime().exec(execCommand).getInputStream()).useDelimiter("\\A")) {
322+
ProcessBuilder processBuilder = new ProcessBuilder(execCommand.split(" "));
323+
processBuilder.redirectErrorStream(true);
324+
Process process = processBuilder.start();
325+
326+
try (var s = new Scanner(process.getInputStream()).useDelimiter("\\A")) {
323327
return s.hasNext() ? s.next().trim() : "";
324328
}
325329
}
330+
326331
}

io.openems.edge.evcs.dezony/src/io/openems/edge/evcs/dezony/DezonyApi.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.IOException;
55
import java.io.InputStreamReader;
66
import java.net.HttpURLConnection;
7+
import java.net.URI;
78
import java.net.URL;
89
import java.util.Optional;
910

@@ -95,7 +96,8 @@ public JsonElement sendGetRequest(String endpoint) throws OpenemsNamedException
9596
JsonObject result = null;
9697

9798
try {
98-
URL url = new URL(this.baseUrl + endpoint);
99+
URI uri = URI.create(this.baseUrl + endpoint);
100+
URL url = uri.toURL();
99101
HttpURLConnection con = (HttpURLConnection) url.openConnection();
100102

101103
con.setRequestMethod("GET");
@@ -149,7 +151,8 @@ public JsonObject sendPostRequest(String endpoint) throws OpenemsNamedException
149151
JsonObject result = null;
150152

151153
try {
152-
var url = new URL(this.baseUrl + endpoint);
154+
URI uri = URI.create(this.baseUrl + endpoint);
155+
URL url = uri.toURL();
153156
var connection = (HttpURLConnection) url.openConnection();
154157

155158
// Set general information

io.openems.edge.evcs.goe.chargerhome/src/io/openems/edge/evcs/goe/chargerhome/GoeApi.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.io.IOException;
55
import java.io.InputStreamReader;
66
import java.net.HttpURLConnection;
7-
import java.net.URL;
7+
import java.net.URI;
88

99
import com.google.gson.JsonObject;
1010

@@ -177,7 +177,8 @@ public boolean setMaxEnergy(int maxEnergy) {
177177
*/
178178
private JsonObject sendRequest(String urlString, String requestMethod) throws OpenemsNamedException {
179179
try {
180-
var url = new URL(urlString);
180+
var uri = URI.create(urlString);
181+
var url = uri.toURL();
181182
var con = (HttpURLConnection) url.openConnection();
182183
con.setRequestMethod(requestMethod);
183184
con.setConnectTimeout(5000);

io.openems.edge.io.wago/src/io/openems/edge/wago/IoWagoImpl.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.io.InputStream;
66
import java.net.HttpURLConnection;
77
import java.net.InetAddress;
8-
import java.net.URL;
8+
import java.net.URI;
99
import java.util.ArrayList;
1010
import java.util.Base64;
1111
import java.util.Collections;
@@ -160,7 +160,8 @@ private static Document downloadConfigXml(InetAddress ip, String username, Strin
160160

161161
private static Document downloadConfigXml(InetAddress ip, String filename, String username, String password)
162162
throws ParserConfigurationException, SAXException, IOException {
163-
var url = new URL(String.format("http://%s/etc/%s", ip.getHostAddress(), filename));
163+
var uri = URI.create(String.format("http://%s/etc/%s", ip.getHostAddress(), filename));
164+
var url = uri.toURL();
164165
var authStr = String.format("%s:%s", username, password);
165166
var bytesEncoded = Base64.getEncoder().encode(authStr.getBytes());
166167
var authEncoded = new String(bytesEncoded);

io.openems.edge.meter.discovergy/src/io/openems/edge/meter/discovergy/DiscovergyApiClient.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.io.IOException;
55
import java.io.InputStreamReader;
66
import java.net.HttpURLConnection;
7-
import java.net.URL;
7+
import java.net.URI;
88
import java.util.Arrays;
99
import java.util.Base64;
1010
import java.util.stream.Collectors;
@@ -93,7 +93,8 @@ public JsonObject getLastReading(String meterId, Field... fields) throws Openems
9393
*/
9494
private JsonElement sendGetRequest(String endpoint) throws OpenemsNamedException {
9595
try {
96-
var url = new URL(BASE_URL + endpoint);
96+
var uri = URI.create(BASE_URL + endpoint);
97+
var url = uri.toURL();
9798
var con = (HttpURLConnection) url.openConnection();
9899
con.setRequestProperty("Authorization", this.authorizationHeader);
99100
con.setRequestMethod("GET");

io.openems.edge.tesla.powerwall2/src/io/openems/edge/tesla/powerwall2/core/ReadWorker.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.io.IOException;
55
import java.io.InputStreamReader;
66
import java.net.Inet4Address;
7-
import java.net.URL;
7+
import java.net.URI;
88
import java.security.KeyManagementException;
99
import java.security.NoSuchAlgorithmException;
1010
import java.security.SecureRandom;
@@ -122,7 +122,7 @@ protected void forever() throws Throwable {
122122
*/
123123
private JsonObject getResponse(String path) throws OpenemsNamedException {
124124
try {
125-
var url = new URL(this.baseUrl + path);
125+
var url = URI.create(this.baseUrl + path).toURL();
126126
var connection = (HttpsURLConnection) url.openConnection();
127127
connection.setHostnameVerifier((hostname, session) -> true);
128128
try (var reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {

0 commit comments

Comments
 (0)