1 // ASM: a very small and fast Java bytecode manipulation framework 2 // Copyright (c) 2000-2011 INRIA, France Telecom 3 // All rights reserved. 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions 7 // are met: 8 // 1. Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // 2. Redistributions in binary form must reproduce the above copyright 11 // notice, this list of conditions and the following disclaimer in the 12 // documentation and/or other materials provided with the distribution. 13 // 3. Neither the name of the copyright holders nor the names of its 14 // contributors may be used to endorse or promote products derived from 15 // this software without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 // THE POSSIBILITY OF SUCH DAMAGE. 28 package org.objectweb.asm.tree.analysis; 29 30 import org.objectweb.asm.Type; 31 32 /** 33 * A {@link Value} that is represented with its type in a seven types type system. This type system 34 * distinguishes the UNINITIALZED, INT, FLOAT, LONG, DOUBLE, REFERENCE and RETURNADDRESS types. 35 * 36 * @author Eric Bruneton 37 */ 38 public class BasicValue implements Value { 39 40 /** An uninitialized value. */ 41 public static final BasicValue UNINITIALIZED_VALUE = new BasicValue(null); 42 43 /** A byte, boolean, char, short, or int value. */ 44 public static final BasicValue INT_VALUE = new BasicValue(Type.INT_TYPE); 45 46 /** A float value. */ 47 public static final BasicValue FLOAT_VALUE = new BasicValue(Type.FLOAT_TYPE); 48 49 /** A long value. */ 50 public static final BasicValue LONG_VALUE = new BasicValue(Type.LONG_TYPE); 51 52 /** A double value. */ 53 public static final BasicValue DOUBLE_VALUE = new BasicValue(Type.DOUBLE_TYPE); 54 55 /** An object or array reference value. */ 56 public static final BasicValue REFERENCE_VALUE = 57 new BasicValue(Type.getObjectType("java/lang/Object")); 58 59 /** A return address value (produced by a jsr instruction). */ 60 public static final BasicValue RETURNADDRESS_VALUE = new BasicValue(Type.VOID_TYPE); 61 62 /** The {@link Type} of this value, or {@literal null} for uninitialized values. */ 63 private final Type type; 64 65 /** 66 * Constructs a new {@link BasicValue} of the given type. 67 * 68 * @param type the value type. 69 */ BasicValue(final Type type)70 public BasicValue(final Type type) { 71 this.type = type; 72 } 73 74 /** 75 * Returns the {@link Type} of this value. 76 * 77 * @return the {@link Type} of this value. 78 */ getType()79 public Type getType() { 80 return type; 81 } 82 83 @Override getSize()84 public int getSize() { 85 return type == Type.LONG_TYPE || type == Type.DOUBLE_TYPE ? 2 : 1; 86 } 87 88 /** 89 * Returns whether this value corresponds to an object or array reference. 90 * 91 * @return whether this value corresponds to an object or array reference. 92 */ isReference()93 public boolean isReference() { 94 return type != null && (type.getSort() == Type.OBJECT || type.getSort() == Type.ARRAY); 95 } 96 97 @Override equals(final Object value)98 public boolean equals(final Object value) { 99 if (value == this) { 100 return true; 101 } else if (value instanceof BasicValue) { 102 if (type == null) { 103 return ((BasicValue) value).type == null; 104 } else { 105 return type.equals(((BasicValue) value).type); 106 } 107 } else { 108 return false; 109 } 110 } 111 112 @Override hashCode()113 public int hashCode() { 114 return type == null ? 0 : type.hashCode(); 115 } 116 117 @Override toString()118 public String toString() { 119 if (this == UNINITIALIZED_VALUE) { 120 return "."; 121 } else if (this == RETURNADDRESS_VALUE) { 122 return "A"; 123 } else if (this == REFERENCE_VALUE) { 124 return "R"; 125 } else { 126 return type.getDescriptor(); 127 } 128 } 129 } 130