4444import java .util .Set ;
4545import java .util .TreeMap ;
4646
47+ import org .graalvm .collections .Pair ;
4748import org .graalvm .compiler .core .common .util .TypeConversion ;
4849import org .graalvm .compiler .core .common .util .UnsafeArrayTypeWriter ;
4950import org .graalvm .nativeimage .ImageSingletons ;
5758import com .oracle .svm .core .util .ByteArrayReader ;
5859import com .oracle .svm .util .ReflectionUtil ;
5960
61+ import jdk .vm .ci .meta .JavaType ;
6062import sun .invoke .util .Wrapper ;
6163import sun .reflect .annotation .AnnotationType ;
6264import sun .reflect .annotation .TypeAnnotation ;
@@ -67,7 +69,7 @@ public class MethodMetadataEncoder {
6769 public static final int NO_METHOD_METADATA = -1 ;
6870
6971 private CodeInfoEncoder .Encoders encoders ;
70- private TreeMap <SharedType , Set <Executable >> methodData ;
72+ private TreeMap <SharedType , Set <Pair < SharedMethod , Executable > >> methodData ;
7173
7274 private byte [] methodDataEncoding ;
7375 private byte [] methodDataIndexEncoding ;
@@ -111,7 +113,7 @@ public void prepareMetadataForMethod(SharedMethod method, Executable reflectMeth
111113 }
112114
113115 /* Register string values in annotations */
114- registerStrings (GuardedAnnotationAccess .getDeclaredAnnotations (reflectMethod ));
116+ registerStrings (GuardedAnnotationAccess .getDeclaredAnnotations (method ));
115117 for (Annotation [] annotations : reflectMethod .getParameterAnnotations ()) {
116118 registerStrings (annotations );
117119 }
@@ -126,7 +128,7 @@ public void prepareMetadataForMethod(SharedMethod method, Executable reflectMeth
126128 }
127129
128130 SharedType declaringType = (SharedType ) method .getDeclaringClass ();
129- methodData .computeIfAbsent (declaringType , t -> new HashSet <>()).add (reflectMethod );
131+ methodData .computeIfAbsent (declaringType , t -> new HashSet <>()).add (Pair . create ( method , reflectMethod ) );
130132 }
131133
132134 private static final Method hasRealParameterData = ReflectionUtil .lookupMethod (Executable .class , "hasRealParameterData" );
@@ -135,9 +137,9 @@ private void encodeMethodMetadata() {
135137 UnsafeArrayTypeWriter dataEncodingBuffer = UnsafeArrayTypeWriter .create (ByteArrayReader .supportsUnalignedMemoryAccess ());
136138 UnsafeArrayTypeWriter indexEncodingBuffer = UnsafeArrayTypeWriter .create (ByteArrayReader .supportsUnalignedMemoryAccess ());
137139 long lastTypeID = -1 ;
138- for (Map .Entry <SharedType , Set <Executable >> entry : methodData .entrySet ()) {
140+ for (Map .Entry <SharedType , Set <Pair < SharedMethod , Executable > >> entry : methodData .entrySet ()) {
139141 SharedType declaringType = entry .getKey ();
140- Set <Executable > methods = entry .getValue ();
142+ Set <Pair < SharedMethod , Executable > > methods = entry .getValue ();
141143 long typeID = declaringType .getHub ().getTypeID ();
142144 assert typeID > lastTypeID ;
143145 lastTypeID ++;
@@ -148,62 +150,70 @@ private void encodeMethodMetadata() {
148150 long index = dataEncodingBuffer .getBytesWritten ();
149151 indexEncodingBuffer .putS4 (index );
150152 dataEncodingBuffer .putUV (methods .size ());
151- for (Executable method : methods ) {
152- Class <?> declaringClass = method .getDeclaringClass ();
153+ for (Pair <SharedMethod , Executable > method : methods ) {
154+ SharedMethod hostedMethod = method .getLeft ();
155+ Executable reflectMethod = method .getRight ();
156+
157+ Class <?> declaringClass = getJavaClass ((SharedType ) hostedMethod .getDeclaringClass ());
153158 final int classIndex = encoders .sourceClasses .getIndex (declaringClass );
154159 dataEncodingBuffer .putSV (classIndex );
155160
156- String name = method instanceof Constructor <?> ? "<init>" : (( Method ) method ) .getName ();
161+ String name = hostedMethod . isConstructor () ? "<init>" : hostedMethod .getName ();
157162 final int nameIndex = encoders .sourceMethodNames .getIndex (name );
158163 dataEncodingBuffer .putSV (nameIndex );
159164
160- dataEncodingBuffer .putUV (method .getModifiers ());
165+ dataEncodingBuffer .putUV (reflectMethod .getModifiers ());
161166
162- Class <?>[] parameterTypes = method .getParameterTypes ();
167+ /* Parameter types do not include the receiver */
168+ JavaType [] parameterTypes = hostedMethod .getSignature ().toParameterTypes (null );
163169 dataEncodingBuffer .putUV (parameterTypes .length );
164- for (Class <?> parameterType : parameterTypes ) {
165- final int paramClassIndex = encoders .sourceClasses .getIndex (parameterType );
170+ for (JavaType parameterType : parameterTypes ) {
171+ Class <?> parameterClass = getJavaClass ((SharedType ) parameterType );
172+ final int paramClassIndex = encoders .sourceClasses .getIndex (parameterClass );
166173 dataEncodingBuffer .putSV (paramClassIndex );
167174 }
168175
169- Class <?> returnType = method instanceof Constructor <?> ? void .class : ((Method ) method ).getReturnType ();
176+ Class <?> returnType = void .class ;
177+ if (!hostedMethod .isConstructor ()) {
178+ returnType = getJavaClass ((SharedType ) hostedMethod .getSignature ().getReturnType (null ));
179+ }
170180 final int returnTypeIndex = encoders .sourceClasses .getIndex (returnType );
171181 dataEncodingBuffer .putSV (returnTypeIndex );
172182
173183 /* Only include types that are in the image (i.e. that can actually be thrown) */
174- Class <?>[] exceptionTypes = filterTypes (method .getExceptionTypes ());
184+ Class <?>[] exceptionTypes = filterTypes (reflectMethod .getExceptionTypes ());
175185 dataEncodingBuffer .putUV (exceptionTypes .length );
176186 for (Class <?> exceptionClazz : exceptionTypes ) {
177187 final int exceptionClassIndex = encoders .sourceClasses .getIndex (exceptionClazz );
178188 dataEncodingBuffer .putSV (exceptionClassIndex );
179189 }
180190
181- final int signatureIndex = encoders .sourceMethodNames .getIndex (getSignature (method ));
191+ final int signatureIndex = encoders .sourceMethodNames .getIndex (getSignature (reflectMethod ));
182192 dataEncodingBuffer .putSV (signatureIndex );
183193
184194 try {
185- byte [] annotations = encodeAnnotations (GuardedAnnotationAccess .getDeclaredAnnotations (method ));
195+ byte [] annotations = encodeAnnotations (GuardedAnnotationAccess .getDeclaredAnnotations (hostedMethod ));
186196 dataEncodingBuffer .putUV (annotations .length );
187197 for (byte b : annotations ) {
188198 dataEncodingBuffer .putS1 (b );
189199 }
190200
191- byte [] parameterAnnotations = encodeParameterAnnotations (method .getParameterAnnotations ());
201+ byte [] parameterAnnotations = encodeParameterAnnotations (reflectMethod .getParameterAnnotations ());
192202 dataEncodingBuffer .putUV (parameterAnnotations .length );
193203 for (byte b : parameterAnnotations ) {
194204 dataEncodingBuffer .putS1 (b );
195205 }
196206
197- byte [] typeAnnotations = encodeTypeAnnotations ((TypeAnnotation []) parseAllTypeAnnotations .invoke (null , method ));
207+ byte [] typeAnnotations = encodeTypeAnnotations ((TypeAnnotation []) parseAllTypeAnnotations .invoke (null , reflectMethod ));
198208 dataEncodingBuffer .putUV (typeAnnotations .length );
199209 for (byte b : typeAnnotations ) {
200210 dataEncodingBuffer .putS1 (b );
201211 }
202212
203- boolean parameterDataPresent = (boolean ) hasRealParameterData .invoke (method );
213+ boolean parameterDataPresent = (boolean ) hasRealParameterData .invoke (reflectMethod );
204214 dataEncodingBuffer .putU1 (parameterDataPresent ? 1 : 0 );
205215 if (parameterDataPresent ) {
206- Parameter [] parameters = method .getParameters ();
216+ Parameter [] parameters = reflectMethod .getParameters ();
207217 dataEncodingBuffer .putUV (parameters .length );
208218 for (Parameter parameter : parameters ) {
209219 final int parameterNameIndex = encoders .sourceMethodNames .getIndex (parameter .getName ());
@@ -226,6 +236,10 @@ private void encodeMethodMetadata() {
226236 indexEncodingBuffer .toArray (methodDataIndexEncoding );
227237 }
228238
239+ private static Class <?> getJavaClass (SharedType sharedType ) {
240+ return sharedType .getHub ().getHostedJavaClass ();
241+ }
242+
229243 private Class <?>[] filterTypes (Class <?>[] types ) {
230244 List <Class <?>> filteredTypes = new ArrayList <>();
231245 for (Class <?> type : types ) {
0 commit comments