Skip to content

Commit

Permalink
Change functions in enumerations and bitfields into instance methods …
Browse files Browse the repository at this point in the history
…(when applicable)
  • Loading branch information
jwharm committed Jun 8, 2024
1 parent f61c63f commit 71b8294
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,16 @@ PartialStatement marshalParameters() {
PartialStatement stmt = PartialStatement.of(null,
"memorySegment", MemorySegment.class);

if (parameters.instanceParameter() != null)
stmt.add("handle()");
// Marshal instance parameter
InstanceParameter iParam = parameters.instanceParameter();
if (iParam != null) {
if (iParam.type().get() instanceof FlaggedType)
stmt.add("getValue()"); // method in Enumeration class
else
stmt.add("handle()"); // method in regular TypeInstance class
}

// Marshal other parameters
for (Parameter p : parameters.parameters()) {
if (!stmt.format().isEmpty()) stmt.add(", ");
stmt.add("$Z"); // emit newline
Expand All @@ -146,7 +153,7 @@ PartialStatement marshalParameters() {
if (generator.checkNull())
stmt.add("($memorySegment:T) (" + generator.getName() + " == null ? $memorySegment:T.NULL : ");

// callback destroy
// Callback destroy
if (p.isDestroyNotifyParameter()) {
var notify = parameters.parameters().stream()
.filter(q -> q.destroy() == p)
Expand All @@ -159,7 +166,7 @@ PartialStatement marshalParameters() {
}
}

// user_data
// User_data
else if (p.isUserDataParameter())
stmt.add("$memorySegment:T.NULL");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package io.github.jwharm.javagi.patches;

import io.github.jwharm.javagi.gir.GirElement;
import io.github.jwharm.javagi.gir.*;
import io.github.jwharm.javagi.gir.Record;
import io.github.jwharm.javagi.util.Patch;

import java.util.ArrayList;
import java.util.List;

import static io.github.jwharm.javagi.util.CollectionUtils.listOfNonNull;

/**
* This patch will apply to all GIR files
*/
Expand All @@ -21,6 +26,52 @@ public GirElement patch(GirElement element, String namespace) {
&& rec.name().endsWith("Private"))
return element.withAttribute("java-gi-skip", "1");

/*
* Enumerations and Bitfields (flagged types) cannot have methods in
* GObject-Introspection, but this is allowed in Java. We change all
* functions where the first parameter is of the enclosing flagged type
* into methods with an instance parameter.
*/
if (element instanceof FlaggedType t
&& t.children().stream().anyMatch(Function.class::isInstance)) {

// Create a new list of nodes for the enumeration/bitfield,
// replacing functions by methods when possible.
List<Node> children = new ArrayList<>();
for (Node child : t.children()) {
if (child instanceof Function func
&& func.parameters() != null
&& func.parameters().parameters().getFirst().anyType() instanceof Type type
&& t.name().equals(type.name())) {

// Create new <parameters> element for method
var pList = func.parameters().parameters();
var pFirst = pList.getFirst();
var pOthers = pList.subList(1, pList.size());
var pInstance = new InstanceParameter(pFirst.attributes(), pFirst.children());
ArrayList<Node> paramList = new ArrayList<>(pOthers);
paramList.addFirst(pInstance);

// Construct <method> element
Method method = new Method(
func.attributes(),
listOfNonNull(
func.infoElements().doc(),
func.infoElements().sourcePosition(),
func.returnValue(),
new Parameters(paramList)),
func.platforms());

// Add method to enumeration
children.add(method);
} else {
// Add node unmodified to enumeration
children.add(child);
}
}
return t.withChildren(children);
}

return element;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import io.github.jwharm.javagi.gir.Multiplatform;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Utility functions for working with collections.
Expand Down Expand Up @@ -78,4 +80,17 @@ public static <A, B> Map<A, B> union(Map<A, B> map1, Map<A, B> map2) {
map3.putAll(map2);
return map3;
}

/**
* Similar to {@link List#of}, but ignores {@code null} arguments.
*/
@SafeVarargs
public static <T> List<T> listOfNonNull(T... elements) {
if (elements == null)
return Collections.emptyList();

return Stream.of(elements)
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
}

1 comment on commit 71b8294

@jwharm
Copy link
Owner Author

@jwharm jwharm commented on 71b8294 Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the list of functions that have been moved to instance methods with this change:

  • org.gnome.adwaita.Easing::ease
  • org.gnome.adwaita.LengthUnit::fromPx and toPx
  • org.gnome.glib.UnicodeScript::toIso15924
  • org.freedesktop.gstreamer.gst.DebugLevel::getName
  • org.freedesktop.gstreamer.gst.EventType::getFlags, getName, toQuark and toStickyOrdering
  • org.freedesktop.gstreamer.gst.Format::getDetails, getName and toQuark
  • org.freedesktop.gstreamer.gst.PadMode::getName
  • org.freedesktop.gstreamer.gst.QueryType::getFlags, getName and toQuark
  • org.freedesktop.gstreamer.gst.StateChange::getName
  • org.freedesktop.gstreamer.gst.TocEntryType::getNick
  • org.freedesktop.gstreamer.audio.AudioFormat::getInfo and toString
  • org.freedesktop.gstreamer.pbutils.InstallPluginsReturn::getName
  • org.freedesktop.gstreamer.video.VideoCaptionType::toCaps
  • org.freedesktop.gstreamer.video.VideoColorMatrix::getKrKb and toIso
  • org.freedesktop.gstreamer.video.VideoColorPrimaries::getInfo, isEquivalent and toIso
  • org.freedesktop.gstreamer.video.VideoColorRange::offsets
  • org.freedesktop.gstreamer.video.VideoFieldOrder::toString
  • org.freedesktop.gstreamer.video.VideoFormatInfo::getInfo, getPalette, toFourcc and toString
  • org.freedesktop.gstreamer.video.VideoInterlaceMode::toString
  • org.freedesktop.gstreamer.video.VideoMultiviewMode::toCapsString
  • org.freedesktop.gstreamer.video.VideoTransferFunction::decode, encode, fromIsio, isEquivalent and toIso
  • org.gnome.gtk.AccessibleProperty::initValue
  • org.gnome.gtk.AccessibleRelation::initValue
  • org.gnome.gtk.AccessibleState::initValue
  • org.gnome.pango.AttrType::getName and register
  • org.gnome.pango.Gravity::toRotation
  • org.gnome.pango.Script::getSampleLanguage

Please sign in to comment.