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

Revisit the name mangling rule usage for the '$' character (method and filenames named differently) #57

Closed
wants to merge 2 commits into from
Closed
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
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: 28 additions & 12 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,25 +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(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 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);
} else {
builder.append(String.format("_0%04x", (int) ch));
builder.append(mangleChar(ch));
}
}
return builder.toString();
Expand Down