Path: blob/master/src/jdk.jdi/share/classes/com/sun/tools/jdi/LocalVariableImpl.java
41161 views
/*1* Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.jdi;2627import com.sun.jdi.AbsentInformationException;28import com.sun.jdi.ClassNotLoadedException;29import com.sun.jdi.InternalException;30import com.sun.jdi.LocalVariable;31import com.sun.jdi.Location;32import com.sun.jdi.Method;33import com.sun.jdi.StackFrame;34import com.sun.jdi.Type;35import com.sun.jdi.VirtualMachine;3637public class LocalVariableImpl extends MirrorImpl38implements LocalVariable, ValueContainer39{40private final Method method;41private final int slot;42private final Location scopeStart;43private final Location scopeEnd;44private final String name;45private final String signature;46private String genericSignature = null;4748LocalVariableImpl(VirtualMachine vm, Method method,49int slot, Location scopeStart, Location scopeEnd,50String name, String signature,51String genericSignature) {52super(vm);53this.method = method;54this.slot = slot;55this.scopeStart = scopeStart;56this.scopeEnd = scopeEnd;57this.name = name;58this.signature = signature;59if (genericSignature != null && genericSignature.length() > 0) {60this.genericSignature = genericSignature;61} else {62// The Spec says to return null for non-generic types63this.genericSignature = null;64}65}6667public boolean equals(Object obj) {68if ((obj != null) && (obj instanceof LocalVariableImpl)) {69LocalVariableImpl other = (LocalVariableImpl)obj;70return ((slot() == other.slot()) &&71(scopeStart != null) &&72(scopeStart.equals(other.scopeStart)) &&73(super.equals(obj)));74} else {75return false;76}77}7879public int hashCode() {80/*81* TO DO: Better hash code82*/83return ((scopeStart.hashCode() << 4) + slot());84}8586public int compareTo(LocalVariable object) {87LocalVariableImpl other = (LocalVariableImpl)object;8889int rc = scopeStart.compareTo(other.scopeStart);90if (rc == 0) {91rc = slot() - other.slot();92}93return rc;94}9596public String name() {97return name;98}99100/**101* @return a text representation of the declared type102* of this variable.103*/104public String typeName() {105JNITypeParser parser = new JNITypeParser(signature);106return parser.typeName();107}108109public Type type() throws ClassNotLoadedException {110return findType(signature());111}112113public Type findType(String signature) throws ClassNotLoadedException {114ReferenceTypeImpl enclosing = (ReferenceTypeImpl)method.declaringType();115return enclosing.findType(signature);116}117118public String signature() {119return signature;120}121122public String genericSignature() {123return genericSignature;124}125126public boolean isVisible(StackFrame frame) {127validateMirror(frame);128Method frameMethod = frame.location().method();129130if (!frameMethod.equals(method)) {131throw new IllegalArgumentException(132"frame method different than variable's method");133}134135// this is here to cover the possibility that we will136// allow LocalVariables for native methods. If we do137// so we will have to re-examinine this.138if (frameMethod.isNative()) {139return false;140}141142return ((scopeStart.compareTo(frame.location()) <= 0)143&& (scopeEnd.compareTo(frame.location()) >= 0));144}145146public boolean isArgument() {147try {148MethodImpl method = (MethodImpl)scopeStart.method();149return (slot < method.argSlotCount());150} catch (AbsentInformationException e) {151// If this variable object exists, there shouldn't be absent info152throw new InternalException();153}154}155156int slot() {157return slot;158}159160/*161* Compilers/VMs can have byte code ranges for variables of the162* same names that overlap. This is because the byte code ranges163* aren't necessarily scopes; they may have more to do with the164* lifetime of the variable's slot, depending on implementation.165*166* This method determines whether this variable hides an167* identically named variable; ie, their byte code ranges overlap168* this one starts after the given one. If it returns true this169* variable should be preferred when looking for a single variable170* with its name when both variables are visible.171*/172boolean hides(LocalVariable other) {173LocalVariableImpl otherImpl = (LocalVariableImpl)other;174if (!method.equals(otherImpl.method) ||175!name.equals(otherImpl.name)) {176return false;177} else {178return (scopeStart.compareTo(otherImpl.scopeStart) > 0);179}180}181182public String toString() {183return name() + " in " + method.toString() +184"@" + scopeStart.toString();185}186}187188189