Search in sources :

Example 1 with WhitelistClass

use of org.opensearch.painless.spi.WhitelistClass 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();
}
Also used : WhitelistConstructor(org.opensearch.painless.spi.WhitelistConstructor) WhitelistField(org.opensearch.painless.spi.WhitelistField) WhitelistClassBinding(org.opensearch.painless.spi.WhitelistClassBinding) Whitelist(org.opensearch.painless.spi.Whitelist) WhitelistInstanceBinding(org.opensearch.painless.spi.WhitelistInstanceBinding) WhitelistClass(org.opensearch.painless.spi.WhitelistClass) WhitelistMethod(org.opensearch.painless.spi.WhitelistMethod) MalformedURLException(java.net.MalformedURLException)

Example 2 with WhitelistClass

use of org.opensearch.painless.spi.WhitelistClass 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);
}
Also used : NoImportAnnotation(org.opensearch.painless.spi.annotation.NoImportAnnotation) HashMap(java.util.HashMap) WhitelistAnnotationParser(org.opensearch.painless.spi.annotation.WhitelistAnnotationParser) Whitelist(org.opensearch.painless.spi.Whitelist) WhitelistClass(org.opensearch.painless.spi.WhitelistClass) WhitelistMethod(org.opensearch.painless.spi.WhitelistMethod)

Aggregations

Whitelist (org.opensearch.painless.spi.Whitelist)2 WhitelistClass (org.opensearch.painless.spi.WhitelistClass)2 WhitelistMethod (org.opensearch.painless.spi.WhitelistMethod)2 MalformedURLException (java.net.MalformedURLException)1 HashMap (java.util.HashMap)1 WhitelistClassBinding (org.opensearch.painless.spi.WhitelistClassBinding)1 WhitelistConstructor (org.opensearch.painless.spi.WhitelistConstructor)1 WhitelistField (org.opensearch.painless.spi.WhitelistField)1 WhitelistInstanceBinding (org.opensearch.painless.spi.WhitelistInstanceBinding)1 NoImportAnnotation (org.opensearch.painless.spi.annotation.NoImportAnnotation)1 WhitelistAnnotationParser (org.opensearch.painless.spi.annotation.WhitelistAnnotationParser)1