308N/A/*
553N/A * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
308N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
308N/A *
308N/A * This code is free software; you can redistribute it and/or modify it
308N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
308N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
308N/A *
308N/A * This code is distributed in the hope that it will be useful, but WITHOUT
308N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
308N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
308N/A * version 2 for more details (a copy is included in the LICENSE file that
308N/A * accompanied this code).
308N/A *
308N/A * You should have received a copy of the GNU General Public License version
308N/A * 2 along with this work; if not, write to the Free Software Foundation,
308N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
308N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
308N/A */
308N/A
308N/Apackage com.sun.tools.javac.code;
308N/A
308N/Aimport static com.sun.tools.javac.code.TargetType.TargetAttribute.*;
308N/A
308N/Aimport java.util.EnumSet;
308N/Aimport java.util.Set;
308N/A
308N/A/**
308N/A * Describes the type of program element an extended annotation (or extended
308N/A * compound attribute) targets.
308N/A *
308N/A * By comparison, a Tree.Kind has enum values for all elements in the AST, and
308N/A * it does not provide enough resolution for type arguments (i.e., whether an
308N/A * annotation targets a type argument in a local variable, method return type,
308N/A * or a typecast).
308N/A *
580N/A * <p><b>This is NOT part of any supported API.
580N/A * If you write code that depends on this, you do so at your own risk.
308N/A * This code and its internal interfaces are subject to change or
308N/A * deletion without notice.</b>
308N/A */
308N/Apublic enum TargetType {
308N/A
308N/A //
308N/A // Some target types are commented out, because Java doesn't permit such
308N/A // targets. They are included here to confirm that their omission is
308N/A // intentional omission not an accidental omission.
308N/A //
308N/A
308N/A /** For annotations on typecasts. */
309N/A TYPECAST(0x00, IsLocal),
308N/A
308N/A /** For annotations on a type argument or nested array of a typecast. */
309N/A TYPECAST_GENERIC_OR_ARRAY(0x01, HasLocation, IsLocal),
308N/A
308N/A /** For annotations on type tests. */
309N/A INSTANCEOF(0x02, IsLocal),
308N/A
308N/A /** For annotations on a type argument or nested array of a type test. */
309N/A INSTANCEOF_GENERIC_OR_ARRAY(0x03, HasLocation, IsLocal),
308N/A
308N/A /** For annotations on object creation expressions. */
309N/A NEW(0x04, IsLocal),
308N/A
308N/A /**
308N/A * For annotations on a type argument or nested array of an object creation
308N/A * expression.
308N/A */
309N/A NEW_GENERIC_OR_ARRAY(0x05, HasLocation, IsLocal),
308N/A
308N/A
308N/A /** For annotations on the method receiver. */
308N/A METHOD_RECEIVER(0x06),
308N/A
308N/A // invalid location
308N/A //@Deprecated METHOD_RECEIVER_GENERIC_OR_ARRAY(0x07, HasLocation),
308N/A
308N/A /** For annotations on local variables. */
309N/A LOCAL_VARIABLE(0x08, IsLocal),
308N/A
308N/A /** For annotations on a type argument or nested array of a local. */
309N/A LOCAL_VARIABLE_GENERIC_OR_ARRAY(0x09, HasLocation, IsLocal),
308N/A
308N/A // handled by regular annotations
308N/A //@Deprecated METHOD_RETURN(0x0A),
308N/A
308N/A /**
308N/A * For annotations on a type argument or nested array of a method return
308N/A * type.
308N/A */
308N/A METHOD_RETURN_GENERIC_OR_ARRAY(0x0B, HasLocation),
308N/A
308N/A // handled by regular annotations
308N/A //@Deprecated METHOD_PARAMETER(0x0C),
308N/A
308N/A /** For annotations on a type argument or nested array of a method parameter. */
308N/A METHOD_PARAMETER_GENERIC_OR_ARRAY(0x0D, HasLocation),
308N/A
308N/A // handled by regular annotations
308N/A //@Deprecated FIELD(0x0E),
308N/A
308N/A /** For annotations on a type argument or nested array of a field. */
308N/A FIELD_GENERIC_OR_ARRAY(0x0F, HasLocation),
308N/A
308N/A /** For annotations on a bound of a type parameter of a class. */
308N/A CLASS_TYPE_PARAMETER_BOUND(0x10, HasBound, HasParameter),
308N/A
308N/A /**
308N/A * For annotations on a type argument or nested array of a bound of a type
308N/A * parameter of a class.
308N/A */
308N/A CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x11, HasBound, HasLocation, HasParameter),
308N/A
308N/A /** For annotations on a bound of a type parameter of a method. */
308N/A METHOD_TYPE_PARAMETER_BOUND(0x12, HasBound, HasParameter),
308N/A
308N/A /**
308N/A * For annotations on a type argument or nested array of a bound of a type
308N/A * parameter of a method.
308N/A */
308N/A METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x13, HasBound, HasLocation, HasParameter),
308N/A
308N/A /** For annotations on the type of an "extends" or "implements" clause. */
308N/A CLASS_EXTENDS(0x14),
308N/A
308N/A /** For annotations on the inner type of an "extends" or "implements" clause. */
308N/A CLASS_EXTENDS_GENERIC_OR_ARRAY(0x15, HasLocation),
308N/A
308N/A /** For annotations on a throws clause in a method declaration. */
308N/A THROWS(0x16),
308N/A
308N/A // invalid location
308N/A //@Deprecated THROWS_GENERIC_OR_ARRAY(0x17, HasLocation),
308N/A
308N/A /** For annotations in type arguments of object creation expressions. */
309N/A NEW_TYPE_ARGUMENT(0x18, IsLocal),
309N/A NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x19, HasLocation, IsLocal),
308N/A
309N/A METHOD_TYPE_ARGUMENT(0x1A, IsLocal),
309N/A METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x1B, HasLocation, IsLocal),
308N/A
308N/A WILDCARD_BOUND(0x1C, HasBound),
308N/A WILDCARD_BOUND_GENERIC_OR_ARRAY(0x1D, HasBound, HasLocation),
308N/A
309N/A CLASS_LITERAL(0x1E, IsLocal),
309N/A CLASS_LITERAL_GENERIC_OR_ARRAY(0x1F, HasLocation, IsLocal),
308N/A
308N/A METHOD_TYPE_PARAMETER(0x20, HasParameter),
308N/A
308N/A // invalid location
308N/A //@Deprecated METHOD_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x21, HasLocation, HasParameter),
308N/A
308N/A CLASS_TYPE_PARAMETER(0x22, HasParameter),
308N/A
308N/A // invalid location
308N/A //@Deprecated CLASS_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x23, HasLocation, HasParameter),
308N/A
308N/A /** For annotations with an unknown target. */
308N/A UNKNOWN(-1);
308N/A
308N/A static final int MAXIMUM_TARGET_TYPE_VALUE = 0x22;
308N/A
308N/A private final int targetTypeValue;
308N/A private Set<TargetAttribute> flags;
308N/A
308N/A TargetType(int targetTypeValue, TargetAttribute... attributes) {
308N/A if (targetTypeValue < Byte.MIN_VALUE
308N/A || targetTypeValue > Byte.MAX_VALUE)
308N/A throw new AssertionError("attribute type value needs to be a byte: " + targetTypeValue);
308N/A this.targetTypeValue = (byte)targetTypeValue;
308N/A flags = EnumSet.noneOf(TargetAttribute.class);
308N/A for (TargetAttribute attr : attributes)
308N/A flags.add(attr);
308N/A }
308N/A
308N/A /**
308N/A * Returns whether or not this TargetType represents an annotation whose
308N/A * target is an inner type of a generic or array type.
308N/A *
308N/A * @return true if this TargetType represents an annotation on an inner
308N/A * type, false otherwise
308N/A */
308N/A public boolean hasLocation() {
308N/A return flags.contains(HasLocation);
308N/A }
308N/A
308N/A public TargetType getGenericComplement() {
308N/A if (hasLocation())
308N/A return this;
308N/A else
308N/A return fromTargetTypeValue(targetTypeValue() + 1);
308N/A }
308N/A
308N/A /**
308N/A * Returns whether or not this TargetType represents an annotation whose
308N/A * target has a parameter index.
308N/A *
308N/A * @return true if this TargetType has a parameter index,
308N/A * false otherwise
308N/A */
308N/A public boolean hasParameter() {
308N/A return flags.contains(HasParameter);
308N/A }
308N/A
308N/A /**
308N/A * Returns whether or not this TargetType represents an annotation whose
308N/A * target is a type parameter bound.
308N/A *
308N/A * @return true if this TargetType represents an type parameter bound
308N/A * annotation, false otherwise
308N/A */
308N/A public boolean hasBound() {
308N/A return flags.contains(HasBound);
308N/A }
308N/A
309N/A /**
309N/A * Returns whether or not this TargetType represents an annotation whose
309N/A * target is exclusively a tree in a method body
309N/A *
309N/A * Note: wildcard bound targets could target a local tree and a class
309N/A * member declaration signature tree
309N/A */
309N/A public boolean isLocal() {
309N/A return flags.contains(IsLocal);
309N/A }
309N/A
308N/A public int targetTypeValue() {
308N/A return this.targetTypeValue;
308N/A }
308N/A
308N/A private static TargetType[] targets = null;
308N/A
308N/A private static TargetType[] buildTargets() {
308N/A TargetType[] targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1];
308N/A TargetType[] alltargets = values();
308N/A for (TargetType target : alltargets) {
308N/A if (target.targetTypeValue >= 0)
308N/A targets[target.targetTypeValue] = target;
308N/A }
308N/A for (int i = 0; i <= MAXIMUM_TARGET_TYPE_VALUE; ++i) {
308N/A if (targets[i] == null)
308N/A targets[i] = UNKNOWN;
308N/A }
308N/A return targets;
308N/A }
308N/A
308N/A public static boolean isValidTargetTypeValue(int tag) {
308N/A if (targets == null)
308N/A targets = buildTargets();
308N/A
308N/A if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
308N/A return true;
308N/A
308N/A return (tag >= 0 && tag < targets.length);
308N/A }
308N/A
308N/A public static TargetType fromTargetTypeValue(int tag) {
308N/A if (targets == null)
308N/A targets = buildTargets();
308N/A
308N/A if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
308N/A return UNKNOWN;
308N/A
308N/A if (tag < 0 || tag >= targets.length)
308N/A throw new IllegalArgumentException("Unknown TargetType: " + tag);
308N/A return targets[tag];
308N/A }
308N/A
308N/A static enum TargetAttribute {
309N/A HasLocation, HasParameter, HasBound, IsLocal;
308N/A }
308N/A}