summaryrefslogtreecommitdiff
path: root/src/library/scala/runtime/StructuralCallSite.java
blob: f73b4f08e622d5b6d8126538acb061b273bad9ac (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
package scala.runtime;


import java.lang.invoke.*;
import java.lang.ref.SoftReference;
import java.lang.reflect.Method;

public final class StructuralCallSite {

    private Class<?>[] parameterTypes;
    private SoftReference<MethodCache> cache = new SoftReference<>(new EmptyMethodCache());

    private StructuralCallSite(MethodType callType) {
        parameterTypes = callType.parameterArray();
    }

    public MethodCache get() {
        MethodCache cache = this.cache.get();
        if (cache == null) {
            cache = new EmptyMethodCache();
            this.cache = new SoftReference<>(cache);
        }
        return cache;
    }

    public Method find(Class<?> receiver) {
        return get().find(receiver);
    }

    public Method add(Class<?> receiver, Method m) {
        cache = new SoftReference<MethodCache>(get().add(receiver, m));
        return m;
    }
    public Class<?>[] parameterTypes() {
        return parameterTypes;
    }

    public static CallSite bootstrap(MethodHandles.Lookup lookup, String invokedName,
                                     MethodType invokedType, MethodType reflectiveCallType) throws Throwable {
        StructuralCallSite structuralCallSite = new StructuralCallSite(reflectiveCallType);
        return new ConstantCallSite(MethodHandles.constant(StructuralCallSite.class, structuralCallSite));
    }
}