-
Notifications
You must be signed in to change notification settings - Fork 72
/
ModuleInfoCompiler.java
164 lines (134 loc) · 5.91 KB
/
ModuleInfoCompiler.java
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
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright The original authors
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.moditect.internal.compiler;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Iterator;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.ModuleVisitor;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.expr.Name;
import com.github.javaparser.ast.modules.ModuleDeclaration;
import com.github.javaparser.ast.modules.ModuleExportsDirective;
import com.github.javaparser.ast.modules.ModuleOpensDirective;
import com.github.javaparser.ast.modules.ModuleProvidesDirective;
import com.github.javaparser.ast.modules.ModuleRequiresDirective;
import com.github.javaparser.ast.modules.ModuleUsesDirective;
import com.github.javaparser.ast.nodeTypes.NodeWithModifiers;
import static com.github.javaparser.ParserConfiguration.LanguageLevel.*;
import static org.objectweb.asm.Opcodes.ACC_MANDATED;
import static org.objectweb.asm.Opcodes.ACC_MODULE;
import static org.objectweb.asm.Opcodes.ACC_OPEN;
import static org.objectweb.asm.Opcodes.ACC_STATIC_PHASE;
import static org.objectweb.asm.Opcodes.ACC_SYNTHETIC;
import static org.objectweb.asm.Opcodes.ACC_TRANSITIVE;
import static org.objectweb.asm.Opcodes.V9;
public class ModuleInfoCompiler {
static {
StaticJavaParser.getConfiguration().setLanguageLevel(JAVA_9);
}
public static ModuleDeclaration parseModuleInfo(Path moduleInfo) {
CompilationUnit ast;
try {
ast = StaticJavaParser.parse(moduleInfo);
}
catch (IOException e) {
throw new RuntimeException("Couldn't parse " + moduleInfo, e);
}
return ast.getModule()
.orElseThrow(() -> new IllegalArgumentException("Not a module-info.java: " + moduleInfo));
}
public static ModuleDeclaration parseModuleInfo(String moduleInfoSource) {
CompilationUnit ast = StaticJavaParser.parse(moduleInfoSource);
return ast.getModule()
.orElseThrow(() -> new IllegalArgumentException("Not a module-info.java: " + moduleInfoSource));
}
public static byte[] compileModuleInfo(ModuleDeclaration module, String mainClass, String version) {
ClassWriter classWriter = new ClassWriter(0);
classWriter.visit(V9, ACC_MODULE, "module-info", null, null, null);
int moduleAccess = module.isOpen() ? ACC_SYNTHETIC | ACC_OPEN : ACC_SYNTHETIC;
ModuleVisitor mv = classWriter.visitModule(module.getNameAsString(), moduleAccess, version);
if (mainClass != null) {
mv.visitMainClass(getNameForBinary(mainClass));
}
for (ModuleRequiresDirective requires : module.findAll(ModuleRequiresDirective.class)) {
mv.visitRequire(
requires.getName().asString(),
requiresModifiersAsInt(requires),
null);
}
for (ModuleExportsDirective export : module.findAll(ModuleExportsDirective.class)) {
mv.visitExport(
getNameForBinary(export.getNameAsString()),
0,
export.getModuleNames()
.stream()
.map(Name::toString)
.toArray(String[]::new));
}
for (ModuleProvidesDirective provides : module.findAll(ModuleProvidesDirective.class)) {
mv.visitProvide(
getNameForBinary(provides.getName()),
provides.getWith()
.stream()
.map(ModuleInfoCompiler::getNameForBinary)
.toArray(String[]::new));
}
for (ModuleUsesDirective uses : module.findAll(ModuleUsesDirective.class)) {
mv.visitUse(getNameForBinary(uses.getName()));
}
for (ModuleOpensDirective opens : module.findAll(ModuleOpensDirective.class)) {
mv.visitOpen(
getNameForBinary(opens.getNameAsString()),
0,
opens.getModuleNames()
.stream()
.map(Name::toString)
.toArray(String[]::new));
}
mv.visitRequire("java.base", ACC_MANDATED, null);
mv.visitEnd();
classWriter.visitEnd();
return classWriter.toByteArray();
}
private static String getNameForBinary(Name name) {
return getNameForBinary(name.asString());
}
private static String getNameForBinary(String typeName) {
Iterator<String> parts = Arrays.asList(typeName.split("\\.")).iterator();
StringBuilder typeNameForBinary = new StringBuilder();
while (parts.hasNext()) {
String part = parts.next();
typeNameForBinary.append(part);
// if the current part is upper-case, we assume it's a class and the following part is a nested class
// that's as good as it gets without fully resolving all the type names against the module's classes
if (parts.hasNext()) {
if (Character.isUpperCase(part.charAt(0))) {
typeNameForBinary.append("$");
}
else {
typeNameForBinary.append("/");
}
}
}
return typeNameForBinary.toString();
}
private static int requiresModifiersAsInt(NodeWithModifiers<?> modifiers) {
int result = 0;
if (modifiers.hasModifier(Modifier.Keyword.STATIC)) {
result |= ACC_STATIC_PHASE;
}
if (modifiers.hasModifier(Modifier.Keyword.TRANSITIVE)) {
result |= ACC_TRANSITIVE;
}
return result;
}
}