516 if ((tree.mods.flags & DEFAULT) != 0) { |
516 if ((tree.mods.flags & DEFAULT) != 0) { |
517 m.enclClass().flags_field |= DEFAULT; |
517 m.enclClass().flags_field |= DEFAULT; |
518 } |
518 } |
519 |
519 |
520 Env<AttrContext> localEnv = methodEnv(tree, env); |
520 Env<AttrContext> localEnv = methodEnv(tree, env); |
521 |
521 DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos()); |
522 annotate.enterStart(); |
|
523 try { |
522 try { |
524 DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos()); |
523 // Compute the method type |
525 try { |
524 m.type = signature(m, tree.typarams, tree.params, |
526 // Compute the method type |
525 tree.restype, tree.recvparam, |
527 m.type = signature(m, tree.typarams, tree.params, |
526 tree.thrown, |
528 tree.restype, tree.recvparam, |
527 localEnv); |
529 tree.thrown, |
|
530 localEnv); |
|
531 } finally { |
|
532 deferredLintHandler.setPos(prevLintPos); |
|
533 } |
|
534 |
|
535 if (types.isSignaturePolymorphic(m)) { |
|
536 m.flags_field |= SIGNATURE_POLYMORPHIC; |
|
537 } |
|
538 |
|
539 // Set m.params |
|
540 ListBuffer<VarSymbol> params = new ListBuffer<>(); |
|
541 JCVariableDecl lastParam = null; |
|
542 for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) { |
|
543 JCVariableDecl param = lastParam = l.head; |
|
544 params.append(Assert.checkNonNull(param.sym)); |
|
545 } |
|
546 m.params = params.toList(); |
|
547 |
|
548 // mark the method varargs, if necessary |
|
549 if (lastParam != null && (lastParam.mods.flags & Flags.VARARGS) != 0) |
|
550 m.flags_field |= Flags.VARARGS; |
|
551 |
|
552 localEnv.info.scope.leave(); |
|
553 if (chk.checkUnique(tree.pos(), m, enclScope)) { |
|
554 enclScope.enter(m); |
|
555 } |
|
556 |
|
557 annotate.annotateLater(tree.mods.annotations, localEnv, m, tree.pos()); |
|
558 // Visit the signature of the method. Note that |
|
559 // TypeAnnotate doesn't descend into the body. |
|
560 annotate.annotateTypeLater(tree, localEnv, m, tree.pos()); |
|
561 |
|
562 if (tree.defaultValue != null) |
|
563 annotateDefaultValueLater(tree.defaultValue, localEnv, m); |
|
564 } finally { |
528 } finally { |
565 annotate.enterDone(); |
529 deferredLintHandler.setPos(prevLintPos); |
566 } |
530 } |
|
531 |
|
532 if (types.isSignaturePolymorphic(m)) { |
|
533 m.flags_field |= SIGNATURE_POLYMORPHIC; |
|
534 } |
|
535 |
|
536 // Set m.params |
|
537 ListBuffer<VarSymbol> params = new ListBuffer<>(); |
|
538 JCVariableDecl lastParam = null; |
|
539 for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) { |
|
540 JCVariableDecl param = lastParam = l.head; |
|
541 params.append(Assert.checkNonNull(param.sym)); |
|
542 } |
|
543 m.params = params.toList(); |
|
544 |
|
545 // mark the method varargs, if necessary |
|
546 if (lastParam != null && (lastParam.mods.flags & Flags.VARARGS) != 0) |
|
547 m.flags_field |= Flags.VARARGS; |
|
548 |
|
549 localEnv.info.scope.leave(); |
|
550 if (chk.checkUnique(tree.pos(), m, enclScope)) { |
|
551 enclScope.enter(m); |
|
552 } |
|
553 |
|
554 annotate.annotateLater(tree.mods.annotations, localEnv, m, tree.pos()); |
|
555 // Visit the signature of the method. Note that |
|
556 // TypeAnnotate doesn't descend into the body. |
|
557 annotate.annotateTypeLater(tree, localEnv, m, tree.pos()); |
|
558 |
|
559 if (tree.defaultValue != null) |
|
560 annotateDefaultValueLater(tree.defaultValue, localEnv, m); |
567 } |
561 } |
568 |
562 |
569 /** Create a fresh environment for method bodies. |
563 /** Create a fresh environment for method bodies. |
570 * @param tree The method definition. |
564 * @param tree The method definition. |
571 * @param env The environment current outside of the method definition. |
565 * @param env The environment current outside of the method definition. |
589 (env.info.scope.owner.flags() & INTERFACE) != 0) { |
583 (env.info.scope.owner.flags() & INTERFACE) != 0) { |
590 localEnv = env.dup(tree, env.info.dup()); |
584 localEnv = env.dup(tree, env.info.dup()); |
591 localEnv.info.staticLevel++; |
585 localEnv.info.staticLevel++; |
592 } |
586 } |
593 DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos()); |
587 DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos()); |
594 annotate.enterStart(); |
|
595 try { |
588 try { |
596 try { |
589 if (TreeInfo.isEnumInit(tree)) { |
597 if (TreeInfo.isEnumInit(tree)) { |
590 attr.attribIdentAsEnumType(localEnv, (JCIdent)tree.vartype); |
598 attr.attribIdentAsEnumType(localEnv, (JCIdent)tree.vartype); |
591 } else { |
599 } else { |
592 attr.attribType(tree.vartype, localEnv); |
600 attr.attribType(tree.vartype, localEnv); |
593 if (TreeInfo.isReceiverParam(tree)) |
601 if (TreeInfo.isReceiverParam(tree)) |
594 checkReceiver(tree, localEnv); |
602 checkReceiver(tree, localEnv); |
595 } |
603 } |
|
604 } finally { |
|
605 deferredLintHandler.setPos(prevLintPos); |
|
606 } |
|
607 |
|
608 if ((tree.mods.flags & VARARGS) != 0) { |
|
609 //if we are entering a varargs parameter, we need to |
|
610 //replace its type (a plain array type) with the more |
|
611 //precise VarargsType --- we need to do it this way |
|
612 //because varargs is represented in the tree as a |
|
613 //modifier on the parameter declaration, and not as a |
|
614 //distinct type of array node. |
|
615 ArrayType atype = (ArrayType)tree.vartype.type; |
|
616 tree.vartype.type = atype.makeVarargs(); |
|
617 } |
|
618 WriteableScope enclScope = enter.enterScope(env); |
|
619 VarSymbol v = |
|
620 new VarSymbol(0, tree.name, tree.vartype.type, enclScope.owner); |
|
621 v.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, v, tree); |
|
622 tree.sym = v; |
|
623 if (tree.init != null) { |
|
624 v.flags_field |= HASINIT; |
|
625 if ((v.flags_field & FINAL) != 0 && |
|
626 needsLazyConstValue(tree.init)) { |
|
627 Env<AttrContext> initEnv = getInitEnv(tree, env); |
|
628 initEnv.info.enclVar = v; |
|
629 v.setLazyConstValue(initEnv(tree, initEnv), attr, tree); |
|
630 } |
|
631 } |
|
632 if (chk.checkUnique(tree.pos(), v, enclScope)) { |
|
633 chk.checkTransparentVar(tree.pos(), v, enclScope); |
|
634 enclScope.enter(v); |
|
635 } |
|
636 |
|
637 annotate.annotateLater(tree.mods.annotations, localEnv, v, tree.pos()); |
|
638 annotate.annotateTypeLater(tree.vartype, localEnv, v, tree.pos()); |
|
639 |
|
640 v.pos = tree.pos; |
|
641 } finally { |
596 } finally { |
642 annotate.enterDone(); |
597 deferredLintHandler.setPos(prevLintPos); |
643 } |
598 } |
|
599 |
|
600 if ((tree.mods.flags & VARARGS) != 0) { |
|
601 //if we are entering a varargs parameter, we need to |
|
602 //replace its type (a plain array type) with the more |
|
603 //precise VarargsType --- we need to do it this way |
|
604 //because varargs is represented in the tree as a |
|
605 //modifier on the parameter declaration, and not as a |
|
606 //distinct type of array node. |
|
607 ArrayType atype = (ArrayType)tree.vartype.type; |
|
608 tree.vartype.type = atype.makeVarargs(); |
|
609 } |
|
610 WriteableScope enclScope = enter.enterScope(env); |
|
611 VarSymbol v = |
|
612 new VarSymbol(0, tree.name, tree.vartype.type, enclScope.owner); |
|
613 v.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, v, tree); |
|
614 tree.sym = v; |
|
615 if (tree.init != null) { |
|
616 v.flags_field |= HASINIT; |
|
617 if ((v.flags_field & FINAL) != 0 && |
|
618 needsLazyConstValue(tree.init)) { |
|
619 Env<AttrContext> initEnv = getInitEnv(tree, env); |
|
620 initEnv.info.enclVar = v; |
|
621 v.setLazyConstValue(initEnv(tree, initEnv), attr, tree); |
|
622 } |
|
623 } |
|
624 if (chk.checkUnique(tree.pos(), v, enclScope)) { |
|
625 chk.checkTransparentVar(tree.pos(), v, enclScope); |
|
626 enclScope.enter(v); |
|
627 } |
|
628 |
|
629 annotate.annotateLater(tree.mods.annotations, localEnv, v, tree.pos()); |
|
630 annotate.annotateTypeLater(tree.vartype, localEnv, v, tree.pos()); |
|
631 |
|
632 v.pos = tree.pos; |
644 } |
633 } |
645 // where |
634 // where |
646 void checkType(JCTree tree, Type type, String diag) { |
635 void checkType(JCTree tree, Type type, String diag) { |
647 if (!tree.type.isErroneous() && !types.isSameType(tree.type, type)) { |
636 if (!tree.type.isErroneous() && !types.isSameType(tree.type, type)) { |
648 log.error(tree, diag, type, tree.type); |
637 log.error(tree, diag, type, tree.type); |
864 Assert.check((sym.flags() & Flags.COMPOUND) == 0); |
853 Assert.check((sym.flags() & Flags.COMPOUND) == 0); |
865 sym.completer = this; |
854 sym.completer = this; |
866 return; |
855 return; |
867 } |
856 } |
868 |
857 |
869 ClassSymbol c = (ClassSymbol)sym; |
|
870 ClassType ct = (ClassType)c.type; |
|
871 Env<AttrContext> env = typeEnvs.get(c); |
|
872 JCClassDecl tree = (JCClassDecl)env.tree; |
|
873 boolean wasFirst = isFirst; |
|
874 isFirst = false; |
|
875 |
|
876 JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
|
877 DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos()); |
|
878 try { |
858 try { |
879 dependencies.push(c); |
859 annotate.enterStart(); |
880 |
860 |
881 // Save class environment for later member enter (2) processing. |
861 ClassSymbol c = (ClassSymbol)sym; |
882 halfcompleted.append(env); |
862 ClassType ct = (ClassType)c.type; |
883 |
863 Env<AttrContext> env = typeEnvs.get(c); |
884 // Mark class as not yet attributed. |
864 JCClassDecl tree = (JCClassDecl)env.tree; |
885 c.flags_field |= UNATTRIBUTED; |
865 boolean wasFirst = isFirst; |
886 |
866 isFirst = false; |
887 // If this is a toplevel-class, make sure any preceding import |
867 |
888 // clauses have been seen. |
868 JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
889 if (c.owner.kind == PCK) { |
869 DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos()); |
890 memberEnter(env.toplevel, env.enclosing(TOPLEVEL)); |
870 try { |
891 todo.append(env); |
871 dependencies.push(c); |
892 } |
872 |
893 |
873 // Save class environment for later member enter (2) processing. |
894 if (c.owner.kind == TYP) |
874 halfcompleted.append(env); |
895 c.owner.complete(); |
875 |
896 |
876 // Mark class as not yet attributed. |
897 // create an environment for evaluating the base clauses |
877 c.flags_field |= UNATTRIBUTED; |
898 Env<AttrContext> baseEnv = baseEnv(tree, env); |
878 |
899 |
879 // If this is a toplevel-class, make sure any preceding import |
900 if (tree.extending != null) |
880 // clauses have been seen. |
901 annotate.annotateTypeLater(tree.extending, baseEnv, sym, tree.pos()); |
881 if (c.owner.kind == PCK) { |
902 for (JCExpression impl : tree.implementing) |
882 memberEnter(env.toplevel, env.enclosing(TOPLEVEL)); |
903 annotate.annotateTypeLater(impl, baseEnv, sym, tree.pos()); |
883 todo.append(env); |
904 annotate.flush(); |
884 } |
905 |
885 |
906 // Determine supertype. |
886 if (c.owner.kind == TYP) |
907 Type supertype; |
887 c.owner.complete(); |
908 if (tree.extending != null) { |
888 |
909 dependencies.push(AttributionKind.EXTENDS, tree.extending); |
889 // create an environment for evaluating the base clauses |
|
890 Env<AttrContext> baseEnv = baseEnv(tree, env); |
|
891 |
|
892 if (tree.extending != null) |
|
893 annotate.annotateTypeLater(tree.extending, baseEnv, sym, tree.pos()); |
|
894 for (JCExpression impl : tree.implementing) |
|
895 annotate.annotateTypeLater(impl, baseEnv, sym, tree.pos()); |
|
896 annotate.flush(); |
|
897 |
|
898 // Determine supertype. |
|
899 Type supertype; |
|
900 if (tree.extending != null) { |
|
901 dependencies.push(AttributionKind.EXTENDS, tree.extending); |
|
902 try { |
|
903 supertype = attr.attribBase(tree.extending, baseEnv, |
|
904 true, false, true); |
|
905 } finally { |
|
906 dependencies.pop(); |
|
907 } |
|
908 } else { |
|
909 supertype = ((tree.mods.flags & Flags.ENUM) != 0) |
|
910 ? attr.attribBase(enumBase(tree.pos, c), baseEnv, |
|
911 true, false, false) |
|
912 : (c.fullname == names.java_lang_Object) |
|
913 ? Type.noType |
|
914 : syms.objectType; |
|
915 } |
|
916 ct.supertype_field = modelMissingTypes(supertype, tree.extending, false); |
|
917 |
|
918 // Determine interfaces. |
|
919 ListBuffer<Type> interfaces = new ListBuffer<>(); |
|
920 ListBuffer<Type> all_interfaces = null; // lazy init |
|
921 Set<Type> interfaceSet = new HashSet<>(); |
|
922 List<JCExpression> interfaceTrees = tree.implementing; |
|
923 for (JCExpression iface : interfaceTrees) { |
|
924 dependencies.push(AttributionKind.IMPLEMENTS, iface); |
|
925 try { |
|
926 Type it = attr.attribBase(iface, baseEnv, false, true, true); |
|
927 if (it.hasTag(CLASS)) { |
|
928 interfaces.append(it); |
|
929 if (all_interfaces != null) all_interfaces.append(it); |
|
930 chk.checkNotRepeated(iface.pos(), types.erasure(it), interfaceSet); |
|
931 } else { |
|
932 if (all_interfaces == null) |
|
933 all_interfaces = new ListBuffer<Type>().appendList(interfaces); |
|
934 all_interfaces.append(modelMissingTypes(it, iface, true)); |
|
935 } |
|
936 } finally { |
|
937 dependencies.pop(); |
|
938 } |
|
939 } |
|
940 |
|
941 if ((c.flags_field & ANNOTATION) != 0) { |
|
942 ct.interfaces_field = List.of(syms.annotationType); |
|
943 ct.all_interfaces_field = ct.interfaces_field; |
|
944 } else { |
|
945 ct.interfaces_field = interfaces.toList(); |
|
946 ct.all_interfaces_field = (all_interfaces == null) |
|
947 ? ct.interfaces_field : all_interfaces.toList(); |
|
948 } |
|
949 |
|
950 if (c.fullname == names.java_lang_Object) { |
|
951 if (tree.extending != null) { |
|
952 chk.checkNonCyclic(tree.extending.pos(), |
|
953 supertype); |
|
954 ct.supertype_field = Type.noType; |
|
955 } |
|
956 else if (tree.implementing.nonEmpty()) { |
|
957 chk.checkNonCyclic(tree.implementing.head.pos(), |
|
958 ct.interfaces_field.head); |
|
959 ct.interfaces_field = List.nil(); |
|
960 } |
|
961 } |
|
962 |
|
963 // Annotations. |
|
964 // In general, we cannot fully process annotations yet, but we |
|
965 // can attribute the annotation types and then check to see if the |
|
966 // @Deprecated annotation is present. |
|
967 attr.attribAnnotationTypes(tree.mods.annotations, baseEnv); |
|
968 if (hasDeprecatedAnnotation(tree.mods.annotations)) |
|
969 c.flags_field |= DEPRECATED; |
|
970 annotate.annotateLater(tree.mods.annotations, baseEnv, |
|
971 c, tree.pos()); |
|
972 |
|
973 chk.checkNonCyclicDecl(tree); |
|
974 |
|
975 // class type parameters use baseEnv but everything uses env |
|
976 attr.attribTypeVariables(tree.typarams, baseEnv); |
|
977 for (JCTypeParameter tp : tree.typarams) |
|
978 annotate.annotateTypeLater(tp, baseEnv, sym, tree.pos()); |
|
979 |
|
980 // Add default constructor if needed. |
|
981 if ((c.flags() & INTERFACE) == 0 && |
|
982 !TreeInfo.hasConstructors(tree.defs)) { |
|
983 List<Type> argtypes = List.nil(); |
|
984 List<Type> typarams = List.nil(); |
|
985 List<Type> thrown = List.nil(); |
|
986 long ctorFlags = 0; |
|
987 boolean based = false; |
|
988 boolean addConstructor = true; |
|
989 JCNewClass nc = null; |
|
990 if (c.name.isEmpty()) { |
|
991 nc = (JCNewClass)env.next.tree; |
|
992 if (nc.constructor != null) { |
|
993 addConstructor = nc.constructor.kind != ERR; |
|
994 Type superConstrType = types.memberType(c.type, |
|
995 nc.constructor); |
|
996 argtypes = superConstrType.getParameterTypes(); |
|
997 typarams = superConstrType.getTypeArguments(); |
|
998 ctorFlags = nc.constructor.flags() & VARARGS; |
|
999 if (nc.encl != null) { |
|
1000 argtypes = argtypes.prepend(nc.encl.type); |
|
1001 based = true; |
|
1002 } |
|
1003 thrown = superConstrType.getThrownTypes(); |
|
1004 } |
|
1005 } |
|
1006 if (addConstructor) { |
|
1007 MethodSymbol basedConstructor = nc != null ? |
|
1008 (MethodSymbol)nc.constructor : null; |
|
1009 JCTree constrDef = DefaultConstructor(make.at(tree.pos), c, |
|
1010 basedConstructor, |
|
1011 typarams, argtypes, thrown, |
|
1012 ctorFlags, based); |
|
1013 tree.defs = tree.defs.prepend(constrDef); |
|
1014 } |
|
1015 } |
|
1016 |
|
1017 // enter symbols for 'this' into current scope. |
|
1018 VarSymbol thisSym = |
|
1019 new VarSymbol(FINAL | HASINIT, names._this, c.type, c); |
|
1020 thisSym.pos = Position.FIRSTPOS; |
|
1021 env.info.scope.enter(thisSym); |
|
1022 // if this is a class, enter symbol for 'super' into current scope. |
|
1023 if ((c.flags_field & INTERFACE) == 0 && |
|
1024 ct.supertype_field.hasTag(CLASS)) { |
|
1025 VarSymbol superSym = |
|
1026 new VarSymbol(FINAL | HASINIT, names._super, |
|
1027 ct.supertype_field, c); |
|
1028 superSym.pos = Position.FIRSTPOS; |
|
1029 env.info.scope.enter(superSym); |
|
1030 } |
|
1031 |
|
1032 // check that no package exists with same fully qualified name, |
|
1033 // but admit classes in the unnamed package which have the same |
|
1034 // name as a top-level package. |
|
1035 if (checkClash && |
|
1036 c.owner.kind == PCK && c.owner != syms.unnamedPackage && |
|
1037 syms.packageExists(c.fullname)) { |
|
1038 log.error(tree.pos, "clash.with.pkg.of.same.name", Kinds.kindName(sym), c); |
|
1039 } |
|
1040 if (c.owner.kind == PCK && (c.flags_field & PUBLIC) == 0 && |
|
1041 !env.toplevel.sourcefile.isNameCompatible(c.name.toString(),JavaFileObject.Kind.SOURCE)) { |
|
1042 c.flags_field |= AUXILIARY; |
|
1043 } |
|
1044 } catch (CompletionFailure ex) { |
|
1045 chk.completionError(tree.pos(), ex); |
|
1046 } finally { |
|
1047 deferredLintHandler.setPos(prevLintPos); |
|
1048 log.useSource(prev); |
|
1049 dependencies.pop(); |
|
1050 } |
|
1051 |
|
1052 // Enter all member fields and methods of a set of half completed |
|
1053 // classes in a second phase. |
|
1054 if (wasFirst) { |
|
1055 Set<JCCompilationUnit> topLevels = new HashSet<>(); |
910 try { |
1056 try { |
911 supertype = attr.attribBase(tree.extending, baseEnv, |
1057 while (halfcompleted.nonEmpty()) { |
912 true, false, true); |
1058 Env<AttrContext> toFinish = halfcompleted.next(); |
913 } finally { |
1059 topLevels.add(toFinish.toplevel); |
914 dependencies.pop(); |
1060 finish(toFinish); |
915 } |
1061 if (allowTypeAnnos) { |
916 } else { |
1062 typeAnnotations.organizeTypeAnnotationsSignatures(toFinish, (JCClassDecl)toFinish.tree); |
917 supertype = ((tree.mods.flags & Flags.ENUM) != 0) |
1063 typeAnnotations.validateTypeAnnotationsSignatures(toFinish, (JCClassDecl)toFinish.tree); |
918 ? attr.attribBase(enumBase(tree.pos, c), baseEnv, |
1064 } |
919 true, false, false) |
|
920 : (c.fullname == names.java_lang_Object) |
|
921 ? Type.noType |
|
922 : syms.objectType; |
|
923 } |
|
924 ct.supertype_field = modelMissingTypes(supertype, tree.extending, false); |
|
925 |
|
926 // Determine interfaces. |
|
927 ListBuffer<Type> interfaces = new ListBuffer<>(); |
|
928 ListBuffer<Type> all_interfaces = null; // lazy init |
|
929 Set<Type> interfaceSet = new HashSet<>(); |
|
930 List<JCExpression> interfaceTrees = tree.implementing; |
|
931 for (JCExpression iface : interfaceTrees) { |
|
932 dependencies.push(AttributionKind.IMPLEMENTS, iface); |
|
933 try { |
|
934 Type it = attr.attribBase(iface, baseEnv, false, true, true); |
|
935 if (it.hasTag(CLASS)) { |
|
936 interfaces.append(it); |
|
937 if (all_interfaces != null) all_interfaces.append(it); |
|
938 chk.checkNotRepeated(iface.pos(), types.erasure(it), interfaceSet); |
|
939 } else { |
|
940 if (all_interfaces == null) |
|
941 all_interfaces = new ListBuffer<Type>().appendList(interfaces); |
|
942 all_interfaces.append(modelMissingTypes(it, iface, true)); |
|
943 } |
1065 } |
944 } finally { |
1066 } finally { |
945 dependencies.pop(); |
1067 isFirst = true; |
946 } |
1068 } |
947 } |
1069 |
948 |
1070 for (JCCompilationUnit toplevel : topLevels) { |
949 if ((c.flags_field & ANNOTATION) != 0) { |
1071 chk.checkImportsResolvable(toplevel); |
950 ct.interfaces_field = List.of(syms.annotationType); |
1072 } |
951 ct.all_interfaces_field = ct.interfaces_field; |
1073 |
952 } else { |
1074 } |
953 ct.interfaces_field = interfaces.toList(); |
|
954 ct.all_interfaces_field = (all_interfaces == null) |
|
955 ? ct.interfaces_field : all_interfaces.toList(); |
|
956 } |
|
957 |
|
958 if (c.fullname == names.java_lang_Object) { |
|
959 if (tree.extending != null) { |
|
960 chk.checkNonCyclic(tree.extending.pos(), |
|
961 supertype); |
|
962 ct.supertype_field = Type.noType; |
|
963 } |
|
964 else if (tree.implementing.nonEmpty()) { |
|
965 chk.checkNonCyclic(tree.implementing.head.pos(), |
|
966 ct.interfaces_field.head); |
|
967 ct.interfaces_field = List.nil(); |
|
968 } |
|
969 } |
|
970 |
|
971 // Annotations. |
|
972 // In general, we cannot fully process annotations yet, but we |
|
973 // can attribute the annotation types and then check to see if the |
|
974 // @Deprecated annotation is present. |
|
975 attr.attribAnnotationTypes(tree.mods.annotations, baseEnv); |
|
976 if (hasDeprecatedAnnotation(tree.mods.annotations)) |
|
977 c.flags_field |= DEPRECATED; |
|
978 annotate.annotateLater(tree.mods.annotations, baseEnv, |
|
979 c, tree.pos()); |
|
980 |
|
981 chk.checkNonCyclicDecl(tree); |
|
982 |
|
983 // class type parameters use baseEnv but everything uses env |
|
984 attr.attribTypeVariables(tree.typarams, baseEnv); |
|
985 for (JCTypeParameter tp : tree.typarams) |
|
986 annotate.annotateTypeLater(tp, baseEnv, sym, tree.pos()); |
|
987 |
|
988 // Add default constructor if needed. |
|
989 if ((c.flags() & INTERFACE) == 0 && |
|
990 !TreeInfo.hasConstructors(tree.defs)) { |
|
991 List<Type> argtypes = List.nil(); |
|
992 List<Type> typarams = List.nil(); |
|
993 List<Type> thrown = List.nil(); |
|
994 long ctorFlags = 0; |
|
995 boolean based = false; |
|
996 boolean addConstructor = true; |
|
997 JCNewClass nc = null; |
|
998 if (c.name.isEmpty()) { |
|
999 nc = (JCNewClass)env.next.tree; |
|
1000 if (nc.constructor != null) { |
|
1001 addConstructor = nc.constructor.kind != ERR; |
|
1002 Type superConstrType = types.memberType(c.type, |
|
1003 nc.constructor); |
|
1004 argtypes = superConstrType.getParameterTypes(); |
|
1005 typarams = superConstrType.getTypeArguments(); |
|
1006 ctorFlags = nc.constructor.flags() & VARARGS; |
|
1007 if (nc.encl != null) { |
|
1008 argtypes = argtypes.prepend(nc.encl.type); |
|
1009 based = true; |
|
1010 } |
|
1011 thrown = superConstrType.getThrownTypes(); |
|
1012 } |
|
1013 } |
|
1014 if (addConstructor) { |
|
1015 MethodSymbol basedConstructor = nc != null ? |
|
1016 (MethodSymbol)nc.constructor : null; |
|
1017 JCTree constrDef = DefaultConstructor(make.at(tree.pos), c, |
|
1018 basedConstructor, |
|
1019 typarams, argtypes, thrown, |
|
1020 ctorFlags, based); |
|
1021 tree.defs = tree.defs.prepend(constrDef); |
|
1022 } |
|
1023 } |
|
1024 |
|
1025 // enter symbols for 'this' into current scope. |
|
1026 VarSymbol thisSym = |
|
1027 new VarSymbol(FINAL | HASINIT, names._this, c.type, c); |
|
1028 thisSym.pos = Position.FIRSTPOS; |
|
1029 env.info.scope.enter(thisSym); |
|
1030 // if this is a class, enter symbol for 'super' into current scope. |
|
1031 if ((c.flags_field & INTERFACE) == 0 && |
|
1032 ct.supertype_field.hasTag(CLASS)) { |
|
1033 VarSymbol superSym = |
|
1034 new VarSymbol(FINAL | HASINIT, names._super, |
|
1035 ct.supertype_field, c); |
|
1036 superSym.pos = Position.FIRSTPOS; |
|
1037 env.info.scope.enter(superSym); |
|
1038 } |
|
1039 |
|
1040 // check that no package exists with same fully qualified name, |
|
1041 // but admit classes in the unnamed package which have the same |
|
1042 // name as a top-level package. |
|
1043 if (checkClash && |
|
1044 c.owner.kind == PCK && c.owner != syms.unnamedPackage && |
|
1045 syms.packageExists(c.fullname)) { |
|
1046 log.error(tree.pos, "clash.with.pkg.of.same.name", Kinds.kindName(sym), c); |
|
1047 } |
|
1048 if (c.owner.kind == PCK && (c.flags_field & PUBLIC) == 0 && |
|
1049 !env.toplevel.sourcefile.isNameCompatible(c.name.toString(),JavaFileObject.Kind.SOURCE)) { |
|
1050 c.flags_field |= AUXILIARY; |
|
1051 } |
|
1052 } catch (CompletionFailure ex) { |
|
1053 chk.completionError(tree.pos(), ex); |
|
1054 } finally { |
1075 } finally { |
1055 deferredLintHandler.setPos(prevLintPos); |
1076 annotate.enterDone(); |
1056 log.useSource(prev); |
|
1057 dependencies.pop(); |
|
1058 } |
|
1059 |
|
1060 // Enter all member fields and methods of a set of half completed |
|
1061 // classes in a second phase. |
|
1062 if (wasFirst) { |
|
1063 Set<JCCompilationUnit> topLevels = new HashSet<>(); |
|
1064 try { |
|
1065 while (halfcompleted.nonEmpty()) { |
|
1066 Env<AttrContext> toFinish = halfcompleted.next(); |
|
1067 topLevels.add(toFinish.toplevel); |
|
1068 finish(toFinish); |
|
1069 if (allowTypeAnnos) { |
|
1070 typeAnnotations.organizeTypeAnnotationsSignatures(toFinish, (JCClassDecl)toFinish.tree); |
|
1071 typeAnnotations.validateTypeAnnotationsSignatures(toFinish, (JCClassDecl)toFinish.tree); |
|
1072 } |
|
1073 } |
|
1074 } finally { |
|
1075 isFirst = true; |
|
1076 } |
|
1077 |
|
1078 for (JCCompilationUnit toplevel : topLevels) { |
|
1079 chk.checkImportsResolvable(toplevel); |
|
1080 } |
|
1081 |
|
1082 } |
1077 } |
1083 } |
1078 } |
1084 |
1079 |
1085 private Env<AttrContext> baseEnv(JCClassDecl tree, Env<AttrContext> env) { |
1080 private Env<AttrContext> baseEnv(JCClassDecl tree, Env<AttrContext> env) { |
1086 WriteableScope baseScope = WriteableScope.create(tree.sym); |
1081 WriteableScope baseScope = WriteableScope.create(tree.sym); |