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

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1563
bc456436c613
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

jjg@309 1 /*
jjg@1521 2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. 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
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
jjg@309 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle 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 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
jjg@309 24 */
jjg@309 25
jjg@309 26 package com.sun.tools.javac.code;
jjg@309 27
jjg@1521 28 import com.sun.tools.javac.util.Assert;
jjg@1357 29
jjg@309 30 /**
jjg@309 31 * Describes the type of program element an extended annotation (or extended
jjg@309 32 * compound attribute) targets.
jjg@309 33 *
jjg@309 34 * By comparison, a Tree.Kind has enum values for all elements in the AST, and
jjg@309 35 * it does not provide enough resolution for type arguments (i.e., whether an
jjg@309 36 * annotation targets a type argument in a local variable, method return type,
jjg@309 37 * or a typecast).
jjg@309 38 *
jjg@581 39 * <p><b>This is NOT part of any supported API.
jjg@581 40 * If you write code that depends on this, you do so at your own risk.
jjg@309 41 * This code and its internal interfaces are subject to change or
jjg@309 42 * deletion without notice.</b>
jjg@309 43 */
jjg@1521 44 // Code duplicated in com.sun.tools.classfile.TypeAnnotation.TargetType
jjg@309 45 public enum TargetType {
jjg@1521 46 /** For annotations on a class type parameter declaration. */
jjg@1521 47 CLASS_TYPE_PARAMETER(0x00),
jjg@309 48
jjg@1521 49 /** For annotations on a method type parameter declaration. */
jjg@1521 50 METHOD_TYPE_PARAMETER(0x01),
jjg@309 51
jjg@1521 52 /** For annotations on the type of an "extends" or "implements" clause. */
jjg@1521 53 CLASS_EXTENDS(0x10),
jjg@309 54
jjg@1521 55 /** For annotations on a bound of a type parameter of a class. */
jjg@1521 56 CLASS_TYPE_PARAMETER_BOUND(0x11),
jjg@309 57
jjg@1521 58 /** For annotations on a bound of a type parameter of a method. */
jjg@1521 59 METHOD_TYPE_PARAMETER_BOUND(0x12),
jjg@309 60
jjg@1521 61 /** For annotations on a field. */
jjg@1521 62 FIELD(0x13),
jjg@309 63
jjg@1521 64 /** For annotations on a method return type. */
jjg@1521 65 METHOD_RETURN(0x14),
jjg@309 66
jjg@309 67 /** For annotations on the method receiver. */
jjg@1521 68 METHOD_RECEIVER(0x15),
jjg@309 69
jjg@1521 70 /** For annotations on a method parameter. */
jjg@1521 71 METHOD_FORMAL_PARAMETER(0x16),
jjg@309 72
jjg@309 73 /** For annotations on a throws clause in a method declaration. */
jjg@1521 74 THROWS(0x17),
jjg@309 75
jjg@1521 76 /** For annotations on a local variable. */
jjg@1521 77 LOCAL_VARIABLE(0x40, true),
jjg@309 78
jjg@1521 79 /** For annotations on a resource variable. */
jjg@1521 80 RESOURCE_VARIABLE(0x41, true),
jjg@309 81
jjg@1521 82 /** For annotations on an exception parameter. */
jjg@1521 83 EXCEPTION_PARAMETER(0x42, true),
jjg@309 84
jjg@1521 85 /** For annotations on a type test. */
jjg@1563 86 INSTANCEOF(0x43, true),
jjg@309 87
jjg@1521 88 /** For annotations on an object creation expression. */
jjg@1563 89 NEW(0x44, true),
jjg@1563 90
jjg@1563 91 /** For annotations on a constructor reference receiver. */
jjg@1563 92 CONSTRUCTOR_REFERENCE(0x45, true),
jjg@1563 93
jjg@1563 94 /** For annotations on a method reference receiver. */
jjg@1563 95 METHOD_REFERENCE(0x46, true),
jjg@1563 96
jjg@1563 97 /** For annotations on a typecast. */
jjg@1563 98 CAST(0x47, true),
jjg@309 99
jjg@1521 100 /** For annotations on a type argument of an object creation expression. */
jjg@1563 101 CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT(0x48, true),
jjg@309 102
jjg@1521 103 /** For annotations on a type argument of a method call. */
jjg@1563 104 METHOD_INVOCATION_TYPE_ARGUMENT(0x49, true),
jjg@309 105
jjg@1563 106 /** For annotations on a type argument of a constructor reference. */
jjg@1563 107 CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT(0x4A, true),
jjg@1521 108
jjg@1521 109 /** For annotations on a type argument of a method reference. */
jjg@1563 110 METHOD_REFERENCE_TYPE_ARGUMENT(0x4B, true),
jjg@309 111
jjg@309 112 /** For annotations with an unknown target. */
jjg@1521 113 UNKNOWN(0xFF);
jjg@309 114
jjg@1563 115 private static final int MAXIMUM_TARGET_TYPE_VALUE = 0x4B;
jjg@309 116
jjg@309 117 private final int targetTypeValue;
jjg@1521 118 private final boolean isLocal;
jjg@309 119
jjg@1521 120 private TargetType(int targetTypeValue) {
jjg@1521 121 this(targetTypeValue, false);
jjg@309 122 }
jjg@309 123
jjg@1521 124 private TargetType(int targetTypeValue, boolean isLocal) {
jjg@1521 125 if (targetTypeValue < 0
jjg@1521 126 || targetTypeValue > 255)
jjg@1521 127 Assert.error("Attribute type value needs to be an unsigned byte: " + String.format("0x%02X", targetTypeValue));
jjg@1521 128 this.targetTypeValue = targetTypeValue;
jjg@1521 129 this.isLocal = isLocal;
jjg@309 130 }
jjg@309 131
jjg@310 132 /**
jjg@310 133 * Returns whether or not this TargetType represents an annotation whose
jjg@310 134 * target is exclusively a tree in a method body
jjg@310 135 *
jjg@310 136 * Note: wildcard bound targets could target a local tree and a class
jjg@310 137 * member declaration signature tree
jjg@310 138 */
jjg@310 139 public boolean isLocal() {
jjg@1521 140 return isLocal;
jjg@310 141 }
jjg@310 142
jjg@309 143 public int targetTypeValue() {
jjg@309 144 return this.targetTypeValue;
jjg@309 145 }
jjg@309 146
vromero@1442 147 private static final TargetType[] targets;
jjg@309 148
vromero@1442 149 static {
vromero@1442 150 targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1];
jjg@309 151 TargetType[] alltargets = values();
jjg@309 152 for (TargetType target : alltargets) {
jjg@1521 153 if (target.targetTypeValue != UNKNOWN.targetTypeValue)
jjg@309 154 targets[target.targetTypeValue] = target;
jjg@309 155 }
jjg@309 156 for (int i = 0; i <= MAXIMUM_TARGET_TYPE_VALUE; ++i) {
jjg@309 157 if (targets[i] == null)
jjg@309 158 targets[i] = UNKNOWN;
jjg@309 159 }
jjg@309 160 }
jjg@309 161
jjg@309 162 public static boolean isValidTargetTypeValue(int tag) {
jjg@1521 163 if (tag == UNKNOWN.targetTypeValue)
jjg@309 164 return true;
jjg@309 165
jjg@309 166 return (tag >= 0 && tag < targets.length);
jjg@309 167 }
jjg@309 168
jjg@309 169 public static TargetType fromTargetTypeValue(int tag) {
jjg@1521 170 if (tag == UNKNOWN.targetTypeValue)
jjg@309 171 return UNKNOWN;
jjg@309 172
jjg@309 173 if (tag < 0 || tag >= targets.length)
jjg@1521 174 Assert.error("Unknown TargetType: " + tag);
jjg@309 175 return targets[tag];
jjg@309 176 }
jjg@309 177 }

mercurial