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 issues identified with static code analysis #5351

Merged
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 @@ -88,7 +88,7 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa
if ("docker".equals(containerRuntime)) {
String uid = getLinuxID("-ur");
String gid = getLinuxID("-gr");
if (uid != null & gid != null & !"".equals(uid) & !"".equals(gid)) {
if (uid != null && gid != null && !"".equals(uid) && !"".equals(gid)) {
Collections.addAll(nativeImage, "--user", uid + ":" + gid);
}
} else if ("podman".equals(containerRuntime)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,9 @@ private JsonObject processDependency(Artifact artifact) throws IOException {
if (onode != null) {
// TODO: this is a dirty hack to avoid redoing existing javax.json code
String json = getMapper(false).writeValueAsString(onode);
JsonReader jsonReader = Json.createReader(new StringReader(json));
return jsonReader.readObject();
try (JsonReader jsonReader = Json.createReader(new StringReader(json))) {
return jsonReader.readObject();
}
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ public void registerField(FieldInfo fieldInfo) {
case SERVICE_PROVIDER:
generatedResource.produce(
new GeneratedResourceBuildItem("META-INF/services/" + resource.getName(), resource.getData()));
break;
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.quarkus.elytron.security.runtime;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -84,9 +86,11 @@ public void run() {
throw new IllegalStateException(msg);
}
LegacyPropertiesSecurityRealm propsRealm = (LegacyPropertiesSecurityRealm) secRealm;
propsRealm.load(users.openStream(), roles.openStream());
try (InputStream usersStream = users.openStream(); InputStream rolesStream = roles.openStream()) {
propsRealm.load(usersStream, rolesStream);
}
} catch (IOException e) {
throw new RuntimeException(e);
throw new UncheckedIOException(e);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,22 @@ public SecurityIdentity get() {
return null;
}
PasswordCredential cred = id.getCredential(PasswordCredential.class);
ServerAuthenticationContext ac = domain.createNewAuthenticationContext();
ac.setAuthenticationName(request.getPrincipal());
ac.addPrivateCredential(cred);
ac.authorize();
result = ac.getAuthorizedIdentity();
try (ServerAuthenticationContext ac = domain.createNewAuthenticationContext()) {
ac.setAuthenticationName(request.getPrincipal());
ac.addPrivateCredential(cred);
ac.authorize();
result = ac.getAuthorizedIdentity();

if (result == null) {
throw new AuthenticationFailedException();
if (result == null) {
throw new AuthenticationFailedException();
}
QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder();
builder.setPrincipal(result.getPrincipal());
for (String i : result.getRoles()) {
builder.addRole(i);
}
return builder.build();
}
QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder();
builder.setPrincipal(result.getPrincipal());
for (String i : result.getRoles()) {
builder.addRole(i);
}
return builder.build();
} catch (RealmUnavailableException e) {
throw new RuntimeException(e);
} catch (SecurityException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public Result parse(MethodInfo methodInfo) {
boolean allIgnoreCase = false;
if (afterByPart.contains(ALL_IGNORE_CASE)) {
allIgnoreCase = true;
afterByPart = afterByPart.replaceAll(ALL_IGNORE_CASE, "");
afterByPart = afterByPart.replace(ALL_IGNORE_CASE, "");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prevents a regex pattern compilation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting!

}

// handle the 'OrderBy' clause which is assumed to be at the end of the query
Expand All @@ -131,10 +131,10 @@ public Result parse(MethodInfo methodInfo) {
boolean ascending = true;
if (afterOrderByPart.endsWith("Asc")) {
ascending = true;
afterOrderByPart = afterOrderByPart.replaceAll("Asc", "");
afterOrderByPart = afterOrderByPart.replace("Asc", "");
} else if (afterOrderByPart.endsWith("Desc")) {
ascending = false;
afterOrderByPart = afterOrderByPart.replaceAll("Desc", "");
afterOrderByPart = afterOrderByPart.replace("Desc", "");
}
String orderField = lowerFirstLetter(afterOrderByPart);
if (!entityContainsField(orderField)) {
Expand Down Expand Up @@ -176,7 +176,7 @@ public Result parse(MethodInfo methodInfo) {

if (part.endsWith(IGNORE_CASE)) {
ignoreCase = true;
part = part.replaceAll(IGNORE_CASE, "");
part = part.replace(IGNORE_CASE, "");
}

String operation = getFieldOperation(part);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private static boolean matchLabels(Pattern pattern, List<String> labels) {
boolean matches = false;
// if any label match it's ok
for(String label : labels) {
matches = matches | pattern.matcher(label.toLowerCase()).matches();
matches = matches || pattern.matcher(label.toLowerCase()).matches();
}
return matches;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javax.inject.Inject;

import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.api.core.client.ClientConsumer;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.client.ClientSession;
import org.apache.activemq.artemis.api.core.client.ClientSessionFactory;
Expand All @@ -26,9 +27,11 @@ public void init() throws Exception {
public String receive() {
try (ClientSession session = connection.createSession()) {
session.start();
ClientMessage message = session.createConsumer("test-core").receive(1000L);
message.acknowledge();
return message.getBodyBuffer().readString();
try (ClientConsumer consumer = session.createConsumer("test-core")) {
ClientMessage message = consumer.receive(1000L);
message.acknowledge();
return message.getBodyBuffer().readString();
}
} catch (ActiveMQException e) {
throw new RuntimeException("Could not receive message", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.client.ClientProducer;
import org.apache.activemq.artemis.api.core.client.ClientSession;
import org.apache.activemq.artemis.api.core.client.ClientSessionFactory;
import org.apache.activemq.artemis.api.core.client.ServerLocator;
Expand All @@ -27,7 +28,9 @@ public void send(String body) {
try (ClientSession session = connection.createSession()) {
ClientMessage message = session.createMessage(true);
message.getBodyBuffer().writeString(body);
session.createProducer("test-core").send(message);
try (ClientProducer producer = session.createProducer("test-core")) {
producer.send(message);
}
} catch (ActiveMQException e) {
throw new RuntimeException("Could not send message", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class ArtemisConsumerManager {
ConnectionFactory connectionFactory;

public String receive() {
try (JMSContext context = connectionFactory.createContext(Session.AUTO_ACKNOWLEDGE)) {
JMSConsumer consumer = context.createConsumer(context.createQueue("test-jms"));
try (JMSContext context = connectionFactory.createContext(Session.AUTO_ACKNOWLEDGE);
JMSConsumer consumer = context.createConsumer(context.createQueue("test-jms"))) {
return consumer.receive(1000L).getBody(String.class);
} catch (JMSException e) {
throw new RuntimeException("Could not receive message", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,18 @@ public String fooProvider() {
@GET
@Path("/from-json")
@Produces("application/json")
public MyEntity fromJson() {
public MyEntity fromJson() throws Exception {
MyEntity entity = new MyEntity();
entity.name = "my entity name";
entity.value = "my entity value";

JsonbConfig config = new JsonbConfig();
Jsonb jsonb = JsonbBuilder.create(config);
String json = jsonb.toJson(entity);
MyEntity fromJsonEntity = jsonb.fromJson(json, MyEntity.class);
try (Jsonb jsonb = JsonbBuilder.create(config)) {
String json = jsonb.toJson(entity);
MyEntity fromJsonEntity = jsonb.fromJson(json, MyEntity.class);

return fromJsonEntity;
return fromJsonEntity;
}
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;

public class ServerDtoDecoder implements Decoder.TextStream<Dto> {
@Override
public Dto decode(Reader reader) {
JsonObject jsonObject = Json.createReader(reader).readObject();
Dto result = new Dto();
result.setContent(jsonObject.getString("content"));
return result;
try (JsonReader jsonReader = Json.createReader(reader)) {
JsonObject jsonObject = jsonReader.readObject();
Dto result = new Dto();
result.setContent(jsonObject.getString("content"));
return result;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonWriter;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;

Expand All @@ -13,8 +14,9 @@ public void encode(Dto object, Writer writer) {
JsonObject jsonObject = Json.createObjectBuilder()
.add("content", object.getContent())
.build();
Json.createWriter(writer)
.writeObject(jsonObject);
try (JsonWriter jsonWriter = Json.createWriter(writer)) {
jsonWriter.writeObject(jsonObject);
}
}

@Override
Expand Down