001/*
002 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package jdk.internal.jvmci.hotspot;
024
025import jdk.internal.jvmci.meta.*;
026
027/**
028 * Implementation of {@link JavaType} for unresolved HotSpot classes.
029 */
030public class HotSpotUnresolvedJavaType extends HotSpotJavaType {
031
032    private final HotSpotJVMCIRuntimeProvider runtime;
033
034    public HotSpotUnresolvedJavaType(String name, HotSpotJVMCIRuntimeProvider runtime) {
035        super(name);
036        assert name.charAt(0) == '[' || name.charAt(name.length() - 1) == ';' : name;
037        this.runtime = runtime;
038    }
039
040    /**
041     * Creates an unresolved type for a valid {@link JavaType#getName() type name}.
042     */
043    public static HotSpotUnresolvedJavaType create(HotSpotJVMCIRuntimeProvider runtime, String name) {
044        return new HotSpotUnresolvedJavaType(name, runtime);
045    }
046
047    @Override
048    public JavaType getComponentType() {
049        assert getName().charAt(0) == '[' : "no array class" + getName();
050        return new HotSpotUnresolvedJavaType(getName().substring(1), runtime);
051    }
052
053    @Override
054    public JavaType getArrayClass() {
055        return new HotSpotUnresolvedJavaType('[' + getName(), runtime);
056    }
057
058    @Override
059    public Kind getKind() {
060        return Kind.Object;
061    }
062
063    @Override
064    public int hashCode() {
065        return getName().hashCode();
066    }
067
068    @Override
069    public boolean equals(Object obj) {
070        if (this == obj) {
071            return true;
072        }
073        if (obj == null || !(obj instanceof HotSpotUnresolvedJavaType)) {
074            return false;
075        }
076        HotSpotUnresolvedJavaType that = (HotSpotUnresolvedJavaType) obj;
077        return this.getName().equals(that.getName());
078    }
079
080    @Override
081    public String toString() {
082        return "HotSpotType<" + getName() + ", unresolved>";
083    }
084
085    @Override
086    public ResolvedJavaType resolve(ResolvedJavaType accessingClass) {
087        return (ResolvedJavaType) runtime.lookupType(getName(), (HotSpotResolvedObjectType) accessingClass, true);
088    }
089
090    /**
091     * Try to find a loaded version of this class.
092     *
093     * @param accessingClass
094     * @return the resolved class or null.
095     */
096    ResolvedJavaType reresolve(HotSpotResolvedObjectType accessingClass) {
097        JavaType type = runtime.lookupType(getName(), accessingClass, false);
098        if (type instanceof ResolvedJavaType) {
099            return (ResolvedJavaType) type;
100        }
101        return null;
102    }
103}