Search in sources :

Example 1 with ImportHandler

use of jakarta.el.ImportHandler in project tomcat by apache.

the class PageContextImpl method getELContext.

@Override
public ELContext getELContext() {
    if (elContext == null) {
        elContext = applicationContext.createELContext(this);
        if (servlet instanceof JspSourceImports) {
            ImportHandler ih = elContext.getImportHandler();
            Set<String> packageImports = ((JspSourceImports) servlet).getPackageImports();
            if (packageImports != null) {
                for (String packageImport : packageImports) {
                    ih.importPackage(packageImport);
                }
            }
            Set<String> classImports = ((JspSourceImports) servlet).getClassImports();
            if (classImports != null) {
                for (String classImport : classImports) {
                    ih.importClass(classImport);
                }
            }
        }
        if (servlet instanceof JspSourceDirectives) {
            if (((JspSourceDirectives) servlet).getErrorOnELNotFound()) {
                elContext.putContext(NotFoundELResolver.class, Boolean.TRUE);
            }
        }
    }
    return this.elContext;
}
Also used : ImportHandler(jakarta.el.ImportHandler)

Example 2 with ImportHandler

use of jakarta.el.ImportHandler in project tomcat by apache.

the class ImportELResolver method getValue.

@Override
public Object getValue(ELContext context, Object base, Object property) {
    Objects.requireNonNull(context);
    Object result = null;
    if (base == null) {
        if (property != null) {
            boolean resolveClass = true;
            // Performance short-cut available when running on Tomcat
            if (AST_IDENTIFIER_KEY != null) {
                // Tomcat will set this key to Boolean.TRUE if the
                // identifier is a stand-alone identifier (i.e.
                // identifier) rather than part of an AstValue (i.e.
                // identifier.something). Imports do not need to be
                // checked if this is a stand-alone identifier
                Boolean value = (Boolean) context.getContext(AST_IDENTIFIER_KEY);
                if (value != null && value.booleanValue()) {
                    resolveClass = false;
                }
            }
            ImportHandler importHandler = context.getImportHandler();
            if (importHandler != null) {
                String key = property.toString();
                Class<?> clazz = null;
                if (resolveClass) {
                    clazz = importHandler.resolveClass(key);
                    if (clazz != null) {
                        result = new ELClass(clazz);
                    }
                }
                if (result == null) {
                    // This might be the name of an imported static field
                    clazz = importHandler.resolveStatic(key);
                    if (clazz != null) {
                        try {
                            result = clazz.getField(key).get(null);
                        } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
                        // Most (all?) of these should have been
                        // prevented by the checks when the import
                        // was defined.
                        }
                    }
                }
            }
        }
    }
    if (result != null) {
        context.setPropertyResolved(base, property);
    }
    return result;
}
Also used : ELClass(jakarta.el.ELClass) ImportHandler(jakarta.el.ImportHandler)

Aggregations

ImportHandler (jakarta.el.ImportHandler)2 ELClass (jakarta.el.ELClass)1