-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate a GObject constructor that takes a Java class as first argum…
…ent instead of a GType, followed by varargs for (optionally) specifying property names and values. A new utility class `VarargsUtil` was created that simplifies splitting the varargs into a "first" and "rest" component like `g_object_new` expects, and also adds a `null` at the end, in case the user forgets it.
- Loading branch information
Showing
4 changed files
with
121 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
modules/glib/src/main/java/io/github/jwharm/javagi/interop/VarargsUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* Java-GI - Java language bindings for GObject-Introspection-based libraries | ||
* Copyright (C) 2022-2025 Jan-Willem Harmannij | ||
* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package io.github.jwharm.javagi.interop; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Utility functions to split an array of variadic arguments into a first | ||
* argument and a null-terminated array of remaining arguments. | ||
*/ | ||
public class VarargsUtil { | ||
|
||
/** | ||
* Return the first array element. | ||
* | ||
* @param array input array, can be {@code null} | ||
* @param <T> array element type | ||
* @return the first element, or {@code null} if the input array is | ||
* {@code null} or empty | ||
*/ | ||
public static <T> @Nullable T first(@Nullable T @Nullable[] array) { | ||
return array == null || array.length == 0 ? null : array[0]; | ||
} | ||
|
||
/** | ||
* Return all but the first array elements, terminated with a {@code null}. | ||
* For example, {@code [1, 2, 3]} returns {@code [2, 3, null]}. | ||
* | ||
* @param array input array, can be {@code null} | ||
* @param <T> array element type | ||
* @return a new array of all elements except the first, terminated with a | ||
* {@code null}, or {@code null} if the input array is {@code null} | ||
* or empty | ||
*/ | ||
@SuppressWarnings("unchecked") // cast is safe because the array is empty | ||
public static <T> @Nullable T @Nullable[] rest(@Nullable T @Nullable[] array) { | ||
return array == null ? null | ||
: array.length == 0 ? (T[]) new Object[] {} | ||
: Arrays.copyOfRange(array, 1, array.length + 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters