Skip to content

Commit fa8e62d

Browse files
authored
Removes Painless Type in favor of Java Class from the expression nodes related to function references and lambdas. (#28433)
1 parent b779fb3 commit fa8e62d

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

modules/lang-painless/src/main/java/org/elasticsearch/painless/FunctionRef.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.painless;
2121

2222
import org.elasticsearch.painless.Definition.Method;
23+
import org.objectweb.asm.Type;
2324

2425
import java.lang.invoke.MethodType;
2526
import java.lang.reflect.Modifier;
@@ -61,9 +62,9 @@ public class FunctionRef {
6162
/** factory method type descriptor */
6263
public final String factoryDescriptor;
6364
/** functional interface method as type */
64-
public final org.objectweb.asm.Type interfaceType;
65+
public final Type interfaceType;
6566
/** delegate method type method as type */
66-
public final org.objectweb.asm.Type delegateType;
67+
public final Type delegateType;
6768

6869
/**
6970
* Creates a new FunctionRef, which will resolve {@code type::call} from the whitelist.
@@ -119,8 +120,8 @@ public FunctionRef(Class<?> expected, Method interfaceMethod, Method delegateMet
119120
this.delegateMethod = delegateMethod;
120121

121122
factoryDescriptor = factoryMethodType.toMethodDescriptorString();
122-
interfaceType = org.objectweb.asm.Type.getMethodType(interfaceMethodType.toMethodDescriptorString());
123-
delegateType = org.objectweb.asm.Type.getMethodType(this.delegateMethodType.toMethodDescriptorString());
123+
interfaceType = Type.getMethodType(interfaceMethodType.toMethodDescriptorString());
124+
delegateType = Type.getMethodType(this.delegateMethodType.toMethodDescriptorString());
124125
}
125126

126127
/**

modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ECapturingFunctionRef.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,16 @@ void analyze(Locals locals) {
7676
// static case
7777
if (captured.type.dynamic == false) {
7878
try {
79-
ref = new FunctionRef(
80-
locals.getDefinition(), expected, captured.type.name, call, 1);
79+
ref = new FunctionRef(locals.getDefinition(), expected, captured.type.name, call, 1);
8180

8281
// check casts between the interface method and the delegate method are legal
8382
for (int i = 0; i < ref.interfaceMethod.arguments.size(); ++i) {
84-
Definition.Type from = ref.interfaceMethod.arguments.get(i);
85-
Definition.Type to = ref.delegateMethod.arguments.get(i);
86-
AnalyzerCaster.getLegalCast(location, Definition.TypeToClass(from), Definition.TypeToClass(to), false, true);
83+
Class<?> from = Definition.TypeToClass(ref.interfaceMethod.arguments.get(i));
84+
Class<?> to = Definition.TypeToClass(ref.delegateMethod.arguments.get(i));
85+
AnalyzerCaster.getLegalCast(location, from, to, false, true);
8786
}
8887

89-
if (ref.interfaceMethod.rtn.equals(locals.getDefinition().voidType) == false) {
88+
if (ref.interfaceMethod.rtn.clazz != void.class) {
9089
AnalyzerCaster.getLegalCast(location,
9190
Definition.TypeToClass(ref.delegateMethod.rtn), Definition.TypeToClass(ref.interfaceMethod.rtn), false, true);
9291
}

modules/lang-painless/src/main/java/org/elasticsearch/painless/node/EFunctionRef.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ void analyze(Locals locals) {
8080

8181
// check casts between the interface method and the delegate method are legal
8282
for (int i = 0; i < interfaceMethod.arguments.size(); ++i) {
83-
Definition.Type from = interfaceMethod.arguments.get(i);
84-
Definition.Type to = delegateMethod.arguments.get(i);
85-
AnalyzerCaster.getLegalCast(location, Definition.TypeToClass(from), Definition.TypeToClass(to), false, true);
83+
Class<?> from = Definition.TypeToClass(interfaceMethod.arguments.get(i));
84+
Class<?> to = Definition.TypeToClass(delegateMethod.arguments.get(i));
85+
AnalyzerCaster.getLegalCast(location, from, to, false, true);
8686
}
8787

88-
if (interfaceMethod.rtn.equals(locals.getDefinition().voidType) == false) {
88+
if (interfaceMethod.rtn.clazz != void.class) {
8989
AnalyzerCaster.getLegalCast(
9090
location, Definition.TypeToClass(delegateMethod.rtn), Definition.TypeToClass(interfaceMethod.rtn), false, true);
9191
}

modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ELambda.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.elasticsearch.painless.AnalyzerCaster;
2323
import org.elasticsearch.painless.Definition;
2424
import org.elasticsearch.painless.Definition.Method;
25-
import org.elasticsearch.painless.Definition.Type;
25+
import org.elasticsearch.painless.Definition.def;
2626
import org.elasticsearch.painless.FunctionRef;
2727
import org.elasticsearch.painless.Globals;
2828
import org.elasticsearch.painless.Locals;
@@ -101,14 +101,14 @@ void extractVariables(Set<String> variables) {
101101

102102
@Override
103103
void analyze(Locals locals) {
104-
final Type returnType;
105-
final List<String> actualParamTypeStrs;
104+
Class<?> returnType;
105+
List<String> actualParamTypeStrs;
106106
Method interfaceMethod;
107107
// inspect the target first, set interface method if we know it.
108108
if (expected == null) {
109109
interfaceMethod = null;
110110
// we don't know anything: treat as def
111-
returnType = locals.getDefinition().DefType;
111+
returnType = def.class;
112112
// don't infer any types, replace any null types with def
113113
actualParamTypeStrs = new ArrayList<>(paramTypeStrs.size());
114114
for (String type : paramTypeStrs) {
@@ -131,9 +131,9 @@ void analyze(Locals locals) {
131131
"] in [" + Definition.ClassToName(expected) + "]");
132132
// for method invocation, its allowed to ignore the return value
133133
if (interfaceMethod.rtn.equals(locals.getDefinition().voidType)) {
134-
returnType = locals.getDefinition().DefType;
134+
returnType = def.class;
135135
} else {
136-
returnType = interfaceMethod.rtn;
136+
returnType = Definition.TypeToClass(interfaceMethod.rtn);
137137
}
138138
// replace any null types with the actual type
139139
actualParamTypeStrs = new ArrayList<>(paramTypeStrs.size());
@@ -169,11 +169,11 @@ void analyze(Locals locals) {
169169
paramNames.addAll(paramNameStrs);
170170

171171
// desugar lambda body into a synthetic method
172-
desugared = new SFunction(reserved, location, returnType.name, name,
172+
desugared = new SFunction(reserved, location, Definition.ClassToName(returnType), name,
173173
paramTypes, paramNames, statements, true);
174174
desugared.generateSignature(locals.getDefinition());
175-
desugared.analyze(Locals.newLambdaScope(locals.getProgramScope(), returnType, desugared.parameters,
176-
captures.size(), reserved.getMaxLoopCounter()));
175+
desugared.analyze(Locals.newLambdaScope(locals.getProgramScope(), locals.getDefinition().ClassToType(returnType),
176+
desugared.parameters, captures.size(), reserved.getMaxLoopCounter()));
177177

178178
// setup method reference to synthetic method
179179
if (expected == null) {
@@ -190,12 +190,12 @@ void analyze(Locals locals) {
190190

191191
// check casts between the interface method and the delegate method are legal
192192
for (int i = 0; i < interfaceMethod.arguments.size(); ++i) {
193-
Type from = interfaceMethod.arguments.get(i);
194-
Type to = desugared.parameters.get(i + captures.size()).type;
195-
AnalyzerCaster.getLegalCast(location, Definition.TypeToClass(from), Definition.TypeToClass(to), false, true);
193+
Class<?> from = Definition.TypeToClass(interfaceMethod.arguments.get(i));
194+
Class<?> to = Definition.TypeToClass(desugared.parameters.get(i + captures.size()).type);
195+
AnalyzerCaster.getLegalCast(location, from, to, false, true);
196196
}
197197

198-
if (interfaceMethod.rtn.equals(locals.getDefinition().voidType) == false) {
198+
if (interfaceMethod.rtn.clazz != void.class) {
199199
AnalyzerCaster.getLegalCast(
200200
location, Definition.TypeToClass(desugared.rtnType), Definition.TypeToClass(interfaceMethod.rtn), false, true);
201201
}
@@ -212,7 +212,7 @@ void write(MethodWriter writer, Globals globals) {
212212
writer.writeDebugInfo(location);
213213
// load captures
214214
for (Variable capture : captures) {
215-
writer.visitVarInsn(capture.type.type.getOpcode(Opcodes.ILOAD), capture.getSlot());
215+
writer.visitVarInsn(MethodWriter.getType(capture.type.clazz).getOpcode(Opcodes.ILOAD), capture.getSlot());
216216
}
217217

218218
writer.invokeDynamic(
@@ -230,7 +230,7 @@ void write(MethodWriter writer, Globals globals) {
230230
writer.push((String)null);
231231
// load captures
232232
for (Variable capture : captures) {
233-
writer.visitVarInsn(capture.type.type.getOpcode(Opcodes.ILOAD), capture.getSlot());
233+
writer.visitVarInsn(MethodWriter.getType(capture.type.clazz).getOpcode(Opcodes.ILOAD), capture.getSlot());
234234
}
235235
}
236236

@@ -247,7 +247,7 @@ public String getPointer() {
247247
public org.objectweb.asm.Type[] getCaptures() {
248248
org.objectweb.asm.Type[] types = new org.objectweb.asm.Type[captures.size()];
249249
for (int i = 0; i < types.length; i++) {
250-
types[i] = captures.get(i).type.type;
250+
types[i] = MethodWriter.getType(captures.get(i).type.clazz);
251251
}
252252
return types;
253253
}

0 commit comments

Comments
 (0)