From 8ff6770252158be8ffa3679f21985d3d1f5be388 Mon Sep 17 00:00:00 2001 From: Jan-Willem Harmannij Date: Tue, 2 Apr 2024 21:53:08 +0200 Subject: [PATCH 1/3] Add Bitfield::test, fixes #89 --- .../github/jwharm/javagi/base/Bitfield.java | 10 ++++ .../jwharm/javagi/test/glib/BitfieldTest.java | 51 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 modules/glib/src/test/java/io/github/jwharm/javagi/test/glib/BitfieldTest.java diff --git a/modules/glib/src/main/java/io/github/jwharm/javagi/base/Bitfield.java b/modules/glib/src/main/java/io/github/jwharm/javagi/base/Bitfield.java index 06548d34..f65db599 100644 --- a/modules/glib/src/main/java/io/github/jwharm/javagi/base/Bitfield.java +++ b/modules/glib/src/main/java/io/github/jwharm/javagi/base/Bitfield.java @@ -65,6 +65,16 @@ public boolean equals(Bitfield mask) { return this.value == mask.value; } + public boolean test(Bitfield other) { + if (other == null) + return false; + + if (!this.getClass().equals(other.getClass())) + return false; + + return (getValue() & other.getValue()) == other.getValue(); + } + /** * If the provided object is a {@link Bitfield} instance, the values are * compared. If the provided object is an {@link Integer}, the value is diff --git a/modules/glib/src/test/java/io/github/jwharm/javagi/test/glib/BitfieldTest.java b/modules/glib/src/test/java/io/github/jwharm/javagi/test/glib/BitfieldTest.java new file mode 100644 index 00000000..596707b1 --- /dev/null +++ b/modules/glib/src/test/java/io/github/jwharm/javagi/test/glib/BitfieldTest.java @@ -0,0 +1,51 @@ +/* Java-GI - Java language bindings for GObject-Introspection-based libraries + * Copyright (C) 2022-2023 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 . + */ + +package io.github.jwharm.javagi.test.glib; + +import org.gnome.glib.AsciiType; +import org.gnome.glib.FileTest; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Test bitfield compare operations. + */ +public class BitfieldTest { + + @Test + public void testBitfield() { + FileTest mask = FileTest.IS_DIR.or(FileTest.IS_EXECUTABLE); + + assertTrue(mask.equals( + FileTest.combined(FileTest.IS_DIR, FileTest.IS_EXECUTABLE))); + assertTrue(mask.equals( + FileTest.combined(FileTest.IS_DIR, FileTest.IS_EXECUTABLE) + .getValue())); + assertFalse(mask.equals(FileTest.IS_DIR)); + assertFalse(mask.equals(AsciiType.DIGIT)); + + assertTrue(mask.test(FileTest.IS_DIR)); + assertTrue(mask.test(FileTest.IS_EXECUTABLE.or(FileTest.IS_DIR))); + assertFalse(mask.test(FileTest.EXISTS)); + assertFalse(mask.test(AsciiType.ALPHA)); + } +} From 52856fe659b860a499cfaf56d450e444fff3aae1 Mon Sep 17 00:00:00 2001 From: Jan-Willem Harmannij Date: Tue, 2 Apr 2024 22:36:25 +0200 Subject: [PATCH 2/3] Fix issue with method overrides The fix for #85 introduced a regression: overriding a method from deeper in the parent hierarchy did not work anymore. This is now resolved. --- .../javagi/gobject/types/Overrides.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/modules/gobject/src/main/java/io/github/jwharm/javagi/gobject/types/Overrides.java b/modules/gobject/src/main/java/io/github/jwharm/javagi/gobject/types/Overrides.java index 06cfc9c3..cf734662 100644 --- a/modules/gobject/src/main/java/io/github/jwharm/javagi/gobject/types/Overrides.java +++ b/modules/gobject/src/main/java/io/github/jwharm/javagi/gobject/types/Overrides.java @@ -109,7 +109,7 @@ public static Consumer o List methods = new ArrayList<>(); for (Method method : cls.getDeclaredMethods()) { try { - Method virtual = parentClass.getDeclaredMethod(method.getName(), method.getParameterTypes()); + Method virtual = findMethod(parentClass, method.getName(), method.getParameterTypes()); if (! Proxy.class.isAssignableFrom(virtual.getDeclaringClass())) continue; @@ -152,6 +152,27 @@ public static Consumer o }; } + /* + * Try `Class::getDeclaredMethod` in the requested class and all its + * superclasses. + * Throws NoSuchMethodException if it is not found. + */ + private static Method findMethod(Class cls, String methodName, Class... parameterTypes) + throws NoSuchMethodException { + Class currentClass = cls; + while (currentClass != null) { + try { + return currentClass.getDeclaredMethod(methodName, parameterTypes); + } catch (NoSuchMethodException e) { + // If the method is not found in the current class, try the superclass + currentClass = currentClass.getSuperclass(); + } + } + // Method not found in class hierarchy + throw new NoSuchMethodException("Method %s not found in class hierarchy." + .formatted(methodName)); + } + /** * Find declared methods that implement methods defined in the provided * GObject interface, and return an interface initializer lambda that will From 8b5171411047de76c786a75e84a85249b014c011 Mon Sep 17 00:00:00 2001 From: Jan-Willem Harmannij Date: Tue, 2 Apr 2024 22:36:47 +0200 Subject: [PATCH 3/3] Set version to 0.9.1 --- buildSrc/src/main/groovy/java-gi.library-conventions.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buildSrc/src/main/groovy/java-gi.library-conventions.gradle b/buildSrc/src/main/groovy/java-gi.library-conventions.gradle index c7b21d41..11e37b56 100644 --- a/buildSrc/src/main/groovy/java-gi.library-conventions.gradle +++ b/buildSrc/src/main/groovy/java-gi.library-conventions.gradle @@ -32,7 +32,7 @@ dependencies { } group = 'io.github.jwharm.javagi' -version = '0.9.1-SNAPSHOT' +version = '0.9.1' java { if (! System.getenv('CI')) {