< 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

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 












































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

  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

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