|
| 1 | +/* Copyright (c) 2020 Matthias Bläsing, All Rights Reserved |
| 2 | + * |
| 3 | + * The contents of this file is dual-licensed under 2 |
| 4 | + * alternative Open Source/Free licenses: LGPL 2.1 or later and |
| 5 | + * Apache License 2.0. (starting with JNA version 4.0.0). |
| 6 | + * |
| 7 | + * You can freely decide which license you want to apply to |
| 8 | + * the project. |
| 9 | + * |
| 10 | + * You may obtain a copy of the LGPL License at: |
| 11 | + * |
| 12 | + * http://www.gnu.org/licenses/licenses.html |
| 13 | + * |
| 14 | + * A copy is also included in the downloadable source code package |
| 15 | + * containing JNA, in file "LGPL2.1". |
| 16 | + * |
| 17 | + * You may obtain a copy of the Apache License at: |
| 18 | + * |
| 19 | + * http://www.apache.org/licenses/ |
| 20 | + * |
| 21 | + * A copy is also included in the downloadable source code package |
| 22 | + * containing JNA, in file "AL2.0". |
| 23 | + */ |
| 24 | + |
| 25 | +package com.sun.jna.ant; |
| 26 | + |
| 27 | +import java.io.File; |
| 28 | +import java.io.FileOutputStream; |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | +import org.objectweb.asm.ClassWriter; |
| 33 | +import org.objectweb.asm.ModuleVisitor; |
| 34 | +import static org.objectweb.asm.Opcodes.*; |
| 35 | + |
| 36 | +public class ModuleGenerator { |
| 37 | + private String name; |
| 38 | + private String version; |
| 39 | + private boolean open; |
| 40 | + private String mainClass; |
| 41 | + private File targetFile; |
| 42 | + private List<Exports> exports = new ArrayList<Exports>(); |
| 43 | + private List<Opens> opens = new ArrayList<Opens>(); |
| 44 | + private List<Requires> requires = new ArrayList<Requires>(); |
| 45 | + private List<Package> packages = new ArrayList<Package>(); |
| 46 | + |
| 47 | + public File getTargetFile() { |
| 48 | + return targetFile; |
| 49 | + } |
| 50 | + |
| 51 | + public void setTargetFile(File targetFile) { |
| 52 | + this.targetFile = targetFile; |
| 53 | + } |
| 54 | + |
| 55 | + public String getName() { |
| 56 | + return name; |
| 57 | + } |
| 58 | + |
| 59 | + public void setName(String name) { |
| 60 | + this.name = name; |
| 61 | + } |
| 62 | + |
| 63 | + public String getVersion() { |
| 64 | + return version; |
| 65 | + } |
| 66 | + |
| 67 | + public void setVersion(String version) { |
| 68 | + this.version = version; |
| 69 | + } |
| 70 | + |
| 71 | + public boolean isOpen() { |
| 72 | + return open; |
| 73 | + } |
| 74 | + |
| 75 | + public void setOpen(boolean open) { |
| 76 | + this.open = open; |
| 77 | + } |
| 78 | + |
| 79 | + public String getMainClass() { |
| 80 | + return mainClass; |
| 81 | + } |
| 82 | + |
| 83 | + public void setMainClass(String mainClass) { |
| 84 | + this.mainClass = mainClass; |
| 85 | + } |
| 86 | + |
| 87 | + public String getMainClassBinary() { |
| 88 | + if(mainClass != null) { |
| 89 | + return mainClass.replace(".", "/"); |
| 90 | + } else { |
| 91 | + return mainClass; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public Exports createExports() { |
| 96 | + Exports export = new Exports(); |
| 97 | + this.exports.add(export); |
| 98 | + return export; |
| 99 | + } |
| 100 | + |
| 101 | + public List<Exports> getExports() { |
| 102 | + return exports; |
| 103 | + } |
| 104 | + |
| 105 | + public void setExports(List<Exports> exports) { |
| 106 | + this.exports = exports; |
| 107 | + } |
| 108 | + |
| 109 | + public Requires createRequires() { |
| 110 | + Requires require = new Requires(); |
| 111 | + this.requires.add(require); |
| 112 | + return require; |
| 113 | + } |
| 114 | + |
| 115 | + public List<Requires> getRequires() { |
| 116 | + return requires; |
| 117 | + } |
| 118 | + |
| 119 | + public void setRequires(List<Requires> requires) { |
| 120 | + this.requires = requires; |
| 121 | + } |
| 122 | + |
| 123 | + public Opens createOpens() { |
| 124 | + Opens openEntry = new Opens(); |
| 125 | + this.opens.add(openEntry); |
| 126 | + return openEntry; |
| 127 | + } |
| 128 | + |
| 129 | + public List<Opens> getOpens() { |
| 130 | + return opens; |
| 131 | + } |
| 132 | + |
| 133 | + public void setOpens(List<Opens> opens) { |
| 134 | + this.opens = opens; |
| 135 | + } |
| 136 | + |
| 137 | + public Package createPackage() { |
| 138 | + Package packageEntry = new Package(); |
| 139 | + this.packages.add(packageEntry); |
| 140 | + return packageEntry; |
| 141 | + } |
| 142 | + |
| 143 | + public List<Package> getPackages() { |
| 144 | + return packages; |
| 145 | + } |
| 146 | + |
| 147 | + public void setPackages(List<Package> packageEntry) { |
| 148 | + this.packages = packageEntry; |
| 149 | + } |
| 150 | + |
| 151 | + public void execute() throws IOException { |
| 152 | + System.out.println(this); |
| 153 | + ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); |
| 154 | + cw.visit(V9, ACC_MODULE, "module-info", null, null, null); |
| 155 | + ModuleVisitor mv = cw.visitModule(name, (open ? ACC_OPEN : 0), version); |
| 156 | + mv.visitRequire("java.base", ACC_MANDATED, null); |
| 157 | + for(Requires require: requires) { |
| 158 | + mv.visitRequire( |
| 159 | + require.getModule(), |
| 160 | + (require.isStatic() ? ACC_STATIC : 0)|( require.isTransitive() ? ACC_TRANSITIVE : 0), |
| 161 | + null); |
| 162 | + } |
| 163 | + for(Exports export: exports) { |
| 164 | + mv.visitExport( |
| 165 | + export.getBinaryPackageName(), |
| 166 | + 0, |
| 167 | + export.getToList() |
| 168 | + ); |
| 169 | + } |
| 170 | + for(Opens openEntry: opens) { |
| 171 | + mv.visitOpen( |
| 172 | + openEntry.getBinaryPackageName(), |
| 173 | + 0, |
| 174 | + openEntry.getToList()); |
| 175 | + } |
| 176 | + for(Package packageEntry: packages) { |
| 177 | + mv.visitPackage(packageEntry.getName()); |
| 178 | + } |
| 179 | + if(getMainClassBinary() != null) { |
| 180 | + mv.visitMainClass(getMainClassBinary()); |
| 181 | + } |
| 182 | + mv.visitEnd(); |
| 183 | + cw.visitEnd(); |
| 184 | + try (FileOutputStream fos = new FileOutputStream(targetFile)) { |
| 185 | + fos.write(cw.toByteArray()); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public String toString() { |
| 191 | + return "ModuleGenerator{" + "name=" + name + ", version=" + version + ", open=" + open + ", mainClass=" + mainClass + ", targetFile=" + targetFile + ", exports=" + exports + ", opens=" + opens + ", requires=" + requires + ", packages=" + packages + '}'; |
| 192 | + } |
| 193 | +} |
0 commit comments