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

Mon, 04 Feb 2013 18:08:53 -0500

author
dholmes
date
Mon, 04 Feb 2013 18:08:53 -0500
changeset 1570
f91144b7da75
parent 1521
71f35e4b93a5
child 1563
bc456436c613
permissions
-rw-r--r--

Merge

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

mercurial