From 07db76a98122dcc6cdf119a89887ee993924be98 Mon Sep 17 00:00:00 2001 From: shiraji Date: Wed, 18 Nov 2015 23:47:45 +0900 Subject: [PATCH 1/2] Removed path placeholder function --- .../helper/ValidatorHelper.java | 10 ---------- .../spring/helper/RestAnnotationHelper.java | 17 +++-------------- .../helper/RestSpringValidatorHelper.java | 16 +++++----------- 3 files changed, 8 insertions(+), 35 deletions(-) diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java index 7894c48c8f..efd5dcfd7f 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java @@ -330,16 +330,6 @@ public int numberOfElementParameterHasAnnotation(ExecutableElement element, Clas return count; } - public int numberOfNotAnnotatedElementParameter(ExecutableElement element) { - int count = 0; - for (VariableElement parameter : element.getParameters()) { - if (parameter.getAnnotationMirrors().size() == 0) { - count++; - } - } - return count; - } - public void doesntThrowException(Element element, ElementValidation valid) { ExecutableElement executableElement = (ExecutableElement) element; if (executableElement.getThrownTypes().size() > 0) { diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/helper/RestAnnotationHelper.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/helper/RestAnnotationHelper.java index b76be56fe8..d482ccfeca 100644 --- a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/helper/RestAnnotationHelper.java +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/helper/RestAnnotationHelper.java @@ -109,7 +109,7 @@ public Set extractUrlVariableNames(String uriTemplate) { public JVar declareUrlVariables(ExecutableElement element, RestHolder holder, JBlock methodBody, SortedMap methodParams) { Map urlNameToElementName = new HashMap(); for (VariableElement variableElement : element.getParameters()) { - if (!hasPostParameterAnnotation(variableElement)) { + if (variableElement.getAnnotation(Path.class) != null) { urlNameToElementName.put(getUrlVariableCorrespondingTo(variableElement), variableElement.getSimpleName().toString()); } } @@ -331,14 +331,7 @@ public JVar getEntitySentToServer(ExecutableElement element, SortedMap parametersName = new HashSet<>(); for (VariableElement parameter : parameters) { - if (restAnnotationHelper.hasPostParameterAnnotation(parameter)) { + if (parameter.getAnnotation(Path.class) == null) { continue; } @@ -441,8 +441,7 @@ public void urlVariableNameExistsInEnclosingAnnotation(Element element, ElementV Set urlVariableNames = restAnnotationHelper.extractUrlVariableNames(url); - String annotationValue = restAnnotationHelper.extractAnnotationValueParameter(element); - String expectedUrlVariableName = !annotationValue.equals("") ? annotationValue : element.getSimpleName().toString(); + String expectedUrlVariableName = restAnnotationHelper.getUrlVariableCorrespondingTo((VariableElement) element); if (!urlVariableNames.contains(expectedUrlVariableName)) { validation.addError(element, "%s annotated parameter is has no corresponding url variable"); @@ -601,17 +600,12 @@ public void hasAnnotatedAllParameters(ExecutableElement element, ElementValidati } Set urlVariableNames = restAnnotationHelper.extractUrlVariableNames(element); - if (urlVariableNames.size() != numberOfNotAnnotatedElementParameter(element) + numberOfPathAnnotatedParameter(element) + numberOfRequiresCookieInUrl(element)) { - validation.addError(element, "%s parameters must add annotations or define as @Path placeholders"); + if (urlVariableNames.size() != numberOfPathAnnotatedParameter(element) + numberOfRequiresCookieInUrl(element)) { + validation.addError(element, "%s must have url variables corresponding to the @Path or @RequiresCookieInUrl annotation"); } for (VariableElement variableElement : element.getParameters()) { - List annotationMirrors = variableElement.getAnnotationMirrors(); - if (annotationMirrors.size() == 0) { - if (!urlVariableNames.contains(variableElement.getSimpleName().toString())) { - validation.addError(element, "%s method parameters '" + variableElement + "' must be annotated"); - } - } else if (!restAnnotationHelper.hasRequestParameterAnnotation(variableElement)) { + if (!restAnnotationHelper.hasRestApiMethodParameterAnnotation(variableElement)) { validation.addError(element, "%s method parameters '" + variableElement + "' must be annotated"); } } From 5fd6f07608c29033bba3fbaf9ebf7e05271640eb Mon Sep 17 00:00:00 2001 From: shiraji Date: Wed, 18 Nov 2015 23:47:59 +0900 Subject: [PATCH 2/2] Fixed test cases for `@Path` because of removing placeholder function --- .../rest/spring/test/MyService.java | 88 +++++++++---------- .../rest/spring/test/PathRestService.java | 2 +- .../rest/spring/ClientWithParameters.java | 3 +- .../rest/spring/ClientWithPathVariable.java | 15 ++-- .../rest/spring/ClientWithPostParameters.java | 3 +- .../rest/spring/RestTest.java | 2 +- 6 files changed, 56 insertions(+), 57 deletions(-) diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring-test/src/main/java/org/androidannotations/rest/spring/test/MyService.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring-test/src/main/java/org/androidannotations/rest/spring/test/MyService.java index 723f4afba9..339f748623 100644 --- a/AndroidAnnotations/androidannotations-rest-spring/rest-spring-test/src/main/java/org/androidannotations/rest/spring/test/MyService.java +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring-test/src/main/java/org/androidannotations/rest/spring/test/MyService.java @@ -60,77 +60,77 @@ public interface MyService { @RequiresCookie("xt") @Get("/events/{year}/{location}") @Accept(MediaType.APPLICATION_JSON) - EventList getEvents(String location, int year); + EventList getEvents(@Path String location, @Path int year); @Get("/events/{year}/{location}") @Accept("application/json") - Event[] getEventsArray(String location, int year); + Event[] getEventsArray(@Path String location, @Path int year); @Get("/events/{year}/{year}") @Accept(MediaType.APPLICATION_JSON) - Event[][] urlWithAParameterDeclaredTwice(int year); + Event[][] urlWithAParameterDeclaredTwice(@Path int year); @Get("/events/{year}/{location}") @Accept(MediaType.APPLICATION_JSON) - Event[][] getEventsArrayOfArrays(String location, int year); + Event[][] getEventsArrayOfArrays(@Path String location, @Path int year); // The response can be a ResponseEntity @Get("/events/{year}/{location}") /* * You may (or may not) declare throwing RestClientException (as a reminder, since it's a RuntimeException), but nothing else. */ - ResponseEntity getEvents2(String location, int year) throws RestClientException; + ResponseEntity getEvents2(@Path String location, @Path int year) throws RestClientException; @Get("/events/{year}/{location}") - ResponseEntity getEventsArray2(String location, int year) throws RestClientException; + ResponseEntity getEventsArray2(@Path String location, @Path int year) throws RestClientException; @Get("/events/{year}/{location}") - ResponseEntity getEventsArrayOfArrays2(String location, int year) throws RestClientException; + ResponseEntity getEventsArrayOfArrays2(@Path String location, @Path int year) throws RestClientException; @Get("/events/{year}/{location}") - List getEventsGenericsList(String location, int year) throws RestClientException; + List getEventsGenericsList(@Path String location, @Path int year) throws RestClientException; @Get("/events/{year}/{location}") - List[] getEventsGenericsArrayList(String location, int year) throws RestClientException; + List[] getEventsGenericsArrayList(@Path String location, @Path int year) throws RestClientException; @Get("/events/{year}/{location}") - List[][] getEventsGenericsArrayList2(String location, int year) throws RestClientException; + List[][] getEventsGenericsArrayList2(@Path String location, @Path int year) throws RestClientException; @Get("/events/{year}/{location}") - List> getEventsGenericsListListEvent(String location, int year) throws RestClientException; + List> getEventsGenericsListListEvent(@Path String location, @Path int year) throws RestClientException; @Get("/events/{year}/{location}") - List> getEventsGenericsListListEvents(String location, int year) throws RestClientException; + List> getEventsGenericsListListEvents(@Path String location, @Path int year) throws RestClientException; @Get("/events/{year}/{location}") - List getEventsGenericsListArray(String location, int year) throws RestClientException; + List getEventsGenericsListArray(@Path String location, @Path int year) throws RestClientException; @Get("/events/{year}/{location}") - List getEventsGenericsListArrayArray(String location, int year) throws RestClientException; + List getEventsGenericsListArrayArray(@Path String location, @Path int year) throws RestClientException; @Get("/events/{year}/{location}") - Set getEventsGenericsSet(String location, int year) throws RestClientException; + Set getEventsGenericsSet(@Path String location, @Path int year) throws RestClientException; @Get("/events/{year}/{location}") - GenericEvent getEventsGenericString(String location, int year) throws RestClientException; + GenericEvent getEventsGenericString(@Path String location, @Path int year) throws RestClientException; @Get("/events/{year}/{location}") - GenericEvent getEventsGenericInteger(String location, int year) throws RestClientException; + GenericEvent getEventsGenericInteger(@Path String location, @Path int year) throws RestClientException; @Get("/events/{year}/{location}") - GenericEvent> getEventsGenericListEvent(String location, int year) throws RestClientException; + GenericEvent> getEventsGenericListEvent(@Path String location, @Path int year) throws RestClientException; @Get("/events/{year}/{location}") - GenericEvent>> getEventsGenericsInception(String location, int year) throws RestClientException; + GenericEvent>> getEventsGenericsInception(@Path String location, @Path int year) throws RestClientException; @Get("/events/{year}/{location}") @SetsCookie({ "xt", "sjsaid" }) - Map getEventsGenericsMap(String location, int year) throws RestClientException; + Map getEventsGenericsMap(@Path String location, @Path int year) throws RestClientException; @RequiresCookie("sjsaid") @RequiresCookieInUrl("xt") @Get("/events/{year}/{location}?xt={xt}") - void getEventsVoid(String location, int year) throws RestClientException; + void getEventsVoid(@Path String location, @Path int year) throws RestClientException; // *** POST *** @RequiresHeader("SomeFancyHeader") @@ -147,10 +147,10 @@ public interface MyService { Event addEvent(@Body String event); @Post("/events/{year}/") - Event addEvent(@Body Event event, int year); + Event addEvent(@Body Event event, @Path int year); @Post("/events/{year}/") - Event addEvent(int year); + Event addEvent(@Path int year); @Post("/events/") ResponseEntity addEvent2(@Body Event event); @@ -159,13 +159,13 @@ public interface MyService { @RequiresHeader("SomeFancyHeader") @RequiresCookie("myCookie") @RequiresCookieInUrl("myCookieInUrl") - void addEventWithParameters(String date, @Field String parameter, @Field String otherParameter); + void addEventWithParameters(@Path String date, @Field String parameter, @Field String otherParameter); @Post("/events/{date}?myCookieInUrl={myCookieInUrl}") @RequiresHeader("SomeFancyHeader") @RequiresCookie("myCookie") @RequiresCookieInUrl("myCookieInUrl") - void addEventWithParts(String date, @Part String parameter, @Part String otherParameter); + void addEventWithParts(@Path String date, @Part String parameter, @Part String otherParameter); @Post("/events/{date}?myCookieInUrl={myCookieInUrl}") @RequiresHeader("SomeFancyHeader") @@ -178,13 +178,13 @@ public interface MyService { @Header(name = "SomeFancyHeader", value = "fancy") @RequiresCookie("myCookie") @RequiresCookieInUrl("myCookieInUrl") - void addEventWithHeaders(String date, @Body String parameter); + void addEventWithHeaders(@Path String date, @Body String parameter); @Post("/events/{date}?myCookieInUrl={myCookieInUrl}") @Headers(@Header(name = "SomeFancyHeader", value = "fancy")) @RequiresCookie("myCookie") @RequiresCookieInUrl("myCookieInUrl") - void addEventWithHeadersHeadersAnnotation(String date, @Body String parameter); + void addEventWithHeadersHeadersAnnotation(@Path String date, @Body String parameter); /** * Output different then input @@ -222,78 +222,78 @@ public interface MyService { @Post("/events/{year}/") @Accept(MediaType.APPLICATION_JSON) - ResponseEntity addEvent2(@Body Event event, int year); + ResponseEntity addEvent2(@Body Event event, @Path int year); // *** PUT *** @Put("/events/{id}") - void updateEvent(@Body Event event, int id); + void updateEvent(@Body Event event, @Path int id); @Put("/events/{date}") - void updateEvent(long date); + void updateEvent(@Path long date); @Put("/events/{date}") - Event updateEventWithResponse(long date); + Event updateEventWithResponse(@Path long date); @Put("/events/{date}?myCookieInUrl={myCookieInUrl}") @RequiresHeader("SomeFancyHeader") @RequiresCookie("myCookie") @RequiresCookieInUrl("myCookieInUrl") - Event updateEventWithRequires(long date); + Event updateEventWithRequires(@Path long date); // *** DELETE *** @Delete("/events/{id}") - void removeEvent(long id); + void removeEvent(@Path long id); @Delete("/events/{id}") - Event removeEventWithResponse(long id); + Event removeEventWithResponse(@Path long id); @Delete("/events/{id}?myCookieInUrl={myCookieInUrl}") @RequiresHeader("SomeFancyHeader") @RequiresCookie("myCookie") @RequiresCookieInUrl("myCookieInUrl") - void removeEventWithRequires(long id); + void removeEventWithRequires(@Path long id); @Delete("/events/{id}") @RequiresAuthentication - void removeEventWithAuthentication(long id); + void removeEventWithAuthentication(@Path long id); // *** HEAD *** @Head("/events/{year}/{location}") - HttpHeaders getEventHeaders(String location, int year); + HttpHeaders getEventHeaders(@Path String location, @Path int year); @Head("/events/{date}") - HttpHeaders getEventheaders(long date); + HttpHeaders getEventheaders(@Path long date); @Head("/events/{date}?myCookieInUrl={myCookieInUrl}") @RequiresHeader("SomeFancyHeader") @RequiresCookie("myCookie") @RequiresCookieInUrl("myCookieInUrl") - HttpHeaders getEventheadersWithRequires(long date); + HttpHeaders getEventheadersWithRequires(@Path long date); @Head("/events/{date}") @RequiresAuthentication - HttpHeaders getEventheadersWithAuthentication(long date); + HttpHeaders getEventheadersWithAuthentication(@Path long date); // *** OPTIONS *** @Options("/events/{year}/{location}") - Set getEventOptions(String location, int year); + Set getEventOptions(@Path String location, @Path int year); @Options("/events/{date}") - Set getEventOptions(long date); + Set getEventOptions(@Path long date); @Options("/events/{date}?myCookieInUrl={myCookieInUrl}") @RequiresHeader("SomeFancyHeader") @RequiresCookie("myCookie") @RequiresCookieInUrl("myCookieInUrl") - Set getEventOptionsWithRequires(long date); + Set getEventOptionsWithRequires(@Path long date); @Options("/events/{date}") @RequiresAuthentication - Set getEventOptionsWithAuthentication(long date); + Set getEventOptionsWithAuthentication(@Path long date); // if you need to add some configuration to the Spring RestTemplate. RestTemplate getRestTemplate(); diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring-test/src/main/java/org/androidannotations/rest/spring/test/PathRestService.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring-test/src/main/java/org/androidannotations/rest/spring/test/PathRestService.java index 77022a1706..fc1a5f2860 100644 --- a/AndroidAnnotations/androidannotations-rest-spring/rest-spring-test/src/main/java/org/androidannotations/rest/spring/test/PathRestService.java +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring-test/src/main/java/org/androidannotations/rest/spring/test/PathRestService.java @@ -25,5 +25,5 @@ public interface PathRestService extends RestClientSupport { @Get(value = "{hello}{bye}{parameterName}") - void get(@Path("hello") String bye, @Path("bye") String hello, String parameterName); + void get(@Path("hello") String bye, @Path("bye") String hello, @Path String parameterName); } diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithParameters.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithParameters.java index 0bbade12b1..3cd5905e59 100644 --- a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithParameters.java +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithParameters.java @@ -16,6 +16,7 @@ package org.androidannotations.rest.spring; import org.androidannotations.rest.spring.annotations.Field; +import org.androidannotations.rest.spring.annotations.Path; import org.androidannotations.rest.spring.annotations.Put; import org.androidannotations.rest.spring.annotations.Rest; import org.springframework.http.converter.FormHttpMessageConverter; @@ -33,6 +34,6 @@ public interface ClientWithParameters { void moreParameter(@Field int id, @Field String str); @Put("/{url}") - void oneParameterWithUrl(@Field int id, String url); + void oneParameterWithUrl(@Field int id, @Path String url); } diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithPathVariable.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithPathVariable.java index 07998309f1..a155be83bf 100644 --- a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithPathVariable.java +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithPathVariable.java @@ -33,29 +33,26 @@ public interface ClientWithPathVariable { @Delete("/test/{v1}/{v2}") - void deleteWithParameterEntity(int v1, String v2); + void deleteWithParameterEntity(@Path int v1, @Path String v2); @Get("/test/{v1}/{v2}") - void getWithParameterEntity(int v1, String v2); + void getWithParameterEntity(@Path int v1, @Path String v2); @Head("/test/{v1}/{v2}") - HttpHeaders headWithParameterEntity(int v1, String v2); + HttpHeaders headWithParameterEntity(@Path int v1, @Path String v2); @Options("/test/{v1}/{v2}") - Set optionsWithParameterEntity(int v1, String v2); + Set optionsWithParameterEntity(@Path int v1, @Path String v2); @Post("/test/{v1}/{v2}") - void postWithParameterEntity(int v1, String v2); + void postWithParameterEntity(@Path int v1, @Path String v2); @Put("/test/{v1}/{v2}") - void putWithParameterEntity(int v1, String v2); + void putWithParameterEntity(@Path int v1, @Path String v2); @Get("/test/{v1}") void getWithPathAnnotation(@Path("v1") int version); - @Get("/test/{v1}/{v2}") - void getWithPathAnnotationAndParam(@Path("v1") int v1, String v2); - @Get("/test/{v1}/{v2}") void getWithCrossParamAnnotations(@Path("v1") int v2, @Path("v2") int v1); } diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithPostParameters.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithPostParameters.java index cb3ade10bd..5502ff24fd 100644 --- a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithPostParameters.java +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithPostParameters.java @@ -16,6 +16,7 @@ package org.androidannotations.rest.spring; import org.androidannotations.rest.spring.annotations.Field; +import org.androidannotations.rest.spring.annotations.Path; import org.androidannotations.rest.spring.annotations.Post; import org.androidannotations.rest.spring.annotations.Rest; import org.springframework.core.io.ClassPathResource; @@ -46,7 +47,7 @@ public interface ClientWithPostParameters { void twoFieldssOneWithName(@Field String a, @Field("c") String b); @Post("/{url}") - void fieldAndUrlVariable(@Field String a, String url); + void fieldAndUrlVariable(@Field String a, @Path String url); @Post("/") void fieldClassPathResource(@Field ClassPathResource res); diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/RestTest.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/RestTest.java index ec8cef2630..2143636f1f 100644 --- a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/RestTest.java +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/RestTest.java @@ -132,7 +132,7 @@ public void fieldPathParamOnSameArgument() throws IOException { assertCompilationErrorOn(FieldPathParamOnSameArgument.class, "@Field", result); assertCompilationErrorOn(FieldPathParamOnSameArgument.class, "@Path", result); - assertCompilationErrorCount(3, result); + assertCompilationErrorCount(2, result); } @Test