Skip to content

Commit

Permalink
Started migration to JDK 21 (unfinished)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMosigItemis committed Oct 6, 2023
1 parent 395977e commit 6cab8b3
Show file tree
Hide file tree
Showing 43 changed files with 465 additions and 474 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
</organization>

<properties>
<java.version>20</java.version>
<maven.compiler.release>20</maven.compiler.release>
<java.version>21</java.version>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven-surefire-plugin.version>3.1.2</maven-surefire-plugin.version>
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/itemis/fluffyj/memory/FluffyMemory.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.itemis.fluffyj.exceptions.InstantiationNotPermittedException;
import com.itemis.fluffyj.memory.api.FluffySegment;

import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;

/**
Expand Down Expand Up @@ -36,7 +37,7 @@ public static FluffyMemoryPointerBuilder pointer() {
*
* @param nativeSeg - The raw {@link MemorySegment} to wrap.
*/
public static FluffyMemorySegmentWrapper wrap(MemorySegment nativeSeg) {
public static FluffyMemorySegmentWrapper wrap(final MemorySegment nativeSeg) {
requireNonNull(nativeSeg, "nativeSeg");
return new FluffyMemorySegmentWrapper(nativeSeg);
}
Expand All @@ -47,8 +48,8 @@ public static FluffyMemorySegmentWrapper wrap(MemorySegment nativeSeg) {
* @param nativePtr - Dereference the address found inside this ptr segment.
* @return - Entry point for easy dereferenciation.
*/
public static FluffyMemoryDereferencer dereference(MemorySegment nativePtr) {
return new FluffyMemoryDereferencer(wrap(nativePtr), nativePtr);
public static FluffyMemoryDereferencer dereference(final MemorySegment nativePtr) {
return new FluffyMemoryDereferencer(wrap(nativePtr), Arena.ofAuto());
}

/**
Expand All @@ -57,8 +58,8 @@ public static FluffyMemoryDereferencer dereference(MemorySegment nativePtr) {
* @param address - Dereference this address.
* @return - Entry point for easy dereferenciation.
*/
public static FluffyMemoryDereferencer dereference(long address) {
var nativeSeg = MemorySegment.ofAddress(address);
public static FluffyMemoryDereferencer dereference(final long address) {
final var nativeSeg = MemorySegment.ofAddress(address);
return dereference(nativeSeg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import static java.util.Objects.requireNonNull;

import java.lang.foreign.MemorySegment;
import java.lang.foreign.Arena;

/**
* Provides shortcuts for dereferencing pointers.
*/
public final class FluffyMemoryDereferencer {

private FluffyMemorySegmentWrapper wrapper;
private MemorySegment nativePtr;
private final FluffyMemorySegmentWrapper wrapper;
private final Arena arena;

/**
* Construct a new instance.
Expand All @@ -19,9 +19,9 @@ public final class FluffyMemoryDereferencer {
* {@link FluffyMemory#wrap(java.lang.foreign.MemorySegment)}.
* @param nativePtr - The segment that was wrapped with the {@code wrapper}.
*/
FluffyMemoryDereferencer(FluffyMemorySegmentWrapper wrapper, MemorySegment nativePtr) {
FluffyMemoryDereferencer(final FluffyMemorySegmentWrapper wrapper, final Arena arena) {
this.wrapper = requireNonNull(wrapper, "wrapper");
this.nativePtr = requireNonNull(nativePtr, "nativePtr");
this.arena = requireNonNull(arena, "arena");
}

/**
Expand All @@ -31,7 +31,7 @@ public final class FluffyMemoryDereferencer {
* @param targetType - An instance of this class will be returned.
* @return An instance of T that was read from an address of off heap memory.
*/
public <T> T as(Class<? extends T> targetType) {
return wrapper.asPointerOf(targetType).allocate(nativePtr.scope()).dereference();
public <T> T as(final Class<? extends T> targetType) {
return wrapper.asPointerOf(targetType).allocate(arena).dereference();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.itemis.fluffyj.memory;

import static java.lang.foreign.Linker.nativeLinker;
import static java.lang.foreign.SegmentScope.global;
import static java.lang.invoke.MethodHandles.lookup;
import static java.lang.invoke.MethodType.methodType;
import static java.util.Arrays.stream;
Expand All @@ -17,10 +16,10 @@
import com.itemis.fluffyj.memory.internal.FluffyMemoryFuncPointerBuilderStages.ReturnTypeStage;
import com.itemis.fluffyj.memory.internal.FluffyMemoryFuncPointerBuilderStages.TypeConverterStage;

import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.SegmentScope;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Method;
import java.util.List;
Expand Down Expand Up @@ -50,7 +49,7 @@ public final class FluffyMemoryFuncPointerBuilder
* @param conv - A {@link FluffyMemoryTypeConverter} that will be used to convert between JVM
* and native type arguments and return types.
*/
public FluffyMemoryFuncPointerBuilder(String funcName, FluffyMemoryTypeConverter conv) {
public FluffyMemoryFuncPointerBuilder(final String funcName, final FluffyMemoryTypeConverter conv) {
this.funcName = requireNonNull(funcName, "funcName");
this.typeConverter = requireNonNull(conv, "conv");
}
Expand All @@ -61,30 +60,30 @@ public FluffyMemoryFuncPointerBuilder(String funcName, FluffyMemoryTypeConverter
*
* @param funcName - Name of the JVM method to point to.
*/
public FluffyMemoryFuncPointerBuilder(String funcName) {
public FluffyMemoryFuncPointerBuilder(final String funcName) {
this.funcName = requireNonNull(funcName, "funcName");
}

@Override
public ArgsStage of(Object receiver) {
public ArgsStage of(final Object receiver) {
this.receiver = requireNonNull(receiver, "receiver");
return this;
}

@Override
public TypeConverterStage ofType(Object receiver) {
public TypeConverterStage ofType(final Object receiver) {
this.receiver = requireNonNull(receiver, "receiver");
return this;
}

@Override
public ArgsStage withTypeConverter(FluffyMemoryTypeConverter conv) {
public ArgsStage withTypeConverter(final FluffyMemoryTypeConverter conv) {
this.typeConverter = requireNonNull(conv, "conv");
return this;
}

@Override
public ReturnTypeStage withArgs(MemoryLayout... args) {
public ReturnTypeStage withArgs(final MemoryLayout... args) {
nativeArgTypes = requireNonNull(args, "args");
javaArgTypes = typeConverter.getJvmTypes(args);
return this;
Expand All @@ -97,14 +96,14 @@ public ReturnTypeStage withoutArgs() {

@Override
public MemorySegment autoBind() {
return autoBindTo(global());
return autoBindTo(Arena.global());
}

@Override
public MemorySegment autoBindTo(SegmentScope scope) {
requireNonNull(scope, "scope");
public MemorySegment autoBindTo(final Arena arena) {
requireNonNull(arena, "arena");

var candidateMethods = extractMethodCandidates();
final var candidateMethods = extractMethodCandidates();

if (candidateMethods.isEmpty()) {
throwCannotFindMethod(null);
Expand All @@ -114,7 +113,7 @@ public MemorySegment autoBindTo(SegmentScope scope) {
throw new FluffyMemoryException(
"Cannot autobind overloaded method '" + funcName + "'. Please perform manual bind.");
}
var method = candidateMethods.get(0);
final var method = candidateMethods.get(0);

if (!method.canAccess(receiver)) {
throwCannotBindNonAccessibleMethod();
Expand All @@ -123,7 +122,7 @@ public MemorySegment autoBindTo(SegmentScope scope) {
javaArgTypes = method.getParameterTypes();
try {
nativeArgTypes = typeConverter.getNativeTypes(javaArgTypes);
} catch (FluffyMemoryException e) {
} catch (final FluffyMemoryException e) {
throw new FluffyMemoryException(
"Method '" + funcName + "' of type " + receiver.getClass().getCanonicalName()
+ " has unsupported argument types.",
Expand All @@ -133,21 +132,22 @@ public MemorySegment autoBindTo(SegmentScope scope) {
if (javaReturnType.equals(Void.class)) {
throw new FluffyMemoryException("Return type " + Void.class.getCanonicalName() + " is unsupported. Use "
+ void.class.getCanonicalName());
} else if (!(javaReturnType.equals(void.class))) {
}
if (!(javaReturnType.equals(void.class))) {
try {
nativeReturnType = Optional.of(typeConverter.getNativeType(javaReturnType));
} catch (FluffyMemoryException e) {
} catch (final FluffyMemoryException e) {
throw new FluffyMemoryException(
"Method '" + funcName + "' of type " + receiver.getClass().getCanonicalName()
+ " has unsupported return type.",
e);
}
}
return bindTo(scope);
return bindTo(arena);
}

@Override
public BinderStage andReturnType(MemoryLayout returnType) {
public BinderStage andReturnType(final MemoryLayout returnType) {
requireNonNull(returnType, "returnType");

nativeReturnType = Optional.of(returnType);
Expand All @@ -162,8 +162,8 @@ public BinderStage andNoReturnType() {
}

@Override
public MemorySegment bindTo(SegmentScope scope) {
requireNonNull(scope, "scope");
public MemorySegment bindTo(final Arena arena) {
requireNonNull(arena, "scope");

if (extractMethodCandidates().stream().anyMatch(Method::isSynthetic)) {
throwCannotBindSynthMethod();
Expand All @@ -172,23 +172,22 @@ public MemorySegment bindTo(SegmentScope scope) {
MethodHandle callback = null;
try {
callback = lookup().bind(receiver, funcName, methodType(javaReturnType, javaArgTypes));
} catch (NoSuchMethodException e) {
} catch (final NoSuchMethodException e) {
throwCannotFindMethod(e);
} catch (IllegalAccessException e) {
} catch (final IllegalAccessException e) {
throwCannotBindNonAccessibleMethod();
}

if (nativeReturnType.isPresent()) {
return nativeLinker()
.upcallStub(callback, FunctionDescriptor.of(nativeReturnType.get(), nativeArgTypes), scope);
} else {
return nativeLinker().upcallStub(callback, FunctionDescriptor.ofVoid(nativeArgTypes), scope);
.upcallStub(callback, FunctionDescriptor.of(nativeReturnType.get(), nativeArgTypes), arena);
}
return nativeLinker().upcallStub(callback, FunctionDescriptor.ofVoid(nativeArgTypes), arena);
}

@Override
public MemorySegment bindToGlobalScope() {
return bindTo(SegmentScope.global());
public MemorySegment bindToGlobalArena() {
return bindTo(Arena.global());
}

private List<Method> extractMethodCandidates() {
Expand All @@ -204,12 +203,11 @@ private void throwCannotBindNonAccessibleMethod() {
throw new FluffyMemoryException("Cannot create function pointer to non accessible JVM methods.");
}

private void throwCannotFindMethod(Throwable cause) {
var errMsg = "Could not find method '" + funcName + "' in type " + receiver.getClass().getCanonicalName();
private void throwCannotFindMethod(final Throwable cause) {
final var errMsg = "Could not find method '" + funcName + "' in type " + receiver.getClass().getCanonicalName();
if (cause == null) {
throw new FluffyMemoryException(errMsg);
} else {
throw new FluffyMemoryException(errMsg, cause);
}
throw new FluffyMemoryException(errMsg, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.itemis.fluffyj.memory.internal.PointerOfThing;
import com.itemis.fluffyj.memory.internal.impl.CDataTypeConverter;

import java.lang.foreign.SegmentScope;
import java.lang.foreign.Arena;

/**
* Intermediate pointer creation helper.
Expand All @@ -24,7 +24,7 @@ public final class FluffyMemoryPointerBuilder {
* @return A {@link FluffyMemoryScalarPointerAllocator} instance that is able to allocate
* pointers to data of type {@code T}.
*/
public <T> FluffyMemoryScalarPointerAllocator<T> to(FluffyScalarSegment<? extends T> toHere) {
public <T> FluffyMemoryScalarPointerAllocator<T> to(final FluffyScalarSegment<? extends T> toHere) {
requireNonNull(toHere, "toHere");
return new FluffyMemoryScalarPointerAllocator<>(toHere);
}
Expand All @@ -35,7 +35,7 @@ public <T> FluffyMemoryScalarPointerAllocator<T> to(FluffyScalarSegment<? extend
* @return A {@link FluffyMemoryScalarPointerAllocator} instance that is able to allocate
* pointers to data of type {@code T}.
*/
public <T> FluffyMemoryVectorPointerAllocator<T> toArray(FluffyVectorSegment<? extends T> toHere) {
public <T> FluffyMemoryVectorPointerAllocator<T> toArray(final FluffyVectorSegment<? extends T> toHere) {
requireNonNull(toHere, "toHere");
return new FluffyMemoryVectorPointerAllocator<>(toHere);
}
Expand All @@ -45,7 +45,7 @@ public <T> FluffyMemoryVectorPointerAllocator<T> toArray(FluffyVectorSegment<? e
* @return A {@link FluffyMemoryScalarPointerAllocator} instance that is able to allocate
* pointers to data of type {@code T}.
*/
public FluffyMemoryTypedPointerBuilder to(long address) {
public FluffyMemoryTypedPointerBuilder to(final long address) {
requireNonNull(address, "address");
return new FluffyMemoryTypedPointerBuilder(address);
}
Expand All @@ -58,7 +58,7 @@ public FluffyMemoryTypedPointerBuilder to(long address) {
* @return A new instance of {@link FluffyPointer} tied to the global scope.
*/
public FluffyPointer allocate() {
return allocate(SegmentScope.global());
return allocate(Arena.ofAuto());
}

/**
Expand All @@ -68,11 +68,11 @@ public FluffyPointer allocate() {
* @param scope - The scope to tie the pointer to.
* @return A new instance of {@link FluffyPointer}.
*/
public FluffyPointer allocate(SegmentScope scope) {
return new PointerOfThing(requireNonNull(scope, "scope"));
public FluffyPointer allocate(final Arena arena) {
return new PointerOfThing(requireNonNull(arena, "arena"));
}

public <T> FluffyMemoryScalarPointerAllocator<T> of(Class<? extends T> type) {
public <T> FluffyMemoryScalarPointerAllocator<T> of(final Class<? extends T> type) {
return new FluffyMemoryTypedPointerBuilder(NULL.address()).as(type);
}

Expand All @@ -82,7 +82,7 @@ public <T> FluffyMemoryScalarPointerAllocator<T> of(Class<? extends T> type) {
* @param funcName - Name of the JVM method to point to.
* @return A builder that helps with creating the function pointer.
*/
public CFuncStage toCFunc(String funcName) {
public CFuncStage toCFunc(final String funcName) {
return new FluffyMemoryFuncPointerBuilder(requireNonNull(funcName, "funcName"), new CDataTypeConverter());
}

Expand All @@ -93,7 +93,7 @@ public CFuncStage toCFunc(String funcName) {
* @param funcName - Name of the JVM method to point to.
* @return A builder that helps with creating the function pointer.
*/
public FuncStage toFunc(String funcName) {
public FuncStage toFunc(final String funcName) {
return new FluffyMemoryFuncPointerBuilder(requireNonNull(funcName, "funcName"));
}

Expand All @@ -108,7 +108,7 @@ public static final class FluffyMemoryTypedPointerBuilder {
*
* @param address - Created pointer will point to this address.
*/
public FluffyMemoryTypedPointerBuilder(long address) {
public FluffyMemoryTypedPointerBuilder(final long address) {
requireNonNull(address, "address");
this.address = address;
}
Expand All @@ -120,14 +120,14 @@ public FluffyMemoryTypedPointerBuilder(long address) {
* @param type - Type of scalar data to point to.
* @return A new builder instance that helps with creating this kind of pointer.
*/
public <T> FluffyMemoryScalarPointerAllocator<T> as(Class<? extends T> type) {
public <T> FluffyMemoryScalarPointerAllocator<T> as(final Class<? extends T> type) {
return new FluffyMemoryScalarPointerAllocator<>(address, type);
}

/**
* @param byteSize - The size of the array the pointer shall point to in bytes.
*/
public FluffyMemoryTypedArrayPointerBuilder asArray(int byteSize) {
public FluffyMemoryTypedArrayPointerBuilder asArray(final int byteSize) {
return new FluffyMemoryTypedArrayPointerBuilder(address, byteSize);
}
}
Expand All @@ -145,7 +145,7 @@ public static final class FluffyMemoryTypedArrayPointerBuilder {
* @param address - Created pointer will point to this address.
* @param byteSize - Size of the array to point to in bytes.
*/
public FluffyMemoryTypedArrayPointerBuilder(long address, long byteSize) {
public FluffyMemoryTypedArrayPointerBuilder(final long address, final long byteSize) {
this.address = requireNonNull(address, "address");
this.byteSize = byteSize;
}
Expand All @@ -156,7 +156,7 @@ public FluffyMemoryTypedArrayPointerBuilder(long address, long byteSize) {
* @param <T> - Component type of the array to point to.
* @return A new builder instance that helps with creating this kind of pointer.
*/
public <T> FluffyMemoryVectorPointerAllocator<T> of(Class<? extends T[]> arrayType) {
public <T> FluffyMemoryVectorPointerAllocator<T> of(final Class<? extends T[]> arrayType) {
return new FluffyMemoryVectorPointerAllocator<>(address, byteSize, arrayType);
}
}
Expand Down
Loading

0 comments on commit 6cab8b3

Please sign in to comment.