@Test public void parses_method_to_mapping_representation() { ExecutableElement method = methodElements.of( "fr.vidal.oss.jax_rs_linker.parser.ProductResource", "productById" ); Mapping mapping = parser.parse(method).get(); JavaLocation javaLocation = mapping.getJavaLocation(); assertThat(javaLocation.getClassNameGeneration().getClassName().fullyQualifiedName()) .isEqualTo("fr.vidal.oss.jax_rs_linker.parser.ProductResource"); assertThat(javaLocation.getMethodName()) .isEqualTo("productById"); Api api = mapping.getApi(); assertThat(api.getApiLink().getApiLinkType()) .isEqualTo(ApiLinkType.SELF); assertThat(api.getApiLink().getTarget().isPresent()) .isFalse(); assertThat(api.getApiPath().getPath()) .isEqualTo("/api/product/{id}"); assertThat(api.getApiPath().getPathParameters()) .containsExactly(new PathParameter(ClassName.valueOf("int"), "id")); }
public Optional<Mapping> parse(Element element) { if (element.getKind() != ElementKind.METHOD) { return compilationError(element, CompilationError.NOT_A_METHOD.format(element)); } ExecutableElement method = (ExecutableElement) element; Optional<String> maybePath = pathVisitor.visitPath(method); if (!maybePath.isPresent()) { return compilationError(method, CompilationError.NO_PATH_FOUND.format(qualified(method))); } Optional<HttpVerb> maybeHttpVerb = httpVerbVisitor.visit(method); if (!maybeHttpVerb.isPresent()) { return compilationError(method, CompilationError.NO_HTTP_ANNOTATION_FOUND.format(qualified(method))); } boolean withSelf = byAnnotation(Self.class).test(method); boolean withSubResource = byAnnotation(SubResource.class).test(method); if (!(withSelf ^ withSubResource)) { return compilationError(method, CompilationError.ANNOTATION_MISUSE.format(qualified(method))); } Optional<ApiLink> link = link(method, withSelf); if (!link.isPresent()) { return compilationError(method, CompilationError.MISSING_ANNOTATIONS.format(qualified(method))); } Mapping mapping = mapping(method, link.get(), maybeHttpVerb.get(), maybePath.get()); Optional<CompilationError> maybeError = trackMandatoryParsing(link.get(), mapping); if (maybeError.isPresent()) { return compilationError(method, CompilationError.TOO_MANY_SELF.format(qualified(method))); } return Optional.of(mapping); }
ElementParser { public Optional<Mapping> parse(Element element) { if (element.getKind() != ElementKind.METHOD) { return compilationError(element, CompilationError.NOT_A_METHOD.format(element)); } ExecutableElement method = (ExecutableElement) element; Optional<String> maybePath = pathVisitor.visitPath(method); if (!maybePath.isPresent()) { return compilationError(method, CompilationError.NO_PATH_FOUND.format(qualified(method))); } Optional<HttpVerb> maybeHttpVerb = httpVerbVisitor.visit(method); if (!maybeHttpVerb.isPresent()) { return compilationError(method, CompilationError.NO_HTTP_ANNOTATION_FOUND.format(qualified(method))); } boolean withSelf = byAnnotation(Self.class).test(method); boolean withSubResource = byAnnotation(SubResource.class).test(method); if (!(withSelf ^ withSubResource)) { return compilationError(method, CompilationError.ANNOTATION_MISUSE.format(qualified(method))); } Optional<ApiLink> link = link(method, withSelf); if (!link.isPresent()) { return compilationError(method, CompilationError.MISSING_ANNOTATIONS.format(qualified(method))); } Mapping mapping = mapping(method, link.get(), maybeHttpVerb.get(), maybePath.get()); Optional<CompilationError> maybeError = trackMandatoryParsing(link.get(), mapping); if (maybeError.isPresent()) { return compilationError(method, CompilationError.TOO_MANY_SELF.format(qualified(method))); } return Optional.of(mapping); } }
ElementParser { public Optional<Mapping> parse(Element element) { if (element.getKind() != ElementKind.METHOD) { return compilationError(element, CompilationError.NOT_A_METHOD.format(element)); } ExecutableElement method = (ExecutableElement) element; Optional<String> maybePath = pathVisitor.visitPath(method); if (!maybePath.isPresent()) { return compilationError(method, CompilationError.NO_PATH_FOUND.format(qualified(method))); } Optional<HttpVerb> maybeHttpVerb = httpVerbVisitor.visit(method); if (!maybeHttpVerb.isPresent()) { return compilationError(method, CompilationError.NO_HTTP_ANNOTATION_FOUND.format(qualified(method))); } boolean withSelf = byAnnotation(Self.class).test(method); boolean withSubResource = byAnnotation(SubResource.class).test(method); if (!(withSelf ^ withSubResource)) { return compilationError(method, CompilationError.ANNOTATION_MISUSE.format(qualified(method))); } Optional<ApiLink> link = link(method, withSelf); if (!link.isPresent()) { return compilationError(method, CompilationError.MISSING_ANNOTATIONS.format(qualified(method))); } Mapping mapping = mapping(method, link.get(), maybeHttpVerb.get(), maybePath.get()); Optional<CompilationError> maybeError = trackMandatoryParsing(link.get(), mapping); if (maybeError.isPresent()) { return compilationError(method, CompilationError.TOO_MANY_SELF.format(qualified(method))); } return Optional.of(mapping); } ElementParser(Messager messager,
                         Types typeUtils); }
ElementParser { public Optional<Mapping> parse(Element element) { if (element.getKind() != ElementKind.METHOD) { return compilationError(element, CompilationError.NOT_A_METHOD.format(element)); } ExecutableElement method = (ExecutableElement) element; Optional<String> maybePath = pathVisitor.visitPath(method); if (!maybePath.isPresent()) { return compilationError(method, CompilationError.NO_PATH_FOUND.format(qualified(method))); } Optional<HttpVerb> maybeHttpVerb = httpVerbVisitor.visit(method); if (!maybeHttpVerb.isPresent()) { return compilationError(method, CompilationError.NO_HTTP_ANNOTATION_FOUND.format(qualified(method))); } boolean withSelf = byAnnotation(Self.class).test(method); boolean withSubResource = byAnnotation(SubResource.class).test(method); if (!(withSelf ^ withSubResource)) { return compilationError(method, CompilationError.ANNOTATION_MISUSE.format(qualified(method))); } Optional<ApiLink> link = link(method, withSelf); if (!link.isPresent()) { return compilationError(method, CompilationError.MISSING_ANNOTATIONS.format(qualified(method))); } Mapping mapping = mapping(method, link.get(), maybeHttpVerb.get(), maybePath.get()); Optional<CompilationError> maybeError = trackMandatoryParsing(link.get(), mapping); if (maybeError.isPresent()) { return compilationError(method, CompilationError.TOO_MANY_SELF.format(qualified(method))); } return Optional.of(mapping); } ElementParser(Messager messager,
                         Types typeUtils); Optional<Mapping> parse(Element element); }
ElementParser { public Optional<Mapping> parse(Element element) { if (element.getKind() != ElementKind.METHOD) { return compilationError(element, CompilationError.NOT_A_METHOD.format(element)); } ExecutableElement method = (ExecutableElement) element; Optional<String> maybePath = pathVisitor.visitPath(method); if (!maybePath.isPresent()) { return compilationError(method, CompilationError.NO_PATH_FOUND.format(qualified(method))); } Optional<HttpVerb> maybeHttpVerb = httpVerbVisitor.visit(method); if (!maybeHttpVerb.isPresent()) { return compilationError(method, CompilationError.NO_HTTP_ANNOTATION_FOUND.format(qualified(method))); } boolean withSelf = byAnnotation(Self.class).test(method); boolean withSubResource = byAnnotation(SubResource.class).test(method); if (!(withSelf ^ withSubResource)) { return compilationError(method, CompilationError.ANNOTATION_MISUSE.format(qualified(method))); } Optional<ApiLink> link = link(method, withSelf); if (!link.isPresent()) { return compilationError(method, CompilationError.MISSING_ANNOTATIONS.format(qualified(method))); } Mapping mapping = mapping(method, link.get(), maybeHttpVerb.get(), maybePath.get()); Optional<CompilationError> maybeError = trackMandatoryParsing(link.get(), mapping); if (maybeError.isPresent()) { return compilationError(method, CompilationError.TOO_MANY_SELF.format(qualified(method))); } return Optional.of(mapping); } ElementParser(Messager messager,
                         Types typeUtils); Optional<Mapping> parse(Element element);  }