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: Move and Rename Several Methods in the lookup package #32105

Merged
merged 11 commits into from
Jul 16, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

package org.elasticsearch.painless;

import org.elasticsearch.painless.lookup.PainlessLookup;
import org.elasticsearch.painless.lookup.PainlessCast;
import org.elasticsearch.painless.lookup.PainlessLookupUtility;
import org.elasticsearch.painless.lookup.def;

import java.util.Objects;
Expand Down Expand Up @@ -465,8 +465,9 @@ public static PainlessCast getLegalCast(Location location, Class<?> actual, Clas
(actual.isAssignableFrom(expected) && explicit)) {
return PainlessCast.standard(actual, expected, explicit);
} else {
throw location.createError(new ClassCastException(
"Cannot cast from [" + PainlessLookup.ClassToName(actual) + "] to [" + PainlessLookup.ClassToName(expected) + "]."));
throw location.createError(new ClassCastException("Cannot cast from " +
"[" + PainlessLookupUtility.anyTypeToPainlessTypeName(actual) + "] to " +
"[" + PainlessLookupUtility.anyTypeToPainlessTypeName(expected) + "]."));
}
}

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

package org.elasticsearch.painless;

import org.elasticsearch.painless.lookup.PainlessClass;
import org.elasticsearch.painless.lookup.PainlessLookup;
import org.elasticsearch.painless.lookup.PainlessLookupUtility;
import org.elasticsearch.painless.lookup.PainlessMethod;
import org.elasticsearch.painless.lookup.PainlessClass;
import org.elasticsearch.painless.lookup.PainlessMethodKey;

import java.lang.invoke.CallSite;
Expand Down Expand Up @@ -302,7 +303,7 @@ static MethodHandle lookupMethod(PainlessLookup painlessLookup, MethodHandles.Lo
nestedType,
0,
DefBootstrap.REFERENCE,
PainlessLookup.ClassToName(interfaceType));
PainlessLookupUtility.anyTypeToPainlessTypeName(interfaceType));
filter = nested.dynamicInvoker();
} else {
throw new AssertionError();
Expand Down Expand Up @@ -347,7 +348,7 @@ private static MethodHandle lookupReferenceInternal(PainlessLookup painlessLooku
PainlessMethod interfaceMethod = painlessLookup.getPainlessStructFromJavaClass(clazz).functionalMethod;
if (interfaceMethod == null) {
throw new IllegalArgumentException("Cannot convert function reference [" + type + "::" + call + "] " +
"to [" + PainlessLookup.ClassToName(clazz) + "], not a functional interface");
"to [" + PainlessLookupUtility.anyTypeToPainlessTypeName(clazz) + "], not a functional interface");
}
int arity = interfaceMethod.arguments.size() + captures.length;
final MethodHandle handle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.elasticsearch.painless.lookup.PainlessClass;
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;
Expand Down Expand Up @@ -168,7 +169,7 @@ private static PainlessMethod lookup(PainlessLookup painlessLookup, Class<?> exp
PainlessMethod method = painlessLookup.getPainlessStructFromJavaClass(expected).functionalMethod;
if (method == null) {
throw new IllegalArgumentException("Cannot convert function reference [" + type + "::" + call + "] " +
"to [" + PainlessLookup.ClassToName(expected) + "], not a functional interface");
"to [" + PainlessLookupUtility.anyTypeToPainlessTypeName(expected) + "], not a functional interface");
}

// lookup requested method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@

package org.elasticsearch.painless;

import org.elasticsearch.painless.ScriptClassInfo.MethodArgument;
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.elasticsearch.painless.ScriptClassInfo.MethodArgument;

import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -292,7 +293,7 @@ public int getSlot() {
@Override
public String toString() {
StringBuilder b = new StringBuilder();
b.append("Variable[type=").append(PainlessLookup.ClassToName(clazz));
b.append("Variable[type=").append(PainlessLookupUtility.anyTypeToPainlessTypeName(clazz));
b.append(",name=").append(name);
b.append(",slot=").append(slot);
if (readonly) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.painless;

import org.elasticsearch.painless.lookup.PainlessLookup;
import org.elasticsearch.painless.lookup.PainlessLookupUtility;

import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -182,7 +183,7 @@ private MethodArgument methodArgument(PainlessLookup painlessLookup, Class<?> cl

private static Class<?> definitionTypeForClass(PainlessLookup painlessLookup, Class<?> type,
Function<Class<?>, String> unknownErrorMessageSource) {
type = PainlessLookup.ObjectClassTodefClass(type);
type = PainlessLookupUtility.javaObjectTypeToPainlessDefType(type);
Class<?> componentType = type;

while (componentType.isArray()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.antlr.v4.runtime.LexerNoViableAltException;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.misc.Interval;
import org.elasticsearch.painless.lookup.PainlessLookup;
import org.elasticsearch.painless.Location;
import org.elasticsearch.painless.lookup.PainlessLookup;

/**
* A lexer that is customized for painless. It:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.painless.lookup;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
Expand All @@ -30,154 +29,6 @@
*/
public final class PainlessLookup {

public static Class<?> getBoxedType(Class<?> clazz) {
if (clazz == boolean.class) {
return Boolean.class;
} else if (clazz == byte.class) {
return Byte.class;
} else if (clazz == short.class) {
return Short.class;
} else if (clazz == char.class) {
return Character.class;
} else if (clazz == int.class) {
return Integer.class;
} else if (clazz == long.class) {
return Long.class;
} else if (clazz == float.class) {
return Float.class;
} else if (clazz == double.class) {
return Double.class;
}

return clazz;
}

public static Class<?> getUnboxedype(Class<?> clazz) {
if (clazz == Boolean.class) {
return boolean.class;
} else if (clazz == Byte.class) {
return byte.class;
} else if (clazz == Short.class) {
return short.class;
} else if (clazz == Character.class) {
return char.class;
} else if (clazz == Integer.class) {
return int.class;
} else if (clazz == Long.class) {
return long.class;
} else if (clazz == Float.class) {
return float.class;
} else if (clazz == Double.class) {
return double.class;
}

return clazz;
}

public static boolean isConstantType(Class<?> clazz) {
return clazz == boolean.class ||
clazz == byte.class ||
clazz == short.class ||
clazz == char.class ||
clazz == int.class ||
clazz == long.class ||
clazz == float.class ||
clazz == double.class ||
clazz == String.class;
}

public Class<?> getClassFromBinaryName(String painlessType) {
return painlessTypesToJavaClasses.get(painlessType.replace('$', '.'));
}

public static Class<?> ObjectClassTodefClass(Class<?> clazz) {
if (clazz.isArray()) {
Class<?> component = clazz.getComponentType();
int dimensions = 1;

while (component.isArray()) {
component = component.getComponentType();
++dimensions;
}

if (component == Object.class) {
char[] braces = new char[dimensions];
Arrays.fill(braces, '[');

String descriptor = new String(braces) + org.objectweb.asm.Type.getType(def.class).getDescriptor();
org.objectweb.asm.Type type = org.objectweb.asm.Type.getType(descriptor);

try {
return Class.forName(type.getInternalName().replace('/', '.'));
} catch (ClassNotFoundException exception) {
throw new IllegalStateException("internal error", exception);
}
}
} else if (clazz == Object.class) {
return def.class;
}

return clazz;
}

public static Class<?> defClassToObjectClass(Class<?> clazz) {
if (clazz.isArray()) {
Class<?> component = clazz.getComponentType();
int dimensions = 1;

while (component.isArray()) {
component = component.getComponentType();
++dimensions;
}

if (component == def.class) {
char[] braces = new char[dimensions];
Arrays.fill(braces, '[');

String descriptor = new String(braces) + org.objectweb.asm.Type.getType(Object.class).getDescriptor();
org.objectweb.asm.Type type = org.objectweb.asm.Type.getType(descriptor);

try {
return Class.forName(type.getInternalName().replace('/', '.'));
} catch (ClassNotFoundException exception) {
throw new IllegalStateException("internal error", exception);
}
}
} else if (clazz == def.class) {
return Object.class;
}

return clazz;
}

public static String ClassToName(Class<?> clazz) {
if (clazz.isLocalClass() || clazz.isAnonymousClass()) {
return null;
} else if (clazz.isArray()) {
Class<?> component = clazz.getComponentType();
int dimensions = 1;

while (component.isArray()) {
component = component.getComponentType();
++dimensions;
}

if (component == def.class) {
StringBuilder builder = new StringBuilder(def.class.getSimpleName());

for (int dimension = 0; dimension < dimensions; dimension++) {
builder.append("[]");
}

return builder.toString();
}
} else if (clazz == def.class) {
return def.class.getSimpleName();
}

return clazz.getCanonicalName().replace('$', '.');
}

public Collection<PainlessClass> getStructs() {
return javaClassesToPainlessStructs.values();
}
Expand All @@ -190,6 +41,10 @@ public Collection<PainlessClass> getStructs() {
this.javaClassesToPainlessStructs = Collections.unmodifiableMap(javaClassesToPainlessStructs);
}

public Class<?> getClassFromBinaryName(String painlessType) {
return painlessTypesToJavaClasses.get(painlessType.replace('$', '.'));
}

public boolean isSimplePainlessType(String painlessType) {
return painlessTypesToJavaClasses.containsKey(painlessType);
}
Expand All @@ -199,59 +54,6 @@ public PainlessClass getPainlessStructFromJavaClass(Class<?> clazz) {
}

public Class<?> getJavaClassFromPainlessType(String painlessType) {
Class<?> javaClass = painlessTypesToJavaClasses.get(painlessType);

if (javaClass != null) {
return javaClass;
}
int arrayDimensions = 0;
int arrayIndex = painlessType.indexOf('[');

if (arrayIndex != -1) {
int length = painlessType.length();

while (arrayIndex < length) {
if (painlessType.charAt(arrayIndex) == '[' && ++arrayIndex < length && painlessType.charAt(arrayIndex++) == ']') {
++arrayDimensions;
} else {
throw new IllegalArgumentException("invalid painless type [" + painlessType + "].");
}
}

painlessType = painlessType.substring(0, painlessType.indexOf('['));
javaClass = painlessTypesToJavaClasses.get(painlessType);

char braces[] = new char[arrayDimensions];
Arrays.fill(braces, '[');
String descriptor = new String(braces);

if (javaClass == boolean.class) {
descriptor += "Z";
} else if (javaClass == byte.class) {
descriptor += "B";
} else if (javaClass == short.class) {
descriptor += "S";
} else if (javaClass == char.class) {
descriptor += "C";
} else if (javaClass == int.class) {
descriptor += "I";
} else if (javaClass == long.class) {
descriptor += "J";
} else if (javaClass == float.class) {
descriptor += "F";
} else if (javaClass == double.class) {
descriptor += "D";
} else {
descriptor += "L" + javaClass.getName() + ";";
}

try {
return Class.forName(descriptor);
} catch (ClassNotFoundException cnfe) {
throw new IllegalStateException("invalid painless type [" + painlessType + "]", cnfe);
}
}

throw new IllegalArgumentException("invalid painless type [" + painlessType + "]");
return PainlessLookupUtility.painlessTypeNameToPainlessType(painlessType, painlessTypesToJavaClasses);
}
}
Loading