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

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 581
f2fdd52e4e87
child 1521
71f35e4b93a5
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

     1 /*
     2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. 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.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * 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 supported API.
    33 *  If 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         case CLASS_LITERAL_GENERIC_OR_ARRAY:
   143             sb.append(", offset = ");
   144             sb.append(offset);
   145             break;
   146             // method parameter: not specified
   147         case METHOD_PARAMETER_GENERIC_OR_ARRAY:
   148             sb.append(", param_index = ");
   149             sb.append(parameter_index);
   150             break;
   151             // method type argument: wasn't specified
   152         case METHOD_TYPE_ARGUMENT:
   153         case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
   154             sb.append(", offset = ");
   155             sb.append(offset);
   156             sb.append(", type_index = ");
   157             sb.append(type_index);
   158             break;
   159             // We don't need to worry abut these
   160         case METHOD_RETURN_GENERIC_OR_ARRAY:
   161         case FIELD_GENERIC_OR_ARRAY:
   162             break;
   163         case UNKNOWN:
   164             break;
   165         default:
   166             //                throw new AssertionError("unknown type: " + type);
   167         }
   169         // Append location data for generics/arrays.
   170         if (type.hasLocation()) {
   171             sb.append(", location = (");
   172             sb.append(location);
   173             sb.append(")");
   174         }
   176         sb.append(", pos = ");
   177         sb.append(pos);
   179         sb.append(']');
   180         return sb.toString();
   181     }
   183     /**
   184      * Indicates whether the target tree of the annotation has been optimized
   185      * away from classfile or not.
   186      * @return true if the target has not been optimized away
   187      */
   188     public boolean emitToClassfile() {
   189         if (type == TargetType.WILDCARD_BOUND
   190             || type == TargetType.WILDCARD_BOUND_GENERIC_OR_ARRAY)
   191             return wildcard_position.isValidOffset;
   192         else
   193             return !type.isLocal() || isValidOffset;
   194     }
   195 }

mercurial