Skip to content

Commit

Permalink
Apply different mangle rules to header name and method name
Browse files Browse the repository at this point in the history
  • Loading branch information
pomadchin committed Jul 27, 2021
1 parent 21e8f94 commit d8250f4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
6 changes: 6 additions & 0 deletions plugin/src/main/java/com/github/sbt/jni/javah/ClassName.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public final class ClassName {
private final String className;
private final String simpleName;
private final String mangledName;
private final String mangledHeaderName;

public static ClassName of(String moduleName, String className) {
Objects.requireNonNull(className, "Class name is null");
Expand Down Expand Up @@ -43,6 +44,7 @@ private ClassName(String moduleName, String className) {
this.className = className;
this.simpleName = className.substring(className.lastIndexOf('.') + 1);
this.mangledName = mangleName(className);
this.mangledHeaderName = mangleHeaderName(className);
}

@Override
Expand Down Expand Up @@ -84,6 +86,10 @@ public final String mangledName() {
return mangledName;
}

public final String mangledHeaderName() {
return mangledHeaderName;
}

public final String relativePath() {
return className.replace('.', '/') + ".class";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void generate(ClassName name) {
return;
}
}
Path op = outputDir.resolve(name.mangledName() + ".h");
Path op = outputDir.resolve(name.mangledHeaderName() + ".h");
try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(op))) {
generateTo(name, out);
} catch (Exception ex) {
Expand Down
40 changes: 29 additions & 11 deletions plugin/src/main/java/com/github/sbt/jni/javah/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,41 @@ public void close() throws IOException {
}
});

public static String mangleChar(char ch) {
if (ch == '.') {
return "_";
} else if (ch == '_') {
return "_1";
} else if (ch == ';') {
return "_2";
} else if (ch == '[') {
return "_3";
} else if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && (ch <= 'Z'))) {
return String.valueOf(ch);
} else {
return String.format("_0%04x", (int) ch);
}
}

public static String mangleName(String name) {
StringBuilder builder = new StringBuilder(name.length() * 2);
int len = name.length();
for (int i = 0; i < len; i++) {
char ch = name.charAt(i);
if (ch == '.') {
builder.append('_');
} else if (ch == '_') {
builder.append("_1");
} else if (ch == ';') {
builder.append("_2");
} else if (ch == '[') {
builder.append("_3");
} else if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && (ch <= 'Z'))) {
builder.append(ch);
builder.append(mangleChar(ch));
}
return builder.toString();
}

public static String mangleHeaderName(String name) {
StringBuilder builder = new StringBuilder(name.length() * 2);
int len = name.length();
for (int i = 0; i < len; i++) {
char ch = name.charAt(i);
if (ch == '$') {
builder.append("__");
} else {
builder.append(String.format("_0%04x", (int) ch));
builder.append(mangleChar(ch));
}
}
return builder.toString();
Expand Down

0 comments on commit d8250f4

Please sign in to comment.