Skip to content

Commit

Permalink
Rename NativeApi to Downcall
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Dec 14, 2023
1 parent d2e0b28 commit 9e0c5c2
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import java.lang.foreign.MemorySegment;
* The documentation will be automatically copied
* into the generated file
*/
@NativeApi(libname = "libglfw.so", name = "GLFW")
@Downcall(libname = "libglfw.so", name = "GLFW")
interface CGLFW {
/**
* A field
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/java/overrun/marshal/test/CMarshalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @author squid233
* @since 0.1.0
*/
@NativeApi(libname = "NativeLib.dll", name = "MarshalTest", makeFinal = false)
@Downcall(libname = "NativeLib.dll", name = "MarshalTest", makeFinal = false)
public interface CMarshalTest {
int CONST_VALUE = 42;
boolean BOOL_VALUE = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package overrun.marshal.test;

import overrun.marshal.NativeApi;
import overrun.marshal.Downcall;

import java.lang.foreign.MemorySegment;

Expand All @@ -26,7 +26,7 @@
* @author squid233
* @since 0.1.0
*/
@NativeApi(libname = "NativeLib", name = "MarshalTestWithLoader", loader = NativeLibLoader.class)
@Downcall(libname = "NativeLib", name = "MarshalTestWithLoader", loader = NativeLibLoader.class)
public interface CMarshalTestWithLoader {
MemorySegment testWithArgAndReturnValue(MemorySegment segment);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.lang.annotation.*;

/**
* Marks a class or interface as a native API provider.
* Marks a class or interface as a downcall handles provider.
* <h2>Constants</h2>
* The generated file will include constants of primitive
* (boolean, byte, short, int, long, float, double) and String types in the marked class or interface.
Expand All @@ -45,7 +45,7 @@
* See {@link FixedSize @FixedSize} and {@link Ref @Ref}.
* <h2>Example</h2>
* <pre>{@code
* @NativeApi(libname = "libGL.so", name = "GL")
* @Downcall(libname = "libGL.so", name = "GL")
* interface CGL {
* int COLOR_BUFFER_BIT = 0x00004000;
* void glClear(int mask);
Expand All @@ -65,7 +65,7 @@
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface NativeApi {
public @interface Downcall {
/**
* {@return the name of the native library}
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@
import java.util.stream.Stream;

/**
* The annotation processor
* Downcall annotation processor
*
* @author squid233
* @since 0.1.0
*/
public final class NativeApiProcessor extends AbstractProcessor {
public final class DowncallProcessor extends AbstractProcessor {
/**
* constructor
*/
public NativeApiProcessor() {
public DowncallProcessor() {
}

@Override
Expand All @@ -64,7 +64,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
}

private void processClasses(RoundEnvironment roundEnv) {
ElementFilter.typesIn(roundEnv.getElementsAnnotatedWith(NativeApi.class)).forEach(e -> {
ElementFilter.typesIn(roundEnv.getElementsAnnotatedWith(Downcall.class)).forEach(e -> {
final var enclosed = e.getEnclosedElements();
try {
writeFile(e, ElementFilter.fieldsIn(enclosed), ElementFilter.methodsIn(enclosed));
Expand All @@ -79,11 +79,11 @@ private void writeFile(
List<VariableElement> fields,
List<ExecutableElement> methods
) throws IOException {
final NativeApi nativeApi = type.getAnnotation(NativeApi.class);
final Downcall downcall = type.getAnnotation(Downcall.class);
final String className = type.getQualifiedName().toString();
final int lastDot = className.lastIndexOf('.');
final String packageName = lastDot > 0 ? className.substring(0, lastDot) : null;
final String simpleClassName = nativeApi.name();
final String simpleClassName = downcall.name();

final SourceFile file = new SourceFile(packageName);
file.addImports(
Expand All @@ -98,7 +98,7 @@ private void writeFile(
);
file.addClass(simpleClassName, classSpec -> {
classSpec.setDocument(getDocument(type));
classSpec.setFinal(nativeApi.makeFinal());
classSpec.setFinal(downcall.makeFinal());

// fields
addFields(fields, classSpec);
Expand Down Expand Up @@ -426,17 +426,17 @@ private void addFields(List<VariableElement> fields, ClassSpec classSpec) {
}

private void addLoader(TypeElement type, ClassSpec classSpec) {
final NativeApi nativeApi = type.getAnnotation(NativeApi.class);
final Downcall downcall = type.getAnnotation(Downcall.class);
final String loader = type.getAnnotationMirrors().stream()
.filter(m -> NativeApi.class.getName().equals(m.getAnnotationType().toString()))
.filter(m -> Downcall.class.getName().equals(m.getAnnotationType().toString()))
.findFirst()
.orElseThrow()
.getElementValues().entrySet().stream()
.filter(e -> "loader()".equals(e.getKey().toString()))
.findFirst()
.map(e -> e.getValue().getValue().toString())
.orElse(null);
final String libname = nativeApi.libname();
final String libname = downcall.libname();
classSpec.addField(new VariableStatement(SymbolLookup.class.getSimpleName(),
"_LOOKUP",
(loader == null ?
Expand All @@ -454,7 +454,7 @@ private void addLoader(TypeElement type, ClassSpec classSpec) {
}

private void addMethodHandles(TypeElement type, List<ExecutableElement> methods, ClassSpec classSpec) {
methods.stream().collect(Collectors.toMap(NativeApiProcessor::methodEntrypoint, Function.identity(), (e1, e2) -> {
methods.stream().collect(Collectors.toMap(DowncallProcessor::methodEntrypoint, Function.identity(), (e1, e2) -> {
final Overload o1 = e1.getAnnotation(Overload.class);
final Overload o2 = e2.getAnnotation(Overload.class);
// if e1 is not an overload
Expand Down Expand Up @@ -690,6 +690,6 @@ public SourceVersion getSupportedSourceVersion() {

@Override
public Set<String> getSupportedAnnotationTypes() {
return Set.of(NativeApi.class.getName());
return Set.of(Downcall.class.getName());
}
}
2 changes: 1 addition & 1 deletion src/main/java/overrun/marshal/LibraryLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* A library loader.
*
* @author squid233
* @see NativeApi
* @see Downcall
* @since 0.1.0
*/
public interface LibraryLoader {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/overrun/marshal/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* The main package of marshal.
*
* @author squid233
* @see overrun.marshal.NativeApi
* @see overrun.marshal.Downcall
* @since 0.1.0
*/
package overrun.marshal;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
overrun.marshal.NativeApiProcessor
overrun.marshal.DowncallProcessor

0 comments on commit 9e0c5c2

Please sign in to comment.