JavApi⁴Swift

本项目提供了一个类似 Java 的 API,但它是 100% 纯 Swift 实现,而不是桥接到 Java 虚拟机。 因此,您不需要 Java 虚拟机。 另一方面,您可以将 Java 代码逐步移植到 Swift,而无需一次性全部迁移。

本项目使用 MinD 模式来减少依赖性,并利用其他优秀资源。

许可证

本项目仅使用对商业友好的 许可型许可证

Floss-license-slide-image 图片来源: David A. Wheeler, et al., CC BY-SA 3.0 , via Wikimedia Commons

部分源代码使用 BSD 许可证和 Public Domain / The Unlicense。 默认情况下,我使用 MIT 许可证。 部分源代码使用 Apache 2.0 许可证。

感谢

特别感谢以下贡献者的工作:

用法

使用 XCode 时,添加依赖项

https://github.com/bastie/JavApi4Swift.git

使用 SwiftPM 时,添加依赖项

.package(url: "https://github.com/bastie/JavApi4Swift.git", from: "0.19.1")

或者

.Package(url: "https://github.com/bastie/JavApi4Swift.git", .upToNextMajor(from: "0.19.1"))

移植项目

我正在实现一些移植项目,以检查 JavApi⁴Swift 的发布版本,并寻找下一个缺失的功能。 可以从 0.4.2 版本开始使用。 检查这些项目并学习如何使用 JavApi⁴Swift。

0.4.2

0.7.4

0.8.0

0.12.0

0.19.1

开发

包含其他项目

为了尊重其他项目的开发人员的工作,让历史记录保留。

要包含具有兼容许可证的其他项目,请执行以下操作:

# Example for some types from a jzlib clone
#
# clone the other project local, jzlib clone are here https://github.com/kohsuke/jzlib.git
 
git clone https://github.com/kohsuke/jzlib.git otherProject

# go into project directory and remove the origin
cd otherProject
git remote rm origin

# filter project to remove all unwanted data and commit it
git filter-branch --subdirectory-filter src/main/java/com/jcraft/jzlib -- --all
git rm Deflat*.java GZIP*.java Inf*.java Z*.java Tree.java StaticTree.java JZl*.java
git commit -m "ready to import"

# go to JavApi project directory
cd ../JavApi4Swift

# add (temporary) the local other project as remote source and pull wanted data with history
git remote add importSource ../otherProject
git pull importSource master --no-rebase --allow-unrelated-histories
git remote rm importSource

# work on conflicts f.e. in .gitignore

# optional take a look into history
git log

JavApi 风格指南

  1. 移植的 Java 源代码不应进行超出需要的修改。
  2. 我喜欢短行,所以缩进是 2 个空格。
  3. 类型与 Java 中的文件名相同,但 java.lang 类型除外。
  4. 左大括号位于同一行。
  5. 由于编译器在文件名相同的情况下会发生问题, 通过枚举映射到名为 java.basepackagename.packagename.swift 的文件中。
  6. Java 翻译的非私有函数参数的导出参数标有下划线。
  7. 异常映射到 Error 枚举的扩展。
  8. 默认方法在扩展中实现。
  9. Swift 化的代码在扩展中实现。
  10. 以上所有要点均为非约束性建议,不具有约束力。

如何将 Java 源代码翻译成 Swift