1 /*
   2  * Copyright (c) 2014, 2015, 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 java.util.*;
  26 
  27 import jdk.vm.ci.hotspot.HotSpotVMConfig.*;
  28 import jdk.vm.ci.meta.*;
  29 
  30 public final class HotSpotMetaspaceConstantImpl extends PrimitiveConstant implements HotSpotMetaspaceConstant, VMConstant, HotSpotProxified {
  31 
  32     static HotSpotMetaspaceConstantImpl forMetaspaceObject(JavaKind kind, long primitive, Object metaspaceObject, boolean compressed) {
  33         return new HotSpotMetaspaceConstantImpl(kind, primitive, metaspaceObject, compressed);
  34     }
  35 
  36     static Object getMetaspaceObject(Constant constant) {
  37         return ((HotSpotMetaspaceConstantImpl) constant).metaspaceObject;
  38     }
  39 
  40     private final Object metaspaceObject;
  41     private final boolean compressed;
  42 
  43     private HotSpotMetaspaceConstantImpl(JavaKind kind, long primitive, Object metaspaceObject, boolean compressed) {
  44         super(kind, primitive);
  45         this.metaspaceObject = metaspaceObject;
  46         this.compressed = compressed;
  47     }
  48 
  49     @Override
  50     public int hashCode() {
  51         return super.hashCode() ^ System.identityHashCode(metaspaceObject);
  52     }
  53 
  54     @Override
  55     public boolean equals(Object o) {
  56         return o == this || (o instanceof HotSpotMetaspaceConstantImpl && super.equals(o) && Objects.equals(metaspaceObject, ((HotSpotMetaspaceConstantImpl) o).metaspaceObject));
  57     }
  58 
  59     @Override
  60     public String toString() {
  61         return super.toString() + "{" + metaspaceObject + (compressed ? ";compressed}" : "}");
  62     }
  63 
  64     public boolean isCompressed() {
  65         return compressed;
  66     }
  67 
  68     public JavaConstant compress(CompressEncoding encoding) {
  69         assert !isCompressed();
  70         HotSpotMetaspaceConstantImpl res = HotSpotMetaspaceConstantImpl.forMetaspaceObject(JavaKind.Int, encoding.compress(asLong()), metaspaceObject, true);
  71         assert res.isCompressed();
  72         return res;
  73     }
  74 
  75     public JavaConstant uncompress(CompressEncoding encoding) {
  76         assert isCompressed();
  77         HotSpotMetaspaceConstantImpl res = HotSpotMetaspaceConstantImpl.forMetaspaceObject(JavaKind.Long, encoding.uncompress(asInt()), metaspaceObject, false);
  78         assert !res.isCompressed();
  79         return res;
  80     }
  81 
  82     public HotSpotResolvedObjectType asResolvedJavaType() {
  83         if (metaspaceObject instanceof HotSpotResolvedObjectType) {
  84             return (HotSpotResolvedObjectType) metaspaceObject;
  85         }
  86         return null;
  87     }
  88 
  89     public HotSpotResolvedJavaMethod asResolvedJavaMethod() {
  90         if (metaspaceObject instanceof HotSpotResolvedJavaMethod) {
  91             return (HotSpotResolvedJavaMethod) metaspaceObject;
  92         }
  93         return null;
  94     }
  95 
  96     public long rawValue() {
  97         return asLong();
  98     }
  99 }