Skip to content

Commit

Permalink
[wip] deduct entity type from one-level generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Gytis Trikleris committed Feb 9, 2021
1 parent 805a7f6 commit 4f58ec1
Showing 1 changed file with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ LinksContainer getLinksContainer(List<ResourceClass> resourceClasses) {
LinksContainer linksContainer = new LinksContainer();

for (ResourceClass resourceClass : resourceClasses) {
ClassInfo resourceClassInfo = index.getClassByName(DotName.createSimple(resourceClass.getClassName()));
for (ResourceMethod resourceMethod : resourceClass.getMethods()) {
AnnotationInstance restLinkAnnotation = getRestLinkAnnotation(resourceClassInfo, resourceMethod);
MethodInfo resourceMethodInfo = getResourceMethodInfo(resourceClass, resourceMethod);
AnnotationInstance restLinkAnnotation = resourceMethodInfo.annotation(REST_LINK_ANNOTATION);
if (restLinkAnnotation != null) {
LinkInfo linkInfo = getLinkInfo(resourceClass, resourceMethod, restLinkAnnotation);
LinkInfo linkInfo = getLinkInfo(resourceClass, resourceMethod, resourceMethodInfo,
restLinkAnnotation);
linksContainer.put(linkInfo);
}
}
Expand All @@ -53,28 +54,39 @@ LinksContainer getLinksContainer(List<ResourceClass> resourceClasses) {
}

private LinkInfo getLinkInfo(ResourceClass resourceClass, ResourceMethod resourceMethod,
AnnotationInstance restLinkAnnotation) {
MethodInfo resourceMethodInfo, AnnotationInstance restLinkAnnotation) {
String rel = getAnnotationValue(restLinkAnnotation, "rel", resourceMethod.getName());
String entityType = getAnnotationValue(restLinkAnnotation, "entityType", resourceMethod.getSimpleReturnType());
String entityType = getAnnotationValue(restLinkAnnotation, "entityType", deductEntityType(resourceMethodInfo));
String path = UriBuilder.fromPath(resourceClass.getPath()).path(resourceMethod.getPath()).toTemplate();
Set<String> pathParameters = getPathParameters(path);

return new LinkInfo(rel, entityType, path, pathParameters);
}

private String deductEntityType(MethodInfo methodInfo) {
if (methodInfo.returnType().kind() == Type.Kind.PARAMETERIZED_TYPE) {
if (methodInfo.returnType().asParameterizedType().arguments().size() == 1) {
return methodInfo.returnType().asParameterizedType().arguments().get(0).name().toString();
}
}
return methodInfo.returnType().name().toString();
}

private Set<String> getPathParameters(String path) {
Set<String> names = new HashSet<>();
URLUtils.parsePathParameters(path, names);
return names;
}

private AnnotationInstance getRestLinkAnnotation(ClassInfo resourceClassInfo, ResourceMethod resourceMethod) {
for (MethodInfo resourceMethodInfo : resourceClassInfo.methods()) {
if (isSameMethod(resourceMethodInfo, resourceMethod)) {
return resourceMethodInfo.annotation(REST_LINK_ANNOTATION);
private MethodInfo getResourceMethodInfo(ResourceClass resourceClass, ResourceMethod resourceMethod) {
ClassInfo classInfo = index.getClassByName(DotName.createSimple(resourceClass.getClassName()));
for (MethodInfo methodInfo : classInfo.methods()) {
if (isSameMethod(methodInfo, resourceMethod)) {
return methodInfo;
}
}
return null;
throw new RuntimeException(String.format("Could not find method info for resource '%s.%s'",
resourceClass.getClassName(), resourceMethod.getName()));
}

private boolean isSameMethod(MethodInfo resourceMethodInfo, ResourceMethod resourceMethod) {
Expand Down

0 comments on commit 4f58ec1

Please sign in to comment.