< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symbol.java

Print this page
@@ -25,10 +25,11 @@
  
  package com.sun.tools.javac.code;
  
  import java.lang.annotation.Annotation;
  import java.lang.annotation.Inherited;
+ import java.util.ArrayList;
  import java.util.Collections;
  import java.util.EnumSet;
  import java.util.HashMap;
  import java.util.Map;
  import java.util.Set;

@@ -1298,23 +1299,25 @@
          private List<RecordComponent> recordComponents = List.nil();
  
          // sealed classes related fields
          /** The classes, or interfaces, permitted to extend this class, or interface
           */
-         public List<Symbol> permitted;
+         private java.util.List<PermittedClassWithPos> permitted;
  
          public boolean isPermittedExplicit = false;
  
+         private record PermittedClassWithPos(Symbol permittedClass, int pos) {}
+ 
          public ClassSymbol(long flags, Name name, Type type, Symbol owner) {
              super(TYP, flags, name, type, owner);
              this.members_field = null;
              this.fullname = formFullName(name, owner);
              this.flatname = formFlatName(name, owner);
              this.sourcefile = null;
              this.classfile = null;
              this.annotationTypeMetadata = AnnotationTypeMetadata.notAnAnnotationType();
-             this.permitted = List.nil();
+             this.permitted = new ArrayList<>();
          }
  
          public ClassSymbol(long flags, Name name, Symbol owner) {
              this(
                  flags,

@@ -1322,10 +1325,41 @@
                  new ClassType(Type.noType, null, null),
                  owner);
              this.type.tsym = this;
          }
  
+         public void addPermittedSubclass(ClassSymbol csym, int pos) {
+             Assert.check(!isPermittedExplicit);
+             // we need to insert at the right pos
+             PermittedClassWithPos element = new PermittedClassWithPos(csym, pos);
+             int index = Collections.binarySearch(permitted, element, java.util.Comparator.comparing(PermittedClassWithPos::pos));
+             if (index < 0) {
+                 index = -index - 1;
+             }
+             permitted.add(index, element);
+         }
+ 
+         public boolean isPermittedSubclass(Symbol csym) {
+             for (PermittedClassWithPos permittedClassWithPos : permitted) {
+                 if (permittedClassWithPos.permittedClass.equals(csym)) {
+                     return true;
+                 }
+             }
+             return false;
+         }
+ 
+         public void clearPermittedSubclasses() {
+             permitted.clear();
+         }
+ 
+         public void setPermittedSubclasses(List<Symbol> permittedSubs) {
+             permitted.clear();
+             for (Symbol csym : permittedSubs) {
+                 permitted.add(new PermittedClassWithPos(csym, 0));
+             }
+         }
+ 
          /** The Java source which this symbol represents.
           */
          public String toString() {
              return className();
          }

@@ -1638,11 +1672,11 @@
              return (flags_field & RECORD) != 0;
          }
  
          @DefinedBy(Api.LANGUAGE_MODEL)
          public List<Type> getPermittedSubclasses() {
-             return permitted.map(s -> s.type);
+             return permitted.stream().map(s -> s.permittedClass().type).collect(List.collector());
          }
  
          @Override @DefinedBy(Api.LANGUAGE_MODEL)
          public boolean isUnnamed() {
              return (flags_field & Flags.UNNAMED_CLASS) != 0 ;
< prev index next >