Java源码示例:javax.cache.annotation.CachePut
示例1
public CachePutOperation(
CacheMethodDetails<CachePut> methodDetails, CacheResolver cacheResolver, KeyGenerator keyGenerator) {
super(methodDetails, cacheResolver, keyGenerator);
CachePut ann = methodDetails.getCacheAnnotation();
this.exceptionTypeFilter = createExceptionTypeFilter(ann.cacheFor(), ann.noCacheFor());
CacheParameterDetail valueParameterDetail =
initializeValueParameterDetail(methodDetails.getMethod(), this.allParameterDetails);
if (valueParameterDetail == null) {
throw new IllegalArgumentException("No parameter annotated with @CacheValue was found for " +
methodDetails.getMethod());
}
this.valueParameterDetail = valueParameterDetail;
}
示例2
public CachePutOperation(
CacheMethodDetails<CachePut> methodDetails, CacheResolver cacheResolver, KeyGenerator keyGenerator) {
super(methodDetails, cacheResolver, keyGenerator);
CachePut ann = methodDetails.getCacheAnnotation();
this.exceptionTypeFilter = createExceptionTypeFilter(ann.cacheFor(), ann.noCacheFor());
CacheParameterDetail valueParameterDetail =
initializeValueParameterDetail(methodDetails.getMethod(), this.allParameterDetails);
if (valueParameterDetail == null) {
throw new IllegalArgumentException("No parameter annotated with @CacheValue was found for " +
methodDetails.getMethod());
}
this.valueParameterDetail = valueParameterDetail;
}
示例3
@Override
protected Object invoke(
CacheOperationInvocationContext<CachePutOperation> context, CacheOperationInvoker invoker) {
CachePutOperation operation = context.getOperation();
CacheKeyInvocationContext<CachePut> invocationContext = createCacheKeyInvocationContext(context);
boolean earlyPut = operation.isEarlyPut();
Object value = invocationContext.getValueParameter().getValue();
if (earlyPut) {
cacheValue(context, value);
}
try {
Object result = invoker.invoke();
if (!earlyPut) {
cacheValue(context, value);
}
return result;
}
catch (CacheOperationInvoker.ThrowableWrapper ex) {
Throwable original = ex.getOriginal();
if (!earlyPut && operation.getExceptionTypeFilter().match(original.getClass())) {
cacheValue(context, value);
}
throw ex;
}
}
示例4
@Override
protected JCacheOperation<?> findCacheOperation(Method method, @Nullable Class<?> targetType) {
CacheResult cacheResult = method.getAnnotation(CacheResult.class);
CachePut cachePut = method.getAnnotation(CachePut.class);
CacheRemove cacheRemove = method.getAnnotation(CacheRemove.class);
CacheRemoveAll cacheRemoveAll = method.getAnnotation(CacheRemoveAll.class);
int found = countNonNull(cacheResult, cachePut, cacheRemove, cacheRemoveAll);
if (found == 0) {
return null;
}
if (found > 1) {
throw new IllegalStateException("More than one cache annotation found on '" + method + "'");
}
CacheDefaults defaults = getCacheDefaults(method, targetType);
if (cacheResult != null) {
return createCacheResultOperation(method, defaults, cacheResult);
}
else if (cachePut != null) {
return createCachePutOperation(method, defaults, cachePut);
}
else if (cacheRemove != null) {
return createCacheRemoveOperation(method, defaults, cacheRemove);
}
else {
return createCacheRemoveAllOperation(method, defaults, cacheRemoveAll);
}
}
示例5
protected CachePutOperation createCachePutOperation(Method method, @Nullable CacheDefaults defaults, CachePut ann) {
String cacheName = determineCacheName(method, defaults, ann.cacheName());
CacheResolverFactory cacheResolverFactory =
determineCacheResolverFactory(defaults, ann.cacheResolverFactory());
KeyGenerator keyGenerator = determineKeyGenerator(defaults, ann.cacheKeyGenerator());
CacheMethodDetails<CachePut> methodDetails = createMethodDetails(method, ann, cacheName);
CacheResolver cacheResolver = getCacheResolver(cacheResolverFactory, methodDetails);
return new CachePutOperation(methodDetails, cacheResolver, keyGenerator);
}
示例6
@Test
public void noCacheValue() {
CacheMethodDetails<CachePut> methodDetails = create(CachePut.class,
SampleObject.class, "noCacheValue", Long.class);
assertThatIllegalArgumentException().isThrownBy(() ->
createDefaultOperation(methodDetails));
}
示例7
@Test
public void multiCacheValues() {
CacheMethodDetails<CachePut> methodDetails = create(CachePut.class,
SampleObject.class, "multiCacheValues", Long.class, SampleObject.class, SampleObject.class);
assertThatIllegalArgumentException().isThrownBy(() ->
createDefaultOperation(methodDetails));
}
示例8
@Test
public void fullPutConfig() {
CacheMethodDetails<CachePut> methodDetails = create(CachePut.class,
SampleObject.class, "fullPutConfig", Long.class, SampleObject.class);
CachePutOperation operation = createDefaultOperation(methodDetails);
assertTrue(operation.isEarlyPut());
assertNotNull(operation.getExceptionTypeFilter());
assertTrue(operation.getExceptionTypeFilter().match(IOException.class));
assertFalse(operation.getExceptionTypeFilter().match(NullPointerException.class));
}
示例9
@Override
@CachePut(afterInvocation = false)
public void earlyPut(String id, @CacheValue Object value) {
Object key = SimpleKeyGenerator.generateKey(id);
Cache.ValueWrapper valueWrapper = defaultCache.get(key);
if (valueWrapper == null) {
throw new AssertionError("Excepted value to be put in cache with key " + key);
}
Object actual = valueWrapper.get();
if (value != actual) { // instance check on purpose
throw new AssertionError("Wrong value set in cache with key " + key + ". " +
"Expected=" + value + ", but got=" + actual);
}
}
示例10
@Override
@CachePut(afterInvocation = false)
public void earlyPut(String id, @CacheValue Object value) {
Object key = SimpleKeyGenerator.generateKey(id);
Cache.ValueWrapper valueWrapper = defaultCache.get(key);
if (valueWrapper == null) {
throw new AssertionError("Excepted value to be put in cache with key " + key);
}
Object actual = valueWrapper.get();
if (value != actual) { // instance check on purpose
throw new AssertionError("Wrong value set in cache with key " + key + ". " +
"Expected=" + value + ", but got=" + actual);
}
}
示例11
@Override
protected Object invoke(
CacheOperationInvocationContext<CachePutOperation> context, CacheOperationInvoker invoker) {
CachePutOperation operation = context.getOperation();
CacheKeyInvocationContext<CachePut> invocationContext = createCacheKeyInvocationContext(context);
boolean earlyPut = operation.isEarlyPut();
Object value = invocationContext.getValueParameter().getValue();
if (earlyPut) {
cacheValue(context, value);
}
try {
Object result = invoker.invoke();
if (!earlyPut) {
cacheValue(context, value);
}
return result;
}
catch (CacheOperationInvoker.ThrowableWrapper ex) {
Throwable original = ex.getOriginal();
if (!earlyPut && operation.getExceptionTypeFilter().match(original.getClass())) {
cacheValue(context, value);
}
throw ex;
}
}
示例12
@Override
protected JCacheOperation<?> findCacheOperation(Method method, @Nullable Class<?> targetType) {
CacheResult cacheResult = method.getAnnotation(CacheResult.class);
CachePut cachePut = method.getAnnotation(CachePut.class);
CacheRemove cacheRemove = method.getAnnotation(CacheRemove.class);
CacheRemoveAll cacheRemoveAll = method.getAnnotation(CacheRemoveAll.class);
int found = countNonNull(cacheResult, cachePut, cacheRemove, cacheRemoveAll);
if (found == 0) {
return null;
}
if (found > 1) {
throw new IllegalStateException("More than one cache annotation found on '" + method + "'");
}
CacheDefaults defaults = getCacheDefaults(method, targetType);
if (cacheResult != null) {
return createCacheResultOperation(method, defaults, cacheResult);
}
else if (cachePut != null) {
return createCachePutOperation(method, defaults, cachePut);
}
else if (cacheRemove != null) {
return createCacheRemoveOperation(method, defaults, cacheRemove);
}
else {
return createCacheRemoveAllOperation(method, defaults, cacheRemoveAll);
}
}
示例13
protected CachePutOperation createCachePutOperation(Method method, @Nullable CacheDefaults defaults, CachePut ann) {
String cacheName = determineCacheName(method, defaults, ann.cacheName());
CacheResolverFactory cacheResolverFactory =
determineCacheResolverFactory(defaults, ann.cacheResolverFactory());
KeyGenerator keyGenerator = determineKeyGenerator(defaults, ann.cacheKeyGenerator());
CacheMethodDetails<CachePut> methodDetails = createMethodDetails(method, ann, cacheName);
CacheResolver cacheResolver = getCacheResolver(cacheResolverFactory, methodDetails);
return new CachePutOperation(methodDetails, cacheResolver, keyGenerator);
}
示例14
@Test
public void noCacheValue() {
CacheMethodDetails<CachePut> methodDetails = create(CachePut.class,
SampleObject.class, "noCacheValue", Long.class);
thrown.expect(IllegalArgumentException.class);
createDefaultOperation(methodDetails);
}
示例15
@Test
public void multiCacheValues() {
CacheMethodDetails<CachePut> methodDetails = create(CachePut.class,
SampleObject.class, "multiCacheValues", Long.class, SampleObject.class, SampleObject.class);
thrown.expect(IllegalArgumentException.class);
createDefaultOperation(methodDetails);
}
示例16
@Test
public void fullPutConfig() {
CacheMethodDetails<CachePut> methodDetails = create(CachePut.class,
SampleObject.class, "fullPutConfig", Long.class, SampleObject.class);
CachePutOperation operation = createDefaultOperation(methodDetails);
assertTrue(operation.isEarlyPut());
assertNotNull(operation.getExceptionTypeFilter());
assertTrue(operation.getExceptionTypeFilter().match(IOException.class));
assertFalse(operation.getExceptionTypeFilter().match(NullPointerException.class));
}
示例17
@Override
@CachePut(afterInvocation = false)
public void earlyPut(String id, @CacheValue Object value) {
Object key = SimpleKeyGenerator.generateKey(id);
Cache.ValueWrapper valueWrapper = defaultCache.get(key);
if (valueWrapper == null) {
throw new AssertionError("Excepted value to be put in cache with key " + key);
}
Object actual = valueWrapper.get();
if (value != actual) { // instance check on purpose
throw new AssertionError("Wrong value set in cache with key " + key + ". " +
"Expected=" + value + ", but got=" + actual);
}
}
示例18
@Override
@CachePut(afterInvocation = false)
public void earlyPut(String id, @CacheValue Object value) {
Object key = SimpleKeyGenerator.generateKey(id);
Cache.ValueWrapper valueWrapper = defaultCache.get(key);
if (valueWrapper == null) {
throw new AssertionError("Excepted value to be put in cache with key " + key);
}
Object actual = valueWrapper.get();
if (value != actual) { // instance check on purpose
throw new AssertionError("Wrong value set in cache with key " + key + ". " +
"Expected=" + value + ", but got=" + actual);
}
}
示例19
public CachePutOperation(
CacheMethodDetails<CachePut> methodDetails, CacheResolver cacheResolver, KeyGenerator keyGenerator) {
super(methodDetails, cacheResolver, keyGenerator);
CachePut ann = methodDetails.getCacheAnnotation();
this.exceptionTypeFilter = createExceptionTypeFilter(ann.cacheFor(), ann.noCacheFor());
this.valueParameterDetail = initializeValueParameterDetail(methodDetails.getMethod(), this.allParameterDetails);
if (this.valueParameterDetail == null) {
throw new IllegalArgumentException("No parameter annotated with @CacheValue was found for " +
"" + methodDetails.getMethod());
}
}
示例20
@Override
protected Object invoke(CacheOperationInvocationContext<CachePutOperation> context,
CacheOperationInvoker invoker) {
CacheKeyInvocationContext<CachePut> invocationContext = createCacheKeyInvocationContext(context);
CachePutOperation operation = context.getOperation();
boolean earlyPut = operation.isEarlyPut();
Object value = invocationContext.getValueParameter().getValue();
if (earlyPut) {
cacheValue(context, value);
}
try {
Object result = invoker.invoke();
if (!earlyPut) {
cacheValue(context, value);
}
return result;
}
catch (CacheOperationInvoker.ThrowableWrapper ex) {
Throwable original = ex.getOriginal();
if (!earlyPut && operation.getExceptionTypeFilter().match(original.getClass())) {
cacheValue(context, value);
}
throw ex;
}
}
示例21
@Override
protected JCacheOperation<?> findCacheOperation(Method method, Class<?> targetType) {
CacheResult cacheResult = method.getAnnotation(CacheResult.class);
CachePut cachePut = method.getAnnotation(CachePut.class);
CacheRemove cacheRemove = method.getAnnotation(CacheRemove.class);
CacheRemoveAll cacheRemoveAll = method.getAnnotation(CacheRemoveAll.class);
int found = countNonNull(cacheResult, cachePut, cacheRemove, cacheRemoveAll);
if (found == 0) {
return null;
}
if (found > 1) {
throw new IllegalStateException("More than one cache annotation found on '" + method + "'");
}
CacheDefaults defaults = getCacheDefaults(method, targetType);
if (cacheResult != null) {
return createCacheResultOperation(method, defaults, cacheResult);
}
else if (cachePut != null) {
return createCachePutOperation(method, defaults, cachePut);
}
else if (cacheRemove != null) {
return createCacheRemoveOperation(method, defaults, cacheRemove);
}
else {
return createCacheRemoveAllOperation(method, defaults, cacheRemoveAll);
}
}
示例22
protected CachePutOperation createCachePutOperation(Method method, CacheDefaults defaults, CachePut ann) {
String cacheName = determineCacheName(method, defaults, ann.cacheName());
CacheResolverFactory cacheResolverFactory =
determineCacheResolverFactory(defaults, ann.cacheResolverFactory());
KeyGenerator keyGenerator = determineKeyGenerator(defaults, ann.cacheKeyGenerator());
CacheMethodDetails<CachePut> methodDetails = createMethodDetails(method, ann, cacheName);
CacheResolver cacheResolver = getCacheResolver(cacheResolverFactory, methodDetails);
return new CachePutOperation(methodDetails, cacheResolver, keyGenerator);
}
示例23
public CachePutOperation(
CacheMethodDetails<CachePut> methodDetails, CacheResolver cacheResolver, KeyGenerator keyGenerator) {
super(methodDetails, cacheResolver, keyGenerator);
CachePut ann = methodDetails.getCacheAnnotation();
this.exceptionTypeFilter = createExceptionTypeFilter(ann.cacheFor(), ann.noCacheFor());
this.valueParameterDetail = initializeValueParameterDetail(methodDetails.getMethod(), this.allParameterDetails);
if (this.valueParameterDetail == null) {
throw new IllegalArgumentException("No parameter annotated with @CacheValue was found for " +
"" + methodDetails.getMethod());
}
}
示例24
@Override
protected Object invoke(CacheOperationInvocationContext<CachePutOperation> context,
CacheOperationInvoker invoker) {
CacheKeyInvocationContext<CachePut> invocationContext = createCacheKeyInvocationContext(context);
CachePutOperation operation = context.getOperation();
boolean earlyPut = operation.isEarlyPut();
Object value = invocationContext.getValueParameter().getValue();
if (earlyPut) {
cacheValue(context, value);
}
try {
Object result = invoker.invoke();
if (!earlyPut) {
cacheValue(context, value);
}
return result;
}
catch (CacheOperationInvoker.ThrowableWrapper ex) {
Throwable original = ex.getOriginal();
if (!earlyPut && operation.getExceptionTypeFilter().match(original.getClass())) {
cacheValue(context, value);
}
throw ex;
}
}
示例25
@Override
protected JCacheOperation<?> findCacheOperation(Method method, Class<?> targetType) {
CacheResult cacheResult = method.getAnnotation(CacheResult.class);
CachePut cachePut = method.getAnnotation(CachePut.class);
CacheRemove cacheRemove = method.getAnnotation(CacheRemove.class);
CacheRemoveAll cacheRemoveAll = method.getAnnotation(CacheRemoveAll.class);
int found = countNonNull(cacheResult, cachePut, cacheRemove, cacheRemoveAll);
if (found == 0) {
return null;
}
if (found > 1) {
throw new IllegalStateException("More than one cache annotation found on '" + method + "'");
}
CacheDefaults defaults = getCacheDefaults(method, targetType);
if (cacheResult != null) {
return createCacheResultOperation(method, defaults, cacheResult);
}
else if (cachePut != null) {
return createCachePutOperation(method, defaults, cachePut);
}
else if (cacheRemove != null) {
return createCacheRemoveOperation(method, defaults, cacheRemove);
}
else {
return createCacheRemoveAllOperation(method, defaults, cacheRemoveAll);
}
}
示例26
protected CachePutOperation createCachePutOperation(Method method, CacheDefaults defaults, CachePut ann) {
String cacheName = determineCacheName(method, defaults, ann.cacheName());
CacheResolverFactory cacheResolverFactory =
determineCacheResolverFactory(defaults, ann.cacheResolverFactory());
KeyGenerator keyGenerator = determineKeyGenerator(defaults, ann.cacheKeyGenerator());
CacheMethodDetails<CachePut> methodDetails = createMethodDetails(method, ann, cacheName);
CacheResolver cacheResolver = getCacheResolver(cacheResolverFactory, methodDetails);
return new CachePutOperation(methodDetails, cacheResolver, keyGenerator);
}
示例27
@Test
public void noCacheValue() {
CacheMethodDetails<CachePut> methodDetails = create(CachePut.class,
SampleObject.class, "noCacheValue", Long.class);
thrown.expect(IllegalArgumentException.class);
createDefaultOperation(methodDetails);
}
示例28
@Test
public void multiCacheValues() {
CacheMethodDetails<CachePut> methodDetails = create(CachePut.class,
SampleObject.class, "multiCacheValues", Long.class, SampleObject.class, SampleObject.class);
thrown.expect(IllegalArgumentException.class);
createDefaultOperation(methodDetails);
}
示例29
@Test
public void fullPutConfig() {
CacheMethodDetails<CachePut> methodDetails = create(CachePut.class,
SampleObject.class, "fullPutConfig", Long.class, SampleObject.class);
CachePutOperation operation = createDefaultOperation(methodDetails);
assertTrue(operation.isEarlyPut());
assertNotNull(operation.getExceptionTypeFilter());
assertTrue(operation.getExceptionTypeFilter().match(IOException.class));
assertFalse(operation.getExceptionTypeFilter().match(NullPointerException.class));
}
示例30
@Override
@CachePut(afterInvocation = false)
public void earlyPut(String id, @CacheValue Object value) {
Object key = SimpleKeyGenerator.generateKey(id);
Cache.ValueWrapper valueWrapper = defaultCache.get(key);
if (valueWrapper == null) {
throw new AssertionError("Excepted value to be put in cache with key " + key);
}
Object actual = valueWrapper.get();
if (value != actual) { // instance check on purpose
throw new AssertionError("Wrong value set in cache with key " + key + ". " +
"Expected=" + value + ", but got=" + actual);
}
}