Skip to content

Commit

Permalink
Fixed some issues reported by IntelliJ's code inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasroefer committed Aug 18, 2023
1 parent 2d31d06 commit db280ea
Show file tree
Hide file tree
Showing 72 changed files with 908 additions and 1,196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
Expand Down Expand Up @@ -102,12 +100,9 @@ protected void init(final RobotState robot) {
@Override
public void robotStateChanged(final RobotStateEvent e) {
if (isVisible()) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
update((RobotState) e.getSource());
repaint();
}
SwingUtilities.invokeLater(() -> {
update((RobotState) e.getSource());
repaint();
});
}
}
Expand All @@ -117,7 +112,7 @@ public void run() {
*/
private void update(final RobotState robot) {
final SPLTeamMessage msg = robot.getLastTeamMessage();
if (msg == null || !BHumanMessage.class.isInstance(msg)) {
if (!BHumanMessage.class.isInstance(msg)) {
return;
}
final BHumanMessage bmsg = (BHumanMessage) msg;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bhuman.message.data;

import common.Log;

import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;

/**
Expand Down Expand Up @@ -45,8 +47,12 @@ public T[] read(final ByteBuffer stream) {
array[i] = reader.read(stream);
} else {
try {
array[i] = readerClass.newInstance().read(stream);
} catch (InstantiationException | IllegalAccessException ex) {
array[i] = readerClass.getConstructor().newInstance().read(stream);
} catch (IllegalAccessException
| InstantiationException
| InvocationTargetException
| NoSuchMethodException
| NullPointerException ex) {
Log.error("Failed to instantiate reader class " + readerClass.getName());
}
}
Expand All @@ -57,13 +63,13 @@ public T[] read(final ByteBuffer stream) {
@Override
public int getStreamedSize(final ByteBuffer stream) {
try {
if (reader != null && SimpleStreamReader.class.isInstance(reader)) {
if (SimpleStreamReader.class.isInstance(reader)) {
return array.length * ((SimpleStreamReader<T>) reader).getStreamedSize();
} else if (readerClass != null && SimpleStreamReader.class.isAssignableFrom(readerClass)) {
return array.length * ((SimpleStreamReader<T>) readerClass.newInstance()).getStreamedSize();
return array.length * ((SimpleStreamReader<T>) readerClass.getConstructor().newInstance()).getStreamedSize();
}

final ComplexStreamReader<T> reader = (ComplexStreamReader<T>) (this.reader != null ? this.reader : readerClass.newInstance());
final ComplexStreamReader<T> reader = (ComplexStreamReader<T>) (this.reader != null ? this.reader : readerClass.getConstructor().newInstance());
if (ProbablySimpleStreamReader.class.isInstance(reader) && ProbablySimpleStreamReader.class.cast(reader).isSimpleStreamReader()) {
return array.length * reader.getStreamedSize(stream);
}
Expand All @@ -83,7 +89,11 @@ public int getStreamedSize(final ByteBuffer stream) {
}
stream.position(position);
return size;
} catch (InstantiationException | IllegalAccessException ex) {
} catch (IllegalAccessException
| InstantiationException
| InvocationTargetException
| NoSuchMethodException
| NullPointerException ex) {
Log.error("Failed to instantiate reader class " + readerClass.getName());
return 0;
}
Expand All @@ -93,8 +103,12 @@ public int getStreamedSize(final ByteBuffer stream) {
public boolean isSimpleStreamReader() {
final StreamReader<T> reader;
try {
reader = this.reader != null ? this.reader : readerClass.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
reader = this.reader != null ? this.reader : readerClass.getConstructor().newInstance();
} catch (IllegalAccessException
| InstantiationException
| InvocationTargetException
| NoSuchMethodException
| NullPointerException ex) {
Log.error("Cannot instantiate reader class " + readerClass.getName());
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
public class BitStream {

private ByteBuffer source;
private final ByteBuffer source;
private short currentByte;
private long offset = 0;

Expand All @@ -25,7 +25,7 @@ public long readBits(long bits) {
currentByte = Unsigned.toUnsigned(source.get());
}
if((currentByte & (1 << (offset % 8))) != 0) {
result |= (1 << i);
result |= (1L << i);
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ public interface ComplexStreamReader<T> extends StreamReader<T> {
* @param stream stream to read from
* @return size in bytes
*/
public int getStreamedSize(final ByteBuffer stream);
int getStreamedSize(final ByteBuffer stream);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bhuman.message.data;

import common.Log;

import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;
import java.util.EnumMap;

Expand Down Expand Up @@ -30,8 +32,12 @@ public EnumMapReader(final Class<K> enumclass, final StreamReader<V> reader) {
public boolean isSimpleStreamReader() {
final StreamReader<V> reader;
try {
reader = this.reader != null ? this.reader : readerclass.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
reader = this.reader != null ? this.reader : readerclass.getConstructor().newInstance();
} catch (IllegalAccessException
| InstantiationException
| InvocationTargetException
| NoSuchMethodException
| NullPointerException ex) {
Log.error("Cannot instantiate reader class " + readerclass.getName());
return false;
}
Expand All @@ -46,8 +52,12 @@ public int getStreamedSize(final ByteBuffer stream) {

final StreamReader<V> reader;
try {
reader = this.reader != null ? this.reader : readerclass.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
reader = this.reader != null ? this.reader : readerclass.getConstructor().newInstance();
} catch (IllegalAccessException
| InstantiationException
| InvocationTargetException
| NoSuchMethodException
| NullPointerException ex) {
Log.error("Cannot instantiate reader class " + readerclass.getName());
return count;
}
Expand Down Expand Up @@ -83,8 +93,12 @@ public EnumMap<K, V> read(final ByteBuffer stream) {
map.put(key, reader.read(stream));
} else {
try {
map.put(key, readerclass.newInstance().read(stream));
} catch (InstantiationException | IllegalAccessException ex) {
map.put(key, readerclass.getConstructor().newInstance().read(stream));
} catch (IllegalAccessException
| InstantiationException
| InvocationTargetException
| NoSuchMethodException
| NullPointerException ex) {
Log.error("Failed to instantiate reader class " + readerclass.getName());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bhuman.message.data;

import common.Log;

import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -71,8 +73,12 @@ public List<T> read(final ByteBuffer stream) {
elems.add(reader.read(stream));
} else {
try {
elems.add(readerClass.newInstance().read(stream));
} catch (InstantiationException | IllegalAccessException ex) {
elems.add(readerClass.getConstructor().newInstance().read(stream));
} catch (IllegalAccessException
| InstantiationException
| InvocationTargetException
| NoSuchMethodException
| NullPointerException ex) {
}
}
}
Expand All @@ -83,13 +89,13 @@ public List<T> read(final ByteBuffer stream) {
public int getStreamedSize(final ByteBuffer stream) {
final int count = getElementCount(stream);
try {
if (reader != null && SimpleStreamReader.class.isInstance(reader)) {
if (SimpleStreamReader.class.isInstance(reader)) {
return listCountSize + count * ((SimpleStreamReader<T>) reader).getStreamedSize();
} else if (readerClass != null && SimpleStreamReader.class.isAssignableFrom(readerClass)) {
return listCountSize + count * ((SimpleStreamReader<T>) readerClass.newInstance()).getStreamedSize();
return listCountSize + count * ((SimpleStreamReader<T>) readerClass.getConstructor().newInstance()).getStreamedSize();
}

final ComplexStreamReader<T> reader = (ComplexStreamReader<T>) (this.reader != null ? this.reader : readerClass.newInstance());
final ComplexStreamReader<T> reader = (ComplexStreamReader<T>) (this.reader != null ? this.reader : readerClass.getConstructor().newInstance());
if (ProbablySimpleStreamReader.class.isInstance(reader) && ProbablySimpleStreamReader.class.cast(reader).isSimpleStreamReader()) {
return listCountSize + count * reader.getStreamedSize(stream);
}
Expand All @@ -110,7 +116,11 @@ public int getStreamedSize(final ByteBuffer stream) {
}
stream.position(position);
return size;
} catch (InstantiationException | IllegalAccessException ex) {
} catch (IllegalAccessException
| InstantiationException
| InvocationTargetException
| NoSuchMethodException
| NullPointerException ex) {
Log.error("Failed to instantiate reader class " + readerClass.getName());
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package bhuman.message.data;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import util.Unsigned;
Expand Down Expand Up @@ -286,15 +286,7 @@ public String read(final ByteBuffer stream) {
final int size = stream.getInt();
final byte[] bytes = new byte[size];
stream.get(bytes);
try {
return new String(bytes, "ISO-8859-1");
} catch (UnsupportedEncodingException ex) {
try {
return new String(bytes, "US-ASCII");
} catch (UnsupportedEncodingException e) {
return new String(bytes);
}
}
return new String(bytes, StandardCharsets.ISO_8859_1);
}

}
Expand All @@ -319,15 +311,7 @@ public int getStreamedSize() {
public String read(final ByteBuffer stream) {
final byte[] bytes = new byte[count];
stream.get(bytes);
try {
return new String(bytes, "ISO-8859-1");
} catch (UnsupportedEncodingException ex) {
try {
return new String(bytes, "US-ASCII");
} catch (UnsupportedEncodingException e) {
return new String(bytes);
}
}
return new String(bytes, StandardCharsets.ISO_8859_1);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ public interface ProbablySimpleStreamReader<T> extends ComplexStreamReader<T> {
*
* @return bool
*/
public boolean isSimpleStreamReader();
boolean isSimpleStreamReader();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ public interface SimpleStreamReader<T> extends StreamReader<T> {
*
* @return size in bytes
*/
public int getStreamedSize();
int getStreamedSize();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ public interface StreamReader<T> {
* @param stream stream to read from
* @return object
*/
public T read(final ByteBuffer stream);
T read(final ByteBuffer stream);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package bhuman.message.data;

import common.Log;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import java.lang.reflect.*;
import java.nio.ByteBuffer;
import java.util.EnumMap;
import java.util.HashMap;
Expand Down Expand Up @@ -83,7 +80,7 @@ protected StreamReader<?> getFieldReader(final Field field) {
final Reader reader = field.getAnnotation(Reader.class);
final Primitive primitive = field.getAnnotation(Primitive.class);
if (reader != null) {
return reader.value().newInstance();
return reader.value().getConstructor().newInstance();
} else if (primitive != null) {
try {
final Field readerField = NativeReaders.class.getField(primitive.value().toLowerCase() + "Reader");
Expand All @@ -99,7 +96,7 @@ protected StreamReader<?> getFieldReader(final Field field) {
if (nativeReader != null) {
return nativeReader;
} else if (StreamReader.class.isAssignableFrom(type)) {
return (StreamReader<?>) type.newInstance();
return (StreamReader<?>) type.getConstructor().newInstance();
} else if (Enum.class.isAssignableFrom(type)) {
return new EnumReader(type);
} else if (EnumMap.class.isAssignableFrom(type)) {
Expand Down Expand Up @@ -134,7 +131,7 @@ protected StreamReader<?> getFieldReader(final Field field) {
Log.error("field " + field.getName() + " in class " + getClass().getName() + " could not be read automatically");
}
}
return new ListReader(componentReader, listCountSize);
return new ListReader<>(componentReader, listCountSize);
} catch (ClassNotFoundException ex) {
Log.error("field " + field.getName() + " in class " + getClass().getName() + " could not be read automatically because the type was not found");
}
Expand All @@ -156,7 +153,10 @@ protected StreamReader<?> getFieldReader(final Field field) {
}
}
}
} catch (IllegalAccessException | InstantiationException ex) {
} catch (IllegalAccessException
| InstantiationException
| InvocationTargetException
| NoSuchMethodException ex) {
Log.error("Could not get reader for field " + field.getName() + " of class " + getClass().getName());
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion resources/plugins/05/B-Human/src/util/Unsigned.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public static int toUnsigned(final short s) {
* @return unsigned value
*/
public static long toUnsigned(final int i) {
return i < 0 ? i + (1l << (long) Integer.SIZE) : i;
return i < 0 ? i + (1L << (long) Integer.SIZE) : i;
}
}
8 changes: 4 additions & 4 deletions src/common/ApplicationLock.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
*/
public class ApplicationLock {
/** The lockFile. */
private File lockFile = null;
private final File lockFile;

/** The acquire. */
private FileLock lock = null;
private FileLock lock;

/** The lockChannel. */
private FileChannel lockChannel = null;
private FileChannel lockChannel;

/** The lockStream. */
private FileOutputStream lockStream = null;
private FileOutputStream lockStream;

/**
* Creates a new ApplicationLock instance.
Expand Down
Loading

0 comments on commit db280ea

Please sign in to comment.