jjg@309: /* jjg@309: * Copyright 2008-2009 Sun Microsystems, Inc. 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 jjg@309: * published by the Free Software Foundation. Sun designates this jjg@309: * particular file as subject to the "Classpath" exception as provided jjg@309: * by Sun 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: * jjg@309: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, jjg@309: * CA 95054 USA or visit www.sun.com if you need additional information or jjg@309: * have any questions. jjg@309: */ jjg@309: jjg@309: package com.sun.tools.javac.code; jjg@309: jjg@309: import static com.sun.tools.javac.code.TargetType.TargetAttribute.*; jjg@309: jjg@309: import java.util.EnumSet; jjg@309: import java.util.Set; jjg@309: 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@309: *

This is NOT part of any API supported by Sun Microsystems. If jjg@309: * 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@309: public enum TargetType { jjg@309: jjg@309: // jjg@309: // Some target types are commented out, because Java doesn't permit such jjg@309: // targets. They are included here to confirm that their omission is jjg@309: // intentional omission not an accidental omission. jjg@309: // jjg@309: jjg@309: /** For annotations on typecasts. */ jjg@309: TYPECAST(0x00), jjg@309: jjg@309: /** For annotations on a type argument or nested array of a typecast. */ jjg@309: TYPECAST_GENERIC_OR_ARRAY(0x01, HasLocation), jjg@309: jjg@309: /** For annotations on type tests. */ jjg@309: INSTANCEOF(0x02), jjg@309: jjg@309: /** For annotations on a type argument or nested array of a type test. */ jjg@309: INSTANCEOF_GENERIC_OR_ARRAY(0x03, HasLocation), jjg@309: jjg@309: /** For annotations on object creation expressions. */ jjg@309: NEW(0x04), jjg@309: jjg@309: /** jjg@309: * For annotations on a type argument or nested array of an object creation jjg@309: * expression. jjg@309: */ jjg@309: NEW_GENERIC_OR_ARRAY(0x05, HasLocation), jjg@309: jjg@309: jjg@309: /** For annotations on the method receiver. */ jjg@309: METHOD_RECEIVER(0x06), jjg@309: jjg@309: // invalid location jjg@309: //@Deprecated METHOD_RECEIVER_GENERIC_OR_ARRAY(0x07, HasLocation), jjg@309: jjg@309: /** For annotations on local variables. */ jjg@309: LOCAL_VARIABLE(0x08), jjg@309: jjg@309: /** For annotations on a type argument or nested array of a local. */ jjg@309: LOCAL_VARIABLE_GENERIC_OR_ARRAY(0x09, HasLocation), jjg@309: jjg@309: // handled by regular annotations jjg@309: //@Deprecated METHOD_RETURN(0x0A), jjg@309: jjg@309: /** jjg@309: * For annotations on a type argument or nested array of a method return jjg@309: * type. jjg@309: */ jjg@309: METHOD_RETURN_GENERIC_OR_ARRAY(0x0B, HasLocation), jjg@309: jjg@309: // handled by regular annotations jjg@309: //@Deprecated METHOD_PARAMETER(0x0C), jjg@309: jjg@309: /** For annotations on a type argument or nested array of a method parameter. */ jjg@309: METHOD_PARAMETER_GENERIC_OR_ARRAY(0x0D, HasLocation), jjg@309: jjg@309: // handled by regular annotations jjg@309: //@Deprecated FIELD(0x0E), jjg@309: jjg@309: /** For annotations on a type argument or nested array of a field. */ jjg@309: FIELD_GENERIC_OR_ARRAY(0x0F, HasLocation), jjg@309: jjg@309: /** For annotations on a bound of a type parameter of a class. */ jjg@309: CLASS_TYPE_PARAMETER_BOUND(0x10, HasBound, HasParameter), jjg@309: jjg@309: /** jjg@309: * For annotations on a type argument or nested array of a bound of a type jjg@309: * parameter of a class. jjg@309: */ jjg@309: CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x11, HasBound, HasLocation, HasParameter), jjg@309: jjg@309: /** For annotations on a bound of a type parameter of a method. */ jjg@309: METHOD_TYPE_PARAMETER_BOUND(0x12, HasBound, HasParameter), jjg@309: jjg@309: /** jjg@309: * For annotations on a type argument or nested array of a bound of a type jjg@309: * parameter of a method. jjg@309: */ jjg@309: METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x13, HasBound, HasLocation, HasParameter), jjg@309: jjg@309: /** For annotations on the type of an "extends" or "implements" clause. */ jjg@309: CLASS_EXTENDS(0x14), jjg@309: jjg@309: /** For annotations on the inner type of an "extends" or "implements" clause. */ jjg@309: CLASS_EXTENDS_GENERIC_OR_ARRAY(0x15, HasLocation), jjg@309: jjg@309: /** For annotations on a throws clause in a method declaration. */ jjg@309: THROWS(0x16), jjg@309: jjg@309: // invalid location jjg@309: //@Deprecated THROWS_GENERIC_OR_ARRAY(0x17, HasLocation), jjg@309: jjg@309: /** For annotations in type arguments of object creation expressions. */ jjg@309: NEW_TYPE_ARGUMENT(0x18), jjg@309: NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x19, HasLocation), jjg@309: jjg@309: METHOD_TYPE_ARGUMENT(0x1A), jjg@309: METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x1B, HasLocation), jjg@309: jjg@309: WILDCARD_BOUND(0x1C, HasBound), jjg@309: WILDCARD_BOUND_GENERIC_OR_ARRAY(0x1D, HasBound, HasLocation), jjg@309: jjg@309: CLASS_LITERAL(0x1E), jjg@309: CLASS_LITERAL_GENERIC_OR_ARRAY(0x1F, HasLocation), jjg@309: jjg@309: METHOD_TYPE_PARAMETER(0x20, HasParameter), jjg@309: jjg@309: // invalid location jjg@309: //@Deprecated METHOD_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x21, HasLocation, HasParameter), jjg@309: jjg@309: CLASS_TYPE_PARAMETER(0x22, HasParameter), jjg@309: jjg@309: // invalid location jjg@309: //@Deprecated CLASS_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x23, HasLocation, HasParameter), jjg@309: jjg@309: /** For annotations with an unknown target. */ jjg@309: UNKNOWN(-1); jjg@309: jjg@309: static final int MAXIMUM_TARGET_TYPE_VALUE = 0x22; jjg@309: jjg@309: private final int targetTypeValue; jjg@309: private Set flags; jjg@309: jjg@309: TargetType(int targetTypeValue, TargetAttribute... attributes) { jjg@309: if (targetTypeValue < Byte.MIN_VALUE jjg@309: || targetTypeValue > Byte.MAX_VALUE) jjg@309: throw new AssertionError("attribute type value needs to be a byte: " + targetTypeValue); jjg@309: this.targetTypeValue = (byte)targetTypeValue; jjg@309: flags = EnumSet.noneOf(TargetAttribute.class); jjg@309: for (TargetAttribute attr : attributes) jjg@309: flags.add(attr); jjg@309: } jjg@309: jjg@309: /** jjg@309: * Returns whether or not this TargetType represents an annotation whose jjg@309: * target is an inner type of a generic or array type. jjg@309: * jjg@309: * @return true if this TargetType represents an annotation on an inner jjg@309: * type, false otherwise jjg@309: */ jjg@309: public boolean hasLocation() { jjg@309: return flags.contains(HasLocation); jjg@309: } jjg@309: jjg@309: public TargetType getGenericComplement() { jjg@309: if (hasLocation()) jjg@309: return this; jjg@309: else jjg@309: return fromTargetTypeValue(targetTypeValue() + 1); jjg@309: } jjg@309: jjg@309: /** jjg@309: * Returns whether or not this TargetType represents an annotation whose jjg@309: * target has a parameter index. jjg@309: * jjg@309: * @return true if this TargetType has a parameter index, jjg@309: * false otherwise jjg@309: */ jjg@309: public boolean hasParameter() { jjg@309: return flags.contains(HasParameter); jjg@309: } jjg@309: jjg@309: /** jjg@309: * Returns whether or not this TargetType represents an annotation whose jjg@309: * target is a type parameter bound. jjg@309: * jjg@309: * @return true if this TargetType represents an type parameter bound jjg@309: * annotation, false otherwise jjg@309: */ jjg@309: public boolean hasBound() { jjg@309: return flags.contains(HasBound); jjg@309: } jjg@309: jjg@309: public int targetTypeValue() { jjg@309: return this.targetTypeValue; jjg@309: } jjg@309: jjg@309: private static TargetType[] targets = null; jjg@309: jjg@309: private static TargetType[] buildTargets() { jjg@309: TargetType[] targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1]; jjg@309: TargetType[] alltargets = values(); jjg@309: for (TargetType target : alltargets) { jjg@309: if (target.targetTypeValue >= 0) 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: return targets; jjg@309: } jjg@309: jjg@309: public static boolean isValidTargetTypeValue(int tag) { jjg@309: if (targets == null) jjg@309: targets = buildTargets(); jjg@309: jjg@309: if (((byte)tag) == ((byte)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@309: if (targets == null) jjg@309: targets = buildTargets(); jjg@309: jjg@309: if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue)) jjg@309: return UNKNOWN; jjg@309: jjg@309: if (tag < 0 || tag >= targets.length) jjg@309: throw new IllegalArgumentException("Unknown TargetType: " + tag); jjg@309: return targets[tag]; jjg@309: } jjg@309: jjg@309: static enum TargetAttribute { jjg@309: HasLocation, HasParameter, HasBound; jjg@309: } jjg@309: }