Skip to content

Commit

Permalink
Fix cache compatibility with rest-client
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenneg committed Jan 4, 2021
1 parent 8ce16d7 commit 1da99b6
Show file tree
Hide file tree
Showing 39 changed files with 781 additions and 607 deletions.
Original file line number Diff line number Diff line change
@@ -1,65 +1,35 @@
package io.quarkus.cache.deployment;

import static io.quarkus.cache.deployment.CacheDeploymentConstants.CACHE_INVALIDATE;
import static io.quarkus.cache.deployment.CacheDeploymentConstants.CACHE_INVALIDATE_ALL;
import static io.quarkus.cache.deployment.CacheDeploymentConstants.CACHE_INVALIDATE_ALL_LIST;
import static io.quarkus.cache.deployment.CacheDeploymentConstants.CACHE_INVALIDATE_LIST;
import static io.quarkus.cache.deployment.CacheDeploymentConstants.CACHE_KEY;
import static io.quarkus.cache.deployment.CacheDeploymentConstants.CACHE_KEY_PARAMETER_POSITIONS;
import static io.quarkus.cache.deployment.CacheDeploymentConstants.CACHE_NAME_PARAM;
import static io.quarkus.cache.deployment.CacheDeploymentConstants.CACHE_RESULT;
import static io.quarkus.cache.deployment.CacheDeploymentConstants.LOCK_TIMEOUT_PARAM;
import static org.jboss.jandex.AnnotationInstance.create;
import static org.jboss.jandex.AnnotationTarget.Kind.METHOD;
import static org.jboss.jandex.AnnotationValue.createArrayValue;
import static org.jboss.jandex.AnnotationValue.createShortValue;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationTarget.Kind;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.DotName;
import org.jboss.jandex.MethodInfo;

import io.quarkus.arc.processor.AnnotationsTransformer;
import io.quarkus.cache.runtime.CacheInvalidateAllInterceptorBinding;
import io.quarkus.cache.runtime.CacheInvalidateInterceptorBinding;
import io.quarkus.cache.runtime.CacheResultInterceptorBinding;

public class CacheAnnotationsTransformer implements AnnotationsTransformer {

@Override
public boolean appliesTo(Kind kind) {
return Kind.METHOD == kind;
return kind == METHOD;
}

@Override
public void transform(TransformationContext context) {
MethodInfo method = context.getTarget().asMethod();
List<AnnotationInstance> interceptorBindings = new ArrayList<>();
for (AnnotationInstance annotation : method.annotations()) {
AnnotationTarget target = annotation.target();
if (target.kind() == Kind.METHOD) {
if (CACHE_INVALIDATE_ALL.equals(annotation.name())) {
interceptorBindings.add(createCacheInvalidateAllBinding(annotation, target));
} else if (CACHE_INVALIDATE_ALL_LIST.equals(annotation.name())) {
for (AnnotationInstance nestedAnnotation : annotation.value("value").asNestedArray()) {
interceptorBindings.add(createCacheInvalidateAllBinding(nestedAnnotation, target));
}
} else if (CACHE_INVALIDATE.equals(annotation.name())) {
interceptorBindings.add(createCacheInvalidateBinding(method, annotation, target));
} else if (CACHE_INVALIDATE_LIST.equals(annotation.name())) {
for (AnnotationInstance nestedAnnotation : annotation.value("value").asNestedArray()) {
interceptorBindings.add(createCacheInvalidateBinding(method, nestedAnnotation, target));
}
} else if (CACHE_RESULT.equals(annotation.name())) {
interceptorBindings.add(createCacheResultBinding(method, annotation, target));
}
}
}
if (requiresCacheKeyParameterPositionsInterceptorBinding(method)) {
List<AnnotationValue> positions = new ArrayList<>();
for (AnnotationInstance annotation : method.annotations(CACHE_KEY)) {
Expand All @@ -69,48 +39,16 @@ public void transform(TransformationContext context) {
AnnotationValue annotationValue = createArrayValue("value", toArray(positions));
AnnotationInstance binding = create(CACHE_KEY_PARAMETER_POSITIONS, method,
new AnnotationValue[] { annotationValue });
interceptorBindings.add(binding);
context.transform().add(binding).done();
}
}
context.transform().addAll(interceptorBindings).done();
}

private boolean requiresCacheKeyParameterPositionsInterceptorBinding(MethodInfo method) {
return method.hasAnnotation(CACHE_KEY) && (method.hasAnnotation(CACHE_INVALIDATE)
|| method.hasAnnotation(CACHE_INVALIDATE_LIST) || method.hasAnnotation(CACHE_RESULT));
}

private AnnotationInstance createCacheInvalidateAllBinding(AnnotationInstance annotation, AnnotationTarget target) {
return createBinding(CacheInvalidateAllInterceptorBinding.class, target, getCacheName(annotation));
}

private AnnotationInstance createCacheInvalidateBinding(MethodInfo method, AnnotationInstance annotation,
AnnotationTarget target) {
List<AnnotationValue> parameters = new ArrayList<>();
parameters.add(getCacheName(annotation));
return createBinding(CacheInvalidateInterceptorBinding.class, target, toArray(parameters));
}

private AnnotationInstance createCacheResultBinding(MethodInfo method, AnnotationInstance annotation,
AnnotationTarget target) {
List<AnnotationValue> parameters = new ArrayList<>();
parameters.add(getCacheName(annotation));
findLockTimeout(annotation).ifPresent(parameters::add);
return createBinding(CacheResultInterceptorBinding.class, target, toArray(parameters));
}

private AnnotationInstance createBinding(Class<?> bindingClass, AnnotationTarget target, AnnotationValue... values) {
return AnnotationInstance.create(DotName.createSimple(bindingClass.getName()), target, values);
}

private AnnotationValue getCacheName(AnnotationInstance annotation) {
return annotation.value(CACHE_NAME_PARAM);
}

private Optional<AnnotationValue> findLockTimeout(AnnotationInstance annotation) {
return Optional.ofNullable(annotation.value(LOCK_TIMEOUT_PARAM));
}

private AnnotationValue[] toArray(List<AnnotationValue> parameters) {
return parameters.toArray(new AnnotationValue[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import io.quarkus.cache.CacheKey;
import io.quarkus.cache.CacheName;
import io.quarkus.cache.CacheResult;
import io.quarkus.cache.runtime.CacheInvalidateAllInterceptor;
import io.quarkus.cache.runtime.CacheInvalidateInterceptor;
import io.quarkus.cache.runtime.CacheKeyParameterPositions;
import io.quarkus.cache.runtime.CacheProducer;
import io.quarkus.cache.runtime.CacheResultInterceptor;

public class CacheDeploymentConstants {

Expand All @@ -23,16 +25,20 @@ public class CacheDeploymentConstants {
public static final DotName CACHE_INVALIDATE_LIST = dotName(CacheInvalidate.List.class);
public static final DotName CACHE_RESULT = dotName(CacheResult.class);
public static final DotName CACHE_KEY = dotName(CacheKey.class);
public static final List<DotName> API_METHODS_ANNOTATIONS = Arrays.asList(
CACHE_RESULT, CACHE_INVALIDATE, CACHE_INVALIDATE_ALL);
public static final List<DotName> API_METHODS_ANNOTATIONS_LISTS = Arrays.asList(
CACHE_INVALIDATE_LIST, CACHE_INVALIDATE_ALL_LIST);
public static final List<DotName> INTERCEPTOR_BINDINGS = Arrays.asList(CACHE_RESULT, CACHE_INVALIDATE,
CACHE_INVALIDATE_ALL);
public static final List<DotName> INTERCEPTOR_BINDING_CONTAINERS = Arrays.asList(CACHE_INVALIDATE_LIST,
CACHE_INVALIDATE_ALL_LIST);
public static final List<DotName> INTERCEPTORS = Arrays.asList(dotName(CacheInvalidateAllInterceptor.class),
dotName(CacheInvalidateInterceptor.class), dotName(CacheResultInterceptor.class));
public static final DotName CACHE_KEY_PARAMETER_POSITIONS = dotName(CacheKeyParameterPositions.class);
public static final DotName CACHE_PRODUCER = dotName(CacheProducer.class);

// MicroProfile REST Client.
public static final DotName REGISTER_REST_CLIENT = DotName
.createSimple("org.eclipse.microprofile.rest.client.inject.RegisterRestClient");

// Annotations parameters.
public static final String CACHE_NAME_PARAM = "cacheName";
public static final String LOCK_TIMEOUT_PARAM = "lockTimeout";

// Caffeine.
public static final String CAFFEINE_CACHE_TYPE = "caffeine";
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.cache.deployment;

import java.util.Set;

import io.quarkus.builder.item.SimpleBuildItem;

/**
* This build item is used to pass the full list of cache names from the validation step to the recording step.
*/
public final class CacheNamesBuildItem extends SimpleBuildItem {

private final Set<String> names;

public CacheNamesBuildItem(Set<String> names) {
this.names = names;
}

public Set<String> getNames() {
return names;
}
}
Loading

0 comments on commit 1da99b6

Please sign in to comment.