< prev index next >

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

Print this page




  40 import com.sun.tools.javac.tree.JCTree.*;
  41 import com.sun.tools.javac.tree.TreeInfo;
  42 import com.sun.tools.javac.tree.TreeMaker;
  43 import com.sun.tools.javac.tree.TreeScanner;
  44 import com.sun.tools.javac.util.*;
  45 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  46 import com.sun.tools.javac.util.List;
  47 
  48 import javax.tools.JavaFileObject;
  49 
  50 import java.util.*;
  51 
  52 import static com.sun.tools.javac.code.Flags.SYNTHETIC;
  53 import static com.sun.tools.javac.code.Kinds.Kind.MDL;
  54 import static com.sun.tools.javac.code.Kinds.Kind.MTH;
  55 import static com.sun.tools.javac.code.Kinds.Kind.PCK;
  56 import static com.sun.tools.javac.code.Kinds.Kind.VAR;
  57 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
  58 import static com.sun.tools.javac.code.TypeTag.ARRAY;
  59 import static com.sun.tools.javac.code.TypeTag.CLASS;

  60 import static com.sun.tools.javac.tree.JCTree.Tag.ANNOTATION;
  61 import static com.sun.tools.javac.tree.JCTree.Tag.ASSIGN;
  62 import static com.sun.tools.javac.tree.JCTree.Tag.IDENT;
  63 import static com.sun.tools.javac.tree.JCTree.Tag.NEWARRAY;
  64 
  65 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
  66 
  67 
  68 /** Enter annotations onto symbols and types (and trees).
  69  *
  70  *  This is also a pseudo stage in the compiler taking care of scheduling when annotations are
  71  *  entered.
  72  *
  73  *  <p><b>This is NOT part of any supported API.
  74  *  If you write code that depends on this, you do so at your own risk.
  75  *  This code and its internal interfaces are subject to change or
  76  *  deletion without notice.</b>
  77  */
  78 public class Annotate {
  79     protected static final Context.Key<Annotate> annotateKey = new Context.Key<>();


 259             try {
 260                 if (s.hasAnnotations() && annotations.nonEmpty())
 261                     log.error(annotations.head.pos, Errors.AlreadyAnnotated(Kinds.kindName(s), s));
 262 
 263                 Assert.checkNonNull(s, "Symbol argument to actualEnterAnnotations is null");
 264 
 265                 // false is passed as fifth parameter since annotateLater is
 266                 // never called for a type parameter
 267                 annotateNow(s, annotations, localEnv, false, false);
 268             } finally {
 269                 if (prevLint != null)
 270                     chk.setLint(prevLint);
 271                 deferredLintHandler.setPos(prevLintPos);
 272                 log.useSource(prev);
 273             }
 274         });
 275 
 276         validate(() -> { //validate annotations
 277             JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
 278             try {
 279                 chk.validateAnnotations(annotations, s);
 280             } finally {
 281                 log.useSource(prev);
 282             }
 283         });
 284     }
 285 
 286 
 287     /** Queue processing of an attribute default value. */
 288     public void annotateDefaultValueLater(JCExpression defaultValue, Env<AttrContext> localEnv,
 289             MethodSymbol m, DiagnosticPosition deferPos)
 290     {
 291         normal(() -> {
 292             JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
 293             DiagnosticPosition prevLintPos = deferredLintHandler.setPos(deferPos);
 294             try {
 295                 enterDefaultValue(defaultValue, localEnv, m);
 296             } finally {
 297                 deferredLintHandler.setPos(prevLintPos);
 298                 log.useSource(prev);
 299             }


1087             scan(tree.thrown);
1088             scan(tree.defaultValue);
1089             // Do not annotate the body, just the signature.
1090         }
1091 
1092         @Override
1093         public void visitVarDef(JCVariableDecl tree) {
1094             DiagnosticPosition prevPos = deferPos;
1095             deferPos = tree.pos();
1096             try {
1097                 if (sym != null && sym.kind == VAR) {
1098                     // Don't visit a parameter once when the sym is the method
1099                     // and once when the sym is the parameter.
1100                     scan(tree.mods);
1101                     scan(tree.vartype);
1102                 }
1103                 scan(tree.init);
1104             } finally {
1105                 deferPos = prevPos;
1106             }







1107         }
1108 
1109         @Override
1110         public void visitClassDef(JCClassDecl tree) {
1111             // We can only hit a classdef if it is declared within
1112             // a method. Ignore it - the class will be visited
1113             // separately later.
1114         }
1115 
1116         @Override
1117         public void visitNewClass(JCNewClass tree) {
1118             scan(tree.encl);
1119             scan(tree.typeargs);
1120             if (tree.def == null) {
1121                 scan(tree.clazz);
1122             }
1123             scan(tree.args);
1124             // the anonymous class instantiation if any will be visited separately.
1125         }
1126     }




  40 import com.sun.tools.javac.tree.JCTree.*;
  41 import com.sun.tools.javac.tree.TreeInfo;
  42 import com.sun.tools.javac.tree.TreeMaker;
  43 import com.sun.tools.javac.tree.TreeScanner;
  44 import com.sun.tools.javac.util.*;
  45 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  46 import com.sun.tools.javac.util.List;
  47 
  48 import javax.tools.JavaFileObject;
  49 
  50 import java.util.*;
  51 
  52 import static com.sun.tools.javac.code.Flags.SYNTHETIC;
  53 import static com.sun.tools.javac.code.Kinds.Kind.MDL;
  54 import static com.sun.tools.javac.code.Kinds.Kind.MTH;
  55 import static com.sun.tools.javac.code.Kinds.Kind.PCK;
  56 import static com.sun.tools.javac.code.Kinds.Kind.VAR;
  57 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
  58 import static com.sun.tools.javac.code.TypeTag.ARRAY;
  59 import static com.sun.tools.javac.code.TypeTag.CLASS;
  60 import com.sun.tools.javac.tree.JCTree;
  61 import static com.sun.tools.javac.tree.JCTree.Tag.ANNOTATION;
  62 import static com.sun.tools.javac.tree.JCTree.Tag.ASSIGN;
  63 import static com.sun.tools.javac.tree.JCTree.Tag.IDENT;
  64 import static com.sun.tools.javac.tree.JCTree.Tag.NEWARRAY;
  65 
  66 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
  67 
  68 
  69 /** Enter annotations onto symbols and types (and trees).
  70  *
  71  *  This is also a pseudo stage in the compiler taking care of scheduling when annotations are
  72  *  entered.
  73  *
  74  *  <p><b>This is NOT part of any supported API.
  75  *  If you write code that depends on this, you do so at your own risk.
  76  *  This code and its internal interfaces are subject to change or
  77  *  deletion without notice.</b>
  78  */
  79 public class Annotate {
  80     protected static final Context.Key<Annotate> annotateKey = new Context.Key<>();


 260             try {
 261                 if (s.hasAnnotations() && annotations.nonEmpty())
 262                     log.error(annotations.head.pos, Errors.AlreadyAnnotated(Kinds.kindName(s), s));
 263 
 264                 Assert.checkNonNull(s, "Symbol argument to actualEnterAnnotations is null");
 265 
 266                 // false is passed as fifth parameter since annotateLater is
 267                 // never called for a type parameter
 268                 annotateNow(s, annotations, localEnv, false, false);
 269             } finally {
 270                 if (prevLint != null)
 271                     chk.setLint(prevLint);
 272                 deferredLintHandler.setPos(prevLintPos);
 273                 log.useSource(prev);
 274             }
 275         });
 276 
 277         validate(() -> { //validate annotations
 278             JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
 279             try {
 280                 chk.validateAnnotations(annotations, TreeInfo.declarationFor(s, localEnv.tree), s);
 281             } finally {
 282                 log.useSource(prev);
 283             }
 284         });
 285     }
 286 
 287 
 288     /** Queue processing of an attribute default value. */
 289     public void annotateDefaultValueLater(JCExpression defaultValue, Env<AttrContext> localEnv,
 290             MethodSymbol m, DiagnosticPosition deferPos)
 291     {
 292         normal(() -> {
 293             JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
 294             DiagnosticPosition prevLintPos = deferredLintHandler.setPos(deferPos);
 295             try {
 296                 enterDefaultValue(defaultValue, localEnv, m);
 297             } finally {
 298                 deferredLintHandler.setPos(prevLintPos);
 299                 log.useSource(prev);
 300             }


1088             scan(tree.thrown);
1089             scan(tree.defaultValue);
1090             // Do not annotate the body, just the signature.
1091         }
1092 
1093         @Override
1094         public void visitVarDef(JCVariableDecl tree) {
1095             DiagnosticPosition prevPos = deferPos;
1096             deferPos = tree.pos();
1097             try {
1098                 if (sym != null && sym.kind == VAR) {
1099                     // Don't visit a parameter once when the sym is the method
1100                     // and once when the sym is the parameter.
1101                     scan(tree.mods);
1102                     scan(tree.vartype);
1103                 }
1104                 scan(tree.init);
1105             } finally {
1106                 deferPos = prevPos;
1107             }
1108         }
1109 
1110         @Override
1111         public void visitBindingPattern(JCTree.JCBindingPattern tree) {
1112             //type binding pattern's type will be annotated separatelly, avoid
1113             //adding its annotations into the owning method here (would clash
1114             //with repeatable annotations).
1115         }
1116 
1117         @Override
1118         public void visitClassDef(JCClassDecl tree) {
1119             // We can only hit a classdef if it is declared within
1120             // a method. Ignore it - the class will be visited
1121             // separately later.
1122         }
1123 
1124         @Override
1125         public void visitNewClass(JCNewClass tree) {
1126             scan(tree.encl);
1127             scan(tree.typeargs);
1128             if (tree.def == null) {
1129                 scan(tree.clazz);
1130             }
1131             scan(tree.args);
1132             // the anonymous class instantiation if any will be visited separately.
1133         }
1134     }


< prev index next >