Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
root = true

[*.swift]
indent_style = space
indent_size = 2
insert_final_newline = true
53 changes: 53 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
.swift-version
.sdkmanrc

.DS_Store
.metals
.build
.idea
.vscode
Packages
xcuserdata/
DerivedData/
.swiftpm
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
*.class
bin/
BuildLogic/out/
.index-build
.build-vscode
**/.vscode

# Ignore gradle build artifacts
.gradle
**/build/
lib/

# Ignore JVM crash logs
**/*.log

# Ignore package resolved
Package.resolved

# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar

# Avoid ignore Gradle wrappper properties
!gradle-wrapper.properties

# Cache of project
.gradletasknamecache

# Ignore files generated by jextract, we always can re-generate them
*/**/src/generated/java/**/*

*/**/*.d
*/**/*.o
*/**/*.swiftdeps
*/**/*.swiftdeps~
*/**/.docc-build/


BuildLogic/.kotlin/
54 changes: 54 additions & 0 deletions .licenseignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
.editorconfig
.gitignore
.licenseignore
.swiftformatignore
.spi.yml
.swift-format
.github/*
*.md
**/*.config
CONTRIBUTORS.txt
LICENSE.txt
NOTICE.txt
Package.swift
Package.resolved
README.md
SECURITY.md
scripts/unacceptable-language.txt
.unacceptablelanguageignore
docker/*
**/*.docc/*
**/.gitignore
**/Package.swift
**/Package.resolved
**/*.md
**/openapi.yml
**/petstore.yaml
**/openapi-generator-config.yaml
**/openapi-generator-config.yml
**/docker-compose.yaml
**/docker/*
**/.dockerignore
Makefile
**/Makefile
**/*.html
**/CMakeLists.txt
**/*.jar
**/generated/*.java
gradle.properties
**/gradle.properties
**/generated/*.swift
gradle/wrapper/gradle-wrapper.properties
gradlew
gradlew.bat
**/gradlew
**/gradlew.bat
**/ci-validate.sh
**/DO_NOT_EDIT.txt
Plugins/**/_PluginsShared
Plugins/**/0_PLEASE_SYMLINK*
Plugins/PluginsShared/JavaKitConfigurationShared
Samples/JavaDependencySampleApp/gradle
Sources/_Subprocess/**
Sources/_SubprocessCShims/**
Samples/gradle
6 changes: 6 additions & 0 deletions .spi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 1
builder:
configs:
- documentation_targets: [
SwiftJavaDocumentation
]
63 changes: 63 additions & 0 deletions .swift-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"version" : 1,
"indentation" : {
"spaces" : 2
},
"tabWidth" : 2,
"fileScopedDeclarationPrivacy" : {
"accessLevel" : "private"
},
"spacesAroundRangeFormationOperators" : false,
"indentConditionalCompilationBlocks" : false,
"indentSwitchCaseLabels" : false,
"lineBreakAroundMultilineExpressionChainComponents" : false,
"lineBreakBeforeControlFlowKeywords" : false,
"lineBreakBeforeEachArgument" : true,
"lineBreakBeforeEachGenericRequirement" : true,
"lineLength" : 180,
"maximumBlankLines" : 2,
"spacesBeforeEndOfLineComments" : 1,
"respectsExistingLineBreaks" : true,
"prioritizeKeepingFunctionOutputTogether" : true,
"rules" : {
"AllPublicDeclarationsHaveDocumentation" : false,
"AlwaysUseLiteralForEmptyCollectionInit" : false,
"AlwaysUseLowerCamelCase" : false,
"AmbiguousTrailingClosureOverload" : true,
"BeginDocumentationCommentWithOneLineSummary" : false,
"DoNotUseSemicolons" : true,
"DontRepeatTypeInStaticProperties" : true,
"FileScopedDeclarationPrivacy" : true,
"FullyIndirectEnum" : true,
"GroupNumericLiterals" : true,
"IdentifiersMustBeASCII" : true,
"NeverForceUnwrap" : false,
"NeverUseForceTry" : false,
"NeverUseImplicitlyUnwrappedOptionals" : false,
"NoAccessLevelOnExtensionDeclaration" : true,
"NoAssignmentInExpressions" : true,
"NoBlockComments" : false,
"NoCasesWithOnlyFallthrough" : true,
"NoEmptyTrailingClosureParentheses" : true,
"NoLabelsInCasePatterns" : true,
"NoLeadingUnderscores" : false,
"NoParensAroundConditions" : true,
"NoVoidReturnOnFunctionSignature" : true,
"OmitExplicitReturns" : true,
"OneCasePerLine" : true,
"OneVariableDeclarationPerLine" : true,
"OnlyOneTrailingClosureArgument" : true,
"OrderedImports" : true,
"ReplaceForEachWithForLoop" : true,
"ReturnVoidInsteadOfEmptyTuple" : true,
"UseEarlyExits" : false,
"UseExplicitNilCheckInConditions" : false,
"UseLetInEveryBoundCaseVariable" : false,
"UseShorthandTypeNames" : true,
"UseSingleLinePropertyGetter" : false,
"UseSynthesizedInitializer" : false,
"UseTripleSlashForDocumentationComments" : true,
"UseWhereClausesInForLoops" : false,
"ValidateDocumentationComments" : false
}
}
7 changes: 7 additions & 0 deletions .unacceptablelanguageignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Sources/SwiftJavaBootstrapJavaTool/SwiftJavaBootstrapJavaTool.swift
Sources/_Subprocess/Platforms/Subprocess+Darwin.swift
Sources/_Subprocess/Platforms/Subprocess+Linux.swift
Sources/_Subprocess/Platforms/Subprocess+Unix.swift
Sources/_Subprocess/Teardown.swift
Sources/_Subprocess/Subprocess.swift
NOTICE.txt
109 changes: 109 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
## Legal

By submitting a pull request, you represent that you have the right to license
your contribution to Apple and the community, and agree by submitting the patch
that your contributions are licensed under the Apache 2.0 license (see
`LICENSE.txt`).

## How to submit a bug report

Please ensure to specify the following:

* Commit hash
* Contextual information (e.g. what you were trying to achieve with swift-java)
* Simplest possible steps to reproduce
* More complex the steps are, lower the priority will be.
* A pull request with failing test case is preferred, but it's just fine to paste the test case into the issue description.
* Anything that might be relevant in your opinion, such as:
* Swift version or the output of `swift --version`
* OS version and the output of `uname -a`

### Example

```
Commit hash: b17a8a9f0f814c01a56977680cb68d8a779c951f

Context:
While testing my application that uses with swift-openapi-generator, I noticed that ...

Steps to reproduce:
1. ...
2. ...
3. ...
4. ...

$ swift --version
Swift version 4.0.2 (swift-4.0.2-RELEASE)
Target: x86_64-unknown-linux-gnu

Operating system: Ubuntu Linux 16.04 64-bit

$ uname -a
Linux beefy.machine 4.4.0-101-generic #124-Ubuntu SMP Fri Nov 10 18:29:59 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

My system has IPv6 disabled.
```

## Writing a Patch

A good patch is:

1. Concise, and contains as few changes as needed to achieve the end result.
2. Tested, ensuring that any tests provided failed before the patch and pass after it.
3. Documented, adding API documentation as needed to cover new functions and properties.
4. Accompanied by a great commit message, using our commit message template.

### Run CI checks locally

You can run the Github Actions workflows locally using
[act](https://github.com/nektos/act). To run all the jobs that run on a pull
request, use the following command:

```
% act pull_request
```

To run just a single job, use `workflow_call -j <job>`, and specify the inputs
the job expects. For example, to run just shellcheck:

```
% act workflow_call -j soundness --input shell_check_enabled=true
```

To bind-mount the working directory to the container, rather than a copy, use
`--bind`. For example, to run just the formatting, and have the results
reflected in your working directory:

```
% act --bind workflow_call -j soundness --input format_check_enabled=true
```

If you'd like `act` to always run with certain flags, these can be be placed in
an `.actrc` file either in the current working directory or your home
directory, for example:

```
--container-architecture=linux/amd64
--remote-name upstream
--action-offline-mode
```

For frequent contributors, we recommend adding the script as a [git pre-push hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks), which you can do via executing the following command in the project root directory:

```bash
cat << EOF > .git/hooks/pre-push

if [[ -f "scripts/soundness.sh" ]]; then
scripts/soundness.sh
fi
EOF
```

Which makes the script execute, and only allow the `git push` to complete if the check has passed.

In the case of formatting issues, you can then `git add` the formatting changes, and attempt the push again.

## How to contribute your work

Please open a pull request at https://github.com/swiftlang/swift-java. Make sure the CI passes, and then wait for code review.

24 changes: 22 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,32 @@ func getJavaHomeFromPath() -> String? {

let javaHome = findJavaHome()

let javaIncludePath = "\(javaHome)/include"
#if os(Linux)
let javaPlatformIncludePath = "\(javaIncludePath)/linux"
#elseif os(macOS)
let javaPlatformIncludePath = "\(javaIncludePath)/darwin"
#elseif os(Windows)
let javaPlatformIncludePath = "\(javaIncludePath)/win32"
#endif

let package = Package(
name: "swift-java-jni-core",
products: [
.library(
name: "SwiftJavaJNICore",
targets: ["SwiftJavaJNICore"]
),
)
],
targets: [
.target(
name: "CSwiftJavaJNI",
cSettings: [
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"], .when(platforms: [.macOS, .linux, .windows]))
],
swiftSettings: [
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"], .when(platforms: [.macOS, .linux, .windows]))
],
linkerSettings: [
.linkedLibrary("log", .when(platforms: [.android]))
]
Expand All @@ -138,10 +153,11 @@ let package = Package(
.target(
name: "SwiftJavaJNICore",
dependencies: [
"CSwiftJavaJNI",
"CSwiftJavaJNI"
],
swiftSettings: [
.swiftLanguageMode(.v5),
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"], .when(platforms: [.macOS, .linux, .windows])),
],
linkerSettings: [
.unsafeFlags(
Expand Down Expand Up @@ -169,6 +185,10 @@ let package = Package(
name: "SwiftJavaJNICoreTests",
dependencies: [
"SwiftJavaJNICore"
],
swiftSettings: [
.swiftLanguageMode(.v5),
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"], .when(platforms: [.macOS, .linux, .windows])),
]
),
]
Expand Down
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
# swift-java-jni-core
# Swift Java JNI Core

## Swift Java Interoperability

Swift offers Java interoperability using the [SwiftJava](https://github.com/swiftlang/swift-java) package, package plugins, and also the `swift-java` command line tool for source generation (similar to OpenJDK jextract, if you are familiar with that concept).

Most developers should prever the use of that package, as it features bullet proof ways to make your inter-op code safe and performant, by using either source generation (`swift-java`) when calling Swift from Java, or Swift macros (`@JavaMethod`, ...) when calling Java from Swift. Multiple safeguards and optimizations are built into the SwiftJava library and we highly recommend using it instead of this low level "raw JNI" package.

## Swift Java JNI Core

The swift-java-jni-core package presents a *low-level* Swift-friendly interface to the Java Native Interface (JNI) specification, which is the universal set of data types and functions for interacting with a Java Virtual Machine and compatible derivatives, such as the Android Runtime (ART). You can view it as a thin layer on top of the `jni.h` along with pre-packaged type conversions and ways to interact with the JVM from Swift.

This package is designed to offer low-level zero-dependency support for higher-level modules, such as [SwiftJava](https://github.com/swiftlang/swift-java) and other projects.

## Features

### JavaValue

A `JavaValue` describes a type that can be bridged with Java. `JavaValue` is the base protocol for bridging between Swift types and their Java counterparts via the Java Native Interface (JNI). It is suitable for describing both value types (such as `Int32` or `Bool`) and object types.

### JavaVirtualMachine

The `JavaVirtualMachine` provides access to a Java Virtual Machine (JVM), which can either be loaded from within a Swift process (via `JNI_CreateJavaVM`), or accessed from a pre-existing in-process handle (`JNI_GetCreatedJavaVMs`). The JavaVirtualMachine is the entry point to interfacing with the JVM, and handles finding and loading classes, looking up and invoking methods, and handling details like locking, threads, and references.

### CSwiftJavaJNI

This C module provides the standardized and implementation-agnostic headers for the Java Native Interface [specification](http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html). The shape of these structures and symbols are guaranteed to be ABI stable between any compatible Java implementation.

## Contributing

Contributions are more than welcome, and you can read more about the process in [CONTRIBUTING.md](CONTRIBUTING.md).

Loading