langtools/src/share/classes/com/sun/tools/javac/code/TypeAnnotations.java
changeset 21010 5ffe0d8a5e24
parent 20258 bdaa691b4da4
child 21018 95d225149128
equal deleted inserted replaced
21009:b35973e2d42e 21010:5ffe0d8a5e24
    69 import com.sun.tools.javac.util.Context;
    69 import com.sun.tools.javac.util.Context;
    70 import com.sun.tools.javac.util.List;
    70 import com.sun.tools.javac.util.List;
    71 import com.sun.tools.javac.util.ListBuffer;
    71 import com.sun.tools.javac.util.ListBuffer;
    72 import com.sun.tools.javac.util.Log;
    72 import com.sun.tools.javac.util.Log;
    73 import com.sun.tools.javac.util.Names;
    73 import com.sun.tools.javac.util.Names;
       
    74 import com.sun.tools.javac.util.Options;
    74 
    75 
    75 /**
    76 /**
    76  * Contains operations specific to processing type annotations.
    77  * Contains operations specific to processing type annotations.
    77  * This class has two functions:
    78  * This class has two functions:
    78  * separate declaration from type annotations and insert the type
    79  * separate declaration from type annotations and insert the type
    92 
    93 
    93     final Log log;
    94     final Log log;
    94     final Names names;
    95     final Names names;
    95     final Symtab syms;
    96     final Symtab syms;
    96     final Annotate annotate;
    97     final Annotate annotate;
       
    98     private final boolean typeAnnoAsserts;
    97 
    99 
    98     protected TypeAnnotations(Context context) {
   100     protected TypeAnnotations(Context context) {
    99         context.put(typeAnnosKey, this);
   101         context.put(typeAnnosKey, this);
   100         names = Names.instance(context);
   102         names = Names.instance(context);
   101         log = Log.instance(context);
   103         log = Log.instance(context);
   102         syms = Symtab.instance(context);
   104         syms = Symtab.instance(context);
   103         annotate = Annotate.instance(context);
   105         annotate = Annotate.instance(context);
       
   106         Options options = Options.instance(context);
       
   107         typeAnnoAsserts = options.isSet("TypeAnnotationAsserts");
   104     }
   108     }
   105 
   109 
   106     /**
   110     /**
   107      * Separate type annotations from declaration annotations and
   111      * Separate type annotations from declaration annotations and
   108      * determine the correct positions for type annotations.
   112      * determine the correct positions for type annotations.
   263          * we never build an JCAnnotatedType. This step finds these
   267          * we never build an JCAnnotatedType. This step finds these
   264          * annotations and marks them as if they were part of the type.
   268          * annotations and marks them as if they were part of the type.
   265          */
   269          */
   266         private void separateAnnotationsKinds(JCTree typetree, Type type, Symbol sym,
   270         private void separateAnnotationsKinds(JCTree typetree, Type type, Symbol sym,
   267                 TypeAnnotationPosition pos) {
   271                 TypeAnnotationPosition pos) {
   268             /*
       
   269             System.out.printf("separateAnnotationsKinds(typetree: %s, type: %s, symbol: %s, pos: %s%n",
       
   270                     typetree, type, sym, pos);
       
   271             */
       
   272             List<Attribute.Compound> annotations = sym.getRawAttributes();
   272             List<Attribute.Compound> annotations = sym.getRawAttributes();
   273             ListBuffer<Attribute.Compound> declAnnos = new ListBuffer<Attribute.Compound>();
   273             ListBuffer<Attribute.Compound> declAnnos = new ListBuffer<Attribute.Compound>();
   274             ListBuffer<Attribute.TypeCompound> typeAnnos = new ListBuffer<Attribute.TypeCompound>();
   274             ListBuffer<Attribute.TypeCompound> typeAnnos = new ListBuffer<Attribute.TypeCompound>();
   275 
   275 
   276             for (Attribute.Compound a : annotations) {
   276             for (Attribute.Compound a : annotations) {
  1021          * then determine the positions.
  1021          * then determine the positions.
  1022          */
  1022          */
  1023         @Override
  1023         @Override
  1024         public void visitMethodDef(final JCMethodDecl tree) {
  1024         public void visitMethodDef(final JCMethodDecl tree) {
  1025             if (tree.sym == null) {
  1025             if (tree.sym == null) {
  1026                 // Something most be wrong, e.g. a class not found.
  1026                 if (typeAnnoAsserts) {
  1027                 // Quietly ignore. (See test FailOver15.java)
  1027                     Assert.error("Visiting tree node before memberEnter");
       
  1028                 } else {
  1028                 return;
  1029                 return;
       
  1030             }
  1029             }
  1031             }
  1030             if (sigOnly) {
  1032             if (sigOnly) {
  1031                 if (!tree.mods.annotations.isEmpty()) {
  1033                 if (!tree.mods.annotations.isEmpty()) {
  1032                     // Nothing to do for separateAnnotationsKinds if
  1034                     // Nothing to do for separateAnnotationsKinds if
  1033                     // there are no annotations of either kind.
  1035                     // there are no annotations of either kind.
  1127         public void visitVarDef(final JCVariableDecl tree) {
  1129         public void visitVarDef(final JCVariableDecl tree) {
  1128             if (tree.mods.annotations.isEmpty()) {
  1130             if (tree.mods.annotations.isEmpty()) {
  1129                 // Nothing to do for separateAnnotationsKinds if
  1131                 // Nothing to do for separateAnnotationsKinds if
  1130                 // there are no annotations of either kind.
  1132                 // there are no annotations of either kind.
  1131             } else if (tree.sym == null) {
  1133             } else if (tree.sym == null) {
       
  1134                 if (typeAnnoAsserts) {
       
  1135                     Assert.error("Visiting tree node before memberEnter");
       
  1136                 }
  1132                 // Something is wrong already. Quietly ignore.
  1137                 // Something is wrong already. Quietly ignore.
  1133             } else if (tree.sym.getKind() == ElementKind.PARAMETER) {
  1138             } else if (tree.sym.getKind() == ElementKind.PARAMETER) {
  1134                 // Parameters are handled in visitMethodDef or visitLambda.
  1139                 // Parameters are handled in visitMethodDef or visitLambda.
  1135             } else if (tree.sym.getKind() == ElementKind.FIELD) {
  1140             } else if (tree.sym.getKind() == ElementKind.FIELD) {
  1136                 if (sigOnly) {
  1141                 if (sigOnly) {
  1280         }
  1285         }
  1281 
  1286 
  1282         private void findPosition(JCTree tree, JCTree frame, List<JCAnnotation> annotations) {
  1287         private void findPosition(JCTree tree, JCTree frame, List<JCAnnotation> annotations) {
  1283             if (!annotations.isEmpty()) {
  1288             if (!annotations.isEmpty()) {
  1284                 /*
  1289                 /*
  1285                 System.out.println("Finding pos for: " + annotations);
  1290                 System.err.println("Finding pos for: " + annotations);
  1286                 System.out.println("    tree: " + tree + " kind: " + tree.getKind());
  1291                 System.err.println("    tree: " + tree + " kind: " + tree.getKind());
  1287                 System.out.println("    frame: " + frame + " kind: " + frame.getKind());
  1292                 System.err.println("    frame: " + frame + " kind: " + frame.getKind());
  1288                 */
  1293                 */
  1289                 TypeAnnotationPosition p = new TypeAnnotationPosition();
  1294                 TypeAnnotationPosition p = new TypeAnnotationPosition();
  1290                 p.onLambda = currentLambda;
  1295                 p.onLambda = currentLambda;
  1291                 resolveFrame(tree, frame, frames.toList(), p);
  1296                 resolveFrame(tree, frame, frames.toList(), p);
  1292                 setTypeAnnotationPos(annotations, p);
  1297                 setTypeAnnotationPos(annotations, p);