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