< prev index next >

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

Print this page

  24  */
  25 
  26 package com.sun.tools.javac.comp;
  27 
  28 import java.util.ArrayList;
  29 import java.util.HashSet;
  30 import java.util.Set;
  31 import java.util.function.BiConsumer;
  32 import java.util.stream.Collectors;
  33 
  34 import javax.tools.JavaFileObject;
  35 
  36 import com.sun.tools.javac.code.*;
  37 import com.sun.tools.javac.code.Lint.LintCategory;
  38 import com.sun.tools.javac.code.Scope.ImportFilter;
  39 import com.sun.tools.javac.code.Scope.NamedImportScope;
  40 import com.sun.tools.javac.code.Scope.StarImportScope;
  41 import com.sun.tools.javac.code.Scope.WriteableScope;
  42 import com.sun.tools.javac.code.Source.Feature;
  43 import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata;

  44 import com.sun.tools.javac.tree.*;
  45 import com.sun.tools.javac.util.*;
  46 import com.sun.tools.javac.util.DefinedBy.Api;
  47 
  48 import com.sun.tools.javac.code.Symbol.*;
  49 import com.sun.tools.javac.code.Type.*;
  50 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  51 import com.sun.tools.javac.tree.JCTree.*;
  52 
  53 import static com.sun.tools.javac.code.Flags.*;
  54 import static com.sun.tools.javac.code.Flags.ANNOTATION;

  55 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
  56 import static com.sun.tools.javac.code.Kinds.Kind.*;
  57 import static com.sun.tools.javac.code.TypeTag.CLASS;
  58 import static com.sun.tools.javac.code.TypeTag.ERROR;
  59 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  60 
  61 import static com.sun.tools.javac.code.TypeTag.*;
  62 import static com.sun.tools.javac.tree.JCTree.Tag.*;
  63 
  64 import com.sun.tools.javac.util.Dependencies.CompletionCause;
  65 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
  66 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  67 
  68 /** This is the second phase of Enter, in which classes are completed
  69  *  by resolving their headers and entering their members in the into
  70  *  the class scope. See Enter for an overall overview.
  71  *
  72  *  This class uses internal phases to process the classes. When a phase
  73  *  processes classes, the lower phases are not invoked until all classes
  74  *  pass through the current phase. Note that it is possible that upper phases

1090                 (types.supertype(tree.sym.type).tsym.flags() & Flags.ENUM) == 0) {
1091                 addEnumMembers(tree, env);
1092             }
1093             boolean isRecord = (tree.sym.flags_field & RECORD) != 0;
1094             List<JCTree> alreadyEntered = null;
1095             if (isRecord) {
1096                 alreadyEntered = List.convert(JCTree.class, TreeInfo.recordFields(tree));
1097                 alreadyEntered = alreadyEntered.prependList(tree.defs.stream()
1098                         .filter(t -> TreeInfo.isConstructor(t) && t != defaultConstructor).collect(List.collector()));
1099             }
1100             List<JCTree> defsToEnter = isRecord ?
1101                     tree.defs.diff(alreadyEntered) : tree.defs;
1102             memberEnter.memberEnter(defsToEnter, env);
1103             if (isRecord) {
1104                 addRecordMembersIfNeeded(tree, env);
1105             }
1106             if (tree.sym.isAnnotationType()) {
1107                 Assert.check(tree.sym.isCompleted());
1108                 tree.sym.setAnnotationTypeMetadata(new AnnotationTypeMetadata(tree.sym, annotate.annotationTypeSourceCompleter()));
1109             }











1110         }
1111 












































1112         private void addAccessor(JCVariableDecl tree, Env<AttrContext> env) {
1113             MethodSymbol implSym = lookupMethod(env.enclClass.sym, tree.sym.name, List.nil());
1114             RecordComponent rec = ((ClassSymbol) tree.sym.owner).getRecordComponent(tree.sym);
1115             if (implSym == null || (implSym.flags_field & GENERATED_MEMBER) != 0) {
1116                 /* here we are pushing the annotations present in the corresponding field down to the accessor
1117                  * it could be that some of those annotations are not applicable to the accessor, they will be striped
1118                  * away later at Check::validateAnnotation
1119                  */
1120                 TreeCopier<JCTree> tc = new TreeCopier<JCTree>(make.at(tree.pos));
1121                 List<JCAnnotation> originalAnnos = rec.getOriginalAnnos().isEmpty() ?
1122                         rec.getOriginalAnnos() :
1123                         tc.copy(rec.getOriginalAnnos());
1124                 JCVariableDecl recordField = TreeInfo.recordFields((JCClassDecl) env.tree).stream().filter(rf -> rf.name == tree.name).findAny().get();
1125                 JCMethodDecl getter = make.at(tree.pos).
1126                         MethodDef(
1127                                 make.Modifiers(PUBLIC | Flags.GENERATED_MEMBER, originalAnnos),
1128                           tree.sym.name,
1129                           /* we need to special case for the case when the user declared the type as an ident
1130                            * if we don't do that then we can have issues if type annotations are applied to the
1131                            * return type: javac issues an error if a type annotation is applied to java.lang.String

1285 
1286         @Override
1287         public Type constructorType() {
1288             if (constructorType == null) {
1289                 constructorType = new MethodType(List.nil(), syms.voidType, List.nil(), syms.methodClass);
1290             }
1291             return constructorType;
1292         }
1293 
1294         @Override
1295         public MethodSymbol constructorSymbol() {
1296             if (constructorSymbol == null) {
1297                 long flags;
1298                 if ((owner().flags() & ENUM) != 0 &&
1299                     (types.supertype(owner().type).tsym == syms.enumSym)) {
1300                     // constructors of true enums are private
1301                     flags = PRIVATE | GENERATEDCONSTR;
1302                 } else {
1303                     flags = (owner().flags() & AccessFlags) | GENERATEDCONSTR;
1304                 }
1305                 constructorSymbol = new MethodSymbol(flags, names.init,

1306                     constructorType(), owner());
1307             }
1308             return constructorSymbol;
1309         }
1310 
1311         @Override
1312         public Type enclosingType() {
1313             return Type.noType;
1314     }
1315 
1316         @Override
1317         public TypeSymbol owner() {
1318             return owner;
1319         }
1320 
1321         @Override
1322         public List<Name> superArgs() {
1323             return List.nil();
1324             }
1325     }

  24  */
  25 
  26 package com.sun.tools.javac.comp;
  27 
  28 import java.util.ArrayList;
  29 import java.util.HashSet;
  30 import java.util.Set;
  31 import java.util.function.BiConsumer;
  32 import java.util.stream.Collectors;
  33 
  34 import javax.tools.JavaFileObject;
  35 
  36 import com.sun.tools.javac.code.*;
  37 import com.sun.tools.javac.code.Lint.LintCategory;
  38 import com.sun.tools.javac.code.Scope.ImportFilter;
  39 import com.sun.tools.javac.code.Scope.NamedImportScope;
  40 import com.sun.tools.javac.code.Scope.StarImportScope;
  41 import com.sun.tools.javac.code.Scope.WriteableScope;
  42 import com.sun.tools.javac.code.Source.Feature;
  43 import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata;
  44 import com.sun.tools.javac.jvm.Target;
  45 import com.sun.tools.javac.tree.*;
  46 import com.sun.tools.javac.util.*;
  47 import com.sun.tools.javac.util.DefinedBy.Api;
  48 
  49 import com.sun.tools.javac.code.Symbol.*;
  50 import com.sun.tools.javac.code.Type.*;
  51 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  52 import com.sun.tools.javac.tree.JCTree.*;
  53 
  54 import static com.sun.tools.javac.code.Flags.*;
  55 import static com.sun.tools.javac.code.Flags.ANNOTATION;
  56 import static com.sun.tools.javac.code.Flags.SYNCHRONIZED;
  57 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
  58 import static com.sun.tools.javac.code.Kinds.Kind.*;
  59 import static com.sun.tools.javac.code.TypeTag.CLASS;
  60 import static com.sun.tools.javac.code.TypeTag.ERROR;
  61 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  62 
  63 import static com.sun.tools.javac.code.TypeTag.*;
  64 import static com.sun.tools.javac.tree.JCTree.Tag.*;
  65 
  66 import com.sun.tools.javac.util.Dependencies.CompletionCause;
  67 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
  68 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  69 
  70 /** This is the second phase of Enter, in which classes are completed
  71  *  by resolving their headers and entering their members in the into
  72  *  the class scope. See Enter for an overall overview.
  73  *
  74  *  This class uses internal phases to process the classes. When a phase
  75  *  processes classes, the lower phases are not invoked until all classes
  76  *  pass through the current phase. Note that it is possible that upper phases

1092                 (types.supertype(tree.sym.type).tsym.flags() & Flags.ENUM) == 0) {
1093                 addEnumMembers(tree, env);
1094             }
1095             boolean isRecord = (tree.sym.flags_field & RECORD) != 0;
1096             List<JCTree> alreadyEntered = null;
1097             if (isRecord) {
1098                 alreadyEntered = List.convert(JCTree.class, TreeInfo.recordFields(tree));
1099                 alreadyEntered = alreadyEntered.prependList(tree.defs.stream()
1100                         .filter(t -> TreeInfo.isConstructor(t) && t != defaultConstructor).collect(List.collector()));
1101             }
1102             List<JCTree> defsToEnter = isRecord ?
1103                     tree.defs.diff(alreadyEntered) : tree.defs;
1104             memberEnter.memberEnter(defsToEnter, env);
1105             if (isRecord) {
1106                 addRecordMembersIfNeeded(tree, env);
1107             }
1108             if (tree.sym.isAnnotationType()) {
1109                 Assert.check(tree.sym.isCompleted());
1110                 tree.sym.setAnnotationTypeMetadata(new AnnotationTypeMetadata(tree.sym, annotate.annotationTypeSourceCompleter()));
1111             }
1112 
1113             if (tree.sym != syms.objectType.tsym && tree.sym != syms.recordType.tsym) {
1114                 if ((tree.sym.flags() & (ABSTRACT | INTERFACE | VALUE_CLASS)) == 0) {
1115                     tree.sym.flags_field |= IDENTITY_TYPE;
1116                 }
1117                 if ((tree.sym.flags() & (ABSTRACT | IDENTITY_TYPE | INTERFACE)) == ABSTRACT) {
1118                     if (abstractClassHasImplicitIdentity(tree)) {
1119                         tree.sym.flags_field |= IDENTITY_TYPE;
1120                     }
1121                 }
1122             }
1123         }
1124 
1125             // where
1126             private boolean abstractClassHasImplicitIdentity(JCClassDecl tree) {
1127 
1128                 Type t = tree.sym.type;
1129 
1130                 if (t == null || t.tsym == null || t.tsym.kind == ERR)
1131                     return false;
1132 
1133                 if ((t.tsym.flags() & HASINITBLOCK) != 0) {
1134                     return true;
1135                 }
1136 
1137                 // No instance fields and no arged constructors both mean inner classes cannot be value class supers.
1138                 Type encl = t.getEnclosingType();
1139                 if (encl != null && encl.hasTag(CLASS)) {
1140                     return true;
1141                 }
1142                 for (Symbol s : t.tsym.members().getSymbols(NON_RECURSIVE)) {
1143                     switch (s.kind) {
1144                         case VAR:
1145                             if ((s.flags() & STATIC) == 0) {
1146                                 return true;
1147                             }
1148                             break;
1149                         case MTH:
1150                             if ((s.flags() & (SYNCHRONIZED | STATIC)) == SYNCHRONIZED) {
1151                                 return true;
1152                             } else if (s.isInitOrVNew()) {
1153                                 MethodSymbol m = (MethodSymbol)s;
1154                                 if (m.getParameters().size() > 0
1155                                         || m.getTypeParameters().size() > 0
1156                                         || m.type.getThrownTypes().nonEmpty()
1157                                         || (m.flags() & EMPTYNOARGCONSTR) == 0
1158                                         || (Check.protection(m.flags()) > Check.protection(m.owner.flags()))) {
1159                                     return true;
1160                                 }
1161                             }
1162                             break;
1163                     }
1164                 }
1165                 return false;
1166             }
1167 
1168 
1169         private void addAccessor(JCVariableDecl tree, Env<AttrContext> env) {
1170             MethodSymbol implSym = lookupMethod(env.enclClass.sym, tree.sym.name, List.nil());
1171             RecordComponent rec = ((ClassSymbol) tree.sym.owner).getRecordComponent(tree.sym);
1172             if (implSym == null || (implSym.flags_field & GENERATED_MEMBER) != 0) {
1173                 /* here we are pushing the annotations present in the corresponding field down to the accessor
1174                  * it could be that some of those annotations are not applicable to the accessor, they will be striped
1175                  * away later at Check::validateAnnotation
1176                  */
1177                 TreeCopier<JCTree> tc = new TreeCopier<JCTree>(make.at(tree.pos));
1178                 List<JCAnnotation> originalAnnos = rec.getOriginalAnnos().isEmpty() ?
1179                         rec.getOriginalAnnos() :
1180                         tc.copy(rec.getOriginalAnnos());
1181                 JCVariableDecl recordField = TreeInfo.recordFields((JCClassDecl) env.tree).stream().filter(rf -> rf.name == tree.name).findAny().get();
1182                 JCMethodDecl getter = make.at(tree.pos).
1183                         MethodDef(
1184                                 make.Modifiers(PUBLIC | Flags.GENERATED_MEMBER, originalAnnos),
1185                           tree.sym.name,
1186                           /* we need to special case for the case when the user declared the type as an ident
1187                            * if we don't do that then we can have issues if type annotations are applied to the
1188                            * return type: javac issues an error if a type annotation is applied to java.lang.String

1342 
1343         @Override
1344         public Type constructorType() {
1345             if (constructorType == null) {
1346                 constructorType = new MethodType(List.nil(), syms.voidType, List.nil(), syms.methodClass);
1347             }
1348             return constructorType;
1349         }
1350 
1351         @Override
1352         public MethodSymbol constructorSymbol() {
1353             if (constructorSymbol == null) {
1354                 long flags;
1355                 if ((owner().flags() & ENUM) != 0 &&
1356                     (types.supertype(owner().type).tsym == syms.enumSym)) {
1357                     // constructors of true enums are private
1358                     flags = PRIVATE | GENERATEDCONSTR;
1359                 } else {
1360                     flags = (owner().flags() & AccessFlags) | GENERATEDCONSTR;
1361                 }
1362                 Name constructorName = owner().isConcreteValueClass() ? names.vnew : names.init;
1363                 constructorSymbol = new MethodSymbol(flags, constructorName,
1364                     constructorType(), owner());
1365             }
1366             return constructorSymbol;
1367         }
1368 
1369         @Override
1370         public Type enclosingType() {
1371             return Type.noType;
1372     }
1373 
1374         @Override
1375         public TypeSymbol owner() {
1376             return owner;
1377         }
1378 
1379         @Override
1380         public List<Name> superArgs() {
1381             return List.nil();
1382             }
1383     }
< prev index next >