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

Fri, 26 Jun 2009 19:47:12 -0700

author
jjg
date
Fri, 26 Jun 2009 19:47:12 -0700
changeset 310
7c154fdc3547
parent 309
664edca41e34
child 323
14b1a8ede954
permissions
-rw-r--r--

6854796: update JSR308 impl with latest code from type-annotations repo
Reviewed-by: jjg, mcimadamore, darcy
Contributed-by: mernst@cs.washington.edu, mali@csail.mit.edu, mpapi@csail.mit.edu

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

mercurial