< prev index next >

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

Print this page

  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.code;
  27 
  28 import java.lang.annotation.Annotation;
  29 import java.lang.annotation.Inherited;

  30 import java.util.Collections;
  31 import java.util.EnumSet;
  32 import java.util.HashMap;
  33 import java.util.Map;
  34 import java.util.Set;
  35 import java.util.concurrent.Callable;
  36 import java.util.function.Supplier;
  37 import java.util.function.Predicate;
  38 
  39 import javax.lang.model.element.Element;
  40 import javax.lang.model.element.ElementKind;
  41 import javax.lang.model.element.ElementVisitor;
  42 import javax.lang.model.element.ExecutableElement;
  43 import javax.lang.model.element.Modifier;
  44 import javax.lang.model.element.ModuleElement;
  45 import javax.lang.model.element.NestingKind;
  46 import javax.lang.model.element.PackageElement;
  47 import javax.lang.model.element.RecordComponentElement;
  48 import javax.lang.model.element.TypeElement;
  49 import javax.lang.model.element.TypeParameterElement;

1283          *  this will have extension .class or .java
1284          */
1285         public JavaFileObject classfile;
1286 
1287         /** the list of translated local classes (used for generating
1288          * InnerClasses attribute)
1289          */
1290         public List<ClassSymbol> trans_local;
1291 
1292         /** the annotation metadata attached to this class */
1293         private AnnotationTypeMetadata annotationTypeMetadata;
1294 
1295         /* the list of any of record components, only non empty if the class is a record
1296          * and it has at least one record component
1297          */
1298         private List<RecordComponent> recordComponents = List.nil();
1299 
1300         // sealed classes related fields
1301         /** The classes, or interfaces, permitted to extend this class, or interface
1302          */
1303         public List<Symbol> permitted;
1304 
1305         public boolean isPermittedExplicit = false;
1306 


1307         public ClassSymbol(long flags, Name name, Type type, Symbol owner) {
1308             super(TYP, flags, name, type, owner);
1309             this.members_field = null;
1310             this.fullname = formFullName(name, owner);
1311             this.flatname = formFlatName(name, owner);
1312             this.sourcefile = null;
1313             this.classfile = null;
1314             this.annotationTypeMetadata = AnnotationTypeMetadata.notAnAnnotationType();
1315             this.permitted = List.nil();
1316         }
1317 
1318         public ClassSymbol(long flags, Name name, Symbol owner) {
1319             this(
1320                 flags,
1321                 name,
1322                 new ClassType(Type.noType, null, null),
1323                 owner);
1324             this.type.tsym = this;
1325         }
1326 































1327         /** The Java source which this symbol represents.
1328          */
1329         public String toString() {
1330             return className();
1331         }
1332 
1333         public long flags() {
1334             complete();
1335             return flags_field;
1336         }
1337 
1338         public WriteableScope members() {
1339             complete();
1340             return members_field;
1341         }
1342 
1343         @Override
1344         public List<Attribute.Compound> getRawAttributes() {
1345             complete();
1346             return super.getRawAttributes();

1623             return annotationTypeMetadata;
1624         }
1625 
1626         @Override
1627         public boolean isAnnotationType() {
1628             return (flags_field & Flags.ANNOTATION) != 0;
1629         }
1630 
1631         public void setAnnotationTypeMetadata(AnnotationTypeMetadata a) {
1632             Assert.checkNonNull(a);
1633             Assert.check(!annotationTypeMetadata.isMetadataForAnnotationType());
1634             this.annotationTypeMetadata = a;
1635         }
1636 
1637         public boolean isRecord() {
1638             return (flags_field & RECORD) != 0;
1639         }
1640 
1641         @DefinedBy(Api.LANGUAGE_MODEL)
1642         public List<Type> getPermittedSubclasses() {
1643             return permitted.map(s -> s.type);
1644         }
1645 
1646         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1647         public boolean isUnnamed() {
1648             return (flags_field & Flags.UNNAMED_CLASS) != 0 ;
1649         }
1650     }
1651 
1652 
1653     /** A class for variable symbols
1654      */
1655     @SuppressWarnings("preview")
1656     public static class VarSymbol extends Symbol implements VariableElement {
1657 
1658         /** The variable's declaration position.
1659          */
1660         public int pos = Position.NOPOS;
1661 
1662         /** The variable's address. Used for different purposes during
1663          *  flow analysis, translation and code generation.

  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.code;
  27 
  28 import java.lang.annotation.Annotation;
  29 import java.lang.annotation.Inherited;
  30 import java.util.ArrayList;
  31 import java.util.Collections;
  32 import java.util.EnumSet;
  33 import java.util.HashMap;
  34 import java.util.Map;
  35 import java.util.Set;
  36 import java.util.concurrent.Callable;
  37 import java.util.function.Supplier;
  38 import java.util.function.Predicate;
  39 
  40 import javax.lang.model.element.Element;
  41 import javax.lang.model.element.ElementKind;
  42 import javax.lang.model.element.ElementVisitor;
  43 import javax.lang.model.element.ExecutableElement;
  44 import javax.lang.model.element.Modifier;
  45 import javax.lang.model.element.ModuleElement;
  46 import javax.lang.model.element.NestingKind;
  47 import javax.lang.model.element.PackageElement;
  48 import javax.lang.model.element.RecordComponentElement;
  49 import javax.lang.model.element.TypeElement;
  50 import javax.lang.model.element.TypeParameterElement;

1284          *  this will have extension .class or .java
1285          */
1286         public JavaFileObject classfile;
1287 
1288         /** the list of translated local classes (used for generating
1289          * InnerClasses attribute)
1290          */
1291         public List<ClassSymbol> trans_local;
1292 
1293         /** the annotation metadata attached to this class */
1294         private AnnotationTypeMetadata annotationTypeMetadata;
1295 
1296         /* the list of any of record components, only non empty if the class is a record
1297          * and it has at least one record component
1298          */
1299         private List<RecordComponent> recordComponents = List.nil();
1300 
1301         // sealed classes related fields
1302         /** The classes, or interfaces, permitted to extend this class, or interface
1303          */
1304         private java.util.List<PermittedClassWithPos> permitted;
1305 
1306         public boolean isPermittedExplicit = false;
1307 
1308         private record PermittedClassWithPos(Symbol permittedClass, int pos) {}
1309 
1310         public ClassSymbol(long flags, Name name, Type type, Symbol owner) {
1311             super(TYP, flags, name, type, owner);
1312             this.members_field = null;
1313             this.fullname = formFullName(name, owner);
1314             this.flatname = formFlatName(name, owner);
1315             this.sourcefile = null;
1316             this.classfile = null;
1317             this.annotationTypeMetadata = AnnotationTypeMetadata.notAnAnnotationType();
1318             this.permitted = new ArrayList<>();
1319         }
1320 
1321         public ClassSymbol(long flags, Name name, Symbol owner) {
1322             this(
1323                 flags,
1324                 name,
1325                 new ClassType(Type.noType, null, null),
1326                 owner);
1327             this.type.tsym = this;
1328         }
1329 
1330         public void addPermittedSubclass(ClassSymbol csym, int pos) {
1331             Assert.check(!isPermittedExplicit);
1332             // we need to insert at the right pos
1333             PermittedClassWithPos element = new PermittedClassWithPos(csym, pos);
1334             int index = Collections.binarySearch(permitted, element, java.util.Comparator.comparing(PermittedClassWithPos::pos));
1335             if (index < 0) {
1336                 index = -index - 1;
1337             }
1338             permitted.add(index, element);
1339         }
1340 
1341         public boolean isPermittedSubclass(Symbol csym) {
1342             for (PermittedClassWithPos permittedClassWithPos : permitted) {
1343                 if (permittedClassWithPos.permittedClass.equals(csym)) {
1344                     return true;
1345                 }
1346             }
1347             return false;
1348         }
1349 
1350         public void clearPermittedSubclasses() {
1351             permitted.clear();
1352         }
1353 
1354         public void setPermittedSubclasses(List<Symbol> permittedSubs) {
1355             permitted.clear();
1356             for (Symbol csym : permittedSubs) {
1357                 permitted.add(new PermittedClassWithPos(csym, 0));
1358             }
1359         }
1360 
1361         /** The Java source which this symbol represents.
1362          */
1363         public String toString() {
1364             return className();
1365         }
1366 
1367         public long flags() {
1368             complete();
1369             return flags_field;
1370         }
1371 
1372         public WriteableScope members() {
1373             complete();
1374             return members_field;
1375         }
1376 
1377         @Override
1378         public List<Attribute.Compound> getRawAttributes() {
1379             complete();
1380             return super.getRawAttributes();

1657             return annotationTypeMetadata;
1658         }
1659 
1660         @Override
1661         public boolean isAnnotationType() {
1662             return (flags_field & Flags.ANNOTATION) != 0;
1663         }
1664 
1665         public void setAnnotationTypeMetadata(AnnotationTypeMetadata a) {
1666             Assert.checkNonNull(a);
1667             Assert.check(!annotationTypeMetadata.isMetadataForAnnotationType());
1668             this.annotationTypeMetadata = a;
1669         }
1670 
1671         public boolean isRecord() {
1672             return (flags_field & RECORD) != 0;
1673         }
1674 
1675         @DefinedBy(Api.LANGUAGE_MODEL)
1676         public List<Type> getPermittedSubclasses() {
1677             return permitted.stream().map(s -> s.permittedClass().type).collect(List.collector());
1678         }
1679 
1680         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1681         public boolean isUnnamed() {
1682             return (flags_field & Flags.UNNAMED_CLASS) != 0 ;
1683         }
1684     }
1685 
1686 
1687     /** A class for variable symbols
1688      */
1689     @SuppressWarnings("preview")
1690     public static class VarSymbol extends Symbol implements VariableElement {
1691 
1692         /** The variable's declaration position.
1693          */
1694         public int pos = Position.NOPOS;
1695 
1696         /** The variable's address. Used for different purposes during
1697          *  flow analysis, translation and code generation.
< prev index next >