jjg@309: /*
xdono@323: * Copyright 2003-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 com.sun.tools.javac.util.*;
jjg@309:
jjg@309: /** A type annotation position.
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 class TypeAnnotationPosition {
jjg@309:
jjg@309: public TargetType type = TargetType.UNKNOWN;
jjg@309:
jjg@309: // For generic/array types.
jjg@309: public List location = List.nil();
jjg@309:
jjg@309: // Tree position.
jjg@309: public int pos = -1;
jjg@309:
jjg@309: // For typecasts, type tests, new (and locals, as start_pc).
jjg@310: public boolean isValidOffset = false;
jjg@309: public int offset = -1;
jjg@309:
jjg@309: // For locals. arrays same length
jjg@309: public int[] lvarOffset = new int[] { -1 };
jjg@309: public int[] lvarLength = new int[] { -1 };
jjg@309: public int[] lvarIndex = new int[] { -1 };
jjg@309:
jjg@309: // For type parameter bound
jjg@309: public int bound_index = -1;
jjg@309:
jjg@309: // For type parameter and method parameter
jjg@309: public int parameter_index = -1;
jjg@309:
jjg@309: // For class extends, implements, and throws classes
jjg@309: public int type_index = -2;
jjg@309:
jjg@309: // For wildcards
jjg@309: public TypeAnnotationPosition wildcard_position = null;
jjg@309:
jjg@309: @Override
jjg@309: public String toString() {
jjg@309: StringBuilder sb = new StringBuilder();
jjg@309: sb.append('[');
jjg@309: sb.append(type);
jjg@309:
jjg@309: switch (type) {
jjg@309: // type case
jjg@309: case TYPECAST:
jjg@309: case TYPECAST_GENERIC_OR_ARRAY:
jjg@309: // object creation
jjg@309: case INSTANCEOF:
jjg@309: case INSTANCEOF_GENERIC_OR_ARRAY:
jjg@309: // new expression
jjg@309: case NEW:
jjg@309: case NEW_GENERIC_OR_ARRAY:
jjg@309: case NEW_TYPE_ARGUMENT:
jjg@309: case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@309: sb.append(", offset = ");
jjg@309: sb.append(offset);
jjg@309: break;
jjg@309: // local variable
jjg@309: case LOCAL_VARIABLE:
jjg@309: case LOCAL_VARIABLE_GENERIC_OR_ARRAY:
jjg@309: sb.append(", {");
jjg@309: for (int i = 0; i < lvarOffset.length; ++i) {
jjg@309: if (i != 0) sb.append("; ");
jjg@309: sb.append(", start_pc = ");
jjg@309: sb.append(lvarOffset[i]);
jjg@309: sb.append(", length = ");
jjg@309: sb.append(lvarLength[i]);
jjg@309: sb.append(", index = ");
jjg@309: sb.append(lvarIndex[i]);
jjg@309: }
jjg@309: sb.append("}");
jjg@309: break;
jjg@309: // method receiver
jjg@309: case METHOD_RECEIVER:
jjg@309: // Do nothing
jjg@309: break;
jjg@309: // type parameters
jjg@309: case CLASS_TYPE_PARAMETER:
jjg@309: case METHOD_TYPE_PARAMETER:
jjg@309: sb.append(", param_index = ");
jjg@309: sb.append(parameter_index);
jjg@309: break;
jjg@309: // type parameters bound
jjg@309: case CLASS_TYPE_PARAMETER_BOUND:
jjg@309: case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
jjg@309: case METHOD_TYPE_PARAMETER_BOUND:
jjg@309: case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
jjg@309: sb.append(", param_index = ");
jjg@309: sb.append(parameter_index);
jjg@309: sb.append(", bound_index = ");
jjg@309: sb.append(bound_index);
jjg@309: break;
jjg@309: // wildcard
jjg@309: case WILDCARD_BOUND:
jjg@309: case WILDCARD_BOUND_GENERIC_OR_ARRAY:
jjg@309: sb.append(", wild_card = ");
jjg@309: sb.append(wildcard_position);
jjg@309: break;
jjg@309: // Class extends and implements clauses
jjg@309: case CLASS_EXTENDS:
jjg@309: case CLASS_EXTENDS_GENERIC_OR_ARRAY:
jjg@309: sb.append(", type_index = ");
jjg@309: sb.append(type_index);
jjg@309: break;
jjg@309: // throws
jjg@309: case THROWS:
jjg@309: sb.append(", type_index = ");
jjg@309: sb.append(type_index);
jjg@309: break;
jjg@309: case CLASS_LITERAL:
jjg@309: sb.append(", offset = ");
jjg@309: sb.append(offset);
jjg@309: break;
jjg@309: // method parameter: not specified
jjg@309: case METHOD_PARAMETER_GENERIC_OR_ARRAY:
jjg@309: sb.append(", param_index = ");
jjg@309: sb.append(parameter_index);
jjg@309: break;
jjg@309: // method type argument: wasn't specified
jjg@309: case METHOD_TYPE_ARGUMENT:
jjg@309: case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@309: sb.append(", offset = ");
jjg@309: sb.append(offset);
jjg@309: sb.append(", type_index = ");
jjg@309: sb.append(type_index);
jjg@309: break;
jjg@309: // We don't need to worry abut these
jjg@309: case METHOD_RETURN_GENERIC_OR_ARRAY:
jjg@309: case FIELD_GENERIC_OR_ARRAY:
jjg@309: break;
jjg@309: case UNKNOWN:
jjg@309: break;
jjg@309: default:
jjg@309: // throw new AssertionError("unknown type: " + type);
jjg@309: }
jjg@309:
jjg@309: // Append location data for generics/arrays.
jjg@309: if (type.hasLocation()) {
jjg@309: sb.append(", location = (");
jjg@309: sb.append(location);
jjg@309: sb.append(")");
jjg@309: }
jjg@309:
jjg@309: sb.append(", pos = ");
jjg@309: sb.append(pos);
jjg@309:
jjg@309: sb.append(']');
jjg@309: return sb.toString();
jjg@309: }
jjg@310:
jjg@310: /**
jjg@310: * Indicates whether the target tree of the annotation has been optimized
jjg@310: * away from classfile or not.
jjg@310: * @return true if the target has not been optimized away
jjg@310: */
jjg@310: public boolean emitToClassfile() {
jjg@310: if (type == TargetType.WILDCARD_BOUND
jjg@310: || type == TargetType.WILDCARD_BOUND_GENERIC_OR_ARRAY)
jjg@310: return wildcard_position.isValidOffset;
jjg@310: else
jjg@310: return !type.isLocal() || isValidOffset;
jjg@310: }
jjg@309: }