KeyIndexesBuilder extends AbstractDataBuilder { private Collection<Integer> getKeyIndexes(final Method targetMethod, final boolean isMulti) { final Annotation[][] paramAnnotationArrays = targetMethod.getParameterAnnotations(); final SortedMap<ParameterValueKeyProvider, Integer> keyProviders = new TreeMap<ParameterValueKeyProvider, Integer>(COMPARATOR); final Set<Integer> order = new HashSet<Integer>(); if (paramAnnotationArrays == null || paramAnnotationArrays.length < 1) { return Collections.emptyList(); } for (int ix = 0; ix < paramAnnotationArrays.length; ix++) { final ParameterValueKeyProvider keyProviderAnnotation = getAnnotation(ParameterValueKeyProvider.class, paramAnnotationArrays[ix]); if (keyProviderAnnotation == null) { continue; } if (keyProviderAnnotation.order() < 0) { throw new InvalidParameterException(String.format( "No valid order [%d] defined in annotation [%s] on method [%s], only no negative integers are allowed.", keyProviderAnnotation.order(), ParameterValueKeyProvider.class.getName(), targetMethod.getName())); } if (!order.add(keyProviderAnnotation.order())) { throwException( "No valid order defined in annotation [%s] on method [%s]. There are two annotations with the same order.", ParameterValueKeyProvider.class, targetMethod); } if (!isMulti && Collection.class.isAssignableFrom(targetMethod.getParameterTypes()[ix])) { LOG.warn("ParameterValueKeyProvider is used on a list parameter {} in method {} with non multi cache annotation." + "Consider changing to *MultiCache annotation or use another parameter for key. Using collections in such case " + "may lead to unexpected consequences: key can exceed allowed memcached key length, key will be varied " + "when order or size are changed." , targetMethod.getParameterTypes()[ix], targetMethod.getName()); } keyProviders.put(keyProviderAnnotation, ix); } return keyProviders.values(); }    }
@Test public void shouldNotPopulateIfExpectedAnnotationNotRequireKeyProvider() throws Exception { final Class<? extends Annotation> expected = InvalidateAssignCache.class; final Method targetMethod = null; builder.populate(data, null, expected, targetMethod); assertTrue(data.getKeyIndexes().isEmpty()); assertFalse(data.isReturnKeyIndex()); }
@Test public void shouldPopulateReturnKeyAnnotation() throws Exception { final Class<? extends Annotation> expected = InvalidateSingleCache.class; final Method targetMethod = new Mirror().on(AnnotationDataDummy.class).reflect().method("populateKeyProvider02") .withArgs(String.class); builder.populate(data, null, expected, targetMethod); assertTrue(data.getKeyIndexes().isEmpty()); assertTrue(data.isReturnKeyIndex()); }
@Test public void shouldPopulateParamKeyAnnotation() throws Exception { final Class<? extends Annotation> expected = InvalidateSingleCache.class; final Method targetMethod = new Mirror().on(AnnotationDataDummy.class).reflect().method("populateKeyProvider02") .withArgs(String.class); builder.populate(data, null, expected, targetMethod); assertTrue(data.getKeyIndexes().isEmpty()); assertTrue(data.isReturnKeyIndex()); }
@Test public void shouldPopulateSingleParamKeyAnnotation() throws Exception { final Class<? extends Annotation> expected = InvalidateSingleCache.class; final Method targetMethod = new Mirror().on(AnnotationDataDummy.class).reflect().method("populateKeyProvider05") .withArgs(String.class, String.class, String.class); builder.populate(data, null, expected, targetMethod); assertCollectionEquals(Collections.singleton(2), data.getKeyIndexes()); assertFalse(data.isReturnKeyIndex()); }
@Test public void shouldPopulateWithCorrectOrder() throws Exception { final Class<? extends Annotation> expected = InvalidateSingleCache.class; final Method targetMethod = new Mirror().on(AnnotationDataDummy.class).reflect().method("populateKeyProvider09") .withArgs(String.class, String.class); builder.populate(data, null, expected, targetMethod); assertEquals(2, data.getKeyIndexes().size()); final Iterator<Integer> iter = data.getKeyIndexes().iterator(); assertEquals(1, iter.next().intValue()); assertEquals(0, iter.next().intValue()); assertFalse(data.isReturnKeyIndex()); }