@@ -131,6 +131,7 @@ protected Gen(Context context) {
131
131
// ignore cldc because we cannot have both stackmap formats
132
132
this .stackMap = StackMapFormat .JSR202 ;
133
133
annotate = Annotate .instance (context );
134
+ qualifiedSymbolCache = new HashMap <>();
134
135
}
135
136
136
137
/** Switches
@@ -174,6 +175,12 @@ protected Gen(Context context) {
174
175
Set <JCMethodInvocation > invocationsWithPatternMatchingCatch = Set .of ();
175
176
ListBuffer <int []> patternMatchingInvocationRanges ;
176
177
178
+ /** Cache the symbol to reflect the qualifying type.
179
+ * key: corresponding type
180
+ * value: qualified symbol
181
+ */
182
+ Map <Type , Symbol > qualifiedSymbolCache ;
183
+
177
184
/** Generate code to load an integer constant.
178
185
* @param n The integer to be loaded.
179
186
*/
@@ -217,6 +224,46 @@ void emitMinusOne(int tc) {
217
224
}
218
225
}
219
226
227
+ /** Construct a symbol to reflect the qualifying type that should
228
+ * appear in the byte code as per JLS 13.1.
229
+ *
230
+ * For {@literal target >= 1.2}: Clone a method with the qualifier as owner (except
231
+ * for those cases where we need to work around VM bugs).
232
+ *
233
+ * For {@literal target <= 1.1}: If qualified variable or method is defined in a
234
+ * non-accessible class, clone it with the qualifier class as owner.
235
+ *
236
+ * @param sym The accessed symbol
237
+ * @param site The qualifier's type.
238
+ */
239
+ Symbol binaryQualifier (Symbol sym , Type site ) {
240
+
241
+ if (site .hasTag (ARRAY )) {
242
+ if (sym == syms .lengthVar ||
243
+ sym .owner != syms .arrayClass )
244
+ return sym ;
245
+ // array clone can be qualified by the array type in later targets
246
+ Symbol qualifier ;
247
+ if ((qualifier = qualifiedSymbolCache .get (site )) == null ) {
248
+ qualifier = new ClassSymbol (Flags .PUBLIC , site .tsym .name , site , syms .noSymbol );
249
+ qualifiedSymbolCache .put (site , qualifier );
250
+ }
251
+ return sym .clone (qualifier );
252
+ }
253
+
254
+ if (sym .owner == site .tsym ||
255
+ (sym .flags () & (STATIC | SYNTHETIC )) == (STATIC | SYNTHETIC )) {
256
+ return sym ;
257
+ }
258
+
259
+ // leave alone methods inherited from Object
260
+ // JLS 13.1.
261
+ if (sym .owner == syms .objectType .tsym )
262
+ return sym ;
263
+
264
+ return sym .clone (site .tsym );
265
+ }
266
+
220
267
/** Insert a reference to given type in the constant pool,
221
268
* checking for an array with too many dimensions;
222
269
* return the reference's index.
@@ -2280,11 +2327,11 @@ public void visitIdent(JCIdent tree) {
2280
2327
result = items .makeLocalItem ((VarSymbol )sym );
2281
2328
} else if ((sym .flags () & STATIC ) != 0 ) {
2282
2329
if (!isAccessSuper (env .enclMethod ))
2283
- sym = types . binaryQualifier (sym , env .enclClass .type );
2330
+ sym = binaryQualifier (sym , env .enclClass .type );
2284
2331
result = items .makeStaticItem (sym );
2285
2332
} else {
2286
2333
items .makeThisItem ().load ();
2287
- sym = types . binaryQualifier (sym , env .enclClass .type );
2334
+ sym = binaryQualifier (sym , env .enclClass .type );
2288
2335
result = items .makeMemberItem (sym , nonVirtualForPrivateAccess (sym ));
2289
2336
}
2290
2337
}
@@ -2337,7 +2384,7 @@ public void visitSelect(JCFieldAccess tree) {
2337
2384
result = items .makeDynamicItem (sym );
2338
2385
return ;
2339
2386
} else {
2340
- sym = types . binaryQualifier (sym , tree .selected .type );
2387
+ sym = binaryQualifier (sym , tree .selected .type );
2341
2388
}
2342
2389
if ((sym .flags () & STATIC ) != 0 ) {
2343
2390
if (!selectSuper && (ssym == null || ssym .kind != TYP ))
@@ -2443,7 +2490,7 @@ public boolean genClass(Env<AttrContext> env, JCClassDecl cdef) {
2443
2490
toplevel = null ;
2444
2491
endPosTable = null ;
2445
2492
nerrs = 0 ;
2446
- types . clearQualifiedSymbolCache ();
2493
+ qualifiedSymbolCache . clear ();
2447
2494
}
2448
2495
}
2449
2496
1 commit comments
openjdk-notifier[bot] commentedon Jan 24, 2023
Review
Issues