Skip to content

Commit

Permalink
Fixed checkstyle issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hypfvieh committed Aug 16, 2023
1 parent 1ff92b4 commit b008f89
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
53 changes: 28 additions & 25 deletions dbus-java-core/src/main/java/org/freedesktop/dbus/StructHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,96 +22,99 @@ private StructHelper() {

/**
* Creates a {@link ArrayList} of struct of the given type using the list of object arrays.
*
*
* @param _obj list of object arrays to process
* @param _structType struct class to create
*
*
* @param <T> struct type
*
*
* @return List of given struct type
*
*
* @throws NoSuchMethodException when no constructor can be found for the arguments of the struct
* @throws SecurityException when constructor cannot be accesses
* @throws InstantiationException when reflection fails
* @throws IllegalAccessException if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
* @throws IllegalArgumentException when data types are incompatible or incompatible argument length
* @throws InvocationTargetException if the underlying constructor throws an exception
*
*
* @since 4.3.1 - 2023-08-16
*/
public static <T extends Struct> List<T> convertToStructList(List<Object[]> _obj, Class<T> _structType) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
public static <T extends Struct> List<T> convertToStructList(List<Object[]> _obj, Class<T> _structType) throws NoSuchMethodException, SecurityException, InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException {
List<T> result = new ArrayList<>();
convertToStructCollection(_obj, _structType, result);
return result;
}

/**
* Creates a {@link LinkedHashSet} of struct of the given type using the list of object arrays.
*
*
* @param _obj list of object arrays to process
* @param _structType struct class to create
*
*
* @param <T> struct type
*
*
* @return List of given struct type
*
*
* @throws NoSuchMethodException when no constructor can be found for the arguments of the struct
* @throws SecurityException when constructor cannot be accesses
* @throws InstantiationException when reflection fails
* @throws IllegalAccessException if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
* @throws IllegalArgumentException when data types are incompatible or incompatible argument length
* @throws InvocationTargetException if the underlying constructor throws an exception
*
*
* @since 4.3.1 - 2023-08-16
*/
public static <T extends Struct> Set<T> convertToStructSet(Set<Object[]> _obj, Class<T> _structType) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
public static <T extends Struct> Set<T> convertToStructSet(Set<Object[]> _obj, Class<T> _structType) throws NoSuchMethodException, SecurityException, InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Set<T> result = new LinkedHashSet<>();
convertToStructCollection(_obj, _structType, result);
return result;
}

/**
* Creates a collection of struct of the given type using the list of object arrays.
*
*
* @param _input list of object arrays to process
* @param _structType struct class to create
* @param _result collection to store results
*
*
* @param <T> struct type
*
*
* @return List of given struct type
*
*
* @throws NoSuchMethodException when no constructor can be found for the arguments of the struct
* @throws SecurityException when constructor cannot be accesses
* @throws InstantiationException when reflection fails
* @throws IllegalAccessException if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
* @throws IllegalArgumentException when data types are incompatible or incompatible argument length
* @throws InvocationTargetException if the underlying constructor throws an exception
*
*
* @since 4.3.1 - 2023-08-16
*/
public static <T extends Struct> void convertToStructCollection(Collection<Object[]> _input, Class<T> _structType, Collection<T> _result) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

public static <T extends Struct> void convertToStructCollection(Collection<Object[]> _input, Class<T> _structType, Collection<T> _result)
throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

Objects.requireNonNull(_structType, "Struct class required");
Objects.requireNonNull(_result, "Collection for result storage required");
Objects.requireNonNull(_input, "Input data required");

Class<?>[] constructorArgClasses = Arrays.stream(_structType.getDeclaredFields())
.filter(f -> f.isAnnotationPresent(Position.class))
.sorted((f1, f2) -> Integer.compare(f1.getAnnotation(Position.class).value(), f2.getAnnotation(Position.class).value()))
.map(f -> f.getType())
.toArray(Class[]::new);

for (Object[] object : _input) {
if (constructorArgClasses.length != object.length) {
throw new IllegalArgumentException("Struct length does not match argument length");
}
T x = StructHelper.createStruct(constructorArgClasses, (Object) object, _structType);
_result.add(x);
}

}

/**
* Creates a instance of the given {@link Struct} subclass if the given variant is some sort of Struct.
* @param _variant variant to convert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@
* </p>
*
* @author hypfvieh
*
*
* @since 2023-02-27
*/
public final class PrintUserSessions {
private PrintUserSessions() {}

public static void main(String[] _args) throws IOException, DBusException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
public static void main(String[] _args)
throws IOException, DBusException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

try (DBusConnection sessionConnection = DBusConnectionBuilder.forSystemBus().build()) {

// fetch properties
Expand All @@ -63,7 +65,7 @@ public static void main(String[] _args) throws IOException, DBusException, NoSuc
for (Object[] us : sessions) {
System.out.println(us[0] + " --> " + us[1]);
}

// Option 2: Use StructHelper and appropriate struct class
List<SessionStruct> convertToStruct = StructHelper.convertToStructList(sessions, SessionStruct.class);
for (SessionStruct us : convertToStruct) {
Expand All @@ -77,7 +79,7 @@ public static class SessionStruct extends Struct {
private final String user;
@Position(1)
private final DBusPath dbusPath;

public SessionStruct(String _user, DBusPath _dbusPath) {
user = _user;
dbusPath = _dbusPath;
Expand All @@ -90,6 +92,6 @@ public String getUser() {
public DBusPath getDbusPath() {
return dbusPath;
}

}
}

0 comments on commit b008f89

Please sign in to comment.