< prev index next >

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

Print this page




 353      *  except for constructors of inner classes which get the enclosing
 354      *  instance class added as first argument.
 355      */
 356     public Type externalType(Types types) {
 357         Type t = erasure(types);
 358         if (name == name.table.names.init && owner.hasOuterInstance()) {
 359             Type outerThisType = types.erasure(owner.type.getEnclosingType());
 360             return new MethodType(t.getParameterTypes().prepend(outerThisType),
 361                                   t.getReturnType(),
 362                                   t.getThrownTypes(),
 363                                   t.tsym);
 364         } else {
 365             return t;
 366         }
 367     }
 368 
 369     public boolean isDeprecated() {
 370         return (flags_field & DEPRECATED) != 0;
 371     }
 372 




 373     public boolean hasDeprecatedAnnotation() {
 374         return (flags_field & DEPRECATED_ANNOTATION) != 0;
 375     }
 376 
 377     public boolean isDeprecatedForRemoval() {
 378         return (flags_field & DEPRECATED_REMOVAL) != 0;
 379     }
 380 
 381     public boolean isDeprecatableViaAnnotation() {
 382         switch (getKind()) {
 383             case LOCAL_VARIABLE:
 384             case PACKAGE:
 385             case PARAMETER:
 386             case RESOURCE_VARIABLE:
 387             case EXCEPTION_PARAMETER:
 388                 return false;
 389             default:
 390                 return true;
 391         }
 392     }
 393 
 394     public boolean isStatic() {
 395         return
 396             (flags() & STATIC) != 0 ||
 397             (owner.flags() & INTERFACE) != 0 && kind != MTH &&
 398              name != name.table.names._this;
 399     }
 400 
 401     public boolean isInterface() {
 402         return (flags() & INTERFACE) != 0;
 403     }
 404 




 405     public boolean isPrivate() {
 406         return (flags_field & Flags.AccessFlags) == PRIVATE;
 407     }
 408 
 409     public boolean isEnum() {
 410         return (flags() & ENUM) != 0;
 411     }
 412 








 413     /** Is this symbol declared (directly or indirectly) local
 414      *  to a method or variable initializer?
 415      *  Also includes fields of inner classes which are in
 416      *  turn local to a method or variable initializer.
 417      */
 418     public boolean isLocal() {
 419         return
 420             (owner.kind.matches(KindSelector.VAL_MTH) ||
 421              (owner.kind == TYP && owner.isLocal()));
 422     }
 423 
 424     /** Has this symbol an empty name? This includes anonymous
 425      *  inner classes.
 426      */
 427     public boolean isAnonymous() {
 428         return name.isEmpty();
 429     }
 430 
 431     /** Is this symbol a constructor?
 432      */


1412         @Override
1413         protected <A extends Annotation> A[] getInheritedAnnotations(Class<A> annoType) {
1414 
1415             ClassSymbol sup = getSuperClassToSearchForAnnotations();
1416 
1417             return sup == null ? super.getInheritedAnnotations(annoType)
1418                                : sup.getAnnotationsByType(annoType);
1419         }
1420 
1421 
1422         @DefinedBy(Api.LANGUAGE_MODEL)
1423         public ElementKind getKind() {
1424             apiComplete();
1425             long flags = flags();
1426             if ((flags & ANNOTATION) != 0)
1427                 return ElementKind.ANNOTATION_TYPE;
1428             else if ((flags & INTERFACE) != 0)
1429                 return ElementKind.INTERFACE;
1430             else if ((flags & ENUM) != 0)
1431                 return ElementKind.ENUM;


1432             else
1433                 return ElementKind.CLASS;
1434         }
1435 
1436         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1437         public Set<Modifier> getModifiers() {
1438             apiComplete();
1439             long flags = flags();
1440             return Flags.asModifierSet(flags & ~DEFAULT);
1441         }
1442 







1443         @DefinedBy(Api.LANGUAGE_MODEL)
1444         public NestingKind getNestingKind() {
1445             apiComplete();
1446             if (owner.kind == PCK)
1447                 return NestingKind.TOP_LEVEL;
1448             else if (name.isEmpty())
1449                 return NestingKind.ANONYMOUS;
1450             else if (owner.kind == MTH)
1451                 return NestingKind.LOCAL;
1452             else
1453                 return NestingKind.MEMBER;
1454         }
1455 
1456 
1457         @Override
1458         protected <A extends Annotation> Attribute.Compound getAttribute(final Class<A> annoType) {
1459 
1460             Attribute.Compound attrib = super.getAttribute(annoType);
1461 
1462             boolean inherited = annoType.isAnnotationPresent(Inherited.class);


1513         public void clearAnnotationMetadata() {
1514             metadata = null;
1515             annotationTypeMetadata = AnnotationTypeMetadata.notAnAnnotationType();
1516         }
1517 
1518         @Override
1519         public AnnotationTypeMetadata getAnnotationTypeMetadata() {
1520             return annotationTypeMetadata;
1521         }
1522 
1523         @Override
1524         public boolean isAnnotationType() {
1525             return (flags_field & Flags.ANNOTATION) != 0;
1526         }
1527 
1528         public void setAnnotationTypeMetadata(AnnotationTypeMetadata a) {
1529             Assert.checkNonNull(a);
1530             Assert.check(!annotationTypeMetadata.isMetadataForAnnotationType());
1531             this.annotationTypeMetadata = a;
1532         }





1533     }
1534 
1535 
1536     /** A class for variable symbols
1537      */
1538     public static class VarSymbol extends Symbol implements VariableElement {
1539 
1540         /** The variable's declaration position.
1541          */
1542         public int pos = Position.NOPOS;
1543 
1544         /** The variable's address. Used for different purposes during
1545          *  flow analysis, translation and code generation.
1546          *  Flow analysis:
1547          *    If this is a blank final or local variable, its sequence number.
1548          *  Translation:
1549          *    If this is a private field, its access number.
1550          *  Code generation:
1551          *    If this is a local variable, its logical slot number.
1552          */
1553         public int adr = -1;
1554 


1555         /** Construct a variable symbol, given its flags, name, type and owner.
1556          */
1557         public VarSymbol(long flags, Name name, Type type, Symbol owner) {
1558             super(VAR, flags, name, type, owner);
1559         }
1560 
1561         @Override
1562         public int poolTag() {
1563             return ClassFile.CONSTANT_Fieldref;
1564         }
1565 
1566         public MethodHandleSymbol asMethodHandle(boolean getter) {
1567             return new MethodHandleSymbol(this, getter);
1568         }
1569 
1570         /** Clone this symbol with new owner.
1571          */
1572         public VarSymbol clone(Symbol newOwner) {
1573             VarSymbol v = new VarSymbol(flags_field, name, type, newOwner) {
1574                 @Override


1579                 @Override
1580                 public Object poolKey(Types types) {
1581                     return new Pair<>(newOwner, baseSymbol());
1582                 }
1583             };
1584             v.pos = pos;
1585             v.adr = adr;
1586             v.data = data;
1587 //          System.out.println("clone " + v + " in " + newOwner);//DEBUG
1588             return v;
1589         }
1590 
1591         public String toString() {
1592             return name.toString();
1593         }
1594 
1595         public Symbol asMemberOf(Type site, Types types) {
1596             return new VarSymbol(flags_field, name, types.memberType(site, this), owner);
1597         }
1598 








1599         @DefinedBy(Api.LANGUAGE_MODEL)
1600         public ElementKind getKind() {
1601             long flags = flags();
1602             if ((flags & PARAMETER) != 0) {
1603                 if (isExceptionParameter())
1604                     return ElementKind.EXCEPTION_PARAMETER;
1605                 else
1606                     return ElementKind.PARAMETER;
1607             } else if ((flags & ENUM) != 0) {
1608                 return ElementKind.ENUM_CONSTANT;
1609             } else if (owner.kind == TYP || owner.kind == ERR) {
1610                 return ElementKind.FIELD;
1611             } else if (isResourceVariable()) {
1612                 return ElementKind.RESOURCE_VARIABLE;
1613             } else {
1614                 return ElementKind.LOCAL_VARIABLE;
1615             }
1616         }
1617 
1618         @DefinedBy(Api.LANGUAGE_MODEL)


1763                 return owner.name.toString();
1764             } else {
1765                 String s = (name == name.table.names.init)
1766                     ? owner.name.toString()
1767                     : name.toString();
1768                 if (type != null) {
1769                     if (type.hasTag(FORALL))
1770                         s = "<" + ((ForAll)type).getTypeArguments() + ">" + s;
1771                     s += "(" + type.argtypes((flags() & VARARGS) != 0) + ")";
1772                 }
1773                 return s;
1774             }
1775         }
1776 
1777         @Override
1778         public int poolTag() {
1779             return owner.isInterface() ?
1780                     ClassFile.CONSTANT_InterfaceMethodref : ClassFile.CONSTANT_Methodref;
1781         }
1782 




1783         public boolean isHandle() {
1784             return false;
1785         }
1786 
1787 
1788         public MethodHandleSymbol asHandle() {
1789             return new MethodHandleSymbol(this);
1790         }
1791 
1792         /** find a symbol that this (proxy method) symbol implements.
1793          *  @param    c       The class whose members are searched for
1794          *                    implementations
1795          */
1796         public Symbol implemented(TypeSymbol c, Types types) {
1797             Symbol impl = null;
1798             for (List<Type> is = types.interfaces(c.type);
1799                  impl == null && is.nonEmpty();
1800                  is = is.tail) {
1801                 TypeSymbol i = is.head.tsym;
1802                 impl = implementedIn(i, types);


2143         public LoadableConstant[] staticArgs() {
2144             return staticArgs;
2145         }
2146 
2147         @Override
2148         public LoadableConstant bootstrapMethod() {
2149             return bsm;
2150         }
2151 
2152         @Override
2153         public int poolTag() {
2154             return ClassFile.CONSTANT_Dynamic;
2155         }
2156     }
2157 
2158     /** A class for method handles.
2159      */
2160     public static class MethodHandleSymbol extends MethodSymbol implements LoadableConstant {
2161 
2162         private Symbol refSym;

2163         private boolean getter;
2164 
2165         public MethodHandleSymbol(Symbol msym) {
2166             this(msym, false);
2167         }
2168 
2169         public MethodHandleSymbol(Symbol msym, boolean getter) {
2170             super(msym.flags_field, msym.name, msym.type, msym.owner);
2171             this.refSym = msym;
2172             this.getter = getter;
2173         }
2174 
2175         /**
2176          * Returns the kind associated with this method handle.
2177          */
2178         public int referenceKind() {
2179             if (refSym.kind == VAR) {
2180                 return getter ?
2181                         refSym.isStatic() ? ClassFile.REF_getStatic : ClassFile.REF_getField :
2182                         refSym.isStatic() ? ClassFile.REF_putStatic : ClassFile.REF_putField;




 353      *  except for constructors of inner classes which get the enclosing
 354      *  instance class added as first argument.
 355      */
 356     public Type externalType(Types types) {
 357         Type t = erasure(types);
 358         if (name == name.table.names.init && owner.hasOuterInstance()) {
 359             Type outerThisType = types.erasure(owner.type.getEnclosingType());
 360             return new MethodType(t.getParameterTypes().prepend(outerThisType),
 361                                   t.getReturnType(),
 362                                   t.getThrownTypes(),
 363                                   t.tsym);
 364         } else {
 365             return t;
 366         }
 367     }
 368 
 369     public boolean isDeprecated() {
 370         return (flags_field & DEPRECATED) != 0;
 371     }
 372 
 373     public boolean isRecord() {
 374         return (flags_field & RECORD) != 0;
 375     }
 376 
 377     public boolean hasDeprecatedAnnotation() {
 378         return (flags_field & DEPRECATED_ANNOTATION) != 0;
 379     }
 380 
 381     public boolean isDeprecatedForRemoval() {
 382         return (flags_field & DEPRECATED_REMOVAL) != 0;
 383     }
 384 
 385     public boolean isDeprecatableViaAnnotation() {
 386         switch (getKind()) {
 387             case LOCAL_VARIABLE:
 388             case PACKAGE:
 389             case PARAMETER:
 390             case RESOURCE_VARIABLE:
 391             case EXCEPTION_PARAMETER:
 392                 return false;
 393             default:
 394                 return true;
 395         }
 396     }
 397 
 398     public boolean isStatic() {
 399         return
 400             (flags() & STATIC) != 0 ||
 401             (owner.flags() & INTERFACE) != 0 && kind != MTH &&
 402              name != name.table.names._this;
 403     }
 404 
 405     public boolean isInterface() {
 406         return (flags() & INTERFACE) != 0;
 407     }
 408 
 409     public boolean isAbstract() {
 410         return (flags() & ABSTRACT) != 0;
 411     }
 412 
 413     public boolean isPrivate() {
 414         return (flags_field & Flags.AccessFlags) == PRIVATE;
 415     }
 416 
 417     public boolean isEnum() {
 418         return (flags() & ENUM) != 0;
 419     }
 420 
 421     public boolean isSealed() {
 422         return (flags_field & SEALED) != 0;
 423     }
 424 
 425     public boolean isFinal() {
 426         return (flags_field & FINAL) != 0;
 427     }
 428 
 429     /** Is this symbol declared (directly or indirectly) local
 430      *  to a method or variable initializer?
 431      *  Also includes fields of inner classes which are in
 432      *  turn local to a method or variable initializer.
 433      */
 434     public boolean isLocal() {
 435         return
 436             (owner.kind.matches(KindSelector.VAL_MTH) ||
 437              (owner.kind == TYP && owner.isLocal()));
 438     }
 439 
 440     /** Has this symbol an empty name? This includes anonymous
 441      *  inner classes.
 442      */
 443     public boolean isAnonymous() {
 444         return name.isEmpty();
 445     }
 446 
 447     /** Is this symbol a constructor?
 448      */


1428         @Override
1429         protected <A extends Annotation> A[] getInheritedAnnotations(Class<A> annoType) {
1430 
1431             ClassSymbol sup = getSuperClassToSearchForAnnotations();
1432 
1433             return sup == null ? super.getInheritedAnnotations(annoType)
1434                                : sup.getAnnotationsByType(annoType);
1435         }
1436 
1437 
1438         @DefinedBy(Api.LANGUAGE_MODEL)
1439         public ElementKind getKind() {
1440             apiComplete();
1441             long flags = flags();
1442             if ((flags & ANNOTATION) != 0)
1443                 return ElementKind.ANNOTATION_TYPE;
1444             else if ((flags & INTERFACE) != 0)
1445                 return ElementKind.INTERFACE;
1446             else if ((flags & ENUM) != 0)
1447                 return ElementKind.ENUM;
1448             else if ((flags & RECORD) != 0)
1449                 return ElementKind.RECORD;
1450             else
1451                 return ElementKind.CLASS;
1452         }
1453 
1454         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1455         public Set<Modifier> getModifiers() {
1456             apiComplete();
1457             long flags = flags();
1458             return Flags.asModifierSet(flags & ~DEFAULT);
1459         }
1460 
1461         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1462         public java.util.List<VariableElement> getStateComponents() {
1463             apiComplete();
1464             // Inital implementation
1465             return javax.lang.model.util.ElementFilter.stateComponentsIn(getEnclosedElements());
1466         }
1467 
1468         @DefinedBy(Api.LANGUAGE_MODEL)
1469         public NestingKind getNestingKind() {
1470             apiComplete();
1471             if (owner.kind == PCK)
1472                 return NestingKind.TOP_LEVEL;
1473             else if (name.isEmpty())
1474                 return NestingKind.ANONYMOUS;
1475             else if (owner.kind == MTH)
1476                 return NestingKind.LOCAL;
1477             else
1478                 return NestingKind.MEMBER;
1479         }
1480 
1481 
1482         @Override
1483         protected <A extends Annotation> Attribute.Compound getAttribute(final Class<A> annoType) {
1484 
1485             Attribute.Compound attrib = super.getAttribute(annoType);
1486 
1487             boolean inherited = annoType.isAnnotationPresent(Inherited.class);


1538         public void clearAnnotationMetadata() {
1539             metadata = null;
1540             annotationTypeMetadata = AnnotationTypeMetadata.notAnAnnotationType();
1541         }
1542 
1543         @Override
1544         public AnnotationTypeMetadata getAnnotationTypeMetadata() {
1545             return annotationTypeMetadata;
1546         }
1547 
1548         @Override
1549         public boolean isAnnotationType() {
1550             return (flags_field & Flags.ANNOTATION) != 0;
1551         }
1552 
1553         public void setAnnotationTypeMetadata(AnnotationTypeMetadata a) {
1554             Assert.checkNonNull(a);
1555             Assert.check(!annotationTypeMetadata.isMetadataForAnnotationType());
1556             this.annotationTypeMetadata = a;
1557         }
1558 
1559         @DefinedBy(Api.LANGUAGE_MODEL)
1560         public List<Type> getPermittedSubtypes() {
1561             return ((ClassType)type).permitted;
1562         }
1563     }
1564 
1565 
1566     /** A class for variable symbols
1567      */
1568     public static class VarSymbol extends Symbol implements VariableElement {
1569 
1570         /** The variable's declaration position.
1571          */
1572         public int pos = Position.NOPOS;
1573 
1574         /** The variable's address. Used for different purposes during
1575          *  flow analysis, translation and code generation.
1576          *  Flow analysis:
1577          *    If this is a blank final or local variable, its sequence number.
1578          *  Translation:
1579          *    If this is a private field, its access number.
1580          *  Code generation:
1581          *    If this is a local variable, its logical slot number.
1582          */
1583         public int adr = -1;
1584 
1585         public List<Pair<Accessors.Kind, MethodSymbol>> accessors = List.nil();
1586 
1587         /** Construct a variable symbol, given its flags, name, type and owner.
1588          */
1589         public VarSymbol(long flags, Name name, Type type, Symbol owner) {
1590             super(VAR, flags, name, type, owner);
1591         }
1592 
1593         @Override
1594         public int poolTag() {
1595             return ClassFile.CONSTANT_Fieldref;
1596         }
1597 
1598         public MethodHandleSymbol asMethodHandle(boolean getter) {
1599             return new MethodHandleSymbol(this, getter);
1600         }
1601 
1602         /** Clone this symbol with new owner.
1603          */
1604         public VarSymbol clone(Symbol newOwner) {
1605             VarSymbol v = new VarSymbol(flags_field, name, type, newOwner) {
1606                 @Override


1611                 @Override
1612                 public Object poolKey(Types types) {
1613                     return new Pair<>(newOwner, baseSymbol());
1614                 }
1615             };
1616             v.pos = pos;
1617             v.adr = adr;
1618             v.data = data;
1619 //          System.out.println("clone " + v + " in " + newOwner);//DEBUG
1620             return v;
1621         }
1622 
1623         public String toString() {
1624             return name.toString();
1625         }
1626 
1627         public Symbol asMemberOf(Type site, Types types) {
1628             return new VarSymbol(flags_field, name, types.memberType(site, this), owner);
1629         }
1630 
1631         @Override
1632         public Type erasure(Types types) {
1633             if (erasure_field == null) {
1634                 erasure_field = types.erasure(type);
1635             }
1636             return erasure_field;
1637         }
1638 
1639         @DefinedBy(Api.LANGUAGE_MODEL)
1640         public ElementKind getKind() {
1641             long flags = flags();
1642             if ((flags & PARAMETER) != 0) {
1643                 if (isExceptionParameter())
1644                     return ElementKind.EXCEPTION_PARAMETER;
1645                 else
1646                     return ElementKind.PARAMETER;
1647             } else if ((flags & ENUM) != 0) {
1648                 return ElementKind.ENUM_CONSTANT;
1649             } else if (owner.kind == TYP || owner.kind == ERR) {
1650                 return ElementKind.FIELD;
1651             } else if (isResourceVariable()) {
1652                 return ElementKind.RESOURCE_VARIABLE;
1653             } else {
1654                 return ElementKind.LOCAL_VARIABLE;
1655             }
1656         }
1657 
1658         @DefinedBy(Api.LANGUAGE_MODEL)


1803                 return owner.name.toString();
1804             } else {
1805                 String s = (name == name.table.names.init)
1806                     ? owner.name.toString()
1807                     : name.toString();
1808                 if (type != null) {
1809                     if (type.hasTag(FORALL))
1810                         s = "<" + ((ForAll)type).getTypeArguments() + ">" + s;
1811                     s += "(" + type.argtypes((flags() & VARARGS) != 0) + ")";
1812                 }
1813                 return s;
1814             }
1815         }
1816 
1817         @Override
1818         public int poolTag() {
1819             return owner.isInterface() ?
1820                     ClassFile.CONSTANT_InterfaceMethodref : ClassFile.CONSTANT_Methodref;
1821         }
1822 
1823         public boolean isDynamic() {
1824             return false;
1825         }
1826 
1827         public boolean isHandle() {
1828             return false;
1829         }
1830 
1831 
1832         public MethodHandleSymbol asHandle() {
1833             return new MethodHandleSymbol(this);
1834         }
1835 
1836         /** find a symbol that this (proxy method) symbol implements.
1837          *  @param    c       The class whose members are searched for
1838          *                    implementations
1839          */
1840         public Symbol implemented(TypeSymbol c, Types types) {
1841             Symbol impl = null;
1842             for (List<Type> is = types.interfaces(c.type);
1843                  impl == null && is.nonEmpty();
1844                  is = is.tail) {
1845                 TypeSymbol i = is.head.tsym;
1846                 impl = implementedIn(i, types);


2187         public LoadableConstant[] staticArgs() {
2188             return staticArgs;
2189         }
2190 
2191         @Override
2192         public LoadableConstant bootstrapMethod() {
2193             return bsm;
2194         }
2195 
2196         @Override
2197         public int poolTag() {
2198             return ClassFile.CONSTANT_Dynamic;
2199         }
2200     }
2201 
2202     /** A class for method handles.
2203      */
2204     public static class MethodHandleSymbol extends MethodSymbol implements LoadableConstant {
2205 
2206         private Symbol refSym;
2207         // in case the simbol is a variable
2208         private boolean getter;
2209 
2210         public MethodHandleSymbol(Symbol msym) {
2211             this(msym, false);
2212         }
2213 
2214         public MethodHandleSymbol(Symbol msym, boolean getter) {
2215             super(msym.flags_field, msym.name, msym.type, msym.owner);
2216             this.refSym = msym;
2217             this.getter = getter;
2218         }
2219 
2220         /**
2221          * Returns the kind associated with this method handle.
2222          */
2223         public int referenceKind() {
2224             if (refSym.kind == VAR) {
2225                 return getter ?
2226                         refSym.isStatic() ? ClassFile.REF_getStatic : ClassFile.REF_getField :
2227                         refSym.isStatic() ? ClassFile.REF_putStatic : ClassFile.REF_putField;


< prev index next >