Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
jwharm committed Apr 4, 2024
2 parents 27a1623 + 8b51714 commit e5bc1c1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies {
}

group = 'io.github.jwharm.javagi'
version = '0.9.1-SNAPSHOT'
version = '0.9.1'

java {
if (! System.getenv('CI')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static <T extends GObject, TC extends GObject.ObjectClass> Consumer<TC> o
List<Method> 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;

Expand Down Expand Up @@ -152,6 +152,27 @@ public static <T extends GObject, TC extends GObject.ObjectClass> Consumer<TC> 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
Expand Down

0 comments on commit e5bc1c1

Please sign in to comment.