29 import java.util.HashSet; |
29 import java.util.HashSet; |
30 import java.util.LinkedHashMap; |
30 import java.util.LinkedHashMap; |
31 import java.util.Map; |
31 import java.util.Map; |
32 import java.util.Set; |
32 import java.util.Set; |
33 |
33 |
34 import javax.lang.model.type.TypeKind; |
|
35 import javax.tools.JavaFileObject; |
34 import javax.tools.JavaFileObject; |
36 |
35 |
37 import com.sun.tools.javac.code.*; |
36 import com.sun.tools.javac.code.*; |
38 import com.sun.tools.javac.jvm.*; |
37 import com.sun.tools.javac.jvm.*; |
39 import com.sun.tools.javac.tree.*; |
38 import com.sun.tools.javac.tree.*; |
615 chk.setDeferredLintHandler(deferredLintHandler.setPos(tree.pos())); |
614 chk.setDeferredLintHandler(deferredLintHandler.setPos(tree.pos())); |
616 try { |
615 try { |
617 if (TreeInfo.isEnumInit(tree)) { |
616 if (TreeInfo.isEnumInit(tree)) { |
618 attr.attribIdentAsEnumType(localEnv, (JCIdent)tree.vartype); |
617 attr.attribIdentAsEnumType(localEnv, (JCIdent)tree.vartype); |
619 } else { |
618 } else { |
|
619 // Make sure type annotations are processed. |
|
620 // But we don't have a symbol to attach them to yet - use null. |
|
621 typeAnnotate(tree.vartype, env, null); |
620 attr.attribType(tree.vartype, localEnv); |
622 attr.attribType(tree.vartype, localEnv); |
|
623 if (tree.nameexpr != null) { |
|
624 attr.attribExpr(tree.nameexpr, localEnv); |
|
625 MethodSymbol m = localEnv.enclMethod.sym; |
|
626 if (m.isConstructor()) { |
|
627 Type outertype = m.owner.owner.type; |
|
628 if (outertype.hasTag(TypeTag.CLASS)) { |
|
629 checkType(tree.vartype, outertype, "incorrect.constructor.receiver.type"); |
|
630 checkType(tree.nameexpr, outertype, "incorrect.constructor.receiver.name"); |
|
631 } else { |
|
632 log.error(tree, "receiver.parameter.not.applicable.constructor.toplevel.class"); |
|
633 } |
|
634 } else { |
|
635 checkType(tree.vartype, m.owner.type, "incorrect.receiver.type"); |
|
636 checkType(tree.nameexpr, m.owner.type, "incorrect.receiver.name"); |
|
637 } |
|
638 } |
621 } |
639 } |
622 } finally { |
640 } finally { |
623 chk.setDeferredLintHandler(prevLintHandler); |
641 chk.setDeferredLintHandler(prevLintHandler); |
624 } |
642 } |
625 |
643 |
649 if (chk.checkUnique(tree.pos(), v, enclScope)) { |
667 if (chk.checkUnique(tree.pos(), v, enclScope)) { |
650 chk.checkTransparentVar(tree.pos(), v, enclScope); |
668 chk.checkTransparentVar(tree.pos(), v, enclScope); |
651 enclScope.enter(v); |
669 enclScope.enter(v); |
652 } |
670 } |
653 annotateLater(tree.mods.annotations, localEnv, v); |
671 annotateLater(tree.mods.annotations, localEnv, v); |
654 typeAnnotate(tree.vartype, env, tree.sym); |
672 typeAnnotate(tree.vartype, env, v); |
655 annotate.flush(); |
673 annotate.flush(); |
656 v.pos = tree.pos; |
674 v.pos = tree.pos; |
|
675 } |
|
676 // where |
|
677 void checkType(JCTree tree, Type type, String diag) { |
|
678 if (!tree.type.isErroneous() && !types.isSameType(tree.type, type)) { |
|
679 log.error(tree, diag, type, tree.type); |
|
680 } |
657 } |
681 } |
658 |
682 |
659 /** Create a fresh environment for a variable's initializer. |
683 /** Create a fresh environment for a variable's initializer. |
660 * If the variable is a field, the owner of the environment's scope |
684 * If the variable is a field, the owner of the environment's scope |
661 * is be the variable itself, otherwise the owner is the method |
685 * is be the variable itself, otherwise the owner is the method |
1038 } |
1062 } |
1039 } finally { |
1063 } finally { |
1040 isFirst = true; |
1064 isFirst = true; |
1041 } |
1065 } |
1042 } |
1066 } |
1043 annotate.afterRepeated(TypeAnnotations.organizeTypeAnnotationsSignatures(syms, names, log, tree)); |
1067 TypeAnnotations.organizeTypeAnnotationsSignatures(syms, names, log, tree, annotate); |
1044 } |
1068 } |
1045 |
1069 |
|
1070 /* |
|
1071 * If the symbol is non-null, attach the type annotation to it. |
|
1072 */ |
1046 private void actualEnterTypeAnnotations(final List<JCAnnotation> annotations, |
1073 private void actualEnterTypeAnnotations(final List<JCAnnotation> annotations, |
1047 final Env<AttrContext> env, |
1074 final Env<AttrContext> env, |
1048 final Symbol s) { |
1075 final Symbol s) { |
1049 Map<TypeSymbol, ListBuffer<Attribute.TypeCompound>> annotated = |
1076 Map<TypeSymbol, ListBuffer<Attribute.TypeCompound>> annotated = |
1050 new LinkedHashMap<TypeSymbol, ListBuffer<Attribute.TypeCompound>>(); |
1077 new LinkedHashMap<TypeSymbol, ListBuffer<Attribute.TypeCompound>>(); |
1073 annotated.put(a.type.tsym, ListBuffer.of(tc)); |
1100 annotated.put(a.type.tsym, ListBuffer.of(tc)); |
1074 pos.put(tc, a.pos()); |
1101 pos.put(tc, a.pos()); |
1075 } |
1102 } |
1076 } |
1103 } |
1077 |
1104 |
1078 s.annotations.appendTypeAttributesWithCompletion( |
1105 if (s != null) { |
1079 annotate.new AnnotateRepeatedContext<Attribute.TypeCompound>(env, annotated, pos, log, true)); |
1106 s.annotations.appendTypeAttributesWithCompletion( |
|
1107 annotate.new AnnotateRepeatedContext<Attribute.TypeCompound>(env, annotated, pos, log, true)); |
|
1108 } |
1080 } |
1109 } |
1081 |
1110 |
1082 public void typeAnnotate(final JCTree tree, final Env<AttrContext> env, final Symbol sym) { |
1111 public void typeAnnotate(final JCTree tree, final Env<AttrContext> env, final Symbol sym) { |
1083 tree.accept(new TypeAnnotate(env, sym)); |
1112 tree.accept(new TypeAnnotate(env, sym)); |
1084 } |
1113 } |
1147 scan(tree.params); |
1176 scan(tree.params); |
1148 scan(tree.thrown); |
1177 scan(tree.thrown); |
1149 scan(tree.defaultValue); |
1178 scan(tree.defaultValue); |
1150 // Do not annotate the body, just the signature. |
1179 // Do not annotate the body, just the signature. |
1151 // scan(tree.body); |
1180 // scan(tree.body); |
|
1181 } |
|
1182 |
|
1183 @Override |
|
1184 public void visitVarDef(final JCVariableDecl tree) { |
|
1185 if (sym != null && sym.kind == Kinds.VAR) { |
|
1186 // Don't visit a parameter once when the sym is the method |
|
1187 // and once when the sym is the parameter. |
|
1188 scan(tree.mods); |
|
1189 scan(tree.vartype); |
|
1190 } |
|
1191 scan(tree.init); |
|
1192 } |
|
1193 |
|
1194 @Override |
|
1195 public void visitClassDef(JCClassDecl tree) { |
|
1196 // We can only hit a classdef if it is declared within |
|
1197 // a method. Ignore it - the class will be visited |
|
1198 // separately later. |
|
1199 } |
|
1200 |
|
1201 @Override |
|
1202 public void visitNewClass(JCNewClass tree) { |
|
1203 if (tree.def == null) { |
|
1204 // For an anonymous class instantiation the class |
|
1205 // will be visited separately. |
|
1206 super.visitNewClass(tree); |
|
1207 } |
1152 } |
1208 } |
1153 } |
1209 } |
1154 |
1210 |
1155 |
1211 |
1156 private Env<AttrContext> baseEnv(JCClassDecl tree, Env<AttrContext> env) { |
1212 private Env<AttrContext> baseEnv(JCClassDecl tree, Env<AttrContext> env) { |