Skip to content

Commit

Permalink
Add more infromative Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
bwRavencl committed Aug 15, 2024
1 parent d21551a commit 39d3ee3
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ final class AssignmentsComponent extends JScrollPane {

private static void checkDimensionIsSquare(final Dimension dimension) {
if (dimension.width != dimension.height) {
throw new IllegalArgumentException();
throw new IllegalArgumentException("Parameter dimension is not square");
}
}

Expand Down Expand Up @@ -292,27 +292,31 @@ private CompoundButton(final Main main, final JPanel parentPanel, final Componen
setModel(peer.getModel());
}

final var componentType = component.getType();
final var componentIndex = component.getIndex();
final var swapLeftAndRightSticks = main.isSwapLeftAndRightSticks();

if (component.getType() == ComponentType.BUTTON) {
if (component.getIndex() == GLFW.GLFW_GAMEPAD_BUTTON_LEFT_THUMB) {
if (componentType == ComponentType.BUTTON) {
switch (componentIndex) {
case GLFW.GLFW_GAMEPAD_BUTTON_LEFT_THUMB -> {
setAction(new EditComponentAction(main,
Main.strings.getString(swapLeftAndRightSticks ? "RIGHT_THUMB" : "LEFT_THUMB"), component));

text = Main.strings.getString(swapLeftAndRightSticks ? "RIGHT_STICK" : "LEFT_STICK");

swapTextPossible = true;
} else if (component.getIndex() == GLFW.GLFW_GAMEPAD_BUTTON_RIGHT_THUMB) {
}
case GLFW.GLFW_GAMEPAD_BUTTON_RIGHT_THUMB -> {
setAction(new EditComponentAction(main,
Main.strings.getString(swapLeftAndRightSticks ? "LEFT_THUMB" : "RIGHT_THUMB"), component));
text = Main.strings.getString(swapLeftAndRightSticks ? "LEFT_STICK" : "RIGHT_STICK");

swapTextPossible = true;
} else {
throw new IllegalArgumentException();
}
default -> throw buildInvalidComponentIndexException(componentType, componentIndex);
}
} else if (peer != null) {
switch (component.getIndex()) {
switch (componentIndex) {
case GLFW.GLFW_GAMEPAD_AXIS_LEFT_X -> setAction(new EditComponentAction(main,
Main.strings.getString(swapLeftAndRightSticks ? "RIGHT_STICK_X_AXIS" : "LEFT_STICK_X_AXIS"),
component));
Expand All @@ -325,7 +329,7 @@ private CompoundButton(final Main main, final JPanel parentPanel, final Componen
case GLFW.GLFW_GAMEPAD_AXIS_RIGHT_Y -> setAction(new EditComponentAction(main,
Main.strings.getString(swapLeftAndRightSticks ? "LEFT_STICK_Y_AXIS" : "RIGHT_STICK_Y_AXIS"),
component));
default -> throw new IllegalArgumentException();
default -> throw buildInvalidComponentIndexException(componentType, componentIndex);
}
}

Expand Down Expand Up @@ -388,6 +392,12 @@ public void paintIcon(final java.awt.Component c, final Graphics g, final int x,
initShape();
}

private static IllegalArgumentException buildInvalidComponentIndexException(final ComponentType componentType,
final int componentIndex) {
return new IllegalArgumentException("Invalid componentIndex for " + ComponentType.class.getSimpleName()
+ " " + componentType + ": " + componentIndex);
}

@Override
public boolean contains(final int x, final int y) {
if (shape == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ public void actionPerformed(final ActionEvent e) {

private static Map<Field, ActionProperty> getFieldToActionPropertiesMap(final Class<?> actionClass) {
if (!IAction.class.isAssignableFrom(actionClass)) {
throw new IllegalArgumentException();
throw new IllegalArgumentException(
"Parameter actionClass does not implement " + IAction.class.getSimpleName());
}

final var propertyMap = new HashMap<Field, ActionProperty>();
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/de/bwravencl/controllerbuddy/gui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -1540,9 +1540,7 @@ public void exportVisualization(final File file) {
}

private SVGDocument generateSvgDocument(final Mode mode, final boolean darkTheme) {
if (templateSvgDocument == null) {
throw new IllegalStateException();
}
Objects.requireNonNull(templateSvgDocument, "Field templateSvgDocument must not be null");

final var workingCopySvgDocument = (SVGDocument) DOMUtilities.deepCloneDocument(templateSvgDocument,
templateSvgDocument.getImplementation());
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/de/bwravencl/controllerbuddy/input/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
Expand Down Expand Up @@ -635,9 +636,7 @@ public void setCursorDeltaY(final int cursorDeltaY) {
}

public boolean setProfile(final Profile profile) {
if (profile == null) {
throw new IllegalArgumentException();
}
Objects.requireNonNull(profile, "Parameter profile must not be null");

for (final var button : profile.getButtonToModeActionsMap().keySet()) {
if (!isValidButton(button)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public void doAction(final Input input, final int component, Byte value) {
final var hot = value != 0;

switch (activation) {
case REPEAT -> throw new IllegalStateException();
case REPEAT -> throw new IllegalStateException(
ButtonToCycleAction.class.getSimpleName() + " must not have activation value: " + Activation.REPEAT);
case SINGLE_IMMEDIATELY -> {
if (!hot) {
activatable = Activatable.YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ private EvdevDriver(final Input input, final ControllerInfo controller, final Ev
log.log(Level.INFO, Main.assembleControllerLoggingMessage("Using evdev driver for controller ", controller));
}

private static IOException buildUnsuccessfulIoctlException() {
return new IOException("Unsuccessful ioctl() system call");
}

private static void closeFileDescriptor(final int fd) {
if (fd != -1) {
CLib.INSTANCE.close(fd);
Expand All @@ -82,7 +86,7 @@ private short createRumbleEffect(final short length) throws IOException {
effect.replay.length = length;

if (CLib.INSTANCE.ioctl(evdevInfo.fd, EVIOCSFF, effect) != 0 || effect.id == -1) {
throw new IOException();
throw buildUnsuccessfulIoctlException();
}

return effect.id;
Expand Down Expand Up @@ -168,7 +172,7 @@ private void write(final Structure pointer) throws IOException {
final var pointerSize = new NativeLong(pointer.size());

if (CLib.INSTANCE.write(evdevInfo.fd, pointer, pointerSize).longValue() != pointerSize.longValue()) {
throw new IOException();
throw buildUnsuccessfulIoctlException();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ public abstract class OutputRunMode extends RunMode {
}
}

private static IllegalArgumentException buildInvalidMouseButtonException(final int button) {
return new IllegalArgumentException("Parameter button has invalid value: " + button);
}

private static UnsupportedOperationException buildNotImplementedException() {
return new UnsupportedOperationException("Not implemented");
}

private static void closeInputDevice(final InputDevice inputDevice) {
if (inputDevice != null && inputDevice.isOpen()) {
try {
Expand Down Expand Up @@ -330,7 +338,7 @@ private void doKeyboardInput(final ScanCode scanCode, final boolean down) throws
} else if (Main.isLinux) {
keyboardInputDevice.emit(new Event(scanCode.eventCode(), down ? 1 : 0));
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}
}

Expand All @@ -345,7 +353,7 @@ private void doMouseButtonInput(final int button, final boolean down) throws IOE
case 2 -> input.input.mi.dwFlags = down ? new DWORD(MOUSEEVENTF_RIGHTDOWN) : new DWORD(MOUSEEVENTF_RIGHTUP);
case 3 ->
input.input.mi.dwFlags = down ? new DWORD(MOUSEEVENTF_MIDDLEDOWN) : new DWORD(MOUSEEVENTF_MIDDLEUP);
default -> throw new IllegalArgumentException();
default -> throw buildInvalidMouseButtonException(button);
}

User32.INSTANCE.SendInput(new DWORD(1L), new INPUT[] { input }, input.size());
Expand All @@ -354,12 +362,12 @@ private void doMouseButtonInput(final int button, final boolean down) throws IOE
case 1 -> EventCode.BTN_LEFT;
case 2 -> EventCode.BTN_RIGHT;
case 3 -> EventCode.BTN_MIDDLE;
default -> throw new IllegalArgumentException("Unexpected value: " + button);
default -> throw buildInvalidMouseButtonException(button);
};

mouseInputDevice.emit(new Event(eventCode, down ? 1 : 0));
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}
}

Expand Down Expand Up @@ -391,7 +399,7 @@ private boolean enoughButtons(final int nButtons) {
requiredButtons),
Main.strings.getString("ERROR_DIALOG_TITLE"), JOptionPane.ERROR_MESSAGE));
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}

return false;
Expand Down Expand Up @@ -632,7 +640,7 @@ final boolean init() {
return false;
}
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}

input.init();
Expand Down Expand Up @@ -682,7 +690,7 @@ private void setLockKeyState(final LockKey lockKey, final boolean on) throws IOE
}
}
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}
}

Expand All @@ -709,7 +717,7 @@ final void writeOutput() {
} else if (Main.isLinux) {
joystickInputDevice.emit(new Event(EventCode.ABS_X, axisX.getUinputValue()));
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}

axisX.setUnchanged();
Expand All @@ -722,7 +730,7 @@ final void writeOutput() {
} else if (Main.isLinux) {
joystickInputDevice.emit(new Event(EventCode.ABS_Y, axisY.getUinputValue()));
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}

axisY.setUnchanged();
Expand All @@ -735,7 +743,7 @@ final void writeOutput() {
} else if (Main.isLinux) {
joystickInputDevice.emit(new Event(EventCode.ABS_Z, axisZ.getUinputValue()));
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}

axisZ.setUnchanged();
Expand All @@ -748,7 +756,7 @@ final void writeOutput() {
} else if (Main.isLinux) {
joystickInputDevice.emit(new Event(EventCode.ABS_RX, axisRX.getUinputValue()));
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}

axisRX.setUnchanged();
Expand All @@ -761,7 +769,7 @@ final void writeOutput() {
} else if (Main.isLinux) {
joystickInputDevice.emit(new Event(EventCode.ABS_RY, axisRY.getUinputValue()));
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}

axisRY.setUnchanged();
Expand All @@ -774,7 +782,7 @@ final void writeOutput() {
} else if (Main.isLinux) {
joystickInputDevice.emit(new Event(EventCode.ABS_RZ, axisRZ.getUinputValue()));
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}

axisRZ.setUnchanged();
Expand All @@ -787,7 +795,7 @@ final void writeOutput() {
} else if (Main.isLinux) {
joystickInputDevice.emit(new Event(EventCode.ABS_THROTTLE, axisS0.getUinputValue()));
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}

axisS0.setUnchanged();
Expand All @@ -800,7 +808,7 @@ final void writeOutput() {
} else if (Main.isLinux) {
joystickInputDevice.emit(new Event(EventCode.ABS_RUDDER, axisS1.getUinputValue()));
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}

axisS1.setUnchanged();
Expand All @@ -815,7 +823,7 @@ final void writeOutput() {
joystickInputDevice
.emit(new Event(UINPUT_JOYSTICK_BUTTON_EVENT_CODES[i], buttons[i].getUinputValue()));
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}

buttons[i].setUnchanged();
Expand Down Expand Up @@ -843,7 +851,7 @@ final void writeOutput() {
}
mouseInputDevice.syn();
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}
}

Expand Down Expand Up @@ -916,7 +924,7 @@ final void writeOutput() {
} else if (Main.isLinux) {
mouseInputDevice.emit(new Event(EventCode.REL_WHEEL, scrollClicks));
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}
}
} catch (final IOException e) {
Expand All @@ -932,7 +940,7 @@ final void writeOutput() {
} else if (Main.isLinux) {
message = "COULD_NOT_WRITE_TO_UINPUT_DEVICE_DIALOG_TEXT";
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}

return main.executeWhileVisible(
Expand Down Expand Up @@ -1013,7 +1021,7 @@ void setValue(final int value) {
changed = uinputValue != value;
uinputValue = value;
} else {
throw new UnsupportedOperationException();
throw buildNotImplementedException();
}
}
}
Expand Down

0 comments on commit 39d3ee3

Please sign in to comment.