summaryrefslogtreecommitdiff
path: root/sources/scala/runtime/types/JavaRefArrayType.java
blob: 2b25592f7a02dfe3f09095a87fb5eaa25d1385c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003, LAMP/EPFL                  **
**  __\ \/ /__/ __ |/ /__/ __ |                                         **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$

package scala.runtime.types;

import scala.Type;
import scala.Array;

/**
 * Type for Java arrays of references.
 *
 * @author Michel Schinz
 * @version 1.0
 */

public class JavaRefArrayType extends Type {
    public final int dimensions;
    public final Type elemType;

    public static JavaRefArrayType javaRefArrayType(Type elemType,
                                                    int dimensions) {
        if (elemType instanceof JavaRefArrayType) {
            JavaRefArrayType other = (JavaRefArrayType)elemType;
            return new JavaRefArrayType(other.elemType,
                                        dimensions + other.dimensions);
        } else
            return new JavaRefArrayType(elemType, dimensions);
    }

    private JavaRefArrayType(Type elemType, int dimensions) {
        this.elemType = elemType;
        this.dimensions = dimensions;
    }

    public Array newArray(int size) {
        throw new Error();      // TODO
    }

    public Object defaultValue() {
        return null;
    }

    public boolean isInstance(Object o) {
        assert Statistics.incInstanceOf();
        return this.isSameAsJavaType(o.getClass());
    }

    public boolean isSameType(Type that) {
        return (that instanceof JavaRefArrayType)
            && (elemType.isSameType(((JavaRefArrayType)that).elemType));
    }

    public boolean isSameAsJavaType(Class that) {
        Class thatElemType = that;
        for (int i = 0; i < dimensions && thatElemType != null; ++i)
            thatElemType = thatElemType.getComponentType();

        return (thatElemType != null)
            && (elemType.isSameAsJavaType(thatElemType));
    }

    public boolean isSubType(Type that) {
        return isSameType(that);
    }

    public String toString() {
        return elemType.toString() + "[]";
    }

    public int hashCode() {
        return elemType.hashCode() * 11;
    }
}