-
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.
Fix registration of enums, add registration of flags types, add testcase
- Loading branch information
Showing
3 changed files
with
228 additions
and
22 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
modules/gobject/src/main/java/io/github/jwharm/javagi/gobject/annotations/Flags.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,37 @@ | ||
/* Java-GI - Java language bindings for GObject-Introspection-based libraries | ||
* Copyright (C) 2022-2024 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.gobject.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import io.github.jwharm.javagi.gobject.types.Types; | ||
|
||
/** | ||
* The {@code @Flags} annotation is used by {@link Types#register} to register | ||
* a Java enum as a GObject Flags type. Flags types are like enumerations, but | ||
* allow bitwise comparison of their values. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
public @interface Flags { | ||
} |
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
70 changes: 70 additions & 0 deletions
70
modules/gobject/src/test/java/io/github/jwharm/javagi/test/gobject/CustomEnumTest.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,70 @@ | ||
package io.github.jwharm.javagi.test.gobject; | ||
|
||
import io.github.jwharm.javagi.gobject.annotations.Flags; | ||
import io.github.jwharm.javagi.gobject.types.Types; | ||
import org.gnome.glib.Type; | ||
import org.gnome.gobject.GObjects; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
/** | ||
* Test custom registered enum and flags types | ||
*/ | ||
public class CustomEnumTest { | ||
|
||
@Test | ||
public void testCustomEnum() { | ||
var foo = MyEnum.FOO; | ||
var bar = MyEnum.BAR; | ||
var baz = MyEnum.BAZ; | ||
|
||
assertNotNull(foo); | ||
assertNotNull(bar); | ||
assertNotNull(baz); | ||
|
||
assertEquals(foo.toString(), GObjects.enumToString(MyEnum.getType(), 0)); | ||
assertEquals(bar.toString(), GObjects.enumToString(MyEnum.getType(), 1)); | ||
assertEquals(baz.toString(), GObjects.enumToString(MyEnum.getType(), 2)); | ||
} | ||
|
||
@Test | ||
public void testCustomFlags() { | ||
var foo = MyFlags.FOO; | ||
var bar = MyFlags.BAR; | ||
var baz = MyFlags.BAZ; | ||
|
||
assertNotNull(foo); | ||
assertNotNull(bar); | ||
assertNotNull(baz); | ||
|
||
assertEquals("FOO | BAR | BAZ", | ||
GObjects.flagsToString(MyFlags.getType(), 1|2|4)); | ||
} | ||
|
||
public enum MyEnum { | ||
FOO, | ||
BAR, | ||
BAZ; | ||
|
||
private static final Type gtype = Types.register(MyEnum.class); | ||
|
||
public static Type getType() { | ||
return gtype; | ||
} | ||
} | ||
|
||
@Flags | ||
public enum MyFlags { | ||
FOO, | ||
BAR, | ||
BAZ; | ||
|
||
private static final Type gtype = Types.register(MyFlags.class); | ||
|
||
public static Type getType() { | ||
return gtype; | ||
} | ||
} | ||
} |