Skip to content

Commit 5aba88e

Browse files
committed
Merge pull request #472 from JLLeitschuh/chore/fixBugs
Cleans up most of FindBugs Warnings
2 parents 1cdfde4 + 014123a commit 5aba88e

12 files changed

Lines changed: 92 additions & 65 deletions

File tree

buildSrc/src/main/java/edu/wpi/gripgenerator/FileParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class FileParser {
5454
*/
5555
private static InputStream preProcessStream(InputStream stream) {
5656
//FIXME: This is a hack around. This should be removed once the above noted issue is resolved.
57-
java.util.Scanner s = new java.util.Scanner(stream).useDelimiter("\\A");
57+
java.util.Scanner s = new java.util.Scanner(stream, StandardCharsets.UTF_8.name()).useDelimiter("\\A");
5858
String input = s.hasNext() ? s.next() : "";
5959
input = input.replaceAll(methodReorderPattern, methodNewOrder);
6060
return new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));

buildSrc/src/main/java/edu/wpi/gripgenerator/templates/OperationList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
public class OperationList {
3030
private final PackageDeclaration packageDec = new PackageDeclaration(new NameExpr("edu.wpi.grip.generated"));
31-
private final String className = "CVOperations";
31+
private static final String className = "CVOperations";
3232
private final List<ImportDeclaration> imports;
3333
private final List<Operation> operations;
3434

buildSrc/src/main/java/edu/wpi/gripgenerator/templates/SocketHintDeclarationCollection.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,11 @@ public BlockStmt getOutputSocketBody() {
246246

247247
public List<SocketHintDeclaration> getAllSocketHints() {
248248
List<SocketHintDeclaration> socketHintDeclarations = new ArrayList<>();
249-
for (Type type : inputHintsMap.keySet()) {
250-
socketHintDeclarations.add(new SocketHintDeclaration(collector, type, inputHintsMap.get(type), DefinedParamType.DefinedParamDirection.INPUT));
249+
for (Map.Entry<Type, List<DefinedParamType>> type : inputHintsMap.entrySet()) {
250+
socketHintDeclarations.add(new SocketHintDeclaration(collector, type.getKey(), type.getValue(), DefinedParamType.DefinedParamDirection.INPUT));
251251
}
252-
for (Type type : outputHintsMap.keySet()) {
253-
socketHintDeclarations.add(new SocketHintDeclaration(collector, type, outputHintsMap.get(type), DefinedParamType.DefinedParamDirection.OUTPUT));
252+
for (Map.Entry<Type, List<DefinedParamType>> type : outputHintsMap.entrySet()) {
253+
socketHintDeclarations.add(new SocketHintDeclaration(collector, type.getKey(), type.getValue(), DefinedParamType.DefinedParamDirection.OUTPUT));
254254
}
255255
return socketHintDeclarations;
256256
}

core/src/main/java/edu/wpi/grip/core/Connection.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ public void onConnectionRemoved(ConnectionRemovedEvent e) {
7272
@Subscribe
7373
public void removeConnection(StepRemovedEvent e) {
7474
// Remove this connection if one of the steps it was connected to was removed
75-
for (Socket socket : e.getStep().getOutputSockets()) {
76-
if (socket == this.inputSocket || socket == this.outputSocket) {
75+
for (OutputSocket socket : e.getStep().getOutputSockets()) {
76+
if (socket == this.outputSocket) {
7777
this.eventBus.post(new ConnectionRemovedEvent(this));
7878
return;
7979
}
8080
}
8181

82-
for (Socket socket : e.getStep().getInputSockets()) {
83-
if (socket == this.inputSocket || socket == this.outputSocket) {
82+
for (InputSocket socket : e.getStep().getInputSockets()) {
83+
if (socket == this.inputSocket) {
8484
this.eventBus.post(new ConnectionRemovedEvent(this));
8585
return;
8686
}

core/src/main/java/edu/wpi/grip/core/serialization/Project.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package edu.wpi.grip.core.serialization;
22

3-
import com.google.common.eventbus.EventBus;
3+
import com.google.common.annotations.VisibleForTesting;
44
import com.thoughtworks.xstream.XStream;
55
import edu.wpi.grip.core.*;
66
import edu.wpi.grip.core.sources.CameraSource;
@@ -10,6 +10,7 @@
1010
import javax.inject.Inject;
1111
import javax.inject.Singleton;
1212
import java.io.*;
13+
import java.nio.charset.StandardCharsets;
1314
import java.util.Optional;
1415

1516
/**
@@ -18,12 +19,8 @@
1819
@Singleton
1920
public class Project {
2021

21-
@Inject
22-
private EventBus eventBus;
2322
@Inject
2423
private Pipeline pipeline;
25-
@Inject
26-
private Palette palette;
2724

2825
protected final XStream xstream = new XStream();
2926
private Optional<File> file = Optional.empty();
@@ -59,11 +56,14 @@ public void setFile(Optional<File> file) {
5956
* Load the project from a file
6057
*/
6158
public void open(File file) throws IOException {
62-
this.open(new FileReader(file));
59+
try (final InputStreamReader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) {
60+
this.open(reader);
61+
}
6362
this.file = Optional.of(file);
6463
}
6564

66-
public void open(Reader reader) {
65+
@VisibleForTesting
66+
void open(Reader reader) {
6767
this.pipeline.clear();
6868
this.xstream.fromXML(reader);
6969
}
@@ -72,7 +72,9 @@ public void open(Reader reader) {
7272
* Save the project to a file
7373
*/
7474
public void save(File file) throws IOException {
75-
this.save(new FileWriter(file));
75+
try (final Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
76+
this.save(writer);
77+
}
7678
this.file = Optional.of(file);
7779
}
7880

core/src/main/java/edu/wpi/grip/core/serialization/SocketConverter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
101101
// (such as connections) can reference sockets in the pipeline.
102102
Socket socket;
103103
if (reader.getAttribute(STEP_ATTRIBUTE) != null) {
104-
final int stepIndex = Integer.valueOf(reader.getAttribute(STEP_ATTRIBUTE));
105-
final int socketIndex = Integer.valueOf(reader.getAttribute(SOCKET_ATTRIBUTE));
104+
final int stepIndex = Integer.parseInt(reader.getAttribute(STEP_ATTRIBUTE));
105+
final int socketIndex = Integer.parseInt(reader.getAttribute(SOCKET_ATTRIBUTE));
106106

107107
final Step step = pipeline.getSteps().get(stepIndex);
108108
socket = direction == Socket.Direction.INPUT ?
109109
step.getInputSockets()[socketIndex] : step.getOutputSockets()[socketIndex];
110110
} else if (reader.getAttribute(SOURCE_ATTRIBUTE) != null) {
111-
final int sourceIndex = Integer.valueOf(reader.getAttribute(SOURCE_ATTRIBUTE));
112-
final int socketIndex = Integer.valueOf(reader.getAttribute(SOCKET_ATTRIBUTE));
111+
final int sourceIndex = Integer.parseInt(reader.getAttribute(SOURCE_ATTRIBUTE));
112+
final int socketIndex = Integer.parseInt(reader.getAttribute(SOCKET_ATTRIBUTE));
113113

114114
final Source source = pipeline.getSources().get(sourceIndex);
115115
socket = source.getOutputSockets()[socketIndex];
@@ -118,7 +118,7 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
118118
}
119119

120120
if (socket.getDirection() == Socket.Direction.OUTPUT) {
121-
((OutputSocket) socket).setPreviewed(Boolean.valueOf(reader.getAttribute(PREVIEWED_ATTRIBUTE)));
121+
((OutputSocket) socket).setPreviewed(Boolean.parseBoolean(reader.getAttribute(PREVIEWED_ATTRIBUTE)));
122122
}
123123

124124
if (reader.hasMoreChildren()) {
@@ -134,7 +134,7 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
134134
reader.moveUp();
135135

136136
return socket;
137-
} catch (Exception e) {
137+
} catch (RuntimeException e) {
138138
throw new ConversionException("Error deserializing socket", e);
139139
}
140140
}

core/src/main/java/edu/wpi/grip/core/sources/CameraSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public FrameGrabber create(String addressProperty) throws MalformedURLException
176176
final String addressProperty = properties.getProperty(ADDRESS_PROPERTY);
177177

178178
if (deviceNumberProperty != null) {
179-
final int deviceNumber = Integer.valueOf(deviceNumberProperty);
179+
final int deviceNumber = Integer.parseInt(deviceNumberProperty);
180180
this.name = "Webcam " + deviceNumber;
181181
this.grabberSupplier = () -> grabberFactory.create(deviceNumber);
182182
} else if (addressProperty != null) {

core/src/main/java/edu/wpi/grip/core/sources/MultiImageFileSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ private static Mat[] createImagesArray(List<String> paths) throws IOException {
196196
}
197197

198198
private static int sizeFromProperties(Properties properties) {
199-
return Integer.valueOf(properties.getProperty(SIZE_PROPERTY));
199+
return Integer.parseInt(properties.getProperty(SIZE_PROPERTY));
200200
}
201201

202202
private static int indexFromProperties(Properties properties) {
203-
return Integer.valueOf(properties.getProperty(INDEX_PROPERTY));
203+
return Integer.parseInt(properties.getProperty(INDEX_PROPERTY));
204204
}
205205

206206
private static String[] pathsFromProperties(Properties properties) {

core/src/main/java/edu/wpi/grip/core/util/SafeShutdown.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public final class SafeShutdown {
1919
Runtime.getRuntime().addShutdownHook(new Thread() {
2020
@Override
2121
public void run() {
22-
stopping = true;
22+
SafeShutdown.stopping = true;
2323
}
2424
});
2525
}

ui/src/main/java/edu/wpi/grip/ui/DeployController.java

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.net.URL;
3434
import java.net.URLDecoder;
3535
import java.net.UnknownHostException;
36+
import java.nio.charset.StandardCharsets;
3637
import java.util.Optional;
3738
import java.util.logging.Level;
3839
import java.util.logging.Logger;
@@ -50,23 +51,39 @@ public class DeployController {
5051
private final static URL LOCAL_GRIP_URL = Project.class.getProtectionDomain().getCodeSource().getLocation();
5152
private final static String LOCAL_GRIP_PATH = URLDecoder.decode(LOCAL_GRIP_URL.getPath());
5253

53-
@FXML private TextField address;
54-
@FXML private TextField user;
55-
@FXML private TextField password;
56-
@FXML private TextField javaHome;
57-
@FXML private TextField jvmArgs;
58-
@FXML private TextField deployDir;
59-
@FXML private TextField projectFile;
60-
@FXML private ProgressIndicator progress;
61-
@FXML private Label status;
62-
@FXML private TextArea console;
63-
@FXML private BooleanProperty deploying;
64-
@FXML private StringProperty command;
65-
66-
@Inject private EventBus eventBus;
67-
@Inject private Project project;
68-
@Inject private Pipeline pipeline;
69-
@Inject private Logger logger;
54+
@FXML
55+
private TextField address;
56+
@FXML
57+
private TextField user;
58+
@FXML
59+
private TextField password;
60+
@FXML
61+
private TextField javaHome;
62+
@FXML
63+
private TextField jvmArgs;
64+
@FXML
65+
private TextField deployDir;
66+
@FXML
67+
private TextField projectFile;
68+
@FXML
69+
private ProgressIndicator progress;
70+
@FXML
71+
private Label status;
72+
@FXML
73+
private TextArea console;
74+
@FXML
75+
private BooleanProperty deploying;
76+
@FXML
77+
private StringProperty command;
78+
79+
@Inject
80+
private EventBus eventBus;
81+
@Inject
82+
private Project project;
83+
@Inject
84+
private Pipeline pipeline;
85+
@Inject
86+
private Logger logger;
7087

7188
private Optional<Thread> deployThread = Optional.empty();
7289

@@ -164,9 +181,12 @@ public StreamCopier.Listener file(String name, long size) {
164181
// Upload the core GRIP JAR only if there isn't already one with the same hash on the robot. This prevents
165182
// us from redundantly deploying the same JAR over and over again (so deploy times after the first are
166183
// much faster), while still ensuring that the JAR is deployed if it has to be.
167-
try (Session session = ssh.startSession()) {
168-
Session.Command md5Cmd = session.exec("md5sum " + pathStr + GRIP_JAR);
169-
String remoteMd5Sum = new DataInputStream(md5Cmd.getInputStream()).readLine();
184+
try (Session session = ssh.startSession()) { // SSH doesn't store a reference to the session to close it.
185+
final Session.Command md5Cmd = session.exec("md5sum " + pathStr + GRIP_JAR);
186+
final String remoteMd5Sum;
187+
try (DataInputStream stream = new DataInputStream(md5Cmd.getInputStream())) {
188+
remoteMd5Sum = stream.readLine();
189+
}
170190
String localMd5Sum = Resources.asByteSource(LOCAL_GRIP_URL).hash(Hashing.md5()).toString();
171191

172192
// If the MD5 sum doesn't match up or there's any kind of error (there isn't a JAR there, etc...),
@@ -195,18 +215,21 @@ public StreamCopier.Listener file(String name, long size) {
195215
setStatusAsync("Running GRIP", false);
196216
logger.info("Running " + commandStr);
197217

198-
Session session = ssh.startSession();
199-
session.allocateDefaultPTY();
200-
Session.Command cmd = session.exec(String.format("'%s/%s'", pathStr, GRIP_WRAPPER));
218+
try (Session session = ssh.startSession()) {
219+
session.allocateDefaultPTY();
220+
Session.Command cmd = session.exec(String.format("'%s/%s'", pathStr, GRIP_WRAPPER));
221+
222+
try (final InputStreamReader reader = new InputStreamReader(cmd.getInputStream(), StandardCharsets.UTF_8)) {
223+
final LineReader inputReader = new LineReader(reader);
224+
while (isNotCanceled()) {
225+
String line = inputReader.readLine();
226+
if (line == null) {
227+
return;
228+
}
201229

202-
LineReader inputReader = new LineReader(new InputStreamReader(cmd.getInputStream()));
203-
while (isNotCanceled()) {
204-
String line = inputReader.readLine();
205-
if (line == null) {
206-
return;
230+
Platform.runLater(() -> console.setText(console.getText() + line + "\n"));
231+
}
207232
}
208-
209-
Platform.runLater(() -> console.setText(console.getText() + line + "\n"));
210233
}
211234
} catch (UnknownHostException e) {
212235
setStatusAsync("Unknown host: " + address.getText(), true);

0 commit comments

Comments
 (0)