< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Print this page
*** 5404,62 ***
              Env<AttrContext> env = typeEnvs.get(c);
  
              if (c.isSealed() &&
                      !c.isEnum() &&
                      !c.isPermittedExplicit &&
!                     c.permitted.isEmpty()) {
                  log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.SealedClassMustHaveSubclasses);
              }
  
              if (c.isSealed()) {
                  Set<Symbol> permittedTypes = new HashSet<>();
                  boolean sealedInUnnamed = c.packge().modle == syms.unnamedModule || c.packge().modle == syms.noModule;
!                 for (Symbol subTypeSym : c.permitted) {
                      boolean isTypeVar = false;
!                     if (subTypeSym.type.getTag() == TYPEVAR) {
                          isTypeVar = true; //error recovery
!                         log.error(TreeInfo.diagnosticPositionFor(subTypeSym, env.tree),
!                                 Errors.InvalidPermitsClause(Fragments.IsATypeVariable(subTypeSym.type)));
                      }
!                     if (subTypeSym.isAnonymous() && !c.isEnum()) {
!                         log.error(TreeInfo.diagnosticPositionFor(subTypeSym, env.tree),  Errors.LocalClassesCantExtendSealed(Fragments.Anonymous));
                      }
!                     if (permittedTypes.contains(subTypeSym)) {
                          DiagnosticPosition pos =
                                  env.enclClass.permitting.stream()
!                                         .filter(permittedExpr -> TreeInfo.diagnosticPositionFor(subTypeSym, permittedExpr, true) != null)
                                          .limit(2).collect(List.collector()).get(1);
!                         log.error(pos, Errors.InvalidPermitsClause(Fragments.IsDuplicated(subTypeSym.type)));
                      } else {
!                         permittedTypes.add(subTypeSym);
                      }
                      if (sealedInUnnamed) {
!                         if (subTypeSym.packge() != c.packge()) {
!                             log.error(TreeInfo.diagnosticPositionFor(subTypeSym, env.tree),
                                      Errors.ClassInUnnamedModuleCantExtendSealedInDiffPackage(c)
                              );
                          }
!                     } else if (subTypeSym.packge().modle != c.packge().modle) {
!                         log.error(TreeInfo.diagnosticPositionFor(subTypeSym, env.tree),
                                  Errors.ClassInModuleCantExtendSealedInDiffModule(c, c.packge().modle)
                          );
                      }
!                     if (subTypeSym == c.type.tsym || types.isSuperType(subTypeSym.type, c.type)) {
!                         log.error(TreeInfo.diagnosticPositionFor(subTypeSym, ((JCClassDecl)env.tree).permitting),
                                  Errors.InvalidPermitsClause(
!                                         subTypeSym == c.type.tsym ?
                                                  Fragments.MustNotBeSameClass :
!                                                 Fragments.MustNotBeSupertype(subTypeSym.type)
                                  )
                          );
                      } else if (!isTypeVar) {
!                         boolean thisIsASuper = types.directSupertypes(subTypeSym.type)
                                                      .stream()
                                                      .anyMatch(d -> d.tsym == c);
                          if (!thisIsASuper) {
!                             log.error(TreeInfo.diagnosticPositionFor(subTypeSym, env.tree),
!                                     Errors.InvalidPermitsClause(Fragments.DoesntExtendSealed(subTypeSym.type)));
                          }
                      }
                  }
              }
  
--- 5404,62 ---
              Env<AttrContext> env = typeEnvs.get(c);
  
              if (c.isSealed() &&
                      !c.isEnum() &&
                      !c.isPermittedExplicit &&
!                     c.getPermittedSubclasses().isEmpty()) {
                  log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.SealedClassMustHaveSubclasses);
              }
  
              if (c.isSealed()) {
                  Set<Symbol> permittedTypes = new HashSet<>();
                  boolean sealedInUnnamed = c.packge().modle == syms.unnamedModule || c.packge().modle == syms.noModule;
!                 for (Type subType : c.getPermittedSubclasses()) {
                      boolean isTypeVar = false;
!                     if (subType.getTag() == TYPEVAR) {
                          isTypeVar = true; //error recovery
!                         log.error(TreeInfo.diagnosticPositionFor(subType.tsym, env.tree),
!                                 Errors.InvalidPermitsClause(Fragments.IsATypeVariable(subType)));
                      }
!                     if (subType.tsym.isAnonymous() && !c.isEnum()) {
!                         log.error(TreeInfo.diagnosticPositionFor(subType.tsym, env.tree),  Errors.LocalClassesCantExtendSealed(Fragments.Anonymous));
                      }
!                     if (permittedTypes.contains(subType.tsym)) {
                          DiagnosticPosition pos =
                                  env.enclClass.permitting.stream()
!                                         .filter(permittedExpr -> TreeInfo.diagnosticPositionFor(subType.tsym, permittedExpr, true) != null)
                                          .limit(2).collect(List.collector()).get(1);
!                         log.error(pos, Errors.InvalidPermitsClause(Fragments.IsDuplicated(subType)));
                      } else {
!                         permittedTypes.add(subType.tsym);
                      }
                      if (sealedInUnnamed) {
!                         if (subType.tsym.packge() != c.packge()) {
!                             log.error(TreeInfo.diagnosticPositionFor(subType.tsym, env.tree),
                                      Errors.ClassInUnnamedModuleCantExtendSealedInDiffPackage(c)
                              );
                          }
!                     } else if (subType.tsym.packge().modle != c.packge().modle) {
!                         log.error(TreeInfo.diagnosticPositionFor(subType.tsym, env.tree),
                                  Errors.ClassInModuleCantExtendSealedInDiffModule(c, c.packge().modle)
                          );
                      }
!                     if (subType.tsym == c.type.tsym || types.isSuperType(subType, c.type)) {
!                         log.error(TreeInfo.diagnosticPositionFor(subType.tsym, ((JCClassDecl)env.tree).permitting),
                                  Errors.InvalidPermitsClause(
!                                         subType.tsym == c.type.tsym ?
                                                  Fragments.MustNotBeSameClass :
!                                                 Fragments.MustNotBeSupertype(subType)
                                  )
                          );
                      } else if (!isTypeVar) {
!                         boolean thisIsASuper = types.directSupertypes(subType)
                                                      .stream()
                                                      .anyMatch(d -> d.tsym == c);
                          if (!thisIsASuper) {
!                             log.error(TreeInfo.diagnosticPositionFor(subType.tsym, env.tree),
!                                     Errors.InvalidPermitsClause(Fragments.DoesntExtendSealed(subType)));
                          }
                      }
                  }
              }
  

*** 5490,11 ***
                      log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.LocalClassesCantExtendSealed(c.isAnonymous() ? Fragments.Anonymous : Fragments.Local));
                  }
  
                  if (!c.type.isCompound()) {
                      for (ClassSymbol supertypeSym : sealedSupers) {
!                         if (!supertypeSym.permitted.contains(c.type.tsym)) {
                              log.error(TreeInfo.diagnosticPositionFor(c.type.tsym, env.tree), Errors.CantInheritFromSealed(supertypeSym));
                          }
                      }
                      if (!c.isNonSealed() && !c.isFinal() && !c.isSealed()) {
                          log.error(TreeInfo.diagnosticPositionFor(c, env.tree),
--- 5490,11 ---
                      log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.LocalClassesCantExtendSealed(c.isAnonymous() ? Fragments.Anonymous : Fragments.Local));
                  }
  
                  if (!c.type.isCompound()) {
                      for (ClassSymbol supertypeSym : sealedSupers) {
!                         if (!supertypeSym.isPermittedSubclass(c.type.tsym)) {
                              log.error(TreeInfo.diagnosticPositionFor(c.type.tsym, env.tree), Errors.CantInheritFromSealed(supertypeSym));
                          }
                      }
                      if (!c.isNonSealed() && !c.isFinal() && !c.isSealed()) {
                          log.error(TreeInfo.diagnosticPositionFor(c, env.tree),
< prev index next >