use of org.opensearch.painless.spi.WhitelistMethod in project OpenSearch by opensearch-project.
the class PainlessLookupBuilder method buildFromWhitelists.
public static PainlessLookup buildFromWhitelists(List<Whitelist> allowlists) {
PainlessLookupBuilder painlessLookupBuilder = new PainlessLookupBuilder();
String origin = "internal error";
try {
for (Whitelist allowlist : allowlists) {
for (WhitelistClass allowlistClass : allowlist.whitelistClasses) {
origin = allowlistClass.origin;
painlessLookupBuilder.addPainlessClass(allowlist.classLoader, allowlistClass.javaClassName, allowlistClass.painlessAnnotations.containsKey(NoImportAnnotation.class) == false);
}
}
for (Whitelist allowlist : allowlists) {
for (WhitelistClass allowlistClass : allowlist.whitelistClasses) {
String targetCanonicalClassName = allowlistClass.javaClassName.replace('$', '.');
for (WhitelistConstructor allowlistConstructor : allowlistClass.whitelistConstructors) {
origin = allowlistConstructor.origin;
painlessLookupBuilder.addPainlessConstructor(targetCanonicalClassName, allowlistConstructor.canonicalTypeNameParameters, allowlistConstructor.painlessAnnotations);
}
for (WhitelistMethod allowlistMethod : allowlistClass.whitelistMethods) {
origin = allowlistMethod.origin;
painlessLookupBuilder.addPainlessMethod(allowlist.classLoader, targetCanonicalClassName, allowlistMethod.augmentedCanonicalClassName, allowlistMethod.methodName, allowlistMethod.returnCanonicalTypeName, allowlistMethod.canonicalTypeNameParameters, allowlistMethod.painlessAnnotations);
}
for (WhitelistField allowlistField : allowlistClass.whitelistFields) {
origin = allowlistField.origin;
painlessLookupBuilder.addPainlessField(targetCanonicalClassName, allowlistField.fieldName, allowlistField.canonicalTypeNameParameter);
}
}
for (WhitelistMethod allowlistStatic : allowlist.whitelistImportedMethods) {
origin = allowlistStatic.origin;
painlessLookupBuilder.addImportedPainlessMethod(allowlist.classLoader, allowlistStatic.augmentedCanonicalClassName, allowlistStatic.methodName, allowlistStatic.returnCanonicalTypeName, allowlistStatic.canonicalTypeNameParameters, allowlistStatic.painlessAnnotations);
}
for (WhitelistClassBinding allowlistClassBinding : allowlist.whitelistClassBindings) {
origin = allowlistClassBinding.origin;
painlessLookupBuilder.addPainlessClassBinding(allowlist.classLoader, allowlistClassBinding.targetJavaClassName, allowlistClassBinding.methodName, allowlistClassBinding.returnCanonicalTypeName, allowlistClassBinding.canonicalTypeNameParameters, allowlistClassBinding.painlessAnnotations);
}
for (WhitelistInstanceBinding allowlistInstanceBinding : allowlist.whitelistInstanceBindings) {
origin = allowlistInstanceBinding.origin;
painlessLookupBuilder.addPainlessInstanceBinding(allowlistInstanceBinding.targetInstance, allowlistInstanceBinding.methodName, allowlistInstanceBinding.returnCanonicalTypeName, allowlistInstanceBinding.canonicalTypeNameParameters);
}
}
} catch (Exception exception) {
throw new IllegalArgumentException("error loading allowlist(s) " + origin, exception);
}
return painlessLookupBuilder.build();
}
use of org.opensearch.painless.spi.WhitelistMethod in project OpenSearch by opensearch-project.
the class WhitelistLoaderTests method testAnnotations.
public void testAnnotations() {
Map<String, WhitelistAnnotationParser> parsers = new HashMap<>(WhitelistAnnotationParser.BASE_ANNOTATION_PARSERS);
parsers.put(AnnotationTestObject.TestAnnotation.NAME, AnnotationTestObject.TestAnnotationParser.INSTANCE);
Whitelist allowlist = WhitelistLoader.loadFromResourceFiles(Whitelist.class, parsers, "org.opensearch.painless.annotation");
assertEquals(1, allowlist.whitelistClasses.size());
WhitelistClass allowlistClass = allowlist.whitelistClasses.get(0);
assertNotNull(allowlistClass.painlessAnnotations.get(NoImportAnnotation.class));
assertEquals(1, allowlistClass.painlessAnnotations.size());
assertEquals(3, allowlistClass.whitelistMethods.size());
int count = 0;
for (WhitelistMethod allowlistMethod : allowlistClass.whitelistMethods) {
if ("deprecatedMethod".equals(allowlistMethod.methodName)) {
assertEquals("use another method", ((DeprecatedAnnotation) allowlistMethod.painlessAnnotations.get(DeprecatedAnnotation.class)).getMessage());
assertEquals(1, allowlistMethod.painlessAnnotations.size());
++count;
}
if ("annotatedTestMethod".equals(allowlistMethod.methodName)) {
AnnotationTestObject.TestAnnotation ta = ((AnnotationTestObject.TestAnnotation) allowlistMethod.painlessAnnotations.get(AnnotationTestObject.TestAnnotation.class));
assertEquals("one", ta.getOne());
assertEquals("two", ta.getTwo());
assertEquals("three", ta.getThree());
assertEquals(1, allowlistMethod.painlessAnnotations.size());
++count;
}
if ("annotatedMultipleMethod".equals(allowlistMethod.methodName)) {
assertEquals("test", ((DeprecatedAnnotation) allowlistMethod.painlessAnnotations.get(DeprecatedAnnotation.class)).getMessage());
AnnotationTestObject.TestAnnotation ta = ((AnnotationTestObject.TestAnnotation) allowlistMethod.painlessAnnotations.get(AnnotationTestObject.TestAnnotation.class));
assertEquals("one", ta.getOne());
assertEquals("two", ta.getTwo());
assertEquals("three", ta.getThree());
assertEquals(2, allowlistMethod.painlessAnnotations.size());
++count;
}
}
assertEquals(3, count);
}
Aggregations