1 /*
   2  * Copyright (c) 2017, 2019, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.vm.ci.hotspot;
  24 
  25 import jdk.vm.ci.meta.JavaConstant;
  26 import jdk.vm.ci.meta.JavaKind;
  27 
  28 /**
  29  * Represents a constant that was retrieved from a constant pool. Used to keep track of the constant
  30  * pool slot for the constant.
  31  */
  32 public final class HotSpotConstantPoolObject implements JavaConstant {
  33 
  34     public static JavaConstant forObject(HotSpotResolvedObjectType type, int cpi, JavaConstant object) {
  35         return new HotSpotConstantPoolObject(type, cpi, object);
  36     }
  37 
  38     private final JavaConstant constant;
  39     private final HotSpotResolvedObjectType type;
  40     private final int cpi;
  41 
  42     public HotSpotResolvedObjectType getCpType() {
  43         return type;
  44     }
  45 
  46     public int getCpi() {
  47         return cpi;
  48     }
  49 
  50     HotSpotConstantPoolObject(HotSpotResolvedObjectType type, int cpi, JavaConstant constant) {
  51         this.type = type;
  52         this.cpi = cpi;
  53         this.constant = constant;
  54     }
  55 
  56     @Override
  57     public boolean equals(Object o) {
  58         if (o instanceof HotSpotConstantPoolObject) {
  59             HotSpotConstantPoolObject other = (HotSpotConstantPoolObject) o;
  60             return type.equals(other.type) && cpi == other.cpi && constant.equals(other.constant);
  61         }
  62         return false;
  63     }
  64 
  65     @Override
  66     public int hashCode() {
  67         return constant.hashCode() + cpi + type.hashCode();
  68     }
  69 
  70     @Override
  71     public JavaKind getJavaKind() {
  72         return constant.getJavaKind();
  73     }
  74 
  75     @Override
  76     public boolean isNull() {
  77         return constant.isNull();
  78     }
  79 
  80     @Override
  81     public boolean isDefaultForKind() {
  82         return constant.isDefaultForKind();
  83     }
  84 
  85     @Override
  86     public Object asBoxedPrimitive() {
  87         return constant.asBoxedPrimitive();
  88     }
  89 
  90     @Override
  91     public int asInt() {
  92         return constant.asInt();
  93     }
  94 
  95     @Override
  96     public boolean asBoolean() {
  97         return constant.asBoolean();
  98     }
  99 
 100     @Override
 101     public long asLong() {
 102         return constant.asLong();
 103     }
 104 
 105     @Override
 106     public float asFloat() {
 107         return constant.asFloat();
 108     }
 109 
 110     @Override
 111     public double asDouble() {
 112         return 0;
 113     }
 114 
 115     @Override
 116     public String toValueString() {
 117         return getCpType().getName() + getCpi();
 118     }
 119 
 120     @Override
 121     public String toString() {
 122         return super.toString() + "@" + toValueString();
 123     }
 124 
 125 }