-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Add grok and dissect methods to runtime fields #68088
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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.spi.annotation; | ||
|
||
/** | ||
* Methods annotated with this must be run at compile time so their arguments | ||
* must all be constant and they produce a constant. | ||
*/ | ||
public class CompileTimeOnlyAnnotation { | ||
public static final String NAME = "compile_time_only"; | ||
|
||
public static final CompileTimeOnlyAnnotation INSTANCE = new CompileTimeOnlyAnnotation(); | ||
|
||
private CompileTimeOnlyAnnotation() {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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.spi.annotation; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Methods annotated with {@link CompileTimeOnlyAnnotation} must be run at | ||
* compile time so their arguments must all be constant and they produce a | ||
* constant. | ||
*/ | ||
public class CompileTimeOnlyAnnotationParser implements WhitelistAnnotationParser { | ||
|
||
public static final CompileTimeOnlyAnnotationParser INSTANCE = new CompileTimeOnlyAnnotationParser(); | ||
|
||
private CompileTimeOnlyAnnotationParser() {} | ||
|
||
@Override | ||
public Object parse(Map<String, String> arguments) { | ||
if (arguments.isEmpty() == false) { | ||
throw new IllegalArgumentException( | ||
"unexpected parameters for [@" + CompileTimeOnlyAnnotation.NAME + "] annotation, found " + arguments | ||
); | ||
} | ||
|
||
return CompileTimeOnlyAnnotation.INSTANCE; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
import org.elasticsearch.painless.spi.WhitelistField; | ||
import org.elasticsearch.painless.spi.WhitelistInstanceBinding; | ||
import org.elasticsearch.painless.spi.WhitelistMethod; | ||
import org.elasticsearch.painless.spi.annotation.CompileTimeOnlyAnnotation; | ||
import org.elasticsearch.painless.spi.annotation.InjectConstantAnnotation; | ||
import org.elasticsearch.painless.spi.annotation.NoImportAnnotation; | ||
import org.objectweb.asm.ClassWriter; | ||
|
@@ -174,7 +175,8 @@ public static PainlessLookup buildFromWhitelists(List<Whitelist> whitelists) { | |
origin = whitelistInstanceBinding.origin; | ||
painlessLookupBuilder.addPainlessInstanceBinding( | ||
whitelistInstanceBinding.targetInstance, whitelistInstanceBinding.methodName, | ||
whitelistInstanceBinding.returnCanonicalTypeName, whitelistInstanceBinding.canonicalTypeNameParameters); | ||
whitelistInstanceBinding.returnCanonicalTypeName, whitelistInstanceBinding.canonicalTypeNameParameters, | ||
whitelistInstanceBinding.painlessAnnotations); | ||
} | ||
} | ||
} catch (Exception exception) { | ||
|
@@ -393,6 +395,10 @@ public void addPainlessConstructor(Class<?> targetClass, List<Class<?>> typePara | |
"[[" + targetCanonicalClassName + "], " + typesToCanonicalTypeNames(typeParameters) + "]", iae); | ||
} | ||
|
||
if (annotations.containsKey(CompileTimeOnlyAnnotation.class)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessarily for this PR - I wonder if it would make sense to allow annotations to know what they're applied to as part of the annotation parser so we could reject invalid types there where type is constructor, method, field, binding, etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. As we add more of these this kind of one off stuff isn't going to be nice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added an issue (#68322) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
throw new IllegalArgumentException("constructors can't have @" + CompileTimeOnlyAnnotation.NAME); | ||
} | ||
|
||
MethodType methodType = methodHandle.type(); | ||
|
||
String painlessConstructorKey = buildPainlessConstructorKey(typeParametersSize); | ||
|
@@ -574,6 +580,10 @@ public void addPainlessMethod(Class<?> targetClass, Class<?> augmentedClass, | |
} | ||
} | ||
|
||
if (annotations.containsKey(CompileTimeOnlyAnnotation.class)) { | ||
throw new IllegalArgumentException("regular methods can't have @" + CompileTimeOnlyAnnotation.NAME); | ||
} | ||
|
||
MethodType methodType = methodHandle.type(); | ||
boolean isStatic = augmentedClass == null && Modifier.isStatic(javaMethod.getModifiers()); | ||
String painlessMethodKey = buildPainlessMethodKey(methodName, typeParametersSize); | ||
|
@@ -989,6 +999,10 @@ public void addPainlessClassBinding(Class<?> targetClass, String methodName, Cla | |
"invalid method name [" + methodName + "] for class binding [" + targetCanonicalClassName + "]."); | ||
} | ||
|
||
if (annotations.containsKey(CompileTimeOnlyAnnotation.class)) { | ||
throw new IllegalArgumentException("class bindings can't have @" + CompileTimeOnlyAnnotation.NAME); | ||
} | ||
|
||
Method[] javaMethods = targetClass.getMethods(); | ||
Method javaMethod = null; | ||
|
||
|
@@ -1079,7 +1093,8 @@ public void addPainlessClassBinding(Class<?> targetClass, String methodName, Cla | |
} | ||
|
||
public void addPainlessInstanceBinding(Object targetInstance, | ||
String methodName, String returnCanonicalTypeName, List<String> canonicalTypeNameParameters) { | ||
String methodName, String returnCanonicalTypeName, List<String> canonicalTypeNameParameters, | ||
Map<Class<?>, Object> painlessAnnotations) { | ||
|
||
Objects.requireNonNull(targetInstance); | ||
Objects.requireNonNull(methodName); | ||
|
@@ -1108,10 +1123,16 @@ public void addPainlessInstanceBinding(Object targetInstance, | |
"[[" + targetCanonicalClassName + "], [" + methodName + "], " + canonicalTypeNameParameters + "]"); | ||
} | ||
|
||
addPainlessInstanceBinding(targetInstance, methodName, returnType, typeParameters); | ||
addPainlessInstanceBinding(targetInstance, methodName, returnType, typeParameters, painlessAnnotations); | ||
} | ||
|
||
public void addPainlessInstanceBinding(Object targetInstance, String methodName, Class<?> returnType, List<Class<?>> typeParameters) { | ||
public void addPainlessInstanceBinding( | ||
Object targetInstance, | ||
String methodName, | ||
Class<?> returnType, | ||
List<Class<?>> typeParameters, | ||
Map<Class<?>, Object> painlessAnnotations | ||
) { | ||
Objects.requireNonNull(targetInstance); | ||
Objects.requireNonNull(methodName); | ||
Objects.requireNonNull(returnType); | ||
|
@@ -1189,7 +1210,7 @@ public void addPainlessInstanceBinding(Object targetInstance, String methodName, | |
|
||
PainlessInstanceBinding existingPainlessInstanceBinding = painlessMethodKeysToPainlessInstanceBindings.get(painlessMethodKey); | ||
PainlessInstanceBinding newPainlessInstanceBinding = | ||
new PainlessInstanceBinding(targetInstance, javaMethod, returnType, typeParameters); | ||
new PainlessInstanceBinding(targetInstance, javaMethod, returnType, typeParameters, painlessAnnotations); | ||
|
||
if (existingPainlessInstanceBinding == null) { | ||
newPainlessInstanceBinding = painlessInstanceBindingCache.computeIfAbsent(newPainlessInstanceBinding, key -> key); | ||
|
@@ -1200,11 +1221,13 @@ public void addPainlessInstanceBinding(Object targetInstance, String methodName, | |
"[[" + targetCanonicalClassName + "], " + | ||
"[" + methodName + "], " + | ||
"[" + typeToCanonicalTypeName(returnType) + "], " + | ||
typesToCanonicalTypeNames(typeParameters) + "] and " + | ||
typesToCanonicalTypeNames(typeParameters) + "], " + | ||
painlessAnnotations + " and " + | ||
"[[" + targetCanonicalClassName + "], " + | ||
"[" + methodName + "], " + | ||
"[" + typeToCanonicalTypeName(existingPainlessInstanceBinding.returnType) + "], " + | ||
typesToCanonicalTypeNames(existingPainlessInstanceBinding.typeParameters) + "]"); | ||
typesToCanonicalTypeNames(existingPainlessInstanceBinding.typeParameters) + "], " + | ||
existingPainlessInstanceBinding.annotations); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know we haven't been great about this on previous annotations, but would you please add sentence or two on what this one does as a class-style JavaDoc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+++