Merge pull request #91 from janosvitok/fix-sonar-findings

Fix sonar findings
This commit is contained in:
Samuel Carlsson 2018-07-31 08:52:47 +02:00 committed by GitHub
commit 5750321eaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 45 additions and 38 deletions

View File

@ -32,11 +32,11 @@ public class AdbServerLauncher {
} }
private static String findAdbExecutable(Map<String, String> environment) { private static String findAdbExecutable(Map<String, String> environment) {
String android_home = environment.get("ANDROID_HOME"); String androidHome = environment.get("ANDROID_HOME");
if (android_home == null || android_home.equals("")) { if (androidHome == null || androidHome.equals("")) {
return "adb"; return "adb";
} }
return android_home + "/platform-tools/adb"; return androidHome + "/platform-tools/adb";
} }
public void launch() throws IOException, InterruptedException { public void launch() throws IOException, InterruptedException {

View File

@ -18,6 +18,7 @@ public class DeviceWatcher implements Runnable {
watch(); watch();
} }
@SuppressWarnings("squid:S2189") // watcher is stopped by closing transport
public void watch() { public void watch() {
try { try {
while (true) { while (true) {

View File

@ -41,9 +41,9 @@ class HostConnectToRemoteTcpDevice {
void validate(String response) throws ConnectionToRemoteDeviceException; void validate(String response) throws ConnectionToRemoteDeviceException;
} }
final static class ResponseValidatorImp implements ResponseValidator { static final class ResponseValidatorImp implements ResponseValidator {
private final static String SUCCESSFULLY_CONNECTED = "connected to"; private static final String SUCCESSFULLY_CONNECTED = "connected to";
private final static String ALREADY_CONNECTED = "already connected to"; private static final String ALREADY_CONNECTED = "already connected to";
ResponseValidatorImp() { ResponseValidatorImp() {
@ -64,7 +64,7 @@ class HostConnectToRemoteTcpDevice {
} }
private String extractError(String response) { private String extractError(String response) {
int lastColon = response.lastIndexOf(":"); int lastColon = response.lastIndexOf(':');
if (lastColon != -1) { if (lastColon != -1) {
return response.substring(lastColon, response.length()); return response.substring(lastColon, response.length());
} else { } else {

View File

@ -41,10 +41,9 @@ public class HostDisconnectFromRemoteTcpDevice {
void validate(String response) throws ConnectionToRemoteDeviceException; void validate(String response) throws ConnectionToRemoteDeviceException;
} }
final static class ResponseValidatorImp implements ResponseValidator { static final class ResponseValidatorImp implements ResponseValidator {
private final static String SUCCESSFULLY_DISCONNECTED = "disconnected"; private static final String SUCCESSFULLY_DISCONNECTED = "disconnected";
private final static String ALREADY_DISCONNECTED = "error: no such device"; private static final String ALREADY_DISCONNECTED = "error: no such device";
ResponseValidatorImp() { ResponseValidatorImp() {
} }
@ -64,7 +63,7 @@ public class HostDisconnectFromRemoteTcpDevice {
} }
private String extractError(String response) { private String extractError(String response) {
int lastColon = response.lastIndexOf(":"); int lastColon = response.lastIndexOf(':');
if (lastColon != -1) { if (lastColon != -1) {
return response.substring(lastColon, response.length()); return response.substring(lastColon, response.length());
} else { } else {

View File

@ -74,7 +74,7 @@ public class JadbConnection implements ITransportFactory {
public List<JadbDevice> parseDevices(String body) { public List<JadbDevice> parseDevices(String body) {
String[] lines = body.split("\n"); String[] lines = body.split("\n");
ArrayList<JadbDevice> devices = new ArrayList<JadbDevice>(lines.length); ArrayList<JadbDevice> devices = new ArrayList<>(lines.length);
for (String line : lines) { for (String line : lines) {
String[] parts = line.split("\t"); String[] parts = line.split("\t");
if (parts.length > 1) { if (parts.length > 1) {

View File

@ -7,13 +7,14 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class JadbDevice { public class JadbDevice {
@SuppressWarnings("squid:S00115")
public enum State { public enum State {
Unknown, Unknown,
Offline, Offline,
Device, Device,
Recovery, Recovery,
BootLoader BootLoader
}; }
private final String serial; private final String serial;
private final ITransportFactory transportFactory; private final ITransportFactory transportFactory;
@ -150,7 +151,7 @@ public class JadbDevice {
SyncTransport sync = transport.startSync(); SyncTransport sync = transport.startSync();
sync.send("LIST", remotePath); sync.send("LIST", remotePath);
List<RemoteFile> result = new ArrayList<RemoteFile>(); List<RemoteFile> result = new ArrayList<>();
for (RemoteFileRecord dent = sync.readDirectoryEntry(); dent != RemoteFileRecord.DONE; dent = sync.readDirectoryEntry()) { for (RemoteFileRecord dent = sync.readDirectoryEntry(); dent != RemoteFileRecord.DONE; dent = sync.readDirectoryEntry()) {
result.add(dent); result.add(dent);
} }

View File

@ -13,7 +13,7 @@ public class LookBackFilteringOutputStream extends FilterOutputStream {
{ {
super(inner); super(inner);
this.lookBackBufferSize = lookBackBufferSize; this.lookBackBufferSize = lookBackBufferSize;
this.buffer = new ArrayDeque<Byte>(lookBackBufferSize); this.buffer = new ArrayDeque<>(lookBackBufferSize);
} }
protected void unwrite() { protected void unwrite() {

View File

@ -21,10 +21,7 @@ public class RemoteFile {
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
RemoteFile that = (RemoteFile) o; RemoteFile that = (RemoteFile) o;
return path.equals(that.path);
if (!path.equals(that.path)) return false;
return true;
} }
@Override @Override

View File

@ -7,6 +7,10 @@ import java.io.OutputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
public class Stream { public class Stream {
private Stream() {
throw new IllegalStateException("Utility class");
}
public static void copy(InputStream in, OutputStream out) throws IOException { public static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024 * 10]; byte[] buffer = new byte[1024 * 10];
int len; int len;

View File

@ -1,6 +1,10 @@
package se.vidstige.jadb.managers; package se.vidstige.jadb.managers;
public class Bash { public class Bash {
private Bash() {
throw new IllegalStateException("Utility class");
}
public static String quote(String s) { public static String quote(String s) {
// Check that s contains no whitespace // Check that s contains no whitespace
if (s.matches("\\S+")) { if (s.matches("\\S+")) {

View File

@ -6,7 +6,7 @@ import se.vidstige.jadb.RemoteFile;
import se.vidstige.jadb.Stream; import se.vidstige.jadb.Stream;
import java.io.*; import java.io.*;
import java.nio.charset.Charset; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -22,10 +22,8 @@ public class PackageManager {
} }
public List<Package> getPackages() throws IOException, JadbException { public List<Package> getPackages() throws IOException, JadbException {
ArrayList<Package> result = new ArrayList<Package>(); try (BufferedReader input = new BufferedReader(new InputStreamReader(device.executeShell("pm", "list", "packages"), StandardCharsets.UTF_8))) {
BufferedReader input = null; ArrayList<Package> result = new ArrayList<>();
try {
input = new BufferedReader(new InputStreamReader(device.executeShell("pm", "list", "packages"), Charset.forName("UTF-8")));
String line; String line;
while ((line = input.readLine()) != null) { while ((line = input.readLine()) != null) {
final String prefix = "package:"; final String prefix = "package:";
@ -33,10 +31,8 @@ public class PackageManager {
result.add(new Package(line.substring(prefix.length()))); result.add(new Package(line.substring(prefix.length())));
} }
} }
} finally { return result;
if (input != null) input.close();
} }
return result;
} }
private String getErrorMessage(String operation, String target, String errorMessage) { private String getErrorMessage(String operation, String target, String errorMessage) {
@ -49,7 +45,7 @@ public class PackageManager {
public void remove(RemoteFile file) throws IOException, JadbException { public void remove(RemoteFile file) throws IOException, JadbException {
InputStream s = device.executeShell("rm", "-f", Bash.quote(file.getPath())); InputStream s = device.executeShell("rm", "-f", Bash.quote(file.getPath()));
Stream.readAll(s, Charset.forName("UTF-8")); Stream.readAll(s, StandardCharsets.UTF_8);
} }
private void install(File apkFile, List<String> extraArguments) throws IOException, JadbException { private void install(File apkFile, List<String> extraArguments) throws IOException, JadbException {
@ -60,7 +56,7 @@ public class PackageManager {
arguments.addAll(extraArguments); arguments.addAll(extraArguments);
arguments.add(remote.getPath()); arguments.add(remote.getPath());
InputStream s = device.executeShell("pm", arguments.toArray(new String[arguments.size()])); InputStream s = device.executeShell("pm", arguments.toArray(new String[arguments.size()]));
String result = Stream.readAll(s, Charset.forName("UTF-8")); String result = Stream.readAll(s, StandardCharsets.UTF_8);
remove(remote); remove(remote);
verifyOperation("install", apkFile.getName(), result); verifyOperation("install", apkFile.getName(), result);
} }
@ -84,7 +80,7 @@ public class PackageManager {
public void uninstall(Package name) throws IOException, JadbException { public void uninstall(Package name) throws IOException, JadbException {
InputStream s = device.executeShell("pm", "uninstall", name.toString()); InputStream s = device.executeShell("pm", "uninstall", name.toString());
String result = Stream.readAll(s, Charset.forName("UTF-8")); String result = Stream.readAll(s, StandardCharsets.UTF_8);
verifyOperation("uninstall", name.toString(), result); verifyOperation("uninstall", name.toString(), result);
} }
@ -116,15 +112,18 @@ public class PackageManager {
public static final InstallOption ALLOW_TEST_APK = public static final InstallOption ALLOW_TEST_APK =
new InstallOption("-t"); new InstallOption("-t");
@SuppressWarnings("squid:S00100")
public static InstallOption WITH_INSTALLER_PACKAGE_NAME(String name) public static InstallOption WITH_INSTALLER_PACKAGE_NAME(String name)
{ {
return new InstallOption("-t", name); return new InstallOption("-t", name);
} }
@SuppressWarnings("squid:S00100")
public static InstallOption ON_SHARED_MASS_STORAGE(String name) { public static InstallOption ON_SHARED_MASS_STORAGE(String name) {
return new InstallOption("-s", name); return new InstallOption("-s", name);
} }
@SuppressWarnings("squid:S00100")
public static InstallOption ON_INTERNAL_SYSTEM_MEMORY(String name) { public static InstallOption ON_INTERNAL_SYSTEM_MEMORY(String name) {
return new InstallOption("-f", name); return new InstallOption("-f", name);
} }

View File

@ -7,7 +7,7 @@ import se.vidstige.jadb.SyncTransport;
import java.io.*; import java.io.*;
import java.net.ProtocolException; import java.net.ProtocolException;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.Charset; import java.nio.charset.StandardCharsets;
class AdbProtocolHandler implements Runnable { class AdbProtocolHandler implements Runnable {
private final Socket socket; private final Socket socket;
@ -43,12 +43,12 @@ class AdbProtocolHandler implements Runnable {
while (true) { while (true) {
byte[] buffer = new byte[4]; byte[] buffer = new byte[4];
input.readFully(buffer); input.readFully(buffer);
String encodedLength = new String(buffer, Charset.forName("utf-8")); String encodedLength = new String(buffer, StandardCharsets.UTF_8);
int length = Integer.parseInt(encodedLength, 16); int length = Integer.parseInt(encodedLength, 16);
buffer = new byte[length]; buffer = new byte[length];
input.readFully(buffer); input.readFully(buffer);
String command = new String(buffer, Charset.forName("utf-8")); String command = new String(buffer, StandardCharsets.UTF_8);
responder.onCommand(command); responder.onCommand(command);
@ -67,7 +67,7 @@ class AdbProtocolHandler implements Runnable {
writer.writeBytes(d.getSerial() + "\t" + d.getType() + "\n"); writer.writeBytes(d.getSerial() + "\t" + d.getType() + "\n");
} }
output.writeBytes("OKAY"); output.writeBytes("OKAY");
send(output, new String(tmp.toByteArray(), Charset.forName("utf-8"))); send(output, new String(tmp.toByteArray(), StandardCharsets.UTF_8));
} else if (command.startsWith("host:transport:")) { } else if (command.startsWith("host:transport:")) {
String serial = command.substring("host:transport:".length()); String serial = command.substring("host:transport:".length());
selected = findDevice(serial); selected = findDevice(serial);
@ -133,7 +133,7 @@ class AdbProtocolHandler implements Runnable {
private String readString(DataInput input, int length) throws IOException { private String readString(DataInput input, int length) throws IOException {
byte[] responseBuffer = new byte[length]; byte[] responseBuffer = new byte[length];
input.readFully(responseBuffer); input.readFully(responseBuffer);
return new String(responseBuffer, Charset.forName("utf-8")); return new String(responseBuffer, StandardCharsets.UTF_8);
} }
private void sync(DataOutput output, DataInput input) throws IOException, JadbException { private void sync(DataOutput output, DataInput input) throws IOException, JadbException {

View File

@ -29,6 +29,7 @@ public abstract class SocketServer implements Runnable {
return port; return port;
} }
@SuppressWarnings("squid:S2189") // server is stopped by closing SocketServer
@Override @Override
public void run() { public void run() {
try { try {
@ -44,19 +45,20 @@ public abstract class SocketServer implements Runnable {
clientThread.start(); clientThread.start();
} }
} catch (IOException e) { } catch (IOException e) {
// Empty on purpose
} }
} }
private void serverReady() { private void serverReady() {
synchronized (lockObject) { synchronized (lockObject) {
isStarted = true; isStarted = true;
lockObject.notify(); lockObject.notifyAll();
} }
} }
private void waitForServer() throws InterruptedException { private void waitForServer() throws InterruptedException {
synchronized (lockObject) { synchronized (lockObject) {
if (!isStarted) { while (!isStarted) {
lockObject.wait(); lockObject.wait();
} }
} }