src/share/classes/com/sun/tools/javac/code/TypeAnnotationPosition.java

Wed, 20 Jan 2010 16:12:26 -0800

author
jjg
date
Wed, 20 Jan 2010 16:12:26 -0800
changeset 478
0eaf89e08564
parent 323
14b1a8ede954
child 485
b0a68258360a
permissions
-rw-r--r--

6918127: improve handling of TypeAnnotationPosition fields
Reviewed-by: jjg, darcy
Contributed-by: mali@csail.mit.edu, mernst@cs.washington.edu

     1 /*
     2  * Copyright 2003-2009 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.javac.code;
    28 import com.sun.tools.javac.util.*;
    30 /** A type annotation position.
    31 *
    32 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    33 *  you write code that depends on this, you do so at your own risk.
    34 *  This code and its internal interfaces are subject to change or
    35 *  deletion without notice.</b>
    36 */
    37 public class TypeAnnotationPosition {
    39     public TargetType type = TargetType.UNKNOWN;
    41     // For generic/array types.
    42     public List<Integer> location = List.nil();
    44     // Tree position.
    45     public int pos = -1;
    47     // For typecasts, type tests, new (and locals, as start_pc).
    48     public boolean isValidOffset = false;
    49     public int offset = -1;
    51     // For locals. arrays same length
    52     public int[] lvarOffset = null;
    53     public int[] lvarLength = null;
    54     public int[] lvarIndex = null;
    56     // For type parameter bound
    57     public int bound_index = Integer.MIN_VALUE;
    59     // For type parameter and method parameter
    60     public int parameter_index = Integer.MIN_VALUE;
    62     // For class extends, implements, and throws classes
    63     public int type_index = Integer.MIN_VALUE;
    65     // For wildcards
    66     public TypeAnnotationPosition wildcard_position = null;
    68     @Override
    69     public String toString() {
    70         StringBuilder sb = new StringBuilder();
    71         sb.append('[');
    72         sb.append(type);
    74         switch (type) {
    75         // type case
    76         case TYPECAST:
    77         case TYPECAST_GENERIC_OR_ARRAY:
    78             // object creation
    79         case INSTANCEOF:
    80         case INSTANCEOF_GENERIC_OR_ARRAY:
    81             // new expression
    82         case NEW:
    83         case NEW_GENERIC_OR_ARRAY:
    84         case NEW_TYPE_ARGUMENT:
    85         case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
    86             sb.append(", offset = ");
    87             sb.append(offset);
    88             break;
    89             // local variable
    90         case LOCAL_VARIABLE:
    91         case LOCAL_VARIABLE_GENERIC_OR_ARRAY:
    92             sb.append(", {");
    93             for (int i = 0; i < lvarOffset.length; ++i) {
    94                 if (i != 0) sb.append("; ");
    95                 sb.append(", start_pc = ");
    96                 sb.append(lvarOffset[i]);
    97                 sb.append(", length = ");
    98                 sb.append(lvarLength[i]);
    99                 sb.append(", index = ");
   100                 sb.append(lvarIndex[i]);
   101             }
   102             sb.append("}");
   103             break;
   104             // method receiver
   105         case METHOD_RECEIVER:
   106             // Do nothing
   107             break;
   108             // type parameters
   109         case CLASS_TYPE_PARAMETER:
   110         case METHOD_TYPE_PARAMETER:
   111             sb.append(", param_index = ");
   112             sb.append(parameter_index);
   113             break;
   114             // type parameters bound
   115         case CLASS_TYPE_PARAMETER_BOUND:
   116         case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
   117         case METHOD_TYPE_PARAMETER_BOUND:
   118         case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
   119             sb.append(", param_index = ");
   120             sb.append(parameter_index);
   121             sb.append(", bound_index = ");
   122             sb.append(bound_index);
   123             break;
   124             // wildcard
   125         case WILDCARD_BOUND:
   126         case WILDCARD_BOUND_GENERIC_OR_ARRAY:
   127             sb.append(", wild_card = ");
   128             sb.append(wildcard_position);
   129             break;
   130             // Class extends and implements clauses
   131         case CLASS_EXTENDS:
   132         case CLASS_EXTENDS_GENERIC_OR_ARRAY:
   133             sb.append(", type_index = ");
   134             sb.append(type_index);
   135             break;
   136             // throws
   137         case THROWS:
   138             sb.append(", type_index = ");
   139             sb.append(type_index);
   140             break;
   141         case CLASS_LITERAL:
   142             sb.append(", offset = ");
   143             sb.append(offset);
   144             break;
   145             // method parameter: not specified
   146         case METHOD_PARAMETER_GENERIC_OR_ARRAY:
   147             sb.append(", param_index = ");
   148             sb.append(parameter_index);
   149             break;
   150             // method type argument: wasn't specified
   151         case METHOD_TYPE_ARGUMENT:
   152         case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
   153             sb.append(", offset = ");
   154             sb.append(offset);
   155             sb.append(", type_index = ");
   156             sb.append(type_index);
   157             break;
   158             // We don't need to worry abut these
   159         case METHOD_RETURN_GENERIC_OR_ARRAY:
   160         case FIELD_GENERIC_OR_ARRAY:
   161             break;
   162         case UNKNOWN:
   163             break;
   164         default:
   165             //                throw new AssertionError("unknown type: " + type);
   166         }
   168         // Append location data for generics/arrays.
   169         if (type.hasLocation()) {
   170             sb.append(", location = (");
   171             sb.append(location);
   172             sb.append(")");
   173         }
   175         sb.append(", pos = ");
   176         sb.append(pos);
   178         sb.append(']');
   179         return sb.toString();
   180     }
   182     /**
   183      * Indicates whether the target tree of the annotation has been optimized
   184      * away from classfile or not.
   185      * @return true if the target has not been optimized away
   186      */
   187     public boolean emitToClassfile() {
   188         if (type == TargetType.WILDCARD_BOUND
   189             || type == TargetType.WILDCARD_BOUND_GENERIC_OR_ARRAY)
   190             return wildcard_position.isValidOffset;
   191         else
   192             return !type.isLocal() || isValidOffset;
   193     }
   194 }

mercurial