Skip to content

Commit

Permalink
feature/76 Introduced Custom annotation metadata support
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Korbel - Contractor authored and Daniel Korbel - Contractor committed Mar 1, 2022
1 parent 0b9ffa5 commit cb13939
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
public class RequestFactorySourceWriter extends AbstractSourceBuilder {

private final Element serviceElement;
private final Set<TypeMirror> metadataAnnotationTypes;
private final String requestsServiceRoot;
private Map<String, Integer> methodCount;

Expand All @@ -64,6 +65,7 @@ public RequestFactorySourceWriter(
super(processingEnvironment);
this.serviceElement = serviceElement;
this.requestsServiceRoot = serviceElement.getAnnotation(RequestFactory.class).serviceRoot();
this.metadataAnnotationTypes = new HashSet<>();

ObjectMapperProcessor.elementUtils = elements;
ObjectMapperProcessor.typeUtils = types;
Expand All @@ -72,10 +74,14 @@ public RequestFactorySourceWriter(
}

public RequestFactorySourceWriter(
Element serviceElement, String serviceRoot, ProcessingEnvironment processingEnvironment) {
Element serviceElement,
String serviceRoot,
Set<TypeMirror> metadataAnnotationTypes,
ProcessingEnvironment processingEnvironment) {
super(processingEnvironment);
this.serviceElement = serviceElement;
this.requestsServiceRoot = serviceRoot;
this.metadataAnnotationTypes = metadataAnnotationTypes;

ObjectMapperProcessor.elementUtils = elements;
ObjectMapperProcessor.typeUtils = types;
Expand Down Expand Up @@ -792,6 +798,8 @@ private MethodSpec constructor(TypeMirror requestBean, ServiceMethod serviceMeth
.addStatement("setAccept(new String[]{$L})", getAcceptResponse(serviceMethod))
.addStatement("setPath($S)", getPath(serviceMethod))
.addStatement("setServiceRoot($S)", getServiceRoot(serviceMethod.method));
Optional<String> metaDataAnnotations = getMetaDataAnnotations(serviceMethod.method);
metaDataAnnotations.ifPresent(annotation -> constructorBuilder.addStatement("setMetaParameter($S, $S)", annotation, "true"));

if (!consumesMultipartForm(serviceMethod)) {
constructorBuilder.addStatement(
Expand Down Expand Up @@ -1094,6 +1102,20 @@ private Optional<NullQueryParamStrategy> getNullQueryParamStrategy(
return Optional.empty();
}

private Optional<String> getMetaDataAnnotations(ExecutableElement method) {
Optional<String> metadata = Optional.empty();
for (AnnotationMirror annotatedOnMethod : method.getAnnotationMirrors()) {
for (TypeMirror metadataAnnotation : this.metadataAnnotationTypes) {
if (annotatedOnMethod.toString().equals("@" + metadataAnnotation.toString())) {
metadata =
Optional.of(
annotatedOnMethod.getAnnotationType().asElement().getSimpleName().toString());
}
}
}
return metadata;
}

private String getHttpMethod(ServiceMethod serviceMethod) {

ExecutableElement method = serviceMethod.method;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.type.TypeMirror;
import org.dominokit.rest.shared.request.service.annotations.MetaDataAnnotations;
import org.dominokit.rest.shared.request.service.annotations.RequestFactory;
import org.dominokit.rest.shared.request.service.annotations.ResourceList;

Expand Down Expand Up @@ -65,12 +66,17 @@ private void generateFactory(Element resourceListElement) {
new HashSet<>(
processorUtil.getClassArrayValueFromAnnotation(
resourceListElement, ResourceList.class, "value"));
Set<TypeMirror> metadataAnnotationTypes =
new HashSet<>(
processorUtil.getClassArrayValueFromAnnotation(
resourceListElement, MetaDataAnnotations.class, "value"));

resourceTypes.forEach(
typeMirror -> {
Element element = types.asElement(typeMirror);
writeSource(
new RequestFactorySourceWriter(element, resourceList.serviceRoot(), processingEnv)
new RequestFactorySourceWriter(
element, resourceList.serviceRoot(), metadataAnnotationTypes, processingEnv)
.asTypeBuilder(),
elements.getPackageOf(resourceListElement).getQualifiedName().toString());
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright © 2019 Dominokit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dominokit.rest.processor;

public @interface JwtRequired {}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
public interface NoFactoryAnnotationSampleService {

@GET
@JwtRequired
@Path("someService/{id}")
SampleResponse getById(@PathParam("id") @RequestBody int id, int count);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/
@ResourceList({NoFactoryAnnotationSampleService.class, NoFactoryAnnotationSampleService.IntX.class})
@MetaDataAnnotations({JwtRequired.class})
package org.dominokit.rest.processor;

import org.dominokit.rest.shared.request.service.annotations.MetaDataAnnotations;
import org.dominokit.rest.shared.request.service.annotations.ResourceList;
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright © 2019 Dominokit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dominokit.rest.shared.request.service.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* This annotation is used to locate the services without annotating them with {@link
* RequestFactory}.
*
* <p>This is helpful when services interfaces not part of the project and they don't have {@link
* RequestFactory} annotation. Including these services are done by providing them inside this
* annotation and define it on <b>package-info.java</b>
*
* @see RequestFactory
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PACKAGE)
public @interface MetaDataAnnotations {

Class<?>[] value() default {};
}

0 comments on commit cb13939

Please sign in to comment.