jjg@309: /* jjg@1521: * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. jjg@309: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@309: * jjg@309: * This code is free software; you can redistribute it and/or modify it jjg@309: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this jjg@309: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. jjg@309: * jjg@309: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@309: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@309: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@309: * version 2 for more details (a copy is included in the LICENSE file that jjg@309: * accompanied this code). jjg@309: * jjg@309: * You should have received a copy of the GNU General Public License version jjg@309: * 2 along with this work; if not, write to the Free Software Foundation, jjg@309: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@309: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@309: */ jjg@309: jjg@309: package com.sun.tools.javac.code; jjg@309: jjg@1521: import com.sun.tools.javac.util.Assert; jjg@1357: jjg@309: /** jjg@309: * Describes the type of program element an extended annotation (or extended jjg@309: * compound attribute) targets. jjg@309: * jjg@309: * By comparison, a Tree.Kind has enum values for all elements in the AST, and jjg@309: * it does not provide enough resolution for type arguments (i.e., whether an jjg@309: * annotation targets a type argument in a local variable, method return type, jjg@309: * or a typecast). jjg@309: * jjg@581: *

This is NOT part of any supported API. jjg@581: * If you write code that depends on this, you do so at your own risk. jjg@309: * This code and its internal interfaces are subject to change or jjg@309: * deletion without notice. jjg@309: */ jjg@1521: // Code duplicated in com.sun.tools.classfile.TypeAnnotation.TargetType jjg@309: public enum TargetType { jjg@1521: /** For annotations on a class type parameter declaration. */ jjg@1521: CLASS_TYPE_PARAMETER(0x00), jjg@309: jjg@1521: /** For annotations on a method type parameter declaration. */ jjg@1521: METHOD_TYPE_PARAMETER(0x01), jjg@309: jjg@1521: /** For annotations on the type of an "extends" or "implements" clause. */ jjg@1521: CLASS_EXTENDS(0x10), jjg@309: jjg@1521: /** For annotations on a bound of a type parameter of a class. */ jjg@1521: CLASS_TYPE_PARAMETER_BOUND(0x11), jjg@309: jjg@1521: /** For annotations on a bound of a type parameter of a method. */ jjg@1521: METHOD_TYPE_PARAMETER_BOUND(0x12), jjg@309: jjg@1521: /** For annotations on a field. */ jjg@1521: FIELD(0x13), jjg@309: jjg@1521: /** For annotations on a method return type. */ jjg@1521: METHOD_RETURN(0x14), jjg@309: jjg@309: /** For annotations on the method receiver. */ jjg@1521: METHOD_RECEIVER(0x15), jjg@309: jjg@1521: /** For annotations on a method parameter. */ jjg@1521: METHOD_FORMAL_PARAMETER(0x16), jjg@309: jjg@309: /** For annotations on a throws clause in a method declaration. */ jjg@1521: THROWS(0x17), jjg@309: jjg@1521: /** For annotations on a local variable. */ jjg@1521: LOCAL_VARIABLE(0x40, true), jjg@309: jjg@1521: /** For annotations on a resource variable. */ jjg@1521: RESOURCE_VARIABLE(0x41, true), jjg@309: jjg@1521: /** For annotations on an exception parameter. */ jjg@1521: EXCEPTION_PARAMETER(0x42, true), jjg@309: jjg@1521: /** For annotations on a type test. */ jjg@1563: INSTANCEOF(0x43, true), jjg@309: jjg@1521: /** For annotations on an object creation expression. */ jjg@1563: NEW(0x44, true), jjg@1563: jjg@1563: /** For annotations on a constructor reference receiver. */ jjg@1563: CONSTRUCTOR_REFERENCE(0x45, true), jjg@1563: jjg@1563: /** For annotations on a method reference receiver. */ jjg@1563: METHOD_REFERENCE(0x46, true), jjg@1563: jjg@1563: /** For annotations on a typecast. */ jjg@1563: CAST(0x47, true), jjg@309: jjg@1521: /** For annotations on a type argument of an object creation expression. */ jjg@1563: CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT(0x48, true), jjg@309: jjg@1521: /** For annotations on a type argument of a method call. */ jjg@1563: METHOD_INVOCATION_TYPE_ARGUMENT(0x49, true), jjg@309: jjg@1563: /** For annotations on a type argument of a constructor reference. */ jjg@1563: CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT(0x4A, true), jjg@1521: jjg@1521: /** For annotations on a type argument of a method reference. */ jjg@1563: METHOD_REFERENCE_TYPE_ARGUMENT(0x4B, true), jjg@309: jjg@309: /** For annotations with an unknown target. */ jjg@1521: UNKNOWN(0xFF); jjg@309: jjg@1563: private static final int MAXIMUM_TARGET_TYPE_VALUE = 0x4B; jjg@309: jjg@309: private final int targetTypeValue; jjg@1521: private final boolean isLocal; jjg@309: jjg@1521: private TargetType(int targetTypeValue) { jjg@1521: this(targetTypeValue, false); jjg@309: } jjg@309: jjg@1521: private TargetType(int targetTypeValue, boolean isLocal) { jjg@1521: if (targetTypeValue < 0 jjg@1521: || targetTypeValue > 255) jjg@1521: Assert.error("Attribute type value needs to be an unsigned byte: " + String.format("0x%02X", targetTypeValue)); jjg@1521: this.targetTypeValue = targetTypeValue; jjg@1521: this.isLocal = isLocal; jjg@309: } jjg@309: jjg@310: /** jjg@310: * Returns whether or not this TargetType represents an annotation whose jjg@310: * target is exclusively a tree in a method body jjg@310: * jjg@310: * Note: wildcard bound targets could target a local tree and a class jjg@310: * member declaration signature tree jjg@310: */ jjg@310: public boolean isLocal() { jjg@1521: return isLocal; jjg@310: } jjg@310: jjg@309: public int targetTypeValue() { jjg@309: return this.targetTypeValue; jjg@309: } jjg@309: vromero@1442: private static final TargetType[] targets; jjg@309: vromero@1442: static { vromero@1442: targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1]; jjg@309: TargetType[] alltargets = values(); jjg@309: for (TargetType target : alltargets) { jjg@1521: if (target.targetTypeValue != UNKNOWN.targetTypeValue) jjg@309: targets[target.targetTypeValue] = target; jjg@309: } jjg@309: for (int i = 0; i <= MAXIMUM_TARGET_TYPE_VALUE; ++i) { jjg@309: if (targets[i] == null) jjg@309: targets[i] = UNKNOWN; jjg@309: } jjg@309: } jjg@309: jjg@309: public static boolean isValidTargetTypeValue(int tag) { jjg@1521: if (tag == UNKNOWN.targetTypeValue) jjg@309: return true; jjg@309: jjg@309: return (tag >= 0 && tag < targets.length); jjg@309: } jjg@309: jjg@309: public static TargetType fromTargetTypeValue(int tag) { jjg@1521: if (tag == UNKNOWN.targetTypeValue) jjg@309: return UNKNOWN; jjg@309: jjg@309: if (tag < 0 || tag >= targets.length) jjg@1521: Assert.error("Unknown TargetType: " + tag); jjg@309: return targets[tag]; jjg@309: } jjg@309: }