1 /*
   2  * Copyright (c) 2011, 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 jdk.vm.ci.meta.*;
  26 
  27 /**
  28  * Implementation of {@link JavaType} for unresolved HotSpot classes.
  29  */
  30 public class HotSpotUnresolvedJavaType extends HotSpotJavaType {
  31 
  32     private final HotSpotJVMCIRuntimeProvider runtime;
  33 
  34     public HotSpotUnresolvedJavaType(String name, HotSpotJVMCIRuntimeProvider runtime) {
  35         super(name);
  36         assert name.charAt(0) == '[' || name.charAt(name.length() - 1) == ';' : name;
  37         this.runtime = runtime;
  38     }
  39 
  40     /**
  41      * Creates an unresolved type for a valid {@link JavaType#getName() type name}.
  42      */
  43     public static HotSpotUnresolvedJavaType create(HotSpotJVMCIRuntimeProvider runtime, String name) {
  44         return new HotSpotUnresolvedJavaType(name, runtime);
  45     }
  46 
  47     @Override
  48     public JavaType getComponentType() {
  49         assert getName().charAt(0) == '[' : "no array class" + getName();
  50         return new HotSpotUnresolvedJavaType(getName().substring(1), runtime);
  51     }
  52 
  53     @Override
  54     public JavaType getArrayClass() {
  55         return new HotSpotUnresolvedJavaType('[' + getName(), runtime);
  56     }
  57 
  58     @Override
  59     public JavaKind getJavaKind() {
  60         return JavaKind.Object;
  61     }
  62 
  63     @Override
  64     public int hashCode() {
  65         return getName().hashCode();
  66     }
  67 
  68     @Override
  69     public boolean equals(Object obj) {
  70         if (this == obj) {
  71             return true;
  72         }
  73         if (obj == null || !(obj instanceof HotSpotUnresolvedJavaType)) {
  74             return false;
  75         }
  76         HotSpotUnresolvedJavaType that = (HotSpotUnresolvedJavaType) obj;
  77         return this.getName().equals(that.getName());
  78     }
  79 
  80     @Override
  81     public String toString() {
  82         return "HotSpotType<" + getName() + ", unresolved>";
  83     }
  84 
  85     @Override
  86     public ResolvedJavaType resolve(ResolvedJavaType accessingClass) {
  87         return (ResolvedJavaType) runtime.lookupType(getName(), (HotSpotResolvedObjectType) accessingClass, true);
  88     }
  89 
  90     /**
  91      * Try to find a loaded version of this class.
  92      *
  93      * @param accessingClass
  94      * @return the resolved class or null.
  95      */
  96     ResolvedJavaType reresolve(HotSpotResolvedObjectType accessingClass) {
  97         JavaType type = runtime.lookupType(getName(), accessingClass, false);
  98         if (type instanceof ResolvedJavaType) {
  99             return (ResolvedJavaType) type;
 100         }
 101         return null;
 102     }
 103 }