global: Clean up Java

Address Java and Android lints.

Signed-off-by: Samuel Holland <samuel@sholland.org>
This commit is contained in:
Samuel Holland
2018-04-30 11:50:21 -05:00
parent 8e4fb91a28
commit 843003f436
13 changed files with 237 additions and 215 deletions
@@ -6,7 +6,6 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
@@ -28,9 +27,11 @@ enum Attribute {
PRIVATE_KEY("PrivateKey"),
PUBLIC_KEY("PublicKey");
private static final String[] EMPTY_LIST = new String[0];
private static final Map<String, Attribute> KEY_MAP;
private static final Pattern LIST_SEPARATOR_PATTERN = Pattern.compile("\\s*,\\s*");
private static final Method NUMERIC_ADDRESS_PARSER;
private static final Pattern SEPARATOR_PATTERN = Pattern.compile("\\s|=");
private static Method parseNumericAddressMethod;
static {
KEY_MAP = new HashMap<>(Attribute.values().length);
@@ -41,8 +42,8 @@ enum Attribute {
static {
try {
parseNumericAddressMethod = InetAddress.class.getMethod("parseNumericAddress", new Class[]{String.class});
} catch (Exception e) {
NUMERIC_ADDRESS_PARSER = InetAddress.class.getMethod("parseNumericAddress", String.class);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
@@ -55,8 +56,8 @@ enum Attribute {
this.token = token;
}
public static <T> String listToString(final List<T> list) {
return TextUtils.join(", ", list);
public static <T> String iterableToString(final Iterable<T> iterable) {
return TextUtils.join(", ", iterable);
}
public static Attribute match(final CharSequence line) {
@@ -67,10 +68,10 @@ enum Attribute {
if (address == null || address.isEmpty())
throw new IllegalArgumentException("Empty address");
try {
return (InetAddress) parseNumericAddressMethod.invoke(null, new Object[]{address});
} catch (IllegalAccessException e) {
return (InetAddress) NUMERIC_ADDRESS_PARSER.invoke(null, address);
} catch (final IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
} catch (final InvocationTargetException e) {
if (e.getCause() instanceof IllegalArgumentException)
throw (IllegalArgumentException) e.getCause();
else
@@ -80,8 +81,8 @@ enum Attribute {
public static String[] stringToList(final String string) {
if (string == null)
return new String[0];
return string.trim().split("\\s*,\\s*");
return EMPTY_LIST;
return LIST_SEPARATOR_PATTERN.split(string.trim());
}
public String composeWith(final Object value) {
@@ -92,8 +93,8 @@ enum Attribute {
return String.format(Locale.getDefault(), "%s = %d%n", token, value);
}
public <T> String composeWith(final List<T> value) {
return String.format("%s = %s%n", token, listToString(value));
public <T> String composeWith(final Iterable<T> value) {
return String.format("%s = %s%n", token, iterableToString(value));
}
public String parse(final CharSequence line) {
@@ -90,7 +90,7 @@ public class Config {
private Interface.Observable observableInterface;
private ObservableList<Peer.Observable> observablePeers;
public Observable(Config parent, String name) {
public Observable(final Config parent, final String name) {
this.name = name;
loadData(parent);
}
@@ -102,11 +102,11 @@ public class Config {
in.readTypedList(observablePeers, Peer.Observable.CREATOR);
}
public void commitData(Config parent) {
this.observableInterface.commitData(parent.interfaceSection);
List<Peer> newPeers = new ArrayList<>(this.observablePeers.size());
for (Peer.Observable observablePeer : this.observablePeers) {
Peer peer = new Peer();
public void commitData(final Config parent) {
observableInterface.commitData(parent.interfaceSection);
final List<Peer> newPeers = new ArrayList<>(observablePeers.size());
for (final Peer.Observable observablePeer : observablePeers) {
final Peer peer = new Peer();
observablePeer.commitData(peer);
newPeers.add(peer);
}
@@ -134,16 +134,16 @@ public class Config {
return observablePeers;
}
public void loadData(Config parent) {
this.observableInterface = new Interface.Observable(parent == null ? null : parent.interfaceSection);
this.observablePeers = new ObservableArrayList<>();
protected void loadData(final Config parent) {
observableInterface = new Interface.Observable(parent == null ? null : parent.interfaceSection);
observablePeers = new ObservableArrayList<>();
if (parent != null) {
for (Peer peer : parent.getPeers())
this.observablePeers.add(new Peer.Observable(peer));
for (final Peer peer : parent.getPeers())
observablePeers.add(new Peer.Observable(peer));
}
}
public void setName(String name) {
public void setName(final String name) {
this.name = name;
notifyPropertyChanged(BR.name);
}
@@ -6,17 +6,17 @@ import java.net.InetAddress;
import java.util.Locale;
public class IPCidr {
private InetAddress address;
private final InetAddress address;
private int cidr;
public IPCidr(String in) {
cidr = -1;
int slash = in.lastIndexOf('/');
final int slash = in.lastIndexOf('/');
if (slash != -1 && slash < in.length() - 1) {
try {
cidr = Integer.parseInt(in.substring(slash + 1), 10);
in = in.substring(0, slash);
} catch (Exception ignored) {
} catch (final Exception ignored) {
}
}
address = Attribute.parseIPString(in);
@@ -9,7 +9,7 @@ import com.wireguard.android.BR;
import com.wireguard.crypto.Keypair;
import java.net.InetAddress;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
/**
@@ -17,30 +17,31 @@ import java.util.List;
*/
public class Interface {
private List<IPCidr> addressList;
private List<InetAddress> dnsList;
private final List<IPCidr> addressList;
private final List<InetAddress> dnsList;
private Keypair keypair;
private int listenPort;
private int mtu;
public Interface() {
addressList = new LinkedList<>();
dnsList = new LinkedList<>();
addressList = new ArrayList<>();
dnsList = new ArrayList<>();
}
private void addAddresses(String[] addresses) {
private void addAddresses(final String[] addresses) {
if (addresses != null && addresses.length > 0) {
for (final String addr : addresses) {
if (addr.isEmpty())
throw new IllegalArgumentException("Address is empty");
this.addressList.add(new IPCidr(addr));
addressList.add(new IPCidr(addr));
}
}
}
private void addDnses(String[] dnses) {
private void addDnses(final String[] dnses) {
if (dnses != null && dnses.length > 0) {
for (final String dns : dnses) {
this.dnsList.add(Attribute.parseIPString(dns));
dnsList.add(Attribute.parseIPString(dns));
}
}
}
@@ -48,7 +49,7 @@ public class Interface {
private String getAddressString() {
if (addressList.isEmpty())
return null;
return Attribute.listToString(addressList);
return Attribute.iterableToString(addressList);
}
public IPCidr[] getAddresses() {
@@ -58,11 +59,11 @@ public class Interface {
private String getDnsString() {
if (dnsList.isEmpty())
return null;
return Attribute.listToString(getDnsStrings());
return Attribute.iterableToString(getDnsStrings());
}
private List<String> getDnsStrings() {
List<String> strings = new LinkedList<>();
final List<String> strings = new ArrayList<>();
for (final InetAddress addr : dnsList)
strings.add(addr.getHostAddress());
return strings;
@@ -106,31 +107,38 @@ public class Interface {
public void parse(final String line) {
final Attribute key = Attribute.match(line);
if (key == Attribute.ADDRESS)
addAddresses(key.parseList(line));
else if (key == Attribute.DNS)
addDnses(key.parseList(line));
else if (key == Attribute.LISTEN_PORT)
setListenPortString(key.parse(line));
else if (key == Attribute.MTU)
setMtuString(key.parse(line));
else if (key == Attribute.PRIVATE_KEY)
setPrivateKey(key.parse(line));
else
throw new IllegalArgumentException(line);
switch (key) {
case ADDRESS:
addAddresses(key.parseList(line));
break;
case DNS:
addDnses(key.parseList(line));
break;
case LISTEN_PORT:
setListenPortString(key.parse(line));
break;
case MTU:
setMtuString(key.parse(line));
break;
case PRIVATE_KEY:
setPrivateKey(key.parse(line));
break;
default:
throw new IllegalArgumentException(line);
}
}
private void setAddressString(final String addressString) {
this.addressList.clear();
addressList.clear();
addAddresses(Attribute.stringToList(addressString));
}
private void setDnsString(final String dnsString) {
this.dnsList.clear();
dnsList.clear();
addDnses(Attribute.stringToList(dnsString));
}
private void setListenPort(int listenPort) {
private void setListenPort(final int listenPort) {
this.listenPort = listenPort;
}
@@ -141,7 +149,7 @@ public class Interface {
setListenPort(0);
}
private void setMtu(int mtu) {
private void setMtu(final int mtu) {
this.mtu = mtu;
}
@@ -155,10 +163,7 @@ public class Interface {
private void setPrivateKey(String privateKey) {
if (privateKey != null && privateKey.isEmpty())
privateKey = null;
if (privateKey == null)
keypair = null;
else
keypair = new Keypair(privateKey);
keypair = privateKey == null ? null : new Keypair(privateKey);
}
@Override
@@ -196,7 +201,7 @@ public class Interface {
private String privateKey;
private String publicKey;
public Observable(Interface parent) {
public Observable(final Interface parent) {
if (parent != null)
loadData(parent);
}
@@ -210,12 +215,12 @@ public class Interface {
mtu = in.readString();
}
public void commitData(Interface parent) {
parent.setAddressString(this.addresses);
parent.setDnsString(this.dnses);
parent.setPrivateKey(this.privateKey);
parent.setListenPortString(this.listenPort);
parent.setMtuString(this.mtu);
public void commitData(final Interface parent) {
parent.setAddressString(addresses);
parent.setDnsString(dnses);
parent.setPrivateKey(privateKey);
parent.setListenPortString(listenPort);
parent.setMtuString(mtu);
loadData(parent);
notifyChange();
}
@@ -226,7 +231,7 @@ public class Interface {
}
public void generateKeypair() {
Keypair keypair = new Keypair();
final Keypair keypair = new Keypair();
privateKey = keypair.getPrivateKey();
publicKey = keypair.getPublicKey();
notifyPropertyChanged(BR.privateKey);
@@ -263,42 +268,42 @@ public class Interface {
return publicKey;
}
public void loadData(Interface parent) {
this.addresses = parent.getAddressString();
this.dnses = parent.getDnsString();
this.publicKey = parent.getPublicKey();
this.privateKey = parent.getPrivateKey();
this.listenPort = parent.getListenPortString();
this.mtu = parent.getMtuString();
protected void loadData(final Interface parent) {
addresses = parent.getAddressString();
dnses = parent.getDnsString();
publicKey = parent.getPublicKey();
privateKey = parent.getPrivateKey();
listenPort = parent.getListenPortString();
mtu = parent.getMtuString();
}
public void setAddresses(String addresses) {
public void setAddresses(final String addresses) {
this.addresses = addresses;
notifyPropertyChanged(BR.addresses);
}
public void setDnses(String dnses) {
public void setDnses(final String dnses) {
this.dnses = dnses;
notifyPropertyChanged(BR.dnses);
}
public void setListenPort(String listenPort) {
public void setListenPort(final String listenPort) {
this.listenPort = listenPort;
notifyPropertyChanged(BR.listenPort);
}
public void setMtu(String mtu) {
public void setMtu(final String mtu) {
this.mtu = mtu;
notifyPropertyChanged(BR.mtu);
}
public void setPrivateKey(String privateKey) {
public void setPrivateKey(final String privateKey) {
this.privateKey = privateKey;
try {
this.publicKey = new Keypair(privateKey).getPublicKey();
} catch (IllegalArgumentException ignored) {
this.publicKey = "";
publicKey = new Keypair(privateKey).getPublicKey();
} catch (final IllegalArgumentException ignored) {
publicKey = "";
}
notifyPropertyChanged(BR.privateKey);
@@ -13,7 +13,7 @@ import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@@ -22,19 +22,20 @@ import java.util.Locale;
*/
public class Peer {
private List<IPCidr> allowedIPsList;
private final List<IPCidr> allowedIPsList;
private InetSocketAddress endpoint;
private int persistentKeepalive;
private String preSharedKey;
private String publicKey;
public Peer() {
allowedIPsList = new LinkedList<>();
allowedIPsList = new ArrayList<>();
}
private void addAllowedIPs(String[] allowedIPs) {
private void addAllowedIPs(final String[] allowedIPs) {
if (allowedIPs != null && allowedIPs.length > 0) {
for (final String allowedIP : allowedIPs) {
this.allowedIPsList.add(new IPCidr(allowedIP));
allowedIPsList.add(new IPCidr(allowedIP));
}
}
}
@@ -46,7 +47,7 @@ public class Peer {
private String getAllowedIPsString() {
if (allowedIPsList.isEmpty())
return null;
return Attribute.listToString(allowedIPsList);
return Attribute.iterableToString(allowedIPsList);
}
public InetSocketAddress getEndpoint() {
@@ -97,38 +98,45 @@ public class Peer {
public void parse(final String line) {
final Attribute key = Attribute.match(line);
if (key == Attribute.ALLOWED_IPS)
addAllowedIPs(key.parseList(line));
else if (key == Attribute.ENDPOINT)
setEndpointString(key.parse(line));
else if (key == Attribute.PERSISTENT_KEEPALIVE)
setPersistentKeepaliveString(key.parse(line));
else if (key == Attribute.PRESHARED_KEY)
setPreSharedKey(key.parse(line));
else if (key == Attribute.PUBLIC_KEY)
setPublicKey(key.parse(line));
else
throw new IllegalArgumentException(line);
switch (key) {
case ALLOWED_IPS:
addAllowedIPs(key.parseList(line));
break;
case ENDPOINT:
setEndpointString(key.parse(line));
break;
case PERSISTENT_KEEPALIVE:
setPersistentKeepaliveString(key.parse(line));
break;
case PRESHARED_KEY:
setPreSharedKey(key.parse(line));
break;
case PUBLIC_KEY:
setPublicKey(key.parse(line));
break;
default:
throw new IllegalArgumentException(line);
}
}
private void setAllowedIPsString(final String allowedIPsString) {
this.allowedIPsList.clear();
allowedIPsList.clear();
addAllowedIPs(Attribute.stringToList(allowedIPsString));
}
private void setEndpoint(InetSocketAddress endpoint) {
private void setEndpoint(final InetSocketAddress endpoint) {
this.endpoint = endpoint;
}
private void setEndpointString(final String endpoint) {
if (endpoint != null && !endpoint.isEmpty()) {
InetSocketAddress constructedEndpoint;
final InetSocketAddress constructedEndpoint;
if (endpoint.indexOf('/') != -1 || endpoint.indexOf('?') != -1 || endpoint.indexOf('#') != -1)
throw new IllegalArgumentException("Forbidden characters in endpoint");
URI uri;
final URI uri;
try {
uri = new URI("wg://" + endpoint);
} catch (URISyntaxException e) {
} catch (final URISyntaxException e) {
throw new IllegalArgumentException(e);
}
constructedEndpoint = InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort());
@@ -137,11 +145,11 @@ public class Peer {
setEndpoint(null);
}
private void setPersistentKeepalive(int persistentKeepalive) {
private void setPersistentKeepalive(final int persistentKeepalive) {
this.persistentKeepalive = persistentKeepalive;
}
private void setPersistentKeepaliveString(String persistentKeepalive) {
private void setPersistentKeepaliveString(final String persistentKeepalive) {
if (persistentKeepalive != null && !persistentKeepalive.isEmpty())
setPersistentKeepalive(Integer.parseInt(persistentKeepalive, 10));
else
@@ -198,7 +206,7 @@ public class Peer {
private String preSharedKey;
private String publicKey;
public Observable(Peer parent) {
public Observable(final Peer parent) {
loadData(parent);
}
@@ -214,12 +222,12 @@ public class Peer {
return new Observable(new Peer());
}
public void commitData(Peer parent) {
parent.setAllowedIPsString(this.allowedIPs);
parent.setEndpointString(this.endpoint);
parent.setPersistentKeepaliveString(this.persistentKeepalive);
parent.setPreSharedKey(this.preSharedKey);
parent.setPublicKey(this.publicKey);
public void commitData(final Peer parent) {
parent.setAllowedIPsString(allowedIPs);
parent.setEndpointString(endpoint);
parent.setPersistentKeepaliveString(persistentKeepalive);
parent.setPreSharedKey(preSharedKey);
parent.setPublicKey(publicKey);
if (parent.getPublicKey() == null)
throw new IllegalArgumentException("Peer public key may not be empty");
loadData(parent);
@@ -256,35 +264,35 @@ public class Peer {
return publicKey;
}
public void loadData(Peer parent) {
this.allowedIPs = parent.getAllowedIPsString();
this.endpoint = parent.getEndpointString();
this.persistentKeepalive = parent.getPersistentKeepaliveString();
this.preSharedKey = parent.getPreSharedKey();
this.publicKey = parent.getPublicKey();
protected void loadData(final Peer parent) {
allowedIPs = parent.getAllowedIPsString();
endpoint = parent.getEndpointString();
persistentKeepalive = parent.getPersistentKeepaliveString();
preSharedKey = parent.getPreSharedKey();
publicKey = parent.getPublicKey();
}
public void setAllowedIPs(String allowedIPs) {
public void setAllowedIPs(final String allowedIPs) {
this.allowedIPs = allowedIPs;
notifyPropertyChanged(BR.allowedIPs);
}
public void setEndpoint(String endpoint) {
public void setEndpoint(final String endpoint) {
this.endpoint = endpoint;
notifyPropertyChanged(BR.endpoint);
}
public void setPersistentKeepalive(String persistentKeepalive) {
public void setPersistentKeepalive(final String persistentKeepalive) {
this.persistentKeepalive = persistentKeepalive;
notifyPropertyChanged(BR.persistentKeepalive);
}
public void setPreSharedKey(String preSharedKey) {
public void setPreSharedKey(final String preSharedKey) {
this.preSharedKey = preSharedKey;
notifyPropertyChanged(BR.preSharedKey);
}
public void setPublicKey(String publicKey) {
public void setPublicKey(final String publicKey) {
this.publicKey = publicKey;
notifyPropertyChanged(BR.publicKey);
}