|
1 | 1 | /******************************************************************************* |
2 | | - * Copyright (c) 2000, 2021 IBM Corporation and others. |
| 2 | + * Copyright (c) 2000, 2023 IBM Corporation and others. |
3 | 3 | * |
4 | 4 | * This program and the accompanying materials |
5 | 5 | * are made available under the terms of the Eclipse Public License 2.0 |
|
47 | 47 | import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; |
48 | 48 | import org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression; |
49 | 49 | import org.eclipse.jdt.internal.compiler.ast.RecordComponent; |
| 50 | +import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference; |
50 | 51 | import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; |
51 | 52 | import org.eclipse.jdt.internal.compiler.ast.TypeParameter; |
52 | 53 | import org.eclipse.jdt.internal.compiler.ast.TypeReference; |
@@ -1309,40 +1310,70 @@ void connectImplicitPermittedTypes() { |
1309 | 1310 | void connectPermittedTypes() { |
1310 | 1311 | SourceTypeBinding sourceType = this.referenceContext.binding; |
1311 | 1312 | sourceType.setPermittedTypes(Binding.NO_PERMITTEDTYPES); |
1312 | | - if (this.referenceContext.permittedTypes == null) { |
1313 | | - return; |
1314 | | - } |
1315 | 1313 | if (sourceType.id == TypeIds.T_JavaLangObject || sourceType.isEnum()) // already handled |
1316 | 1314 | return; |
1317 | 1315 |
|
1318 | | - int length = this.referenceContext.permittedTypes.length; |
1319 | | - ReferenceBinding[] permittedTypeBindings = new ReferenceBinding[length]; |
1320 | | - int count = 0; |
1321 | | - nextPermittedType : for (int i = 0; i < length; i++) { |
1322 | | - TypeReference permittedTypeRef = this.referenceContext.permittedTypes[i]; |
1323 | | - ReferenceBinding permittedType = findPermittedtype(permittedTypeRef); |
1324 | | - if (permittedType == null) { // detected cycle |
1325 | | - continue nextPermittedType; |
1326 | | - } |
1327 | | - // check for simple interface collisions |
1328 | | - // Check for a duplicate interface once the name is resolved, otherwise we may be confused (i.e. a.b.I and c.d.I) |
1329 | | - for (int j = 0; j < i; j++) { |
1330 | | - if (TypeBinding.equalsEquals(permittedTypeBindings[j], permittedType)) { |
1331 | | - problemReporter().sealedDuplicateTypeInPermits(sourceType, permittedTypeRef, permittedType); |
| 1316 | + if (this.referenceContext.permittedTypes != null) { |
| 1317 | + int length = this.referenceContext.permittedTypes.length; |
| 1318 | + ReferenceBinding[] permittedTypeBindings = new ReferenceBinding[length]; |
| 1319 | + int count = 0; |
| 1320 | + nextPermittedType : for (int i = 0; i < length; i++) { |
| 1321 | + TypeReference permittedTypeRef = this.referenceContext.permittedTypes[i]; |
| 1322 | + ReferenceBinding permittedType = findPermittedtype(permittedTypeRef); |
| 1323 | + if (permittedType == null) { // detected cycle |
1332 | 1324 | continue nextPermittedType; |
1333 | 1325 | } |
| 1326 | + if (!isPermittedTypeInAllowedFormat(sourceType, permittedTypeRef, permittedType)) |
| 1327 | + continue nextPermittedType; |
| 1328 | + |
| 1329 | + // check for simple interface collisions |
| 1330 | + // Check for a duplicate interface once the name is resolved, otherwise we may be confused (i.e. a.b.I and c.d.I) |
| 1331 | + for (int j = 0; j < i; j++) { |
| 1332 | + if (TypeBinding.equalsEquals(permittedTypeBindings[j], permittedType)) { |
| 1333 | + problemReporter().sealedDuplicateTypeInPermits(sourceType, permittedTypeRef, permittedType); |
| 1334 | + continue nextPermittedType; |
| 1335 | + } |
| 1336 | + } |
| 1337 | + // only want to reach here when no errors are reported |
| 1338 | + permittedTypeBindings[count++] = permittedType; |
| 1339 | + } |
| 1340 | + // hold onto all correctly resolved superinterfaces |
| 1341 | + if (count > 0) { |
| 1342 | + if (count != length) |
| 1343 | + System.arraycopy(permittedTypeBindings, 0, permittedTypeBindings = new ReferenceBinding[count], 0, count); |
| 1344 | + sourceType.setPermittedTypes(permittedTypeBindings); |
| 1345 | + } else { |
| 1346 | + sourceType.setPermittedTypes(Binding.NO_PERMITTEDTYPES); |
1334 | 1347 | } |
1335 | | - // only want to reach here when no errors are reported |
1336 | | - permittedTypeBindings[count++] = permittedType; |
1337 | 1348 | } |
1338 | | - // hold onto all correctly resolved superinterfaces |
1339 | | - if (count > 0) { |
1340 | | - if (count != length) |
1341 | | - System.arraycopy(permittedTypeBindings, 0, permittedTypeBindings = new ReferenceBinding[count], 0, count); |
1342 | | - sourceType.setPermittedTypes(permittedTypeBindings); |
1343 | | - } else { |
1344 | | - sourceType.setPermittedTypes(Binding.NO_PERMITTEDTYPES); |
| 1349 | + ReferenceBinding[] memberTypes = sourceType.memberTypes; |
| 1350 | + if (memberTypes != null && memberTypes != Binding.NO_MEMBER_TYPES) { |
| 1351 | + for (int j = 0, size = memberTypes.length; j < size; j++) |
| 1352 | + ((SourceTypeBinding) memberTypes[j]).scope.connectPermittedTypes(); |
| 1353 | + } |
| 1354 | + } |
| 1355 | + |
| 1356 | + private boolean isPermittedTypeInAllowedFormat(SourceTypeBinding sourceType, TypeReference permittedTypeRef, |
| 1357 | + ReferenceBinding permittedType) { |
| 1358 | + if (!(permittedType.isMemberType() && permittedTypeRef instanceof SingleTypeReference)) |
| 1359 | + return true; |
| 1360 | + ReferenceBinding enclosingType = permittedType.enclosingType(); |
| 1361 | + while (enclosingType != null) { |
| 1362 | + if (TypeBinding.equalsEquals(sourceType, enclosingType)) { |
| 1363 | + CompilationUnitScope cu = this.compilationUnitScope(); |
| 1364 | + if (cu.imports != null || cu.imports.length > 0) { |
| 1365 | + for (ImportBinding ib : cu.imports) { |
| 1366 | + Binding resolvedImport = cu.resolveSingleImport(ib, Binding.TYPE); |
| 1367 | + if (resolvedImport instanceof TypeBinding && |
| 1368 | + TypeBinding.equalsEquals(permittedType, (TypeBinding) resolvedImport)) |
| 1369 | + return true; |
| 1370 | + } |
| 1371 | + } |
| 1372 | + return false; |
| 1373 | + } |
| 1374 | + enclosingType = enclosingType.enclosingType(); |
1345 | 1375 | } |
| 1376 | + return true; |
1346 | 1377 | } |
1347 | 1378 |
|
1348 | 1379 | private boolean connectRecordSuperclass() { |
@@ -1447,7 +1478,7 @@ void connectTypeHierarchy() { |
1447 | 1478 | noProblems &= connectSuperInterfaces(); |
1448 | 1479 | environment().typesBeingConnected.remove(sourceType); |
1449 | 1480 | sourceType.tagBits |= TagBits.EndHierarchyCheck; |
1450 | | - connectPermittedTypes(); |
| 1481 | +// connectPermittedTypes(); |
1451 | 1482 | noProblems &= connectTypeVariables(this.referenceContext.typeParameters, false); |
1452 | 1483 | sourceType.tagBits |= TagBits.TypeVariablesAreConnected; |
1453 | 1484 | if (noProblems && sourceType.isHierarchyInconsistent()) |
|
0 commit comments