Skip to content

Commit

Permalink
Added annotation Default; update javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Dec 3, 2023
1 parent 6701b9a commit 2289650
Show file tree
Hide file tree
Showing 17 changed files with 376 additions and 70 deletions.
85 changes: 44 additions & 41 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -106,30 +106,6 @@ allprojects {
if (JavaVersion.current() < javaVersion) {
toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
}
if (hasJavadocJar.toBoolean()) withJavadocJar()
if (hasSourcesJar.toBoolean()) withSourcesJar()
}

tasks.withType<Javadoc> {
isFailOnError = false
options {
encoding = "UTF-8"
locale = "en_US"
windowTitle = "$projName $projVersion Javadoc"
if (this is StandardJavadocDocletOptions) {
charSet = "UTF-8"
isAuthor = true
if (jdkEarlyAccessDoc == null) {
links("https://docs.oracle.com/en/java/javase/$jdkVersion/docs/api/")
} else {
links("https://download.java.net/java/early_access/$jdkEarlyAccessDoc/docs/api/")
}
if (jdkEnablePreview.toBoolean()) {
addBooleanOption("-enable-preview", true)
addStringOption("source", jdkVersion)
}
}
}
}

//application {
Expand All @@ -151,23 +127,6 @@ allprojects {
)
}

if (hasSourcesJar.toBoolean()) {
tasks.named<Jar>("sourcesJar") {
dependsOn(tasks["classes"])
archiveClassifier = "sources"
from(sourceSets["main"].allSource)
}
}

if (hasJavadocJar.toBoolean()) {
tasks.named<Jar>("javadocJar") {
val javadoc by tasks
dependsOn(javadoc)
archiveClassifier = "javadoc"
from(javadoc)
}
}

tasks.withType<Jar> {
archiveBaseName = projArtifactId
from(rootProject.file(projLicenseFileName)).rename(
Expand All @@ -181,6 +140,50 @@ tasks.withType<JavaCompile> {
options.compilerArgs.add("-proc:none")
}

java {
if (hasJavadocJar.toBoolean()) withJavadocJar()
if (hasSourcesJar.toBoolean()) withSourcesJar()
}

tasks.withType<Javadoc> {
isFailOnError = false
options {
encoding = "UTF-8"
locale = "en_US"
windowTitle = "$projName $projVersion Javadoc"
if (this is StandardJavadocDocletOptions) {
charSet = "UTF-8"
isAuthor = true
if (jdkEarlyAccessDoc == null) {
links("https://docs.oracle.com/en/java/javase/$jdkVersion/docs/api/")
} else {
links("https://download.java.net/java/early_access/$jdkEarlyAccessDoc/docs/api/")
}
if (jdkEnablePreview.toBoolean()) {
addBooleanOption("-enable-preview", true)
addStringOption("source", jdkVersion)
}
}
}
}

if (hasSourcesJar.toBoolean()) {
tasks.named<Jar>("sourcesJar") {
dependsOn(tasks["classes"])
archiveClassifier = "sources"
from(sourceSets["main"].allSource)
}
}

if (hasJavadocJar.toBoolean()) {
tasks.named<Jar>("javadocJar") {
val javadoc by tasks
dependsOn(javadoc)
archiveClassifier = "javadoc"
from(javadoc)
}
}

if (hasPublication.toBoolean() && publicationRepo != null) {
publishing.publications {
register<MavenPublication>("mavenJava") {
Expand Down
6 changes: 3 additions & 3 deletions demo/src/main/java/overrun/marshal/test/CMarshalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface CMarshalTest {

int testWithReturnValue();

@Native(optional = true)
@Default
void testWithOptional();

void testWithArgument(int i, MemorySegment holder);
Expand Down Expand Up @@ -85,8 +85,8 @@ public interface CMarshalTest {
@param segment A memory segment
@return Another memory segment""",
entrypoint = "testAllFeatures",
scope = MarshalScope.PROTECTED,
optional = true
scope = MarshalScope.PROTECTED
)
@Default("MemorySegment.NULL")
MemorySegment testAll(MemorySegment segment);
}
18 changes: 9 additions & 9 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# Gradle Options
org.gradle.jvmargs=-Dfile.encoding=UTF-8

hasPublication=false
publicationSigning=false
hasPublication=true
publicationSigning=true

hasJavadocJar=false
hasSourcesJar=false
hasJavadocJar=true
hasSourcesJar=true

# Project Information
projGroupId=io.github.over-run
projArtifactId=marshal
# The project name should only contain lowercase letters, numbers and hyphen.
projName=marshal
projVersion=0.1.0-SNAPSHOT
projVersion=0.1.0-alpha.0
projDesc=Marshaller of native libraries
# Uncomment them if you want to publish to maven repository.
#projUrl=https://github.com/Over-Run/project-template
#projLicenseUrl=https://raw.githubusercontent.com/Over-Run/project-template/main/LICENSE
#projScmConnection=scm:git:https://github.com/Over-Run/project-template.git
#projScmUrl=https://github.com/Over-Run/project-template.git
projUrl=https://github.com/Over-Run/marshal
projLicenseUrl=https://raw.githubusercontent.com/Over-Run/marshal/main/LICENSE
projScmConnection=scm:git:https://github.com/Over-Run/marshal.git
projScmUrl=https://github.com/Over-Run/marshal.git
projLicense=MIT
projLicenseFileName=LICENSE

Expand Down
19 changes: 18 additions & 1 deletion src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
/*
* MIT License
*
* Copyright (c) 2023 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/

/**
* The core module
*
* @author squid233
* @since 0.1.0
*/
module io.github.overrun.marshal {
exports overrun.marshal;

requires java.compiler;
requires jdk.compiler;
}
19 changes: 19 additions & 0 deletions src/main/java/overrun/marshal/BoolHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ private BoolHelper() {
//no instance
}

/**
* Allocates a memory segment with the given boolean array.
*
* @param allocator the segment allocator
* @param arr the boolean array
* @return the memory segment
*/
public static MemorySegment of(SegmentAllocator allocator, boolean[] arr) {
final MemorySegment segment = allocator.allocate(ValueLayout.JAVA_BOOLEAN, arr.length);
for (int i = 0; i < arr.length; i++) {
Expand All @@ -39,13 +46,25 @@ public static MemorySegment of(SegmentAllocator allocator, boolean[] arr) {
return segment;
}

/**
* Copies data from the given source memory segment into a boolean array.
*
* @param src the source
* @param dst the destination
*/
public static void copy(MemorySegment src, boolean[] dst) {
final int length = checkArraySize(Math.min(src.byteSize(), dst.length));
for (int i = 0; i < length; i++) {
dst[i] = src.getAtIndex(ValueLayout.JAVA_BOOLEAN, i);
}
}

/**
* Converts a memory segment into a boolean array.
*
* @param segment the memory segment
* @return the boolean array
*/
public static boolean[] toArray(MemorySegment segment) {
boolean[] b = new boolean[checkArraySize(segment.byteSize())];
for (int i = 0; i < b.length; i++) {
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/overrun/marshal/Checks.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,52 @@
import java.util.function.Supplier;

/**
* Checks
*
* @author squid233
* @since 0.1.0
*/
public final class Checks {
/**
* Check size of a fixed size array argument. Default value: {@code true}
*
* @see FixedSize
*/
public static final Entry<Boolean> CHECK_ARRAY_SIZE = new Entry<>(() -> true);

private Checks() {
//no instance
}

/**
* Checks the array size.
*
* @param expected the expected size
* @param got the got size
*/
public static void checkArraySize(int expected, int got) {
if (CHECK_ARRAY_SIZE.get() && expected != got) {
throw new IllegalArgumentException("Expected array of size " + expected + ", got " + got);
}
}

/**
* A check entry
*
* @author squid233
* @since 0.1.0
*/
public static final class Entry<T> {
private final Supplier<T> supplier;
private T value;

public Entry(Supplier<T> supplier) {
private Entry(Supplier<T> supplier) {
this.supplier = supplier;
}

/**
* {@return the value of this entry}
*/
public T get() {
if (value == null) {
value = supplier.get();
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/overrun/marshal/Custom.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,23 @@
import java.lang.annotation.*;

/**
* Marks a method that uses custom code instead of generated code.
* <h2>Example</h2>
* <pre>{@code
* @Custom("""
* System.out.println("Hello world");""")
* void myCode();
* }</pre>
*
* @author squid233
* @since 0.1.0
*/
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Custom {
/**
* {@return the custom code}
*/
String value();
}
46 changes: 46 additions & 0 deletions src/main/java/overrun/marshal/Default.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* MIT License
*
* Copyright (c) 2023 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/

package overrun.marshal;

import java.lang.annotation.*;

/**
* Marks a method that has a default return value.
* <p>
* A method marked as {@code @Default} will not throw an exception or be invoked
* if it couldn't be found in the specified library.
* <h2>Example</h2>
* <pre>{@code
* @Default
* void functionThatMightBeAbsent();
*
* @Default("42")
* int anotherFunction();
* }</pre>
*
* @author squid233
* @since 0.1.0
*/
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Default {
/**
* {@return the default value of the method}
*/
String value() default "";
}
12 changes: 12 additions & 0 deletions src/main/java/overrun/marshal/FixedSize.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,24 @@
import java.lang.annotation.*;

/**
* Marks an array parameter as a fixed size array.
* <p>
* The generated code will try to check the size of a passing array.
* <h2>Example</h2>
* <pre>{@code
* void set(@FixedSize(3) int[] vec);
* }</pre>
*
* @author squid233
* @see Checks#CHECK_ARRAY_SIZE
* @since 0.1.0
*/
@Documented
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.SOURCE)
public @interface FixedSize {
/**
* {@return the size of the array}
*/
int value();
}
3 changes: 3 additions & 0 deletions src/main/java/overrun/marshal/LibrarySelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package overrun.marshal;

/**
* A library selector.
*
* @author squid233
* @see NativeApi
* @since 0.1.0
*/
public interface LibrarySelector {
Expand Down
Loading

0 comments on commit 2289650

Please sign in to comment.