org.springframework.expression.spel.support.ReflectiveMethodResolver Java Examples
The following examples show how to use
org.springframework.expression.spel.support.ReflectiveMethodResolver.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MethodReference.java From spring-analysis-note with MIT License | 6 votes |
@Nullable private MethodExecutor getCachedExecutor(EvaluationContext evaluationContext, Object value, @Nullable TypeDescriptor target, List<TypeDescriptor> argumentTypes) { List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers(); if (methodResolvers.size() != 1 || !(methodResolvers.get(0) instanceof ReflectiveMethodResolver)) { // Not a default ReflectiveMethodResolver - don't know whether caching is valid return null; } CachedMethodExecutor executorToCheck = this.cachedExecutor; if (executorToCheck != null && executorToCheck.isSuitable(value, target, argumentTypes)) { return executorToCheck.get(); } this.cachedExecutor = null; return null; }
Example #2
Source File: SpelReproTests.java From spring-analysis-note with MIT License | 6 votes |
/** * Test whether {@link ReflectiveMethodResolver} handles Widening Primitive Conversion. That's passing an 'int' to a * method accepting 'long' is ok. */ @Test public void wideningPrimitiveConversion_SPR8224() throws Exception { class WideningPrimitiveConversion { public int getX(long i) { return 10; } } final Integer INTEGER_VALUE = Integer.valueOf(7); WideningPrimitiveConversion target = new WideningPrimitiveConversion(); EvaluationContext emptyEvalContext = new StandardEvaluationContext(); List<TypeDescriptor> args = new ArrayList<>(); args.add(TypeDescriptor.forObject(INTEGER_VALUE)); MethodExecutor me = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target, "getX", args); final int actual = (Integer) me.execute(emptyEvalContext, target, INTEGER_VALUE).getValue(); final int compiler = target.getX(INTEGER_VALUE); assertEquals(compiler, actual); }
Example #3
Source File: SpelReproTests.java From spring-analysis-note with MIT License | 6 votes |
/** * Test the ability to subclass the ReflectiveMethodResolver and change how it * determines the set of methods for a type. */ @Test public void customStaticFunctions_SPR9038() { ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); List<MethodResolver> methodResolvers = new ArrayList<>(); methodResolvers.add(new ReflectiveMethodResolver() { @Override protected Method[] getMethods(Class<?> type) { try { return new Method[] {Integer.class.getDeclaredMethod("parseInt", String.class, Integer.TYPE)}; } catch (NoSuchMethodException ex) { return new Method[0]; } } }); context.setMethodResolvers(methodResolvers); Expression expression = parser.parseExpression("parseInt('-FF', 16)"); Integer result = expression.getValue(context, "", Integer.class); assertEquals(-255, result.intValue()); }
Example #4
Source File: MethodReference.java From java-technology-stack with MIT License | 6 votes |
@Nullable private MethodExecutor getCachedExecutor(EvaluationContext evaluationContext, Object value, @Nullable TypeDescriptor target, List<TypeDescriptor> argumentTypes) { List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers(); if (methodResolvers.size() != 1 || !(methodResolvers.get(0) instanceof ReflectiveMethodResolver)) { // Not a default ReflectiveMethodResolver - don't know whether caching is valid return null; } CachedMethodExecutor executorToCheck = this.cachedExecutor; if (executorToCheck != null && executorToCheck.isSuitable(value, target, argumentTypes)) { return executorToCheck.get(); } this.cachedExecutor = null; return null; }
Example #5
Source File: SpelReproTests.java From java-technology-stack with MIT License | 6 votes |
/** * Test whether {@link ReflectiveMethodResolver} handles Widening Primitive Conversion. That's passing an 'int' to a * method accepting 'long' is ok. */ @Test public void wideningPrimitiveConversion_SPR8224() throws Exception { class WideningPrimitiveConversion { public int getX(long i) { return 10; } } final Integer INTEGER_VALUE = Integer.valueOf(7); WideningPrimitiveConversion target = new WideningPrimitiveConversion(); EvaluationContext emptyEvalContext = new StandardEvaluationContext(); List<TypeDescriptor> args = new ArrayList<>(); args.add(TypeDescriptor.forObject(INTEGER_VALUE)); MethodExecutor me = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target, "getX", args); final int actual = (Integer) me.execute(emptyEvalContext, target, INTEGER_VALUE).getValue(); final int compiler = target.getX(INTEGER_VALUE); assertEquals(compiler, actual); }
Example #6
Source File: SpelReproTests.java From java-technology-stack with MIT License | 6 votes |
/** * Test the ability to subclass the ReflectiveMethodResolver and change how it * determines the set of methods for a type. */ @Test public void customStaticFunctions_SPR9038() { ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); List<MethodResolver> methodResolvers = new ArrayList<>(); methodResolvers.add(new ReflectiveMethodResolver() { @Override protected Method[] getMethods(Class<?> type) { try { return new Method[] {Integer.class.getDeclaredMethod("parseInt", String.class, Integer.TYPE)}; } catch (NoSuchMethodException ex) { return new Method[0]; } } }); context.setMethodResolvers(methodResolvers); Expression expression = parser.parseExpression("parseInt('-FF', 16)"); Integer result = expression.getValue(context, "", Integer.class); assertEquals(-255, result.intValue()); }
Example #7
Source File: MethodReference.java From lams with GNU General Public License v2.0 | 6 votes |
private MethodExecutor getCachedExecutor(EvaluationContext evaluationContext, Object value, TypeDescriptor target, List<TypeDescriptor> argumentTypes) { List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers(); if (methodResolvers == null || methodResolvers.size() != 1 || !(methodResolvers.get(0) instanceof ReflectiveMethodResolver)) { // Not a default ReflectiveMethodResolver - don't know whether caching is valid return null; } CachedMethodExecutor executorToCheck = this.cachedExecutor; if (executorToCheck != null && executorToCheck.isSuitable(value, target, argumentTypes)) { return executorToCheck.get(); } this.cachedExecutor = null; return null; }
Example #8
Source File: MethodReference.java From spring4-understanding with Apache License 2.0 | 6 votes |
private MethodExecutor getCachedExecutor(EvaluationContext evaluationContext, Object value, TypeDescriptor target, List<TypeDescriptor> argumentTypes) { List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers(); if (methodResolvers == null || methodResolvers.size() != 1 || !(methodResolvers.get(0) instanceof ReflectiveMethodResolver)) { // Not a default ReflectiveMethodResolver - don't know whether caching is valid return null; } CachedMethodExecutor executorToCheck = this.cachedExecutor; if (executorToCheck != null && executorToCheck.isSuitable(value, target, argumentTypes)) { return executorToCheck.get(); } this.cachedExecutor = null; return null; }
Example #9
Source File: SpelReproTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Test whether {@link ReflectiveMethodResolver} handles Widening Primitive Conversion. That's passing an 'int' to a * method accepting 'long' is ok. */ @Test public void wideningPrimitiveConversion_8224() throws Exception { class WideningPrimitiveConversion { public int getX(long i) { return 10; } } final Integer INTEGER_VALUE = Integer.valueOf(7); WideningPrimitiveConversion target = new WideningPrimitiveConversion(); EvaluationContext emptyEvalContext = new StandardEvaluationContext(); List<TypeDescriptor> args = new ArrayList<TypeDescriptor>(); args.add(TypeDescriptor.forObject(INTEGER_VALUE)); MethodExecutor me = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target, "getX", args); final int actual = (Integer) me.execute(emptyEvalContext, target, INTEGER_VALUE).getValue(); final int compiler = target.getX(INTEGER_VALUE); assertEquals(compiler, actual); }
Example #10
Source File: SpelReproTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Test the ability to subclass the ReflectiveMethodResolver and change how it * determines the set of methods for a type. */ @Test public void customStaticFunctions_SPR9038() { ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); List<MethodResolver> methodResolvers = new ArrayList<MethodResolver>(); methodResolvers.add(new ReflectiveMethodResolver() { @Override protected Method[] getMethods(Class<?> type) { try { return new Method[] { Integer.class.getDeclaredMethod("parseInt", new Class<?>[] {String.class, Integer.TYPE})}; } catch (NoSuchMethodException ex) { return new Method[0]; } } }); context.setMethodResolvers(methodResolvers); Expression expression = parser.parseExpression("parseInt('-FF', 16)"); Integer result = expression.getValue(context, "", Integer.class); assertEquals(-255, result.intValue()); }
Example #11
Source File: DefaultEntitySanitizer.java From titus-control-plane with Apache License 2.0 | 5 votes |
public DefaultEntitySanitizer(VerifierMode verifierMode, List<Function<Object, Optional<Object>>> sanitizers, boolean annotationSanitizersEnabled, boolean stdValueSanitizersEnabled, Function<Class<?>, Boolean> includesPredicate, Function<String, Optional<Object>> templateResolver, Map<String, Method> registeredFunctions, Map<String, Object> registeredBeans, Function<Class<?>, Optional<ConstraintValidator<?, ?>>> applicationValidatorFactory) { Supplier<EvaluationContext> spelContextFactory = () -> { StandardEvaluationContext context = new StandardEvaluationContext(); registeredFunctions.forEach(context::registerFunction); context.setBeanResolver((ctx, beanName) -> registeredBeans.get(beanName)); context.setMethodResolvers(Collections.singletonList(new ReflectiveMethodResolver())); return context; }; this.validator = Validation.buildDefaultValidatorFactory() .usingContext() .constraintValidatorFactory(new ConstraintValidatorFactoryWrapper(verifierMode, applicationValidatorFactory, spelContextFactory)) .messageInterpolator(new SpELMessageInterpolator(spelContextFactory)) .getValidator(); List<Function<Object, Optional<Object>>> allSanitizers = new ArrayList<>(); if (annotationSanitizersEnabled) { allSanitizers.add(new AnnotationBasedSanitizer(spelContextFactory.get(), includesPredicate)); } if (stdValueSanitizersEnabled) { allSanitizers.add(new StdValueSanitizer(includesPredicate)); } allSanitizers.add(new TemplateSanitizer(templateResolver, includesPredicate)); allSanitizers.addAll(sanitizers); this.sanitizers = allSanitizers; }
Example #12
Source File: TestValidator.java From titus-control-plane with Apache License 2.0 | 5 votes |
public static Validator testValidator(String alias, Object bean) { Supplier<EvaluationContext> spelContextFactory = () -> { StandardEvaluationContext context = new StandardEvaluationContext(); context.setBeanResolver((ctx, beanName) -> beanName.equals(alias) ? bean : null); context.setMethodResolvers(Collections.singletonList(new ReflectiveMethodResolver())); return context; }; return Validation.buildDefaultValidatorFactory() .usingContext() .constraintValidatorFactory(new ConstraintValidatorFactoryWrapper(VerifierMode.Strict, type -> Optional.empty(), spelContextFactory)) .messageInterpolator(new SpELMessageInterpolator(spelContextFactory)) .getValidator(); }
Example #13
Source File: SpelReproTests.java From spring-analysis-note with MIT License | 4 votes |
@Test public void varargsAndPrimitives_SPR8174() throws Exception { EvaluationContext emptyEvalContext = new StandardEvaluationContext(); List<TypeDescriptor> args = new ArrayList<>(); args.add(TypeDescriptor.forObject(34L)); ReflectionUtil<Integer> ru = new ReflectionUtil<>(); MethodExecutor me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "methodToCall", args); args.set(0, TypeDescriptor.forObject(23)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, 45); args.set(0, TypeDescriptor.forObject(23f)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, 45f); args.set(0, TypeDescriptor.forObject(23d)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, 23d); args.set(0, TypeDescriptor.forObject((short) 23)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, (short) 23); args.set(0, TypeDescriptor.forObject(23L)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, 23L); args.set(0, TypeDescriptor.forObject((char) 65)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, (char) 65); args.set(0, TypeDescriptor.forObject((byte) 23)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, (byte) 23); args.set(0, TypeDescriptor.forObject(true)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, true); // trickier: args.set(0, TypeDescriptor.forObject(12)); args.add(TypeDescriptor.forObject(23f)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "bar", args); me.execute(emptyEvalContext, ru, 12, 23f); }
Example #14
Source File: SpelReproTests.java From java-technology-stack with MIT License | 4 votes |
@Test public void varargsAndPrimitives_SPR8174() throws Exception { EvaluationContext emptyEvalContext = new StandardEvaluationContext(); List<TypeDescriptor> args = new ArrayList<>(); args.add(TypeDescriptor.forObject(34L)); ReflectionUtil<Integer> ru = new ReflectionUtil<>(); MethodExecutor me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "methodToCall", args); args.set(0, TypeDescriptor.forObject(23)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, 45); args.set(0, TypeDescriptor.forObject(23f)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, 45f); args.set(0, TypeDescriptor.forObject(23d)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, 23d); args.set(0, TypeDescriptor.forObject((short) 23)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, (short) 23); args.set(0, TypeDescriptor.forObject(23L)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, 23L); args.set(0, TypeDescriptor.forObject((char) 65)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, (char) 65); args.set(0, TypeDescriptor.forObject((byte) 23)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, (byte) 23); args.set(0, TypeDescriptor.forObject(true)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, true); // trickier: args.set(0, TypeDescriptor.forObject(12)); args.add(TypeDescriptor.forObject(23f)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "bar", args); me.execute(emptyEvalContext, ru, 12, 23f); }
Example #15
Source File: SpelReproTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Test public void varargsAndPrimitives_SPR8174() throws Exception { EvaluationContext emptyEvalContext = new StandardEvaluationContext(); List<TypeDescriptor> args = new ArrayList<TypeDescriptor>(); args.add(TypeDescriptor.forObject(34L)); ReflectionUtil<Integer> ru = new ReflectionUtil<Integer>(); MethodExecutor me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "methodToCall", args); args.set(0, TypeDescriptor.forObject(23)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, 45); args.set(0, TypeDescriptor.forObject(23f)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, 45f); args.set(0, TypeDescriptor.forObject(23d)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, 23d); args.set(0, TypeDescriptor.forObject((short) 23)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, (short) 23); args.set(0, TypeDescriptor.forObject(23L)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, 23L); args.set(0, TypeDescriptor.forObject((char) 65)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, (char) 65); args.set(0, TypeDescriptor.forObject((byte) 23)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, (byte) 23); args.set(0, TypeDescriptor.forObject(true)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args); me.execute(emptyEvalContext, ru, true); // trickier: args.set(0, TypeDescriptor.forObject(12)); args.add(TypeDescriptor.forObject(23f)); me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "bar", args); me.execute(emptyEvalContext, ru, 12, 23f); }