langtools/src/share/classes/com/sun/tools/javac/code/TypeAnnotations.java
author jjg
Tue, 19 Jan 2010 14:28:45 -0800
changeset 4865 1e4422ac2495
child 4872 6c49b0ac56a6
permissions -rw-r--r--
6917067: refactor type annotations code from TransTypes into new TypeAnnotations class Reviewed-by: jjg, darcy Contributed-by: mali@csail.mit.edu, mernst@cs.washington.edu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4865
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
     1
/*
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
     2
 * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
     4
 *
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    10
 *
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    15
 * accompanied this code).
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    16
 *
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    20
 *
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    23
 * have any questions.
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    24
 */
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    25
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    26
package com.sun.tools.javac.code;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    27
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    28
import javax.lang.model.element.ElementKind;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    29
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    30
import com.sun.tools.javac.code.Symbol.VarSymbol;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    31
import com.sun.tools.javac.tree.JCTree;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    32
import com.sun.tools.javac.tree.TreeInfo;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    33
import com.sun.tools.javac.tree.TreeScanner;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    34
import com.sun.tools.javac.tree.JCTree.*;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    35
import com.sun.tools.javac.util.Context;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    36
import com.sun.tools.javac.util.List;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    37
import com.sun.tools.javac.util.ListBuffer;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    38
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    39
/**
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    40
 * Contains operations specific to processing type annotations
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    41
 */
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    42
public class TypeAnnotations {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    43
    private static final Context.Key<TypeAnnotations> key
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    44
        = new Context.Key<TypeAnnotations>();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    45
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    46
    public static TypeAnnotations instance(Context context) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    47
        TypeAnnotations instance = context.get(key);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    48
        if (instance == null)
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    49
            instance = new TypeAnnotations(context);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    50
        return instance;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    51
    }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    52
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    53
    protected TypeAnnotations(Context context) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    54
        context.put(key, this);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    55
    }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    56
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    57
    public void taFillAndLift(JCClassDecl tree, boolean visitBodies) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    58
        new TypeAnnotationPositions().scan(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    59
        new TypeAnnotationLift().scan(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    60
    }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    61
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    62
    private static class TypeAnnotationPositions extends TreeScanner {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    63
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    64
        private ListBuffer<JCTree> frames = ListBuffer.lb();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    65
        private void push(JCTree t) { frames = frames.prepend(t); }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    66
        private JCTree pop() { return frames.next(); }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    67
        private JCTree peek2() { return frames.toList().tail.head; }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    68
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    69
        @Override
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    70
        public void scan(JCTree tree) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    71
            push(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    72
            super.scan(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    73
            pop();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    74
        }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    75
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    76
        private boolean inClass = false;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    77
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    78
        @Override
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    79
        public void visitClassDef(JCClassDecl tree) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    80
           if (!inClass) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    81
               // Do not recurse into nested and inner classes since
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    82
               // TransTypes.visitClassDef makes an invocation for each class
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    83
               // separately.
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    84
               inClass = true;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    85
               try {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    86
                   super.visitClassDef(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    87
               } finally {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    88
                   inClass = false;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    89
               }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    90
           }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    91
        }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    92
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    93
        private TypeAnnotationPosition resolveFrame(JCTree tree, JCTree frame,
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    94
                List<JCTree> path, TypeAnnotationPosition p) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    95
            switch (frame.getKind()) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    96
                case TYPE_CAST:
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    97
                    p.type = TargetType.TYPECAST;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    98
                    p.pos = frame.pos;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
    99
                    return p;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   100
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   101
                case INSTANCE_OF:
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   102
                    p.type = TargetType.INSTANCEOF;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   103
                    p.pos = frame.pos;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   104
                    return p;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   105
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   106
                case NEW_CLASS:
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   107
                    p.type = TargetType.NEW;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   108
                    p.pos = frame.pos;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   109
                    return p;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   110
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   111
                case NEW_ARRAY:
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   112
                    p.type = TargetType.NEW;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   113
                    p.pos = frame.pos;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   114
                    return p;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   115
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   116
                case CLASS:
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   117
                    p.pos = frame.pos;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   118
                    if (((JCClassDecl)frame).extending == tree) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   119
                        p.type = TargetType.CLASS_EXTENDS;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   120
                        p.type_index = -1;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   121
                    } else if (((JCClassDecl)frame).implementing.contains(tree)) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   122
                        p.type = TargetType.CLASS_EXTENDS;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   123
                        p.type_index = ((JCClassDecl)frame).implementing.indexOf(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   124
                    } else if (((JCClassDecl)frame).typarams.contains(tree)) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   125
                        p.type = TargetType.CLASS_TYPE_PARAMETER;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   126
                        p.parameter_index = ((JCClassDecl)frame).typarams.indexOf(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   127
                    } else
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   128
                        throw new AssertionError();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   129
                    return p;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   130
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   131
                case METHOD: {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   132
                    JCMethodDecl frameMethod = (JCMethodDecl)frame;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   133
                    p.pos = frame.pos;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   134
                    if (frameMethod.receiverAnnotations.contains(tree))
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   135
                        p.type = TargetType.METHOD_RECEIVER;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   136
                    else if (frameMethod.thrown.contains(tree)) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   137
                        p.type = TargetType.THROWS;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   138
                        p.type_index = frameMethod.thrown.indexOf(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   139
                    } else if (((JCMethodDecl)frame).restype == tree) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   140
                        p.type = TargetType.METHOD_RETURN_GENERIC_OR_ARRAY;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   141
                    } else if (frameMethod.typarams.contains(tree)) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   142
                        p.type = TargetType.METHOD_TYPE_PARAMETER;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   143
                        p.parameter_index = frameMethod.typarams.indexOf(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   144
                    } else
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   145
                        throw new AssertionError();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   146
                    return p;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   147
                }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   148
                case MEMBER_SELECT: {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   149
                    JCFieldAccess fieldFrame = (JCFieldAccess)frame;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   150
                    if ("class".contentEquals(fieldFrame.name)) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   151
                        p.type = TargetType.CLASS_LITERAL;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   152
                        if (fieldFrame.selected instanceof JCAnnotatedType) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   153
                            p.pos = TreeInfo.typeIn(fieldFrame).pos;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   154
                        } else if (fieldFrame.selected instanceof JCArrayTypeTree) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   155
                            p.pos = fieldFrame.selected.pos;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   156
                        }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   157
                    } else
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   158
                        throw new AssertionError();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   159
                    return p;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   160
                }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   161
                case PARAMETERIZED_TYPE: {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   162
                    TypeAnnotationPosition nextP;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   163
                    if (((JCTypeApply)frame).clazz == tree)
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   164
                        nextP = p; // generic: RAW; noop
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   165
                    else if (((JCTypeApply)frame).arguments.contains(tree))
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   166
                        p.location = p.location.prepend(
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   167
                                ((JCTypeApply)frame).arguments.indexOf(tree));
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   168
                    else
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   169
                        throw new AssertionError();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   170
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   171
                    List<JCTree> newPath = path.tail;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   172
                    return resolveFrame(newPath.head, newPath.tail.head, newPath, p);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   173
                }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   174
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   175
                case ARRAY_TYPE: {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   176
                    p.location = p.location.prepend(0);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   177
                    List<JCTree> newPath = path.tail;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   178
                    return resolveFrame(newPath.head, newPath.tail.head, newPath, p);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   179
                }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   180
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   181
                case TYPE_PARAMETER:
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   182
                    if (path.tail.tail.head.getTag() == JCTree.CLASSDEF) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   183
                        JCClassDecl clazz = (JCClassDecl)path.tail.tail.head;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   184
                        p.type = TargetType.CLASS_TYPE_PARAMETER_BOUND;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   185
                        p.parameter_index = clazz.typarams.indexOf(path.tail.head);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   186
                        p.bound_index = ((JCTypeParameter)frame).bounds.indexOf(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   187
                    } else if (path.tail.tail.head.getTag() == JCTree.METHODDEF) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   188
                        JCMethodDecl method = (JCMethodDecl)path.tail.tail.head;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   189
                        p.type = TargetType.METHOD_TYPE_PARAMETER_BOUND;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   190
                        p.parameter_index = method.typarams.indexOf(path.tail.head);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   191
                        p.bound_index = ((JCTypeParameter)frame).bounds.indexOf(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   192
                    } else
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   193
                        throw new AssertionError();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   194
                    p.pos = frame.pos;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   195
                    return p;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   196
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   197
                case VARIABLE:
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   198
                    VarSymbol v = ((JCVariableDecl)frame).sym;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   199
                    p.pos = frame.pos;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   200
                    switch (v.getKind()) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   201
                        case LOCAL_VARIABLE:
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   202
                            p.type = TargetType.LOCAL_VARIABLE; break;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   203
                        case FIELD:
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   204
                            p.type = TargetType.FIELD_GENERIC_OR_ARRAY; break;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   205
                        case PARAMETER:
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   206
                            p.type = TargetType.METHOD_PARAMETER_GENERIC_OR_ARRAY;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   207
                            p.parameter_index = methodParamIndex(path, frame);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   208
                            break;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   209
                        default: throw new AssertionError();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   210
                    }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   211
                    return p;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   212
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   213
                case ANNOTATED_TYPE: {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   214
                    List<JCTree> newPath = path.tail;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   215
                    return resolveFrame(newPath.head, newPath.tail.head,
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   216
                            newPath, p);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   217
                }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   218
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   219
                case METHOD_INVOCATION: {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   220
                    JCMethodInvocation invocation = (JCMethodInvocation)frame;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   221
                    if (!invocation.typeargs.contains(tree))
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   222
                        throw new AssertionError("{" + tree + "} is not an argument in the invocation: " + invocation);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   223
                    p.type = TargetType.METHOD_TYPE_ARGUMENT;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   224
                    p.pos = invocation.pos;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   225
                    p.type_index = invocation.typeargs.indexOf(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   226
                    return p;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   227
                }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   228
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   229
                case EXTENDS_WILDCARD:
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   230
                case SUPER_WILDCARD: {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   231
                    p.type = TargetType.WILDCARD_BOUND;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   232
                    List<JCTree> newPath = path.tail;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   233
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   234
                    TypeAnnotationPosition wildcard =
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   235
                        resolveFrame(newPath.head, newPath.tail.head, newPath,
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   236
                                new TypeAnnotationPosition());
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   237
                    if (!wildcard.location.isEmpty())
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   238
                        wildcard.type = wildcard.type.getGenericComplement();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   239
                    p.wildcard_position = wildcard;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   240
                    p.pos = frame.pos;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   241
                    return p;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   242
                }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   243
            }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   244
            return p;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   245
        }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   246
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   247
        @Override
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   248
        public void visitApply(JCMethodInvocation tree) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   249
            scan(tree.meth);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   250
            scan(tree.typeargs);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   251
            scan(tree.args);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   252
        }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   253
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   254
        private void setTypeAnnotationPos(List<JCTypeAnnotation> annotations, TypeAnnotationPosition position) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   255
            for (JCTypeAnnotation anno : annotations) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   256
                anno.annotation_position = position;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   257
                anno.attribute_field.position = position;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   258
            }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   259
        }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   260
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   261
        @Override
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   262
        public void visitNewArray(JCNewArray tree) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   263
            findPosition(tree, tree, tree.annotations);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   264
            int dimAnnosCount = tree.dimAnnotations.size();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   265
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   266
            // handle annotations associated with dimentions
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   267
            for (int i = 0; i < dimAnnosCount; ++i) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   268
                TypeAnnotationPosition p = new TypeAnnotationPosition();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   269
                p.type = TargetType.NEW_GENERIC_OR_ARRAY;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   270
                p.pos = tree.pos;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   271
                p.location = p.location.append(i);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   272
                setTypeAnnotationPos(tree.dimAnnotations.get(i), p);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   273
            }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   274
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   275
            // handle "free" annotations
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   276
            int i = dimAnnosCount == 0 ? 0 : dimAnnosCount - 1;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   277
            JCExpression elemType = tree.elemtype;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   278
            while (elemType != null) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   279
                if (elemType.getTag() == JCTree.ANNOTATED_TYPE) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   280
                    JCAnnotatedType at = (JCAnnotatedType)elemType;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   281
                    TypeAnnotationPosition p = new TypeAnnotationPosition();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   282
                    p.type = TargetType.NEW_GENERIC_OR_ARRAY;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   283
                    p.pos = tree.pos;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   284
                    p.location = p.location.append(i);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   285
                    setTypeAnnotationPos(at.annotations, p);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   286
                    elemType = at.underlyingType;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   287
                } else if (elemType.getTag() == JCTree.TYPEARRAY) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   288
                    ++i;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   289
                    elemType = ((JCArrayTypeTree)elemType).elemtype;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   290
                } else
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   291
                    break;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   292
            }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   293
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   294
            // find annotations locations of initializer elements
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   295
            scan(tree.elems);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   296
        }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   297
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   298
        @Override
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   299
        public void visitAnnotatedType(JCAnnotatedType tree) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   300
            findPosition(tree, peek2(), tree.annotations);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   301
            super.visitAnnotatedType(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   302
        }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   303
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   304
        @Override
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   305
        public void visitMethodDef(JCMethodDecl tree) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   306
            TypeAnnotationPosition p = new TypeAnnotationPosition();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   307
            p.type = TargetType.METHOD_RECEIVER;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   308
            setTypeAnnotationPos(tree.receiverAnnotations, p);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   309
            super.visitMethodDef(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   310
        }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   311
        @Override
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   312
        public void visitTypeParameter(JCTypeParameter tree) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   313
            findPosition(tree, peek2(), tree.annotations);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   314
            super.visitTypeParameter(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   315
        }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   316
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   317
        void findPosition(JCTree tree, JCTree frame, List<JCTypeAnnotation> annotations) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   318
            if (!annotations.isEmpty()) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   319
                TypeAnnotationPosition p =
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   320
                        resolveFrame(tree, frame, frames.toList(),
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   321
                                new TypeAnnotationPosition());
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   322
                if (!p.location.isEmpty())
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   323
                    p.type = p.type.getGenericComplement();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   324
                setTypeAnnotationPos(annotations, p);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   325
            }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   326
        }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   327
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   328
        private int methodParamIndex(List<JCTree> path, JCTree param) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   329
            List<JCTree> curr = path;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   330
            if (curr.head != param)
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   331
                curr = path.tail;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   332
            JCMethodDecl method = (JCMethodDecl)curr.tail.head;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   333
            return method.params.indexOf(param);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   334
        }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   335
    }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   336
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   337
    private static class TypeAnnotationLift extends TreeScanner {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   338
        List<Attribute.TypeCompound> recordedTypeAnnotations = List.nil();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   339
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   340
        boolean isInner = false;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   341
        @Override
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   342
        public void visitClassDef(JCClassDecl tree) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   343
            if (isInner) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   344
                // tree is an inner class tree.  stop now.
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   345
                // TransTypes.visitClassDef makes an invocation for each class
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   346
                // separately.
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   347
                return;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   348
            }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   349
            isInner = true;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   350
            List<Attribute.TypeCompound> prevTAs = recordedTypeAnnotations;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   351
            recordedTypeAnnotations = List.nil();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   352
            try {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   353
                super.visitClassDef(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   354
            } finally {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   355
                tree.sym.typeAnnotations = tree.sym.typeAnnotations.appendList(recordedTypeAnnotations);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   356
                recordedTypeAnnotations = prevTAs;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   357
            }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   358
        }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   359
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   360
        @Override
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   361
        public void visitMethodDef(JCMethodDecl tree) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   362
            List<Attribute.TypeCompound> prevTAs = recordedTypeAnnotations;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   363
            recordedTypeAnnotations = List.nil();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   364
            try {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   365
                super.visitMethodDef(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   366
            } finally {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   367
                tree.sym.typeAnnotations = tree.sym.typeAnnotations.appendList(recordedTypeAnnotations);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   368
                recordedTypeAnnotations = prevTAs;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   369
            }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   370
        }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   371
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   372
        @Override
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   373
        public void visitVarDef(JCVariableDecl tree) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   374
            List<Attribute.TypeCompound> prevTAs = recordedTypeAnnotations;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   375
            recordedTypeAnnotations = List.nil();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   376
            ElementKind kind = tree.sym.getKind();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   377
            if (kind == ElementKind.LOCAL_VARIABLE && tree.mods.annotations.nonEmpty()) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   378
                // need to lift the annotations
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   379
                TypeAnnotationPosition position = new TypeAnnotationPosition();
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   380
                position.pos = tree.pos;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   381
                position.type = TargetType.LOCAL_VARIABLE;
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   382
                for (Attribute.Compound attribute : tree.sym.attributes_field) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   383
                    Attribute.TypeCompound tc =
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   384
                        new Attribute.TypeCompound(attribute.type, attribute.values, position);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   385
                    recordedTypeAnnotations = recordedTypeAnnotations.append(tc);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   386
                }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   387
            }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   388
            try {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   389
                super.visitVarDef(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   390
            } finally {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   391
                if (kind.isField() || kind == ElementKind.LOCAL_VARIABLE)
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   392
                    tree.sym.typeAnnotations = tree.sym.typeAnnotations.appendList(recordedTypeAnnotations);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   393
                recordedTypeAnnotations = kind.isField() ? prevTAs : prevTAs.appendList(recordedTypeAnnotations);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   394
            }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   395
        }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   396
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   397
        @Override
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   398
        public void visitApply(JCMethodInvocation tree) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   399
            scan(tree.meth);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   400
            scan(tree.typeargs);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   401
            scan(tree.args);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   402
        }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   403
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   404
        public void visitAnnotation(JCAnnotation tree) {
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   405
            if (tree instanceof JCTypeAnnotation)
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   406
                recordedTypeAnnotations = recordedTypeAnnotations.append(((JCTypeAnnotation)tree).attribute_field);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   407
            super.visitAnnotation(tree);
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   408
        }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   409
    }
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   410
1e4422ac2495 6917067: refactor type annotations code from TransTypes into new TypeAnnotations class
jjg
parents:
diff changeset
   411
}