Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Painless: Add PainlessClassBuilder #32141

Merged
merged 2 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.elasticsearch.painless.lookup.PainlessLookup;
import org.elasticsearch.painless.lookup.PainlessLookupUtility;
import org.elasticsearch.painless.lookup.PainlessMethod;
import org.elasticsearch.painless.lookup.PainlessMethodKey;

import java.lang.invoke.CallSite;
import java.lang.invoke.MethodHandle;
Expand Down Expand Up @@ -185,7 +184,7 @@ static MethodHandle arrayLengthGetter(Class<?> arrayType) {
* @throws IllegalArgumentException if no matching whitelisted method was found.
*/
static PainlessMethod lookupMethodInternal(PainlessLookup painlessLookup, Class<?> receiverClass, String name, int arity) {
PainlessMethodKey key = new PainlessMethodKey(name, arity);
String key = PainlessLookupUtility.buildPainlessMethodKey(name, arity);
// check whitelist for matching method
for (Class<?> clazz = receiverClass; clazz != null; clazz = clazz.getSuperclass()) {
PainlessClass struct = painlessLookup.getPainlessStructFromJavaClass(clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.elasticsearch.painless.lookup.PainlessLookup;
import org.elasticsearch.painless.lookup.PainlessLookupUtility;
import org.elasticsearch.painless.lookup.PainlessMethod;
import org.elasticsearch.painless.lookup.PainlessMethodKey;
import org.objectweb.asm.Type;

import java.lang.invoke.MethodType;
Expand Down Expand Up @@ -177,10 +176,11 @@ private static PainlessMethod lookup(PainlessLookup painlessLookup, Class<?> exp
final PainlessMethod impl;
// ctor ref
if ("new".equals(call)) {
impl = struct.constructors.get(new PainlessMethodKey("<init>", method.arguments.size()));
impl = struct.constructors.get(PainlessLookupUtility.buildPainlessMethodKey("<init>", method.arguments.size()));
} else {
// look for a static impl first
PainlessMethod staticImpl = struct.staticMethods.get(new PainlessMethodKey(call, method.arguments.size()));
PainlessMethod staticImpl =
struct.staticMethods.get(PainlessLookupUtility.buildPainlessMethodKey(call, method.arguments.size()));
if (staticImpl == null) {
// otherwise a virtual impl
final int arity;
Expand All @@ -191,7 +191,7 @@ private static PainlessMethod lookup(PainlessLookup painlessLookup, Class<?> exp
// receiver passed
arity = method.arguments.size() - 1;
}
impl = struct.methods.get(new PainlessMethodKey(call, arity));
impl = struct.methods.get(PainlessLookupUtility.buildPainlessMethodKey(call, arity));
} else {
impl = staticImpl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.elasticsearch.painless.lookup.PainlessLookup;
import org.elasticsearch.painless.lookup.PainlessLookupUtility;
import org.elasticsearch.painless.lookup.PainlessMethod;
import org.elasticsearch.painless.lookup.PainlessMethodKey;

import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -144,7 +143,7 @@ public Variable getVariable(Location location, String name) {
}

/** Looks up a method. Returns null if the method does not exist. */
public PainlessMethod getMethod(PainlessMethodKey key) {
public PainlessMethod getMethod(String key) {
PainlessMethod method = lookupMethod(key);
if (method != null) {
return method;
Expand Down Expand Up @@ -200,7 +199,7 @@ public PainlessLookup getPainlessLookup() {
// variable name -> variable
private Map<String,Variable> variables;
// method name+arity -> methods
private Map<PainlessMethodKey,PainlessMethod> methods;
private Map<String,PainlessMethod> methods;

/**
* Create a new Locals
Expand Down Expand Up @@ -238,7 +237,7 @@ private Variable lookupVariable(Location location, String name) {
}

/** Looks up a method at this scope only. Returns null if the method does not exist. */
private PainlessMethod lookupMethod(PainlessMethodKey key) {
private PainlessMethod lookupMethod(String key) {
if (methods == null) {
return null;
}
Expand All @@ -261,7 +260,7 @@ private void addMethod(PainlessMethod method) {
if (methods == null) {
methods = new HashMap<>();
}
methods.put(new PainlessMethodKey(method.name, method.arguments.size()), method);
methods.put(PainlessLookupUtility.buildPainlessMethodKey(method.name, method.arguments.size()), method);
// TODO: check result
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@

package org.elasticsearch.painless.lookup;

import org.objectweb.asm.Type;

import java.lang.invoke.MethodHandle;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public final class PainlessClass {
public final String name;
public final Class<?> clazz;
public final org.objectweb.asm.Type type;
public final Type type;

public final Map<PainlessMethodKey, PainlessMethod> constructors;
public final Map<PainlessMethodKey, PainlessMethod> staticMethods;
public final Map<PainlessMethodKey, PainlessMethod> methods;
public final Map<String, PainlessMethod> constructors;
public final Map<String, PainlessMethod> staticMethods;
public final Map<String, PainlessMethod> methods;

public final Map<String, PainlessField> staticMembers;
public final Map<String, PainlessField> members;
Expand All @@ -41,63 +42,25 @@ public final class PainlessClass {

public final PainlessMethod functionalMethod;

PainlessClass(String name, Class<?> clazz, org.objectweb.asm.Type type) {
PainlessClass(String name, Class<?> clazz, Type type,
Map<String, PainlessMethod> constructors, Map<String, PainlessMethod> staticMethods, Map<String, PainlessMethod> methods,
Map<String, PainlessField> staticMembers, Map<String, PainlessField> members,
Map<String, MethodHandle> getters, Map<String, MethodHandle> setters,
PainlessMethod functionalMethod) {
this.name = name;
this.clazz = clazz;
this.type = type;

constructors = new HashMap<>();
staticMethods = new HashMap<>();
methods = new HashMap<>();

staticMembers = new HashMap<>();
members = new HashMap<>();

getters = new HashMap<>();
setters = new HashMap<>();

functionalMethod = null;
}

private PainlessClass(PainlessClass struct, PainlessMethod functionalMethod) {
name = struct.name;
clazz = struct.clazz;
type = struct.type;
this.constructors = Collections.unmodifiableMap(constructors);
this.staticMethods = Collections.unmodifiableMap(staticMethods);
this.methods = Collections.unmodifiableMap(methods);

constructors = Collections.unmodifiableMap(struct.constructors);
staticMethods = Collections.unmodifiableMap(struct.staticMethods);
methods = Collections.unmodifiableMap(struct.methods);
this.staticMembers = Collections.unmodifiableMap(staticMembers);
this.members = Collections.unmodifiableMap(members);

staticMembers = Collections.unmodifiableMap(struct.staticMembers);
members = Collections.unmodifiableMap(struct.members);

getters = Collections.unmodifiableMap(struct.getters);
setters = Collections.unmodifiableMap(struct.setters);
this.getters = Collections.unmodifiableMap(getters);
this.setters = Collections.unmodifiableMap(setters);

this.functionalMethod = functionalMethod;
}

public PainlessClass freeze(PainlessMethod functionalMethod) {
return new PainlessClass(this, functionalMethod);
}

@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}

if (object == null || getClass() != object.getClass()) {
return false;
}

PainlessClass struct = (PainlessClass)object;

return name.equals(struct.name);
}

@Override
public int hashCode() {
return name.hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.lookup;

import org.objectweb.asm.Type;

import java.lang.invoke.MethodHandle;
import java.util.HashMap;
import java.util.Map;

final class PainlessClassBuilder {
final String name;
final Class<?> clazz;
final Type type;

final Map<String, PainlessMethod> constructors;
final Map<String, PainlessMethod> staticMethods;
final Map<String, PainlessMethod> methods;

final Map<String, PainlessField> staticMembers;
final Map<String, PainlessField> members;

final Map<String, MethodHandle> getters;
final Map<String, MethodHandle> setters;

PainlessMethod functionalMethod;

PainlessClassBuilder(String name, Class<?> clazz, Type type) {
this.name = name;
this.clazz = clazz;
this.type = type;

constructors = new HashMap<>();
staticMethods = new HashMap<>();
methods = new HashMap<>();

staticMembers = new HashMap<>();
members = new HashMap<>();

getters = new HashMap<>();
setters = new HashMap<>();

functionalMethod = null;
}

PainlessClass build() {
return new PainlessClass(name, clazz, type,
constructors, staticMethods, methods,
staticMembers, members,
getters, setters,
functionalMethod);
}
}
Loading