forked from elastic/elasticsearch
/
EFunctionRef.java
81 lines (71 loc) · 2.86 KB
/
EFunctionRef.java
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
81
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.painless.node;
import org.elasticsearch.painless.Definition;
import org.elasticsearch.painless.FunctionRef;
import org.elasticsearch.painless.Location;
import org.elasticsearch.painless.MethodWriter;
import org.elasticsearch.painless.Variables;
import static org.elasticsearch.painless.WriterConstants.LAMBDA_BOOTSTRAP_HANDLE;
import java.lang.invoke.LambdaMetafactory;
/**
* Represents a function reference.
*/
public class EFunctionRef extends AExpression {
public final String type;
public final String call;
private FunctionRef ref;
public EFunctionRef(Location location, String type, String call) {
super(location);
this.type = type;
this.call = call;
}
@Override
void analyze(Variables variables) {
if (expected == null) {
ref = null;
actual = Definition.getType("String");
} else {
try {
ref = new FunctionRef(expected.clazz, type, call);
} catch (IllegalArgumentException e) {
throw createError(e);
}
actual = expected;
}
}
@Override
void write(MethodWriter writer) {
if (ref == null) {
writer.push(type + "." + call);
} else {
writer.writeDebugInfo(location);
// currently if the interface differs, we ask for a bridge, but maybe we should do smarter checking?
// either way, stuff will fail if its wrong :)
if (ref.interfaceType.equals(ref.samMethodType)) {
writer.invokeDynamic(ref.invokedName, ref.invokedType.getDescriptor(), LAMBDA_BOOTSTRAP_HANDLE,
ref.samMethodType, ref.implMethod, ref.samMethodType, 0);
} else {
writer.invokeDynamic(ref.invokedName, ref.invokedType.getDescriptor(), LAMBDA_BOOTSTRAP_HANDLE,
ref.samMethodType, ref.implMethod, ref.samMethodType,
LambdaMetafactory.FLAG_BRIDGES, 1, ref.interfaceType);
}
}
}
}