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

Wed, 23 Jan 2013 13:27:24 -0800

author
jjg
date
Wed, 23 Jan 2013 13:27:24 -0800
changeset 1521
71f35e4b93a5
parent 1442
fcf89720ae71
child 1563
bc456436c613
permissions
-rw-r--r--

8006775: JSR 308: Compiler changes in JDK8
Reviewed-by: jjg
Contributed-by: mernst@cs.washington.edu, wmdietl@cs.washington.edu, mpapi@csail.mit.edu, mahmood@notnoop.com

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 typecast. */
jjg@1521 86 CAST(0x43, true),
jjg@309 87
jjg@1521 88 /** For annotations on a type test. */
jjg@1521 89 INSTANCEOF(0x44, true),
jjg@309 90
jjg@1521 91 /** For annotations on an object creation expression. */
jjg@1521 92 NEW(0x45, true),
jjg@309 93
jjg@1521 94 /** For annotations on a type argument of an object creation expression. */
jjg@1521 95 CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT(0x46, true),
jjg@309 96
jjg@1521 97 /** For annotations on a type argument of a method call. */
jjg@1521 98 METHOD_INVOCATION_TYPE_ARGUMENT(0x47, true),
jjg@309 99
jjg@1521 100 /** For annotations on a lambda parameter type. */
jjg@1521 101 LAMBDA_FORMAL_PARAMETER(0x48, true),
jjg@1521 102
jjg@1521 103 /** For annotations on a method reference. */
jjg@1521 104 METHOD_REFERENCE(0x49, true),
jjg@1521 105
jjg@1521 106 /** For annotations on a type argument of a method reference. */
jjg@1521 107 METHOD_REFERENCE_TYPE_ARGUMENT(0x50, true),
jjg@309 108
jjg@309 109 /** For annotations with an unknown target. */
jjg@1521 110 UNKNOWN(0xFF);
jjg@309 111
jjg@1521 112 private static final int MAXIMUM_TARGET_TYPE_VALUE = 0x92;
jjg@309 113
jjg@309 114 private final int targetTypeValue;
jjg@1521 115 private final boolean isLocal;
jjg@309 116
jjg@1521 117 private TargetType(int targetTypeValue) {
jjg@1521 118 this(targetTypeValue, false);
jjg@309 119 }
jjg@309 120
jjg@1521 121 private TargetType(int targetTypeValue, boolean isLocal) {
jjg@1521 122 if (targetTypeValue < 0
jjg@1521 123 || targetTypeValue > 255)
jjg@1521 124 Assert.error("Attribute type value needs to be an unsigned byte: " + String.format("0x%02X", targetTypeValue));
jjg@1521 125 this.targetTypeValue = targetTypeValue;
jjg@1521 126 this.isLocal = isLocal;
jjg@309 127 }
jjg@309 128
jjg@310 129 /**
jjg@310 130 * Returns whether or not this TargetType represents an annotation whose
jjg@310 131 * target is exclusively a tree in a method body
jjg@310 132 *
jjg@310 133 * Note: wildcard bound targets could target a local tree and a class
jjg@310 134 * member declaration signature tree
jjg@310 135 */
jjg@310 136 public boolean isLocal() {
jjg@1521 137 return isLocal;
jjg@310 138 }
jjg@310 139
jjg@309 140 public int targetTypeValue() {
jjg@309 141 return this.targetTypeValue;
jjg@309 142 }
jjg@309 143
vromero@1442 144 private static final TargetType[] targets;
jjg@309 145
vromero@1442 146 static {
vromero@1442 147 targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1];
jjg@309 148 TargetType[] alltargets = values();
jjg@309 149 for (TargetType target : alltargets) {
jjg@1521 150 if (target.targetTypeValue != UNKNOWN.targetTypeValue)
jjg@309 151 targets[target.targetTypeValue] = target;
jjg@309 152 }
jjg@309 153 for (int i = 0; i <= MAXIMUM_TARGET_TYPE_VALUE; ++i) {
jjg@309 154 if (targets[i] == null)
jjg@309 155 targets[i] = UNKNOWN;
jjg@309 156 }
jjg@309 157 }
jjg@309 158
jjg@309 159 public static boolean isValidTargetTypeValue(int tag) {
jjg@1521 160 if (tag == UNKNOWN.targetTypeValue)
jjg@309 161 return true;
jjg@309 162
jjg@309 163 return (tag >= 0 && tag < targets.length);
jjg@309 164 }
jjg@309 165
jjg@309 166 public static TargetType fromTargetTypeValue(int tag) {
jjg@1521 167 if (tag == UNKNOWN.targetTypeValue)
jjg@309 168 return UNKNOWN;
jjg@309 169
jjg@309 170 if (tag < 0 || tag >= targets.length)
jjg@1521 171 Assert.error("Unknown TargetType: " + tag);
jjg@309 172 return targets[tag];
jjg@309 173 }
jjg@309 174 }

mercurial