@@ -65,9 +65,7 @@ private void nullCheck(MethodSpec.Builder builder) {
65
65
|| p .isDestroyNotifyParameter ()
66
66
|| p .isArrayLengthParameter ()
67
67
|| p .varargs ()
68
- || (type != null
69
- && type .isPrimitive ()
70
- && !type .isPointer ())))) {
68
+ || (type != null && type .isPrimitive () && !type .isPointer ())))) {
71
69
builder .addStatement ("$T.requireNonNull($L, $S)" ,
72
70
Objects .class ,
73
71
getName (),
@@ -77,9 +75,7 @@ private void nullCheck(MethodSpec.Builder builder) {
77
75
78
76
// Allocate memory for out-parameter
79
77
private void pointerAllocation (MethodSpec .Builder builder ) {
80
- if (p .isOutParameter ()
81
- && array != null
82
- && (!array .unknownSize ())) {
78
+ if (p .isOutParameter () && array != null && !array .unknownSize ()) {
83
79
/*
84
80
* Out-parameter array with known size: If the array isn't null,
85
81
* copy the contents into the native memory buffer. If it is null,
@@ -100,7 +96,7 @@ private void pointerAllocation(MethodSpec.Builder builder) {
100
96
|| (type != null
101
97
&& type .isPointer ()
102
98
&& target instanceof Alias a
103
- && a .type (). isPrimitive ())) {
99
+ && a .isValueWrapper ())) {
104
100
var stmt = PartialStatement .of (
105
101
"$memorySegment:T _$name:LPointer = _arena.allocate(" ,
106
102
"memorySegment" , MemorySegment .class ,
@@ -116,33 +112,31 @@ private void pointerAllocation(MethodSpec.Builder builder) {
116
112
* can be omitted from the Java API.
117
113
*/
118
114
private void arrayLength (MethodSpec .Builder builder ) {
119
- if (p .isArrayLengthParameter ()) {
120
- if (p .isOutParameter ()) {
121
- // Set the initial value of the allocated pointer to the length
122
- // of the input array
123
- if (p .isArrayLengthParameter () && p .isOutParameter ()) {
124
- var stmt = PartialStatement .of ("_$name:LPointer.set(" ,
125
- "name" , getName ())
126
- .add (generateValueLayoutPlain (type ))
127
- .add (", 0L,$W" )
128
- .add (arrayLengthStatement ())
129
- .add (");\n " );
130
- builder .addNamedCode (stmt .format (), stmt .arguments ());
131
- }
132
- // Declare an Out<> instance
133
- builder .addStatement ("$1T $2L = new $3T<>()" ,
134
- getType (),
135
- getName (),
136
- ClassNames .OUT );
137
- } else {
138
- // Declare a primitive value
139
- var stmt = PartialStatement .of ("$type:T $name:L =$W" ,
140
- "type" , getType (),
141
- "name" , getName ())
115
+ if (!p .isArrayLengthParameter ())
116
+ return ;
117
+
118
+ if (p .isOutParameter ()) {
119
+ // Set the initial value of the allocated pointer to the length
120
+ // of the input array
121
+ if (p .isArrayLengthParameter () && p .isOutParameter ()) {
122
+ var stmt = PartialStatement .of ("_$name:LPointer.set(" , "name" , getName ())
123
+ .add (generateValueLayoutPlain (type ))
124
+ .add (", 0L,$W" )
142
125
.add (arrayLengthStatement ())
143
- .add (";\n " );
126
+ .add (") ;\n " );
144
127
builder .addNamedCode (stmt .format (), stmt .arguments ());
145
128
}
129
+ // Declare an Out<> instance
130
+ builder .addStatement ("$1T $2L = new $3T<>()" ,
131
+ getType (), getName (), ClassNames .OUT );
132
+ } else {
133
+ // Declare a primitive value
134
+ var stmt = PartialStatement .of ("$type:T $name:L =$W" ,
135
+ "type" , getType (),
136
+ "name" , getName ())
137
+ .add (arrayLengthStatement ())
138
+ .add (";\n " );
139
+ builder .addNamedCode (stmt .format (), stmt .arguments ());
146
140
}
147
141
}
148
142
@@ -162,27 +156,23 @@ private PartialStatement arrayLengthStatement() {
162
156
"zero" , literal (p .anyType ().typeName (), "0" ),
163
157
"cast" , List .of ("byte" , "short" ).contains (type .javaType ())
164
158
? "(" + type .javaType () + ") "
165
- : ""
166
- )
159
+ : "" )
167
160
.add (arrayParam .isOutParameter ()
168
161
? "$arr:L.get() == null ? $zero:L : $cast:L$arr:L.get().length"
169
- : "$arr:L.length"
170
- );
162
+ : "$arr:L.length" );
171
163
}
172
164
173
165
// Arena for parameters with async or notified scope
174
166
private void scope (MethodSpec .Builder builder ) {
175
167
if (p .scope () == Scope .NOTIFIED && p .destroy () != null )
176
168
builder .addStatement ("final $1T _$2LScope = $1T.ofConfined()" ,
177
- Arena .class ,
178
- getName ());
179
- else if (p .scope () == Scope .ASYNC && ( !p .isDestroyNotifyParameter () ))
169
+ Arena .class , getName ());
170
+
171
+ if (p .scope () == Scope .ASYNC && !p .isDestroyNotifyParameter ())
180
172
builder .addStatement ("final $1T _$2LScope = $1T.ofConfined()" ,
181
- Arena .class ,
182
- getName ())
183
- .addStatement ("if ($2L != null) $1T.CLEANER.register($2L, new $1T(_$2LScope))" ,
184
- ClassNames .ARENA_CLOSE_ACTION ,
185
- getName ());
173
+ Arena .class , getName ())
174
+ .addStatement ("if ($2L != null) $1T.CLEANER.register($2L, new $1T(_$2LScope))" ,
175
+ ClassNames .ARENA_CLOSE_ACTION , getName ());
186
176
}
187
177
188
178
// If the parameter has attribute transfer-ownership="full", we must
@@ -193,8 +183,8 @@ private void transferOwnership(MethodSpec.Builder builder) {
193
183
// out parameter or a pointer)
194
184
if (target != null && target .checkIsGObject ()
195
185
&& p .transferOwnership () != TransferOwnership .NONE
196
- && ( !p .isOutParameter () )
197
- && (type .cType () == null || (! type .cType ().endsWith ("**" ) ))) {
186
+ && !p .isOutParameter ()
187
+ && (type .cType () == null || ! type .cType ().endsWith ("**" ))) {
198
188
builder .beginControlFlow ("if ($L instanceof $T _gobject)" ,
199
189
getName (), ClassNames .GOBJECT )
200
190
.addStatement ("$T.debug($S, _gobject.handle().address())" ,
@@ -207,12 +197,11 @@ private void transferOwnership(MethodSpec.Builder builder) {
207
197
// Same, but for structs/unions: Disable the cleaner
208
198
else if ((target instanceof StandardLayoutType )
209
199
&& p .transferOwnership () != TransferOwnership .NONE
210
- && ( !p .isOutParameter () )
211
- && (type .cType () == null || (! type .cType ().endsWith ("**" ) ))) {
200
+ && !p .isOutParameter ()
201
+ && (type .cType () == null || ! type .cType ().endsWith ("**" ))) {
212
202
builder .addStatement (
213
- checkNull ()
214
- ? "if ($1L != null) $2T.yieldOwnership($1L)"
215
- : "$2T.yieldOwnership($1L)" ,
203
+ checkNull () ? "if ($1L != null) $2T.yieldOwnership($1L)"
204
+ : "$2T.yieldOwnership($1L)" ,
216
205
getName (),
217
206
ClassNames .MEMORY_CLEANER );
218
207
}
@@ -221,9 +210,7 @@ else if ((target instanceof StandardLayoutType)
221
210
// Read the value from a pointer to a primitive value and store it
222
211
// in a Java Alias object
223
212
private void readPrimitiveAliasPointer (MethodSpec .Builder builder ) {
224
- if (target instanceof Alias a
225
- && a .type ().isPrimitive ()
226
- && type .isPointer ()) {
213
+ if (target instanceof Alias a && a .isValueWrapper () && type .isPointer ()) {
227
214
var stmt = PartialStatement .of (
228
215
"$memorySegment:T $name:LParam = $name:L.reinterpret(" ,
229
216
"memorySegment" , MemorySegment .class ,
@@ -248,49 +235,50 @@ private void readOutParameter(MethodSpec.Builder builder) {
248
235
if (!p .isOutParameter ())
249
236
return ;
250
237
251
- // Pointer to a single value
252
- if (type != null ) {
253
- var stmt = PartialStatement .of (
254
- "$memorySegment:T $name:LParam = $name:L.reinterpret(" ,
255
- "memorySegment" , MemorySegment .class ,
256
- "name" , getName ())
257
- .add (generateValueLayoutPlain (type ))
258
- .add (".byteSize(), _arena, null);\n " );
238
+ // Pointer to an array
239
+ if (array != null ) {
240
+ var stmt = PartialStatement .of ("$outType:T _" + getName () + "Out = new $out:T<>(" ,
241
+ "outType" , getType (),
242
+ "out" , ClassNames .OUT )
243
+ .add (marshalNativeToJava (getName (), true ))
244
+ .add (");\n " );
259
245
builder .addNamedCode (stmt .format (), stmt .arguments ());
246
+ return ;
247
+ }
260
248
261
- if (type .isPrimitive ()
262
- || target instanceof Alias a && a .type ().isPrimitive ()) {
263
- stmt = PartialStatement .of (
264
- "$outType:T _$name:LOut = new $out:T<>($name:LParam.get(" ,
265
- "outType" , getType (),
266
- "name" , getName (),
267
- "out" , ClassNames .OUT )
268
- .add (generateValueLayoutPlain (type ))
269
- .add (", 0)" )
270
- .add (type .isBoolean () ? " != 0" : "" )
271
- .add (");\n " );
272
- builder .addNamedCode (stmt .format (), stmt .arguments ());
273
- } else {
274
- String identifier = getName () + "Param" ;
275
- if (target instanceof FlaggedType )
276
- identifier += ".get($valueLayout:T.JAVA_INT, 0)" ;
249
+ if (type == null )
250
+ return ;
277
251
278
- stmt = PartialStatement .of ("$outType:T _" + getName () + "Out = new $out:T<>(" ,
279
- "valueLayout" , ValueLayout .class ,
280
- "outType" , getType (),
281
- "out" , ClassNames .OUT )
282
- .add (marshalNativeToJava (type , identifier , true ))
283
- .add (");\n " );
284
- builder .addNamedCode (stmt .format (), stmt .arguments ());
285
- }
286
- }
252
+ // Pointer to a single value
253
+ var stmt = PartialStatement .of (
254
+ "$memorySegment:T $name:LParam = $name:L.reinterpret(" ,
255
+ "memorySegment" , MemorySegment .class ,
256
+ "name" , getName ())
257
+ .add (generateValueLayoutPlain (type ))
258
+ .add (".byteSize(), _arena, null);\n " );
259
+ builder .addNamedCode (stmt .format (), stmt .arguments ());
287
260
288
- // Pointer to an array
289
- else if ( array != null ) {
290
- var stmt = PartialStatement . of ( "$outType:T _" + getName () + "Out = new $out:T<>(" ,
261
+ if ( type . isPrimitive () || target instanceof Alias a && a . isValueWrapper ()) {
262
+ stmt = PartialStatement . of (
263
+ "$outType:T _$name:LOut = new $out:T<>($name:LParam.get (" ,
291
264
"outType" , getType (),
265
+ "name" , getName (),
292
266
"out" , ClassNames .OUT )
293
- .add (marshalNativeToJava (getName (), true ))
267
+ .add (generateValueLayoutPlain (type ))
268
+ .add (", 0)" )
269
+ .add (type .isBoolean () ? " != 0" : "" )
270
+ .add (");\n " );
271
+ builder .addNamedCode (stmt .format (), stmt .arguments ());
272
+ } else {
273
+ String identifier = getName () + "Param" ;
274
+ if (target instanceof FlaggedType )
275
+ identifier += ".get($valueLayout:T.JAVA_INT, 0)" ;
276
+
277
+ stmt = PartialStatement .of ("$outType:T _" + getName () + "Out = new $out:T<>(" ,
278
+ "valueLayout" , ValueLayout .class ,
279
+ "outType" , getType (),
280
+ "out" , ClassNames .OUT )
281
+ .add (marshalNativeToJava (type , identifier , true ))
294
282
.add (");\n " );
295
283
builder .addNamedCode (stmt .format (), stmt .arguments ());
296
284
}
0 commit comments