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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1563
bc456436c613
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial