File: ModuleGenerator.java

package info (click to toggle)
libjna-java 5.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,828 kB
  • sloc: java: 90,222; ansic: 4,994; xml: 3,713; makefile: 433; sh: 299
file content (193 lines) | stat: -rw-r--r-- 5,296 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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 + '}';
    }
}