Skip to content

Commit

Permalink
Virtual methods with protected visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jwharm committed Nov 8, 2023
1 parent cb6b8bb commit 2766e49
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public abstract class GenerateSources extends DefaultTask {
void execute() {
try {
Module linux = parse(Platform.LINUX, getInputDirectory().get(), getGirFile().get(),
getUrlPrefix().getOrElse(null), getPatch().getOrElse(null));
getUrlPrefix().getOrNull(), getPatch().getOrNull());
Module windows = parse(Platform.WINDOWS, getInputDirectory().get(), getGirFile().get(),
getUrlPrefix().getOrElse(null), getPatch().getOrElse(null));
getUrlPrefix().getOrNull(), getPatch().getOrNull());
Module macos = parse(Platform.MACOS, getInputDirectory().get(), getGirFile().get(),
getUrlPrefix().getOrElse(null), getPatch().getOrElse(null));
getUrlPrefix().getOrNull(), getPatch().getOrNull());

Module module = new Merge().merge(linux, windows, macos);

Expand Down Expand Up @@ -109,6 +109,9 @@ private static Module parse(Platform platform, Directory sourceDirectory, String
// Flag unsupported va_list methods so they will not be generated
module.flagVaListFunctions();

// Link the type references to the GIR type definition across the GI repositories
module.link();

// Apply patch
if (patch != null) {
patch.patch(r);
Expand All @@ -118,9 +121,6 @@ private static Module parse(Platform platform, Directory sourceDirectory, String
// Gir file not found for this platform: This will generate code with UnsupportedPlatformExceptions
}

// Link the type references to the GIR type definition across the GI repositories
module.link();

return module;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ default void addInstanceMethod(Repository repo, String type, String virtualMetho
method.parameters = vm.parameters;
method.returnValue = vm.returnValue;
vm.parent.methodList.add(method);
vm.skip = true;
}

default void makeGeneric(Repository repo, String type) {
Expand All @@ -201,13 +202,15 @@ default void makeGeneric(Repository repo, String type) {
for (Parameter p : m.parameters.parameterList) {
if (p.type != null && "org.gnome.gobject.GObject".equals(p.type.qualifiedJavaType)) {
p.type.isGeneric = true;
p.type.init(p.type.name);
}
}
}
if (m.returnValue != null) {
Type returnType = m.returnValue.type;
if (returnType != null && "org.gnome.gobject.GObject".equals(returnType.qualifiedJavaType)) {
returnType.isGeneric = true;
returnType.init(returnType.name);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

public interface CallableType {

GirElement getParent();

Parameters getParameters();
void setParameters(Parameters ps);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public String getInteropString(String paramName, boolean isPointer, Scope scope)
return "%s.toCallback(%s)".formatted(paramName, arena);
}

@Override
public GirElement getParent() {
return parent;
}

@Override
public Parameters getParameters() {
return parameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ default void generateFunctionalInterface(SourceWriter writer, String javaName) t
writer.write(" */\n");
}
writer.write("@FunctionalInterface\n");
writer.write("public interface " + javaName + " {\n");
if (! (getParent() instanceof Interface)) {
writer.write("public ");
}
writer.write("interface " + javaName + " {\n");
writer.write("\n");
writer.increaseIndent();

// Generate javadoc for run(...)
Doc doc = getDoc();
if (doc != null)
if (doc != null) {
doc.generate(writer, false);
}

// Deprecation
if ("1".equals(((GirElement) this).deprecated)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public Constructor(GirElement parent, String name, String cIdentifier, String de
}

public void generate(SourceWriter writer) throws IOException {
if (skip) {
return;
}

String privateMethodName = "construct" + Conversions.toCamelCase(name, true);
writer.write("\n");

Expand Down Expand Up @@ -85,6 +89,10 @@ public void generate(SourceWriter writer) throws IOException {
}

public void generateNamed(SourceWriter writer) throws IOException {
if (skip) {
return;
}

String privateMethodName = "construct" + Conversions.toCamelCase(name, true);
RegisteredType constructed = (RegisteredType) parent;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public class Function extends Method {

public Function(GirElement parent, String name, String cIdentifier, String deprecated, String throws_, String movedTo) {
super(parent, name, cIdentifier, deprecated, throws_, null, null, movedTo);
visibility = "public";
visibility = parent instanceof Interface ? "" : "public";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public FunctionMacro(GirElement parent, String name, String cIdentifier, String
this.throws_ = throws_;
}

@Override
public GirElement getParent() {
return parent;
}

@Override
public Parameters getParameters() {
return parameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ public String getMethodDeclaration() {

try {
// Visibility
writer.write(visibility + " ");
if (! visibility.isEmpty()) {
writer.write(visibility + " ");
}

// Static methods (functions and constructor helpers)
if (this instanceof Function || this instanceof Constructor) {
Expand Down Expand Up @@ -307,6 +309,11 @@ public boolean hasVaListParameter() {
var lastParam = parameters.parameterList.get(parameters.parameterList.size() - 1);
return lastParam.type != null && "VaList".equals(lastParam.type.simpleJavaType);
}

@Override
public GirElement getParent() {
return parent;
}

@Override
public Parameters getParameters() {
Expand Down
13 changes: 12 additions & 1 deletion buildSrc/src/main/java/io/github/jwharm/javagi/model/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ public void flagVaListFunctions() {
for (Repository repository : repositories.values()) {
// Methods, virtual methods and functions
for (RegisteredType rt : repository.namespace.registeredTypeMap.values()) {
for (Constructor method : rt.constructorList) {
flagVaListFunction(method);
}
for (Method method : rt.methodList) {
flagVaListFunction(method);
}
Expand All @@ -150,7 +153,8 @@ public void flagVaListFunctions() {
private void flagVaListFunction(Method method) {
if (method.parameters != null) {
for (Parameter parameter : method.parameters.parameterList) {
if (parameter.type != null && "va_list".equals(parameter.type.cType)) {
if (parameter.type != null
&& ("va_list".equals(parameter.type.cType) || "va_list*".equals(parameter.type.cType))) {
method.skip = true;
break;
}
Expand All @@ -170,10 +174,17 @@ private void linkVirtualMethods() {
if (method.getNameAndSignature().equals(vm.getNameAndSignature())) {
method.linkedVirtualMethod = vm;
vm.linkedMethod = method;
vm.skip = true;
break;
}
}
}
// Flag virtual methods in interfaces to not be generated
if (rt instanceof Interface) {
for (VirtualMethod vm : rt.virtualMethodList) {
vm.skip = true;
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package io.github.jwharm.javagi.model;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -70,7 +69,7 @@ public RegisteredType(GirElement parent, String name, String parentClass, String
}
}

// Find out if this tyjpe is a subclass of the provided classname
// Find out if this type is a subclass of the provided classname
protected boolean isInstanceOf(String classname) {
if (this.qualifiedName.equals(classname)) {
return true;
Expand All @@ -86,7 +85,7 @@ protected boolean isInstanceOf(String classname) {
}

public boolean isFloating() {
// GObject has a ref_sink function but we don't want to treat all GObjects as floating references.
// GObject has a ref_sink function, but we don't want to treat all GObjects as floating references.
if ("GObject".equals(javaName) && "org.gnome.gobject".equals(getNamespace().packageName)) {
return false;
}
Expand Down Expand Up @@ -136,7 +135,10 @@ protected void generateGType(SourceWriter writer) throws IOException {
writer.write(" * Get the GType of the " + cType + " " + (this instanceof Interface ? "interface" : "class") + ".\n");
writer.write(" * @return the GType\n");
writer.write(" */\n");
writer.write("public static org.gnome.glib.Type getType() {\n");
if (! (this instanceof Interface)) {
writer.write("public ");
}
writer.write("static org.gnome.glib.Type getType() {\n");
writer.write(" return Interop.getType(\"" + getType + "\");\n");
writer.write("}\n");
}
Expand Down Expand Up @@ -186,7 +188,7 @@ protected void generateMemoryLayout(SourceWriter writer) throws IOException {
writer.increaseIndent();

// Check if this type is either defined as a union, or has a union element
boolean isUnion = this instanceof Union || (! unionList.isEmpty());
boolean isUnion = this instanceof Union || !unionList.isEmpty();

writer.write("return MemoryLayout.");
if (isUnion) {
Expand All @@ -196,8 +198,9 @@ protected void generateMemoryLayout(SourceWriter writer) throws IOException {
}

List<Field> fieldList = this.fieldList;
if (fieldList.isEmpty() && unionList.size() > 0 && unionList.get(0).fieldList.size() > 0)
if (fieldList.isEmpty() && !unionList.isEmpty() && !unionList.get(0).fieldList.isEmpty()) {
fieldList = unionList.get(0).fieldList;
}

// How many bytes have we generated thus far
int size = 0;
Expand Down Expand Up @@ -295,48 +298,25 @@ public void generateSetFreeFunc(SourceWriter writer, String identifier, String c
protected void generateMethodsAndSignals(SourceWriter writer) throws IOException {
// Generate instance methods
for (Method m : methodList) {
if (m.hasVaListParameter()) { // va_list parameters are not supported
continue;
}
m.generate(writer);
}

// Generate virtual methods
for (VirtualMethod vm : virtualMethodList) {
if (vm.hasVaListParameter()) {
continue;
}
vm.generate(writer);
}

// Generate all functions as static methods
// Generate functions as static methods
for (Function f : functionList) {
if (f.hasVaListParameter()) {
continue;
}
f.generate(writer);
}

// Generate signals: functional interface, onSignal method and emitSignal method
for (Signal s : signalList) {
if (s.hasVaListParameter()) {
continue;
}
s.generate(writer);
}
}

protected void generateIsAvailable(SourceWriter writer) throws IOException {
writer.write("\n");
writer.write("/**\n");
writer.write(" * Check whether the type is available on the runtime platform.\n");
writer.write(" * @return {@code true} when the type is available on the runtime platform\n");
writer.write(" */\n");
writer.write("public static boolean isAvailable() {\n");
writer.write(" return Interop.isAvailable(\"" + getType + "\", FunctionDescriptor.of(ValueLayout.JAVA_LONG), false);\n");
writer.write("}\n");
}

protected void generateEnsureInitialized(SourceWriter writer) throws IOException {
writer.write("\n");
writer.write("static {\n");
Expand All @@ -352,9 +332,6 @@ protected void generateEnsureInitialized(SourceWriter writer) throws IOException
*/
protected void generateConstructors(SourceWriter writer) throws IOException {
for (Constructor c : constructorList) {
if (c.hasVaListParameter()) { // va_list parameters are not supported
continue;
}
if (c.name.equals("new")) {
c.generate(writer);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public Signal(GirElement parent, String name, String when, String detailed, Stri
}

public void generate(SourceWriter writer) throws IOException {
if (skip) {
return;
}

writer.write("\n");
generateFunctionalInterface(writer, signalName);
writer.write("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public class VirtualMethod extends Method {

public VirtualMethod(GirElement parent, String name, String deprecated, String throws_) {
super(parent, name, null, deprecated, throws_, null, null, null);
visibility = "protected";
visibility = parent instanceof Interface ? "default" : "protected";
}

public void generate(SourceWriter writer) throws IOException {
if (parent instanceof Interface || linkedMethod != null) {
if (skip) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ public void patch(Repository repo) {
setReturnFloating(findMethod(repo, "PaperSize", "to_gvariant"));
setReturnFloating(findMethod(repo, "PrintSettings", "to_gvariant"));

findVirtualMethod(repo, "BuilderScope", "get_type_from_name").skip = false;
findVirtualMethod(repo, "BuilderScope", "get_type_from_function").skip = false;
findVirtualMethod(repo, "BuilderScope", "create_closure").skip = false;

// Add virtual methods as instance methods
addInstanceMethod(repo, "BuilderScope", "get_type_from_name");
addInstanceMethod(repo, "BuilderScope", "get_type_from_function");
addInstanceMethod(repo, "BuilderScope", "create_closure");
addInstanceMethod(repo, "Window", "activate_default");
addInstanceMethod(repo, "Dialog", "close");
addInstanceMethod(repo, "Popover", "activate_default");
Expand Down
Loading

0 comments on commit 2766e49

Please sign in to comment.