Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate JPMS compatible artifacts in addtion to the existing artifacts #1237

Merged
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Features
--------
* [#1217](https://github.com/java-native-access/jna/pull/1217): Add mappings for AIX `Perfstat` library to `c.s.j.p.unix.aix` - [@dbwiddis](https://github.com/dbwiddis).
* [#1231](https://github.com/java-native-access/jna/pull/1231): The test suite can now be executed on Windows using either ANSI or UNICODE win32 API by passing `-Dw32.ascii=true/false` to ant. Previously, UNICODE was always used. - [@T-Svensson](https://github.com/T-Svensson/)
* [#1237](https://github.com/java-native-access/jna/pull/1237): *Experimental:* Add artifacts that make jna and jna-platform named modules (provide `module-info.class`). The new artifacts are named `jna-jpms.jar` and `jna-platform-jpms.jar` - [@matthiasblaesing](https://github.com/matthiasblaesing).

Bug Fixes
---------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* containing JNA, in file "AL2.0".
*/

package com.sun.jna;
package com.sun.jna.ant;

import java.io.IOException;
import org.apache.tools.ant.Project;
Expand Down
77 changes: 77 additions & 0 deletions ant-tools-src/com/sun/jna/ant/Exports.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* Copyright (c) 2020 Matthias Bläsing, All Rights Reserved
*
* The contents of this file is dual-licensed under 2
* alternative Open Source/Free licenses: LGPL 2.1 or later and
* Apache License 2.0. (starting with JNA version 4.0.0).
*
* You can freely decide which license you want to apply to
* the project.
*
* You may obtain a copy of the LGPL License at:
*
* http://www.gnu.org/licenses/licenses.html
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "LGPL2.1".
*
* You may obtain a copy of the Apache License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/

package com.sun.jna.ant;

import java.util.HashSet;
import java.util.Set;

public class Exports {
private String packageName;
private String to;

public String getPackage() {
return packageName;
}

public void setPackage(String packageName) {
this.packageName = packageName;
}

public String getBinaryPackageName() {
return packageName.replace(".", "/");
}

public String getTo() {
return to;
}

public void setTo(String to) {
this.to = to;
}

public String[] getToList() {
if(to == null) {
return null;
} else {
Set<String> toList = new HashSet<String>();
for(String s: to.split(",")) {
String entry = s.trim();
if(! entry.isEmpty()) {
toList.add(entry);
}
}
if(toList.isEmpty()) {
return null;
} else {
return toList.toArray(new String[0]);
}
}
}

@Override
public String toString() {
return "Export{" + "package=" + packageName + ", to=" + to + '}';
}
}
193 changes: 193 additions & 0 deletions ant-tools-src/com/sun/jna/ant/ModuleGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/* Copyright (c) 2020 Matthias Bläsing, All Rights Reserved
*
* The contents of this file is dual-licensed under 2
* alternative Open Source/Free licenses: LGPL 2.1 or later and
* Apache License 2.0. (starting with JNA version 4.0.0).
*
* You can freely decide which license you want to apply to
* the project.
*
* You may obtain a copy of the LGPL License at:
*
* http://www.gnu.org/licenses/licenses.html
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "LGPL2.1".
*
* You may obtain a copy of the Apache License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/

package com.sun.jna.ant;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.ModuleVisitor;
import static org.objectweb.asm.Opcodes.*;

public class ModuleGenerator {
private String name;
private String version;
private boolean open;
private String mainClass;
private File targetFile;
private List<Exports> exports = new ArrayList<Exports>();
private List<Opens> opens = new ArrayList<Opens>();
private List<Requires> requires = new ArrayList<Requires>();
private List<Package> packages = new ArrayList<Package>();

public File getTargetFile() {
return targetFile;
}

public void setTargetFile(File targetFile) {
this.targetFile = targetFile;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public boolean isOpen() {
return open;
}

public void setOpen(boolean open) {
this.open = open;
}

public String getMainClass() {
return mainClass;
}

public void setMainClass(String mainClass) {
this.mainClass = mainClass;
}

public String getMainClassBinary() {
if(mainClass != null) {
return mainClass.replace(".", "/");
} else {
return mainClass;
}
}

public Exports createExports() {
Exports export = new Exports();
this.exports.add(export);
return export;
}

public List<Exports> getExports() {
return exports;
}

public void setExports(List<Exports> exports) {
this.exports = exports;
}

public Requires createRequires() {
Requires require = new Requires();
this.requires.add(require);
return require;
}

public List<Requires> getRequires() {
return requires;
}

public void setRequires(List<Requires> requires) {
this.requires = requires;
}

public Opens createOpens() {
Opens openEntry = new Opens();
this.opens.add(openEntry);
return openEntry;
}

public List<Opens> getOpens() {
return opens;
}

public void setOpens(List<Opens> opens) {
this.opens = opens;
}

public Package createPackage() {
Package packageEntry = new Package();
this.packages.add(packageEntry);
return packageEntry;
}

public List<Package> getPackages() {
return packages;
}

public void setPackages(List<Package> packageEntry) {
this.packages = packageEntry;
}

public void execute() throws IOException {
System.out.println(this);
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
cw.visit(V9, ACC_MODULE, "module-info", null, null, null);
ModuleVisitor mv = cw.visitModule(name, (open ? ACC_OPEN : 0), version);
mv.visitRequire("java.base", ACC_MANDATED, null);
for(Requires require: requires) {
mv.visitRequire(
require.getModule(),
(require.isStatic() ? ACC_STATIC : 0)|( require.isTransitive() ? ACC_TRANSITIVE : 0),
null);
}
for(Exports export: exports) {
mv.visitExport(
export.getBinaryPackageName(),
0,
export.getToList()
);
}
for(Opens openEntry: opens) {
mv.visitOpen(
openEntry.getBinaryPackageName(),
0,
openEntry.getToList());
}
for(Package packageEntry: packages) {
mv.visitPackage(packageEntry.getName());
}
if(getMainClassBinary() != null) {
mv.visitMainClass(getMainClassBinary());
}
mv.visitEnd();
cw.visitEnd();
try (FileOutputStream fos = new FileOutputStream(targetFile)) {
fos.write(cw.toByteArray());
}
}

@Override
public String toString() {
return "ModuleGenerator{" + "name=" + name + ", version=" + version + ", open=" + open + ", mainClass=" + mainClass + ", targetFile=" + targetFile + ", exports=" + exports + ", opens=" + opens + ", requires=" + requires + ", packages=" + packages + '}';
}
}
77 changes: 77 additions & 0 deletions ant-tools-src/com/sun/jna/ant/Opens.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* Copyright (c) 2020 Matthias Bläsing, All Rights Reserved
*
* The contents of this file is dual-licensed under 2
* alternative Open Source/Free licenses: LGPL 2.1 or later and
* Apache License 2.0. (starting with JNA version 4.0.0).
*
* You can freely decide which license you want to apply to
* the project.
*
* You may obtain a copy of the LGPL License at:
*
* http://www.gnu.org/licenses/licenses.html
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "LGPL2.1".
*
* You may obtain a copy of the Apache License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/

package com.sun.jna.ant;

import java.util.HashSet;
import java.util.Set;

public class Opens {
private String packageName;
private String to;

public String getPackage() {
return packageName;
}

public void setPackage(String packageName) {
this.packageName = packageName;
}

public String getBinaryPackageName() {
return packageName.replace(".", "/");
}

public String getTo() {
return to;
}

public void setTo(String to) {
this.to = to;
}

public String[] getToList() {
if(to == null) {
return null;
} else {
Set<String> toList = new HashSet<String>();
for(String s: to.split(",")) {
String entry = s.trim();
if(! entry.isEmpty()) {
toList.add(entry);
}
}
if(toList.isEmpty()) {
return null;
} else {
return toList.toArray(new String[0]);
}
}
}

@Override
public String toString() {
return "Opens{" + "packageName=" + packageName + ", to=" + to + '}';
}
}
Loading