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

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