Skip to content

Commit 2717fcb

Browse files
author
Joel Borggrén-Franck
committed
8232948: javac -h should mangle the overload argument signature
Reviewed-by: jjg
1 parent 89da202 commit 2717fcb

File tree

2 files changed

+128
-8
lines changed

2 files changed

+128
-8
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/JNIWriter.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ String encodeMethod(Symbol msym, ClassSymbol clazz,
417417
result.append(encode(msym.getSimpleName(), EncoderType.JNI));
418418
if (isOverloaded) {
419419
TypeSignature typeSig = new TypeSignature(types);
420-
StringBuilder sig = typeSig.getParameterSignature(msym.type);
420+
StringBuilder sig = typeSig.getParameterSignature(msym.type, true);
421421
result.append("__").append(encode(sig, EncoderType.JNI));
422422
}
423423
return result.toString();
@@ -542,23 +542,23 @@ public TypeSignature(Types types) {
542542
this.types = types;
543543
}
544544

545-
StringBuilder getParameterSignature(Type mType)
545+
StringBuilder getParameterSignature(Type mType, boolean useFlatname)
546546
throws SignatureException {
547547
StringBuilder result = new StringBuilder();
548548
for (Type pType : mType.getParameterTypes()) {
549-
result.append(getJvmSignature(pType));
549+
result.append(getJvmSignature(pType, useFlatname));
550550
}
551551
return result;
552552
}
553553

554554
StringBuilder getReturnSignature(Type mType)
555555
throws SignatureException {
556-
return getJvmSignature(mType.getReturnType());
556+
return getJvmSignature(mType.getReturnType(), false);
557557
}
558558

559559
StringBuilder getSignature(Type mType) throws SignatureException {
560560
StringBuilder sb = new StringBuilder();
561-
sb.append("(").append(getParameterSignature(mType)).append(")");
561+
sb.append("(").append(getParameterSignature(mType, false)).append(")");
562562
sb.append(getReturnSignature(mType));
563563
return sb;
564564
}
@@ -567,6 +567,12 @@ StringBuilder getSignature(Type mType) throws SignatureException {
567567
* Returns jvm internal signature.
568568
*/
569569
static class JvmTypeVisitor extends JNIWriter.SimpleTypeVisitor<Type, StringBuilder> {
570+
private final boolean useFlatname;
571+
572+
JvmTypeVisitor(boolean useFlatname) {
573+
super();
574+
this.useFlatname = useFlatname;
575+
}
570576

571577
@Override
572578
public Type visitClassType(Type.ClassType t, StringBuilder s) {
@@ -589,7 +595,8 @@ public Type visitType(Type t, StringBuilder s) {
589595
return t.accept(this, s);
590596
}
591597
private void setDeclaredType(Type t, StringBuilder s) {
592-
String classname = t.tsym.getQualifiedName().toString();
598+
String classname = useFlatname ? t.tsym.flatName().toString()
599+
: t.tsym.getQualifiedName().toString();
593600
classname = classname.replace('.', '/');
594601
s.append("L").append(classname).append(";");
595602
}
@@ -611,10 +618,10 @@ private String getJvmPrimitiveSignature(Type t) {
611618
}
612619
}
613620

614-
StringBuilder getJvmSignature(Type type) {
621+
StringBuilder getJvmSignature(Type type, boolean useFlatname) {
615622
Type t = types.erasure(type);
616623
StringBuilder sig = new StringBuilder();
617-
JvmTypeVisitor jv = new JvmTypeVisitor();
624+
JvmTypeVisitor jv = new JvmTypeVisitor(useFlatname);
618625
jv.visitType(t, sig);
619626
return sig;
620627
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8232948
27+
* @summary Make sure inner classes are correctly encoded
28+
* @library /tools/lib
29+
* @modules jdk.compiler/com.sun.tools.javac.api
30+
* @modules jdk.compiler/com.sun.tools.javac.main
31+
* @build toolbox.JavacTask toolbox.TestRunner toolbox.ToolBox
32+
* @run main EncodeInnerClassNameTest
33+
*/
34+
35+
import toolbox.JavacTask;
36+
import toolbox.ToolBox;
37+
38+
import java.nio.file.Files;
39+
import java.nio.file.Path;
40+
import java.util.Arrays;
41+
import java.util.List;
42+
43+
public class EncodeInnerClassNameTest {
44+
private final ToolBox toolBox = new ToolBox();
45+
private final String source = """
46+
package p.q;
47+
public class Outer {
48+
public class Inner {
49+
native Inner aMethod();
50+
native void aMethod(Inner i);
51+
native void aMethod(Inner i, Inner j);
52+
}
53+
}
54+
""";
55+
56+
private final String expected = """
57+
/* DO NOT EDIT THIS FILE - it is machine generated */
58+
#include <jni.h>
59+
/* Header for class p_q_Outer_Inner */
60+
61+
#ifndef _Included_p_q_Outer_Inner
62+
#define _Included_p_q_Outer_Inner
63+
#ifdef __cplusplus
64+
extern "C" {
65+
#endif
66+
/*
67+
* Class: p_q_Outer_Inner
68+
* Method: aMethod
69+
* Signature: ()Lp/q/Outer/Inner;
70+
*/
71+
JNIEXPORT jobject JNICALL Java_p_q_Outer_00024Inner_aMethod__
72+
(JNIEnv *, jobject);
73+
74+
/*
75+
* Class: p_q_Outer_Inner
76+
* Method: aMethod
77+
* Signature: (Lp/q/Outer/Inner;)V
78+
*/
79+
JNIEXPORT void JNICALL Java_p_q_Outer_00024Inner_aMethod__Lp_q_Outer_00024Inner_2
80+
(JNIEnv *, jobject, jobject);
81+
82+
/*
83+
* Class: p_q_Outer_Inner
84+
* Method: aMethod
85+
* Signature: (Lp/q/Outer/Inner;Lp/q/Outer/Inner;)V
86+
*/
87+
JNIEXPORT void JNICALL Java_p_q_Outer_00024Inner_aMethod__Lp_q_Outer_00024Inner_2Lp_q_Outer_00024Inner_2
88+
(JNIEnv *, jobject, jobject, jobject);
89+
90+
#ifdef __cplusplus
91+
}
92+
#endif
93+
#endif
94+
""";
95+
96+
public static void main(String... args) throws Exception {
97+
new EncodeInnerClassNameTest().runTest();
98+
}
99+
100+
public void runTest() throws Exception {
101+
JavacTask task = new JavacTask(toolBox);
102+
task.outdir(".")
103+
.sources(source)
104+
.options("-h", ".")
105+
.run()
106+
.writeAll();
107+
108+
List<String> expected = Arrays.asList(this.expected.split("\\R"));
109+
List<String> res = toolBox.readAllLines(Path.of(".", "p_q_Outer_Inner.h"));
110+
111+
toolBox.checkEqual(expected, res);
112+
}
113+
}

0 commit comments

Comments
 (0)