< 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 

 732                     all_interfaces.append(modelMissingTypes(baseEnv, it, iface, true));
 733                 }
 734             }
 735 
 736             // Determine permits.
 737             ListBuffer<Symbol> permittedSubtypeSymbols = new ListBuffer<>();
 738             List<JCExpression> permittedTrees = tree.permitting;
 739             for (JCExpression permitted : permittedTrees) {
 740                 Type pt = attr.attribBase(permitted, baseEnv, false, false, false);
 741                 permittedSubtypeSymbols.append(pt.tsym);
 742             }
 743 
 744             if ((sym.flags_field & ANNOTATION) != 0) {
 745                 ct.interfaces_field = List.of(syms.annotationType);
 746                 ct.all_interfaces_field = ct.interfaces_field;
 747             }  else {
 748                 ct.interfaces_field = interfaces.toList();
 749                 ct.all_interfaces_field = (all_interfaces == null)
 750                         ? ct.interfaces_field : all_interfaces.toList();
 751             }
 752 
 753             /* it could be that there are already some symbols in the permitted list, for the case
 754              * where there are subtypes in the same compilation unit but the permits list is empty
 755              * so don't overwrite the permitted list if it is not empty
 756              */
 757             if (!permittedSubtypeSymbols.isEmpty()) {
 758                 sym.permitted = permittedSubtypeSymbols.toList();
 759             }
 760             sym.isPermittedExplicit = !permittedSubtypeSymbols.isEmpty();
 761         }
 762             //where:
 763             protected JCExpression clearTypeParams(JCExpression superType) {
 764                 return superType;
 765             }
 766     }
 767 
 768     private final class HierarchyPhase extends AbstractHeaderPhase implements Completer {
 769 
 770         public HierarchyPhase() {
 771             super(CompletionCause.HIERARCHY_PHASE, new PermitsPhase());
 772         }

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











1101         }
1102 
1103         private void addAccessor(JCVariableDecl tree, Env<AttrContext> env) {
1104             MethodSymbol implSym = lookupMethod(env.enclClass.sym, tree.sym.name, List.nil());
1105             RecordComponent rec = ((ClassSymbol) tree.sym.owner).getRecordComponent(tree.sym);
1106             if (implSym == null || (implSym.flags_field & GENERATED_MEMBER) != 0) {
1107                 /* here we are pushing the annotations present in the corresponding field down to the accessor
1108                  * it could be that some of those annotations are not applicable to the accessor, they will be striped
1109                  * away later at Check::validateAnnotation
1110                  */
1111                 TreeCopier<JCTree> tc = new TreeCopier<JCTree>(make.at(tree.pos));
1112                 List<JCAnnotation> originalAnnos = rec.getOriginalAnnos().isEmpty() ?
1113                         rec.getOriginalAnnos() :
1114                         tc.copy(rec.getOriginalAnnos());
1115                 JCVariableDecl recordField = TreeInfo.recordFields((JCClassDecl) env.tree).stream().filter(rf -> rf.name == tree.name).findAny().get();
1116                 JCMethodDecl getter = make.at(tree.pos).
1117                         MethodDef(
1118                                 make.Modifiers(PUBLIC | Flags.GENERATED_MEMBER, originalAnnos),
1119                           tree.sym.name,
1120                           /* we need to special case for the case when the user declared the type as an ident

  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.Scope.LookupKind.NON_RECURSIVE;
  57 import static com.sun.tools.javac.code.Kinds.Kind.*;
  58 import static com.sun.tools.javac.code.TypeTag.CLASS;
  59 import static com.sun.tools.javac.code.TypeTag.ERROR;
  60 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  61 
  62 import static com.sun.tools.javac.code.TypeTag.*;
  63 import static com.sun.tools.javac.tree.JCTree.Tag.*;
  64 

 733                     all_interfaces.append(modelMissingTypes(baseEnv, it, iface, true));
 734                 }
 735             }
 736 
 737             // Determine permits.
 738             ListBuffer<Symbol> permittedSubtypeSymbols = new ListBuffer<>();
 739             List<JCExpression> permittedTrees = tree.permitting;
 740             for (JCExpression permitted : permittedTrees) {
 741                 Type pt = attr.attribBase(permitted, baseEnv, false, false, false);
 742                 permittedSubtypeSymbols.append(pt.tsym);
 743             }
 744 
 745             if ((sym.flags_field & ANNOTATION) != 0) {
 746                 ct.interfaces_field = List.of(syms.annotationType);
 747                 ct.all_interfaces_field = ct.interfaces_field;
 748             }  else {
 749                 ct.interfaces_field = interfaces.toList();
 750                 ct.all_interfaces_field = (all_interfaces == null)
 751                         ? ct.interfaces_field : all_interfaces.toList();
 752             }

 753             /* it could be that there are already some symbols in the permitted list, for the case
 754              * where there are subtypes in the same compilation unit but the permits list is empty
 755              * so don't overwrite the permitted list if it is not empty
 756              */
 757             if (!permittedSubtypeSymbols.isEmpty()) {
 758                 sym.permitted = permittedSubtypeSymbols.toList();
 759             }
 760             sym.isPermittedExplicit = !permittedSubtypeSymbols.isEmpty();
 761         }
 762             //where:
 763             protected JCExpression clearTypeParams(JCExpression superType) {
 764                 return superType;
 765             }
 766     }
 767 
 768     private final class HierarchyPhase extends AbstractHeaderPhase implements Completer {
 769 
 770         public HierarchyPhase() {
 771             super(CompletionCause.HIERARCHY_PHASE, new PermitsPhase());
 772         }

1081                 (types.supertype(tree.sym.type).tsym.flags() & Flags.ENUM) == 0) {
1082                 addEnumMembers(tree, env);
1083             }
1084             boolean isRecord = (tree.sym.flags_field & RECORD) != 0;
1085             List<JCTree> alreadyEntered = null;
1086             if (isRecord) {
1087                 alreadyEntered = List.convert(JCTree.class, TreeInfo.recordFields(tree));
1088                 alreadyEntered = alreadyEntered.prependList(tree.defs.stream()
1089                         .filter(t -> TreeInfo.isConstructor(t) && t != defaultConstructor).collect(List.collector()));
1090             }
1091             List<JCTree> defsToEnter = isRecord ?
1092                     tree.defs.diff(alreadyEntered) : tree.defs;
1093             memberEnter.memberEnter(defsToEnter, env);
1094             if (isRecord) {
1095                 addRecordMembersIfNeeded(tree, env);
1096             }
1097             if (tree.sym.isAnnotationType()) {
1098                 Assert.check(tree.sym.isCompleted());
1099                 tree.sym.setAnnotationTypeMetadata(new AnnotationTypeMetadata(tree.sym, annotate.annotationTypeSourceCompleter()));
1100             }
1101 
1102             if (tree.sym != syms.objectType.tsym) {
1103                 if ((tree.sym.flags() & (ABSTRACT | INTERFACE | VALUE_CLASS)) == 0) {
1104                     tree.sym.flags_field |= IDENTITY_TYPE;
1105                 }
1106                 if ((tree.sym.flags() & (ABSTRACT | INTERFACE)) == ABSTRACT) {
1107                     if (types.isIdentityType(tree.sym.type)) {
1108                         tree.sym.flags_field |= IDENTITY_TYPE;
1109                     }
1110                 }
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
< prev index next >