diff --git a/annotation-processor-test/src/test/java/io/github/digitalsmile/gpio/libcurl/Libcurl.java b/annotation-processor-test/src/test/java/io/github/digitalsmile/gpio/libcurl/Libcurl.java index 924db7b..6675a4c 100644 --- a/annotation-processor-test/src/test/java/io/github/digitalsmile/gpio/libcurl/Libcurl.java +++ b/annotation-processor-test/src/test/java/io/github/digitalsmile/gpio/libcurl/Libcurl.java @@ -1,17 +1,17 @@ package io.github.digitalsmile.gpio.libcurl; import io.github.digitalsmile.annotation.NativeMemory; +import io.github.digitalsmile.annotation.NativeMemoryException; import io.github.digitalsmile.annotation.NativeMemoryOptions; import io.github.digitalsmile.annotation.function.ByAddress; import io.github.digitalsmile.annotation.function.NativeManualFunction; -import io.github.digitalsmile.annotation.NativeMemoryException; import io.github.digitalsmile.annotation.structure.Enum; import io.github.digitalsmile.annotation.structure.Enums; import io.github.digitalsmile.annotation.structure.Struct; import io.github.digitalsmile.annotation.structure.Structs; -import io.github.digitalsmile.gpio.libcurl.enums.CURLcode; -import io.github.digitalsmile.gpio.libcurl.enums.CURLoption; -import io.github.digitalsmile.gpio.libcurl.opaque.CURL; +import io.github.digitalsmile.gpio.libcurl.enums.CURLCode; +import io.github.digitalsmile.gpio.libcurl.enums.CURLOption; +import io.github.digitalsmile.gpio.libcurl.opaque.CurlInstance; @NativeMemory(headers = "libcurl/curl/include/curl/curl.h") @NativeMemoryOptions(systemIncludes = { @@ -27,17 +27,17 @@ public interface Libcurl { @NativeManualFunction(name = "curl_easy_init", library = "/usr/lib/x86_64-linux-gnu/libcurl.so") - CURL easyInit() throws NativeMemoryException; + CurlInstance easyInit() throws NativeMemoryException; @NativeManualFunction(name = "curl_global_init", library = "/usr/lib/x86_64-linux-gnu/libcurl.so") - CURLcode globalInit(long flags) throws NativeMemoryException; + CURLCode globalInit(long flags) throws NativeMemoryException; @NativeManualFunction(name = "curl_easy_setopt", library = "/usr/lib/x86_64-linux-gnu/libcurl.so") - CURLcode easySetOpt(CURL curl, CURLoption option, String value) throws NativeMemoryException; + CURLCode easySetOpt(CurlInstance curl, CURLOption option, String value) throws NativeMemoryException; @NativeManualFunction(name = "curl_easy_setopt", library = "/usr/lib/x86_64-linux-gnu/libcurl.so") - CURLcode easySetOpt(CURL curl, CURLoption option, @ByAddress long value) throws NativeMemoryException; + CURLCode easySetOpt(CurlInstance curl, CURLOption option, @ByAddress long value) throws NativeMemoryException; @NativeManualFunction(name = "curl_easy_perform", library = "/usr/lib/x86_64-linux-gnu/libcurl.so") - CURLcode easyPerform(CURL curl) throws NativeMemoryException; + CURLCode easyPerform(CurlInstance curl) throws NativeMemoryException; } diff --git a/annotation-processor-test/src/test/java/io/github/digitalsmile/gpio/libcurl/LibcurlTest.java b/annotation-processor-test/src/test/java/io/github/digitalsmile/gpio/libcurl/LibcurlTest.java index 8e7ce2a..e3cdb21 100644 --- a/annotation-processor-test/src/test/java/io/github/digitalsmile/gpio/libcurl/LibcurlTest.java +++ b/annotation-processor-test/src/test/java/io/github/digitalsmile/gpio/libcurl/LibcurlTest.java @@ -1,41 +1,27 @@ package io.github.digitalsmile.gpio.libcurl; import io.github.digitalsmile.annotation.NativeMemoryException; -import io.github.digitalsmile.gpio.libcurl.enums.CURLcode; -import io.github.digitalsmile.gpio.libcurl.enums.CURLoption; +import io.github.digitalsmile.gpio.libcurl.enums.CURLCode; +import io.github.digitalsmile.gpio.libcurl.enums.CURLOption; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; + public class LibcurlTest { @Test public void libcurl() throws NativeMemoryException { - try(var libcurl = new LibcurlNative()) { - var code = libcurl.globalInit(CurlConstants.CURL_GLOBAL_DEFAULT); - assertEquals(code, CURLcode.CURLE_OK); - var curl = libcurl.easyInit(); - code = libcurl.easySetOpt(curl, CURLoption.CURLOPT_URL, "https://example.com"); - assertEquals(code, CURLcode.CURLE_OK); - code = libcurl.easySetOpt(curl, CURLoption.CURLOPT_FOLLOWLOCATION, 1L); - assertEquals(code, CURLcode.CURLE_OK); - - code = libcurl.easyPerform(curl); - assertEquals(code, CURLcode.CURLE_OK); - } - } - - public static void main(String[] args) throws NativeMemoryException { - try(var libcurl = new LibcurlNative()) { + try (var libcurl = new LibcurlNative()) { var code = libcurl.globalInit(CurlConstants.CURL_GLOBAL_DEFAULT); - assertEquals(code, CURLcode.CURLE_OK); + assertEquals(code, CURLCode.CURLE_OK); var curl = libcurl.easyInit(); - code = libcurl.easySetOpt(curl, CURLoption.CURLOPT_URL, "https://example.com"); - assertEquals(code, CURLcode.CURLE_OK); - code = libcurl.easySetOpt(curl, CURLoption.CURLOPT_FOLLOWLOCATION, 1L); - assertEquals(code, CURLcode.CURLE_OK); + code = libcurl.easySetOpt(curl, CURLOption.CURLOPT_URL, "https://example.com"); + assertEquals(code, CURLCode.CURLE_OK); + code = libcurl.easySetOpt(curl, CURLOption.CURLOPT_FOLLOWLOCATION, 1L); + assertEquals(code, CURLCode.CURLE_OK); code = libcurl.easyPerform(curl); - assertEquals(code, CURLcode.CURLE_OK); + assertEquals(code, CURLCode.CURLE_OK); } } } diff --git a/annotation-processor-test/src/test/java/io/github/digitalsmile/gpio/shared/SharedTestTwo.java b/annotation-processor-test/src/test/java/io/github/digitalsmile/gpio/shared/SharedTestTwo.java index bad44b8..8dc512f 100644 --- a/annotation-processor-test/src/test/java/io/github/digitalsmile/gpio/shared/SharedTestTwo.java +++ b/annotation-processor-test/src/test/java/io/github/digitalsmile/gpio/shared/SharedTestTwo.java @@ -12,7 +12,7 @@ @NativeMemory(headers = "/usr/include/x86_64-linux-gnu/sys/stat.h") @NativeMemoryOptions(systemHeader = true) @Structs( - @Struct(name = "stat") + @Struct(name = "stat", javaName = "Stat") ) public interface SharedTestTwo { diff --git a/annotation-processor-test/src/test/java/io/github/digitalsmile/gpio/types/custom/GPIOTest.java b/annotation-processor-test/src/test/java/io/github/digitalsmile/gpio/types/custom/GPIOTest.java new file mode 100644 index 0000000..dd3afc8 --- /dev/null +++ b/annotation-processor-test/src/test/java/io/github/digitalsmile/gpio/types/custom/GPIOTest.java @@ -0,0 +1,27 @@ +package io.github.digitalsmile.gpio.types.custom; + +import io.github.digitalsmile.annotation.NativeMemory; +import io.github.digitalsmile.annotation.NativeMemoryOptions; +import io.github.digitalsmile.annotation.structure.Enums; +import io.github.digitalsmile.annotation.structure.Struct; +import io.github.digitalsmile.annotation.structure.Structs; +import io.github.digitalsmile.annotation.structure.Unions; + +@NativeMemory(headers = "/usr/src/linux-headers-${version}/include/uapi/linux/gpio.h") +@NativeMemoryOptions( + processRootConstants = true +) +@Structs({ + @Struct(name = "gpiochip_info", javaName = "ChipInfo"), + @Struct(name = "gpio_v2_line_attribute", javaName = "LineAttribute"), + @Struct(name = "gpio_v2_line_config", javaName = "LineConfig"), + @Struct(name = "gpio_v2_line_config_attribute", javaName = "LineConfigAttribute"), + @Struct(name = "gpio_v2_line_event", javaName = "LineEvent"), + @Struct(name = "gpio_v2_line_info", javaName = "LineInfo"), + @Struct(name = "gpio_v2_line_request", javaName = "LineRequest"), + @Struct(name = "gpio_v2_line_values", javaName = "LineValues") +}) +@Enums +@Unions +public interface GPIOTest { +} diff --git a/annotation-processor/src/main/java/io/github/digitalsmile/NativeProcessor.java b/annotation-processor/src/main/java/io/github/digitalsmile/NativeProcessor.java index 96c8cbe..0906ea0 100644 --- a/annotation-processor/src/main/java/io/github/digitalsmile/NativeProcessor.java +++ b/annotation-processor/src/main/java/io/github/digitalsmile/NativeProcessor.java @@ -112,14 +112,17 @@ private List processHeaderFiles(Element element, String[] head List structs = null; if (structsAnnotation != null) { structs = Arrays.stream(structsAnnotation.value()).map(struct -> new Type(struct.name(), struct.javaName())).toList(); + Arrays.stream(structsAnnotation.value()).forEach(struct -> PrettyName.addName(struct.name(), struct.javaName())); } List enums = null; if (enumsAnnotation != null) { enums = Arrays.stream(enumsAnnotation.value()).map(enoom -> new Type(enoom.name(), enoom.javaName())).toList(); + Arrays.stream(enumsAnnotation.value()).forEach(enoom -> PrettyName.addName(enoom.name(), enoom.javaName())); } List unions = null; if (unionsAnnotation != null) { unions = Arrays.stream(unionsAnnotation.value()).map(union -> new Type(union.name(), union.javaName())).toList(); + Arrays.stream(unionsAnnotation.value()).forEach(union -> PrettyName.addName(union.name(), union.javaName())); } var rootConstants = false; diff --git a/annotation-processor/src/main/java/io/github/digitalsmile/PrettyName.java b/annotation-processor/src/main/java/io/github/digitalsmile/PrettyName.java index 0238f54..649728f 100644 --- a/annotation-processor/src/main/java/io/github/digitalsmile/PrettyName.java +++ b/annotation-processor/src/main/java/io/github/digitalsmile/PrettyName.java @@ -15,7 +15,7 @@ public static void addName(String name, String javaName) { public static String getVariableName(String name) { var cachedName = NAMING_CACHE.get(name); if (cachedName != null) { - return cachedName; + name = cachedName; } name = checkJavaName(name); if (name.matches("([a-z]+[a-zA-Z0-9]+)+")) { @@ -38,7 +38,7 @@ public static String getVariableName(String name) { public static String getObjectName(String name) { var cachedName = NAMING_CACHE.get(name); if (cachedName != null) { - return cachedName; + name = cachedName; } name = checkJavaName(name); if (name.matches("([A-Z]+[a-zA-Z0-9]+)+")) { diff --git a/annotation-processor/src/main/java/io/github/digitalsmile/composers/ContextComposer.java b/annotation-processor/src/main/java/io/github/digitalsmile/composers/ContextComposer.java index 36ff7e9..9d98b72 100644 --- a/annotation-processor/src/main/java/io/github/digitalsmile/composers/ContextComposer.java +++ b/annotation-processor/src/main/java/io/github/digitalsmile/composers/ContextComposer.java @@ -2,6 +2,7 @@ import com.squareup.javapoet.*; import io.github.digitalsmile.PackageName; +import io.github.digitalsmile.PrettyName; import io.github.digitalsmile.annotation.ArenaType; import io.github.digitalsmile.annotation.function.NativeCall; import io.github.digitalsmile.annotation.types.interfaces.NativeMemoryContext; @@ -88,7 +89,7 @@ public String compose(String packageName, String javaName, Map