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

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

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

6855544: add missing files
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 2008-2009 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 static com.sun.tools.javac.code.TargetType.TargetAttribute.*;
jjg@309 29
jjg@309 30 import java.util.EnumSet;
jjg@309 31 import java.util.Set;
jjg@309 32
jjg@309 33 /**
jjg@309 34 * Describes the type of program element an extended annotation (or extended
jjg@309 35 * compound attribute) targets.
jjg@309 36 *
jjg@309 37 * By comparison, a Tree.Kind has enum values for all elements in the AST, and
jjg@309 38 * it does not provide enough resolution for type arguments (i.e., whether an
jjg@309 39 * annotation targets a type argument in a local variable, method return type,
jjg@309 40 * or a typecast).
jjg@309 41 *
jjg@309 42 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
jjg@309 43 * you write code that depends on this, you do so at your own risk.
jjg@309 44 * This code and its internal interfaces are subject to change or
jjg@309 45 * deletion without notice.</b>
jjg@309 46 */
jjg@309 47 public enum TargetType {
jjg@309 48
jjg@309 49 //
jjg@309 50 // Some target types are commented out, because Java doesn't permit such
jjg@309 51 // targets. They are included here to confirm that their omission is
jjg@309 52 // intentional omission not an accidental omission.
jjg@309 53 //
jjg@309 54
jjg@309 55 /** For annotations on typecasts. */
jjg@309 56 TYPECAST(0x00),
jjg@309 57
jjg@309 58 /** For annotations on a type argument or nested array of a typecast. */
jjg@309 59 TYPECAST_GENERIC_OR_ARRAY(0x01, HasLocation),
jjg@309 60
jjg@309 61 /** For annotations on type tests. */
jjg@309 62 INSTANCEOF(0x02),
jjg@309 63
jjg@309 64 /** For annotations on a type argument or nested array of a type test. */
jjg@309 65 INSTANCEOF_GENERIC_OR_ARRAY(0x03, HasLocation),
jjg@309 66
jjg@309 67 /** For annotations on object creation expressions. */
jjg@309 68 NEW(0x04),
jjg@309 69
jjg@309 70 /**
jjg@309 71 * For annotations on a type argument or nested array of an object creation
jjg@309 72 * expression.
jjg@309 73 */
jjg@309 74 NEW_GENERIC_OR_ARRAY(0x05, HasLocation),
jjg@309 75
jjg@309 76
jjg@309 77 /** For annotations on the method receiver. */
jjg@309 78 METHOD_RECEIVER(0x06),
jjg@309 79
jjg@309 80 // invalid location
jjg@309 81 //@Deprecated METHOD_RECEIVER_GENERIC_OR_ARRAY(0x07, HasLocation),
jjg@309 82
jjg@309 83 /** For annotations on local variables. */
jjg@309 84 LOCAL_VARIABLE(0x08),
jjg@309 85
jjg@309 86 /** For annotations on a type argument or nested array of a local. */
jjg@309 87 LOCAL_VARIABLE_GENERIC_OR_ARRAY(0x09, HasLocation),
jjg@309 88
jjg@309 89 // handled by regular annotations
jjg@309 90 //@Deprecated METHOD_RETURN(0x0A),
jjg@309 91
jjg@309 92 /**
jjg@309 93 * For annotations on a type argument or nested array of a method return
jjg@309 94 * type.
jjg@309 95 */
jjg@309 96 METHOD_RETURN_GENERIC_OR_ARRAY(0x0B, HasLocation),
jjg@309 97
jjg@309 98 // handled by regular annotations
jjg@309 99 //@Deprecated METHOD_PARAMETER(0x0C),
jjg@309 100
jjg@309 101 /** For annotations on a type argument or nested array of a method parameter. */
jjg@309 102 METHOD_PARAMETER_GENERIC_OR_ARRAY(0x0D, HasLocation),
jjg@309 103
jjg@309 104 // handled by regular annotations
jjg@309 105 //@Deprecated FIELD(0x0E),
jjg@309 106
jjg@309 107 /** For annotations on a type argument or nested array of a field. */
jjg@309 108 FIELD_GENERIC_OR_ARRAY(0x0F, HasLocation),
jjg@309 109
jjg@309 110 /** For annotations on a bound of a type parameter of a class. */
jjg@309 111 CLASS_TYPE_PARAMETER_BOUND(0x10, HasBound, HasParameter),
jjg@309 112
jjg@309 113 /**
jjg@309 114 * For annotations on a type argument or nested array of a bound of a type
jjg@309 115 * parameter of a class.
jjg@309 116 */
jjg@309 117 CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x11, HasBound, HasLocation, HasParameter),
jjg@309 118
jjg@309 119 /** For annotations on a bound of a type parameter of a method. */
jjg@309 120 METHOD_TYPE_PARAMETER_BOUND(0x12, HasBound, HasParameter),
jjg@309 121
jjg@309 122 /**
jjg@309 123 * For annotations on a type argument or nested array of a bound of a type
jjg@309 124 * parameter of a method.
jjg@309 125 */
jjg@309 126 METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x13, HasBound, HasLocation, HasParameter),
jjg@309 127
jjg@309 128 /** For annotations on the type of an "extends" or "implements" clause. */
jjg@309 129 CLASS_EXTENDS(0x14),
jjg@309 130
jjg@309 131 /** For annotations on the inner type of an "extends" or "implements" clause. */
jjg@309 132 CLASS_EXTENDS_GENERIC_OR_ARRAY(0x15, HasLocation),
jjg@309 133
jjg@309 134 /** For annotations on a throws clause in a method declaration. */
jjg@309 135 THROWS(0x16),
jjg@309 136
jjg@309 137 // invalid location
jjg@309 138 //@Deprecated THROWS_GENERIC_OR_ARRAY(0x17, HasLocation),
jjg@309 139
jjg@309 140 /** For annotations in type arguments of object creation expressions. */
jjg@309 141 NEW_TYPE_ARGUMENT(0x18),
jjg@309 142 NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x19, HasLocation),
jjg@309 143
jjg@309 144 METHOD_TYPE_ARGUMENT(0x1A),
jjg@309 145 METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x1B, HasLocation),
jjg@309 146
jjg@309 147 WILDCARD_BOUND(0x1C, HasBound),
jjg@309 148 WILDCARD_BOUND_GENERIC_OR_ARRAY(0x1D, HasBound, HasLocation),
jjg@309 149
jjg@309 150 CLASS_LITERAL(0x1E),
jjg@309 151 CLASS_LITERAL_GENERIC_OR_ARRAY(0x1F, HasLocation),
jjg@309 152
jjg@309 153 METHOD_TYPE_PARAMETER(0x20, HasParameter),
jjg@309 154
jjg@309 155 // invalid location
jjg@309 156 //@Deprecated METHOD_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x21, HasLocation, HasParameter),
jjg@309 157
jjg@309 158 CLASS_TYPE_PARAMETER(0x22, HasParameter),
jjg@309 159
jjg@309 160 // invalid location
jjg@309 161 //@Deprecated CLASS_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x23, HasLocation, HasParameter),
jjg@309 162
jjg@309 163 /** For annotations with an unknown target. */
jjg@309 164 UNKNOWN(-1);
jjg@309 165
jjg@309 166 static final int MAXIMUM_TARGET_TYPE_VALUE = 0x22;
jjg@309 167
jjg@309 168 private final int targetTypeValue;
jjg@309 169 private Set<TargetAttribute> flags;
jjg@309 170
jjg@309 171 TargetType(int targetTypeValue, TargetAttribute... attributes) {
jjg@309 172 if (targetTypeValue < Byte.MIN_VALUE
jjg@309 173 || targetTypeValue > Byte.MAX_VALUE)
jjg@309 174 throw new AssertionError("attribute type value needs to be a byte: " + targetTypeValue);
jjg@309 175 this.targetTypeValue = (byte)targetTypeValue;
jjg@309 176 flags = EnumSet.noneOf(TargetAttribute.class);
jjg@309 177 for (TargetAttribute attr : attributes)
jjg@309 178 flags.add(attr);
jjg@309 179 }
jjg@309 180
jjg@309 181 /**
jjg@309 182 * Returns whether or not this TargetType represents an annotation whose
jjg@309 183 * target is an inner type of a generic or array type.
jjg@309 184 *
jjg@309 185 * @return true if this TargetType represents an annotation on an inner
jjg@309 186 * type, false otherwise
jjg@309 187 */
jjg@309 188 public boolean hasLocation() {
jjg@309 189 return flags.contains(HasLocation);
jjg@309 190 }
jjg@309 191
jjg@309 192 public TargetType getGenericComplement() {
jjg@309 193 if (hasLocation())
jjg@309 194 return this;
jjg@309 195 else
jjg@309 196 return fromTargetTypeValue(targetTypeValue() + 1);
jjg@309 197 }
jjg@309 198
jjg@309 199 /**
jjg@309 200 * Returns whether or not this TargetType represents an annotation whose
jjg@309 201 * target has a parameter index.
jjg@309 202 *
jjg@309 203 * @return true if this TargetType has a parameter index,
jjg@309 204 * false otherwise
jjg@309 205 */
jjg@309 206 public boolean hasParameter() {
jjg@309 207 return flags.contains(HasParameter);
jjg@309 208 }
jjg@309 209
jjg@309 210 /**
jjg@309 211 * Returns whether or not this TargetType represents an annotation whose
jjg@309 212 * target is a type parameter bound.
jjg@309 213 *
jjg@309 214 * @return true if this TargetType represents an type parameter bound
jjg@309 215 * annotation, false otherwise
jjg@309 216 */
jjg@309 217 public boolean hasBound() {
jjg@309 218 return flags.contains(HasBound);
jjg@309 219 }
jjg@309 220
jjg@309 221 public int targetTypeValue() {
jjg@309 222 return this.targetTypeValue;
jjg@309 223 }
jjg@309 224
jjg@309 225 private static TargetType[] targets = null;
jjg@309 226
jjg@309 227 private static TargetType[] buildTargets() {
jjg@309 228 TargetType[] targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1];
jjg@309 229 TargetType[] alltargets = values();
jjg@309 230 for (TargetType target : alltargets) {
jjg@309 231 if (target.targetTypeValue >= 0)
jjg@309 232 targets[target.targetTypeValue] = target;
jjg@309 233 }
jjg@309 234 for (int i = 0; i <= MAXIMUM_TARGET_TYPE_VALUE; ++i) {
jjg@309 235 if (targets[i] == null)
jjg@309 236 targets[i] = UNKNOWN;
jjg@309 237 }
jjg@309 238 return targets;
jjg@309 239 }
jjg@309 240
jjg@309 241 public static boolean isValidTargetTypeValue(int tag) {
jjg@309 242 if (targets == null)
jjg@309 243 targets = buildTargets();
jjg@309 244
jjg@309 245 if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
jjg@309 246 return true;
jjg@309 247
jjg@309 248 return (tag >= 0 && tag < targets.length);
jjg@309 249 }
jjg@309 250
jjg@309 251 public static TargetType fromTargetTypeValue(int tag) {
jjg@309 252 if (targets == null)
jjg@309 253 targets = buildTargets();
jjg@309 254
jjg@309 255 if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
jjg@309 256 return UNKNOWN;
jjg@309 257
jjg@309 258 if (tag < 0 || tag >= targets.length)
jjg@309 259 throw new IllegalArgumentException("Unknown TargetType: " + tag);
jjg@309 260 return targets[tag];
jjg@309 261 }
jjg@309 262
jjg@309 263 static enum TargetAttribute {
jjg@309 264 HasLocation, HasParameter, HasBound;
jjg@309 265 }
jjg@309 266 }

mercurial