Skip to content
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

Fix Deprecated Warnings for Compatibility with Java 21 #2922

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -101,7 +101,9 @@
HttpURLConnection connection = null;
try {
// Open connection to Odoo
connection = (HttpURLConnection) new URL(url).openConnection();
connection = (HttpURLConnection) URI.create(url) //
.toURL() //
.openConnection(); //

Check warning on line 106 in io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/odoo/OdooUtils.java

View check run for this annotation

Codecov / codecov/patch

io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/odoo/OdooUtils.java#L104-L106

Added lines #L104 - L106 were not covered by tests
connection.setConnectTimeout(5000);// 5 secs
connection.setReadTimeout(timeout);// 5 secs
connection.setRequestProperty("Accept-Charset", "US-ASCII");
Expand Down Expand Up @@ -276,8 +278,9 @@
private static Object executeKw(Credentials creds, String model, String action, Object[] arg, Map<String, ?> kw)
throws MalformedURLException, XMLRPCException {
var params = new Object[] { creds.getDatabase(), creds.getUid(), creds.getPassword(), model, action, arg, kw };
var client = new XMLRPCClient(new URL(String.format("%s/xmlrpc/2/object", creds.getUrl())),
XMLRPCClient.FLAGS_NIL);
var uri = URI.create(String.format("%s/xmlrpc/2/object", creds.getUrl()));
var client = new XMLRPCClient(uri.toURL(), XMLRPCClient.FLAGS_NIL);

Check warning on line 282 in io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/odoo/OdooUtils.java

View check run for this annotation

Codecov / codecov/patch

io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/odoo/OdooUtils.java#L281-L282

Added lines #L281 - L282 were not covered by tests

client.setTimeout(60 /* seconds */);
return client.call("execute_kw", params);
}
Expand Down Expand Up @@ -557,9 +560,9 @@

HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL(
credentials.getUrl() + "/report/pdf/" + report + "/" + id + "?session_id=" + session)
.openConnection();
connection = (HttpURLConnection) URI
.create(credentials.getUrl() + "/report/pdf/" + report + "/" + id + "?session_id=" + session)
.toURL().openConnection();

Check warning on line 565 in io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/odoo/OdooUtils.java

View check run for this annotation

Codecov / codecov/patch

io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/odoo/OdooUtils.java#L563-L565

Added lines #L563 - L565 were not covered by tests
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setRequestMethod("GET");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private byte[] generateXlsxFile() throws OpenemsNamedException, IOException {
) {
var ws = workbook.newWorksheet("Export");

Locale currentLocale = new Locale("en", "EN");
Locale currentLocale = Locale.of("en", "EN");

var translationBundle = ResourceBundle.getBundle("io.openems.common.jsonrpc.response.translation",
currentLocale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,11 @@ public NetAdapterSim(String execCmd, String logFilename, boolean multiThread) th
public NetAdapterSim(String execCmd, String logFilename, int listenPort, boolean multiThread) throws IOException {
// save references to file and command
this.execCommand = execCmd;
this.process = Runtime.getRuntime().exec(execCmd);

// Use ProcessBuilder instead of Runtime.getRuntime().exec()
ProcessBuilder processBuilder = new ProcessBuilder(execCmd.split(" "));
processBuilder.redirectErrorStream(true); // Redirect error stream to standard output
this.process = processBuilder.start();
this.processOutput = new BufferedReader(new InputStreamReader(this.process.getInputStream()));
this.processError = new BufferedReader(new InputStreamReader(this.process.getErrorStream()));
this.processInput = new OutputStreamWriter(this.process.getOutputStream());
Expand Down Expand Up @@ -323,7 +327,11 @@ public NetAdapterSim(String execCmd, String logFilename, ServerSocket serverSock
throws IOException {
// save references to file and command
this.execCommand = execCmd;
this.process = Runtime.getRuntime().exec(execCmd);

// Use ProcessBuilder instead of Runtime.getRuntime().exec()
ProcessBuilder processBuilder = new ProcessBuilder(execCmd.split(" "));
processBuilder.redirectErrorStream(true); // Redirect error stream to standard output
this.process = processBuilder.start();
this.processOutput = new BufferedReader(new InputStreamReader(this.process.getInputStream()));
this.processError = new BufferedReader(new InputStreamReader(this.process.getErrorStream()));
this.processInput = new OutputStreamWriter(this.process.getOutputStream());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URI;
import java.util.Base64;

import org.junit.Test;
Expand Down Expand Up @@ -156,7 +156,8 @@ private static JsonElement sendPostRequest(int port, String password, String end
private static JsonElement sendRequest(int port, String requestMethod, String password, String endpoint,
JsonObject request) throws OpenemsNamedException {
try {
var url = new URL("http://127.0.0.1:" + port + endpoint);
var uri = URI.create("http://127.0.0.1:" + port + endpoint);
var url = uri.toURL();
var con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Authorization",
"Basic " + new String(Base64.getEncoder().encode(("x:" + password).getBytes())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,13 @@
* @throws IOException on error
*/
private static String execReadToString(String execCommand) throws IOException {
try (var s = new Scanner(Runtime.getRuntime().exec(execCommand).getInputStream()).useDelimiter("\\A")) {
ProcessBuilder processBuilder = new ProcessBuilder(execCommand.split(" "));
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();

Check warning on line 324 in io.openems.edge.core/src/io/openems/edge/core/host/HostImpl.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.core/src/io/openems/edge/core/host/HostImpl.java#L322-L324

Added lines #L322 - L324 were not covered by tests

try (var s = new Scanner(process.getInputStream()).useDelimiter("\\A")) {

Check warning on line 326 in io.openems.edge.core/src/io/openems/edge/core/host/HostImpl.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L326 was not covered by tests
return s.hasNext() ? s.next().trim() : "";
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.Optional;

Expand Down Expand Up @@ -95,7 +96,8 @@
JsonObject result = null;

try {
URL url = new URL(this.baseUrl + endpoint);
URI uri = URI.create(this.baseUrl + endpoint);
URL url = uri.toURL();
HttpURLConnection con = (HttpURLConnection) url.openConnection();

con.setRequestMethod("GET");
Expand Down Expand Up @@ -149,7 +151,8 @@
JsonObject result = null;

try {
var url = new URL(this.baseUrl + endpoint);
URI uri = URI.create(this.baseUrl + endpoint);
URL url = uri.toURL();

Check warning on line 155 in io.openems.edge.evcs.dezony/src/io/openems/edge/evcs/dezony/DezonyApi.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.evcs.dezony/src/io/openems/edge/evcs/dezony/DezonyApi.java#L154-L155

Added lines #L154 - L155 were not covered by tests
var connection = (HttpURLConnection) url.openConnection();

// Set general information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URI;

import com.google.gson.JsonObject;

Expand Down Expand Up @@ -177,7 +177,8 @@
*/
private JsonObject sendRequest(String urlString, String requestMethod) throws OpenemsNamedException {
try {
var url = new URL(urlString);
var uri = URI.create(urlString);
var url = uri.toURL();

Check warning on line 181 in io.openems.edge.evcs.goe.chargerhome/src/io/openems/edge/evcs/goe/chargerhome/GoeApi.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.evcs.goe.chargerhome/src/io/openems/edge/evcs/goe/chargerhome/GoeApi.java#L180-L181

Added lines #L180 - L181 were not covered by tests
var con = (HttpURLConnection) url.openConnection();
con.setRequestMethod(requestMethod);
con.setConnectTimeout(5000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.URI;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
Expand Down Expand Up @@ -160,7 +160,8 @@

private static Document downloadConfigXml(InetAddress ip, String filename, String username, String password)
throws ParserConfigurationException, SAXException, IOException {
var url = new URL(String.format("http://%s/etc/%s", ip.getHostAddress(), filename));
var uri = URI.create(String.format("http://%s/etc/%s", ip.getHostAddress(), filename));
var url = uri.toURL();

Check warning on line 164 in io.openems.edge.io.wago/src/io/openems/edge/wago/IoWagoImpl.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.io.wago/src/io/openems/edge/wago/IoWagoImpl.java#L163-L164

Added lines #L163 - L164 were not covered by tests
var authStr = String.format("%s:%s", username, password);
var bytesEncoded = Base64.getEncoder().encode(authStr.getBytes());
var authEncoded = new String(bytesEncoded);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URI;
import java.util.Arrays;
import java.util.Base64;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -93,7 +93,8 @@ public JsonObject getLastReading(String meterId, Field... fields) throws Openems
*/
private JsonElement sendGetRequest(String endpoint) throws OpenemsNamedException {
try {
var url = new URL(BASE_URL + endpoint);
var uri = URI.create(BASE_URL + endpoint);
var url = uri.toURL();
var con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Authorization", this.authorizationHeader);
con.setRequestMethod("GET");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.URL;
import java.net.URI;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
Expand Down Expand Up @@ -122,7 +122,7 @@
*/
private JsonObject getResponse(String path) throws OpenemsNamedException {
try {
var url = new URL(this.baseUrl + path);
var url = URI.create(this.baseUrl + path).toURL();

Check warning on line 125 in io.openems.edge.tesla.powerwall2/src/io/openems/edge/tesla/powerwall2/core/ReadWorker.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L125 was not covered by tests
var connection = (HttpsURLConnection) url.openConnection();
connection.setHostnameVerifier((hostname, session) -> true);
try (var reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
Expand Down
Loading