@Override public TypeLocator getTypeLocator() { if (this.typeLocator == null) { this.typeLocator = new StandardTypeLocator(); } return this.typeLocator; }
/** * Create a StandardTypeLocator for the given ClassLoader. * @param classLoader the ClassLoader to delegate to */ public StandardTypeLocator(@Nullable ClassLoader classLoader) { this.classLoader = classLoader; // Similar to when writing regular Java code, it only knows about java.lang by default registerImport("java.lang"); }
@Test public void testImports() throws EvaluationException { StandardTypeLocator locator = new StandardTypeLocator(); assertEquals(Integer.class,locator.findType("java.lang.Integer")); assertEquals(String.class,locator.findType("java.lang.String")); List<String> prefixes = locator.getImportPrefixes(); assertEquals(1,prefixes.size()); assertTrue(prefixes.contains("java.lang")); assertFalse(prefixes.contains("java.util")); assertEquals(Boolean.class,locator.findType("Boolean")); // currently does not know about java.util by default // assertEquals(java.util.List.class,locator.findType("List")); try { locator.findType("URL"); fail("Should have failed"); } catch (EvaluationException ee) { SpelEvaluationException sEx = (SpelEvaluationException)ee; assertEquals(SpelMessage.TYPE_NOT_FOUND,sEx.getMessageCode()); } locator.registerImport("java.net"); assertEquals(java.net.URL.class,locator.findType("URL")); }
@Test public void testStandardTypeLocator() { StandardTypeLocator tl = new StandardTypeLocator(); List<String> prefixes = tl.getImportPrefixes(); assertEquals(1, prefixes.size()); tl.registerImport("java.util"); prefixes = tl.getImportPrefixes(); assertEquals(2, prefixes.size()); tl.removeImport("java.util"); prefixes = tl.getImportPrefixes(); assertEquals(1, prefixes.size()); }
@Override public Class<?> findType(String typeName) throws EvaluationException { if (typeName.equals("Spr5899Class")) { return Spr5899Class.class; } if (typeName.equals("Outer")) { return Outer.class; } return super.findType(typeName); } }
@Override public TypeLocator getTypeLocator() { if (this.typeLocator == null) { this.typeLocator = new StandardTypeLocator(); } return this.typeLocator; }
/** * Create a StandardTypeLocator for the given ClassLoader. * @param classLoader the ClassLoader to delegate to */ public StandardTypeLocator(@Nullable ClassLoader classLoader) { this.classLoader = classLoader; // Similar to when writing regular Java code, it only knows about java.lang by default registerImport("java.lang"); }
sec.addPropertyAccessor(new EnvironmentAccessor()); sec.setBeanResolver(new BeanFactoryResolver(evalContext.getBeanFactory())); sec.setTypeLocator(new StandardTypeLocator(evalContext.getBeanFactory().getBeanClassLoader())); ConversionService conversionService = evalContext.getBeanFactory().getConversionService(); if (conversionService != null) {
@Test public void testResolvingList() { StandardEvaluationContext context = TestScenarioCreator.getTestEvaluationContext(); try { assertFalse(parser.parseExpression("T(List)!=null").getValue(context, Boolean.class)); fail("should have failed to find List"); } catch (EvaluationException ee) { // success - List not found } ((StandardTypeLocator) context.getTypeLocator()).registerImport("java.util"); assertTrue(parser.parseExpression("T(List)!=null").getValue(context, Boolean.class)); }
sec.addPropertyAccessor(new EnvironmentAccessor()); sec.setBeanResolver(new BeanFactoryResolver(evalContext.getBeanFactory())); sec.setTypeLocator(new StandardTypeLocator(evalContext.getBeanFactory().getBeanClassLoader())); ConversionService conversionService = evalContext.getBeanFactory().getConversionService(); if (conversionService != null) {
@Override protected void doInit() { Assert.state(this.queryExpression != null || this.collectionCallback != null, "no query or collectionCallback is specified"); Assert.state(this.collectionNameExpression != null, "no collection name specified"); if (this.queryExpression != null && this.collectionCallback != null) { throw new IllegalStateException("query and collectionCallback are mutually exclusive"); } if (this.evaluationContext == null) { this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.getBeanFactory()); TypeLocator typeLocator = this.evaluationContext.getTypeLocator(); if (typeLocator instanceof StandardTypeLocator) { ((StandardTypeLocator) typeLocator).registerImport(Query.class.getPackage().getName()); } } if (this.mongoTemplate == null) { this.mongoTemplate = new MongoTemplate(this.mongoDbFactory, this.mongoConverter); } }
@Test public void testStandardEvaluationContext() { StandardEvaluationContext context = new StandardEvaluationContext(); assertNotNull(context.getTypeComparator()); TypeComparator tc = new StandardTypeComparator(); context.setTypeComparator(tc); assertEquals(tc, context.getTypeComparator()); TypeLocator tl = new StandardTypeLocator(); context.setTypeLocator(tl); assertEquals(tl, context.getTypeLocator()); }
@Override protected void onInit() { this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory()); TypeLocator typeLocator = this.evaluationContext.getTypeLocator(); if (typeLocator instanceof StandardTypeLocator) { //Register MongoDB query API package so FQCN can be avoided in query-expression. ((StandardTypeLocator) typeLocator).registerImport("org.springframework.data.mongodb.core.query"); } if (this.mongoTemplate == null) { this.mongoTemplate = new MongoTemplate(this.mongoDbFactory, this.mongoConverter); } this.initialized = true; }
@Override public TypeLocator getTypeLocator() { if (this.typeLocator == null) { this.typeLocator = new StandardTypeLocator(); } return this.typeLocator; }
/** * Create a StandardTypeLocator for the given ClassLoader. * @param classLoader the ClassLoader to delegate to */ public StandardTypeLocator(@Nullable ClassLoader classLoader) { this.classLoader = classLoader; // Similar to when writing regular Java code, it only knows about java.lang by default registerImport("java.lang"); }
public ApplicationRunner(ClassLoader classLoader, String source) { this.classLoader = classLoader; this.source = source; this.config = new SpelParserConfiguration(null, this.classLoader); this.typeLocator = new StandardTypeLocator(this.classLoader); }
@Override protected void doInit() { Assert.state(this.queryExpression != null || this.collectionCallback != null, "no query or collectionCallback is specified"); Assert.state(this.collectionNameExpression != null, "no collection name specified"); if (this.queryExpression != null && this.collectionCallback != null) { throw new IllegalStateException("query and collectionCallback are mutually exclusive"); } if (this.evaluationContext == null) { this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.getBeanFactory()); TypeLocator typeLocator = this.evaluationContext.getTypeLocator(); if (typeLocator instanceof StandardTypeLocator) { ((StandardTypeLocator) typeLocator).registerImport(Query.class.getPackage().getName()); } } if (this.mongoTemplate == null) { this.mongoTemplate = new MongoTemplate(this.mongoDbFactory, this.mongoConverter); } }
public void run(String... args) { ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); try { ClassUtils.overrideThreadContextClassLoader(this.classLoader); Class<?> cls = this.classLoader.loadClass(ContextRunner.class.getName()); this.app = new StandardEvaluationContext(cls.getDeclaredConstructor().newInstance()); this.app.setTypeLocator(new StandardTypeLocator(this.classLoader)); runContext(this.source, defaultProperties(UUID.randomUUID().toString()), args); } catch (Exception e) { logger.error("Cannot deploy", e); } finally { ClassUtils.overrideThreadContextClassLoader(contextLoader); } RuntimeException e = getError(); if (e != null) { throw e; } }
@Override protected void onInit() throws Exception { super.onInit(); TypeLocator typeLocator = getEvaluationContext().getTypeLocator(); if (typeLocator instanceof StandardTypeLocator) { /* * Register the 'org.springframework.integration.aws.support' package * you don't need a FQCN for the 'SnsMessageBuilder'. */ ((StandardTypeLocator) typeLocator).registerImport("org.springframework.integration.aws.support"); } }
private VariableEvaluationContext(PropertyResolver resolver) { this.resolver = resolver; // forbidden to load any classes except default java classes // #{ T(org.springframework.security.core.context.SecurityContextHolder).getContext() } // won't be possible StandardTypeLocator standardTypeLocator = new StandardTypeLocator(getRootClassloader()); setTypeLocator(standardTypeLocator); // disable any method calls // i.e. // #{ T(java.lang.Math).random() } // random() won't be resolve setMethodResolvers(Arrays.asList(new MethodResolver() { @Override public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, List<TypeDescriptor> argumentTypes) throws AccessException { return null; } })); setBeanResolver((context, beanName) -> null); setConstructorResolvers(Arrays.asList(new ConstructorResolver() { @Override public ConstructorExecutor resolve(EvaluationContext context, String typeName, List<TypeDescriptor> argumentTypes) throws AccessException { return null; } })); }