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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/TargetType.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,177 @@
     1.4 +/*
     1.5 + * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.code;
    1.30 +
    1.31 +import com.sun.tools.javac.util.Assert;
    1.32 +
    1.33 +/**
    1.34 + * Describes the type of program element an extended annotation (or extended
    1.35 + * compound attribute) targets.
    1.36 + *
    1.37 + * By comparison, a Tree.Kind has enum values for all elements in the AST, and
    1.38 + * it does not provide enough resolution for type arguments (i.e., whether an
    1.39 + * annotation targets a type argument in a local variable, method return type,
    1.40 + * or a typecast).
    1.41 + *
    1.42 + *  <p><b>This is NOT part of any supported API.
    1.43 + *  If you write code that depends on this, you do so at your own risk.
    1.44 + *  This code and its internal interfaces are subject to change or
    1.45 + *  deletion without notice.</b>
    1.46 + */
    1.47 +// Code duplicated in com.sun.tools.classfile.TypeAnnotation.TargetType
    1.48 +public enum TargetType {
    1.49 +    /** For annotations on a class type parameter declaration. */
    1.50 +    CLASS_TYPE_PARAMETER(0x00),
    1.51 +
    1.52 +    /** For annotations on a method type parameter declaration. */
    1.53 +    METHOD_TYPE_PARAMETER(0x01),
    1.54 +
    1.55 +    /** For annotations on the type of an "extends" or "implements" clause. */
    1.56 +    CLASS_EXTENDS(0x10),
    1.57 +
    1.58 +    /** For annotations on a bound of a type parameter of a class. */
    1.59 +    CLASS_TYPE_PARAMETER_BOUND(0x11),
    1.60 +
    1.61 +    /** For annotations on a bound of a type parameter of a method. */
    1.62 +    METHOD_TYPE_PARAMETER_BOUND(0x12),
    1.63 +
    1.64 +    /** For annotations on a field. */
    1.65 +    FIELD(0x13),
    1.66 +
    1.67 +    /** For annotations on a method return type. */
    1.68 +    METHOD_RETURN(0x14),
    1.69 +
    1.70 +    /** For annotations on the method receiver. */
    1.71 +    METHOD_RECEIVER(0x15),
    1.72 +
    1.73 +    /** For annotations on a method parameter. */
    1.74 +    METHOD_FORMAL_PARAMETER(0x16),
    1.75 +
    1.76 +    /** For annotations on a throws clause in a method declaration. */
    1.77 +    THROWS(0x17),
    1.78 +
    1.79 +    /** For annotations on a local variable. */
    1.80 +    LOCAL_VARIABLE(0x40, true),
    1.81 +
    1.82 +    /** For annotations on a resource variable. */
    1.83 +    RESOURCE_VARIABLE(0x41, true),
    1.84 +
    1.85 +    /** For annotations on an exception parameter. */
    1.86 +    EXCEPTION_PARAMETER(0x42, true),
    1.87 +
    1.88 +    /** For annotations on a type test. */
    1.89 +    INSTANCEOF(0x43, true),
    1.90 +
    1.91 +    /** For annotations on an object creation expression. */
    1.92 +    NEW(0x44, true),
    1.93 +
    1.94 +    /** For annotations on a constructor reference receiver. */
    1.95 +    CONSTRUCTOR_REFERENCE(0x45, true),
    1.96 +
    1.97 +    /** For annotations on a method reference receiver. */
    1.98 +    METHOD_REFERENCE(0x46, true),
    1.99 +
   1.100 +    /** For annotations on a typecast. */
   1.101 +    CAST(0x47, true),
   1.102 +
   1.103 +    /** For annotations on a type argument of an object creation expression. */
   1.104 +    CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT(0x48, true),
   1.105 +
   1.106 +    /** For annotations on a type argument of a method call. */
   1.107 +    METHOD_INVOCATION_TYPE_ARGUMENT(0x49, true),
   1.108 +
   1.109 +    /** For annotations on a type argument of a constructor reference. */
   1.110 +    CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT(0x4A, true),
   1.111 +
   1.112 +    /** For annotations on a type argument of a method reference. */
   1.113 +    METHOD_REFERENCE_TYPE_ARGUMENT(0x4B, true),
   1.114 +
   1.115 +    /** For annotations with an unknown target. */
   1.116 +    UNKNOWN(0xFF);
   1.117 +
   1.118 +    private static final int MAXIMUM_TARGET_TYPE_VALUE = 0x4B;
   1.119 +
   1.120 +    private final int targetTypeValue;
   1.121 +    private final boolean isLocal;
   1.122 +
   1.123 +    private TargetType(int targetTypeValue) {
   1.124 +        this(targetTypeValue, false);
   1.125 +    }
   1.126 +
   1.127 +    private TargetType(int targetTypeValue, boolean isLocal) {
   1.128 +        if (targetTypeValue < 0
   1.129 +                || targetTypeValue > 255)
   1.130 +                Assert.error("Attribute type value needs to be an unsigned byte: " + String.format("0x%02X", targetTypeValue));
   1.131 +        this.targetTypeValue = targetTypeValue;
   1.132 +        this.isLocal = isLocal;
   1.133 +    }
   1.134 +
   1.135 +    /**
   1.136 +     * Returns whether or not this TargetType represents an annotation whose
   1.137 +     * target is exclusively a tree in a method body
   1.138 +     *
   1.139 +     * Note: wildcard bound targets could target a local tree and a class
   1.140 +     * member declaration signature tree
   1.141 +     */
   1.142 +    public boolean isLocal() {
   1.143 +        return isLocal;
   1.144 +    }
   1.145 +
   1.146 +    public int targetTypeValue() {
   1.147 +        return this.targetTypeValue;
   1.148 +    }
   1.149 +
   1.150 +    private static final TargetType[] targets;
   1.151 +
   1.152 +    static {
   1.153 +        targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1];
   1.154 +        TargetType[] alltargets = values();
   1.155 +        for (TargetType target : alltargets) {
   1.156 +            if (target.targetTypeValue != UNKNOWN.targetTypeValue)
   1.157 +                targets[target.targetTypeValue] = target;
   1.158 +        }
   1.159 +        for (int i = 0; i <= MAXIMUM_TARGET_TYPE_VALUE; ++i) {
   1.160 +            if (targets[i] == null)
   1.161 +                targets[i] = UNKNOWN;
   1.162 +        }
   1.163 +    }
   1.164 +
   1.165 +    public static boolean isValidTargetTypeValue(int tag) {
   1.166 +        if (tag == UNKNOWN.targetTypeValue)
   1.167 +            return true;
   1.168 +
   1.169 +        return (tag >= 0 && tag < targets.length);
   1.170 +    }
   1.171 +
   1.172 +    public static TargetType fromTargetTypeValue(int tag) {
   1.173 +        if (tag == UNKNOWN.targetTypeValue)
   1.174 +            return UNKNOWN;
   1.175 +
   1.176 +        if (tag < 0 || tag >= targets.length)
   1.177 +            Assert.error("Unknown TargetType: " + tag);
   1.178 +        return targets[tag];
   1.179 +    }
   1.180 +}

mercurial