Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Commit 774f80f

Browse files
committed
Merge pull request #1448 from WonderCsabo/1438_restFormParamAnnotation
Add @field, @part and @path
2 parents ab0e3c8 + c2fe71e commit 774f80f

25 files changed

Lines changed: 1156 additions & 51 deletions

File tree

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java‎

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929

3030
import java.lang.annotation.Annotation;
3131
import java.util.ArrayList;
32-
import java.util.Arrays;
3332
import java.util.Collection;
33+
import java.util.Collections;
3434
import java.util.List;
3535
import java.util.Set;
3636
import java.util.TreeSet;
@@ -66,7 +66,7 @@ public class ValidatorHelper {
6666

6767
private static final List<String> ANDROID_FRAGMENT_QUALIFIED_NAMES = asList(CanonicalNameConstants.FRAGMENT, CanonicalNameConstants.SUPPORT_V4_FRAGMENT);
6868

69-
private static final Collection<Integer> VALID_LOG_LEVELS = Arrays.asList(LOG_VERBOSE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR);
69+
private static final Collection<Integer> VALID_LOG_LEVELS = asList(LOG_VERBOSE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR);
7070

7171
private static final List<String> VALID_PREFERENCE_CLASSES = asList(CanonicalNameConstants.PREFERENCE_ACTIVITY, CanonicalNameConstants.PREFERENCE_FRAGMENT,
7272
CanonicalNameConstants.SUPPORT_V4_PREFERENCE_FRAGMENT, CanonicalNameConstants.MACHINARIUS_V4_PREFERENCE_FRAGMENT, CanonicalNameConstants.SUPPORT_V7_PREFERENCE_FRAGMENTCOMPAT,
@@ -149,83 +149,98 @@ public void enclosingElementIsNotAbstractIfNotAbstract(Element element, ElementV
149149
}
150150
}
151151

152+
public void enclosingElementHasAnnotation(Class<? extends Annotation> annotation, Element element, ElementValidation validation) {
153+
enclosingElementHasOneOfAnnotations(element, Collections.<Class<? extends Annotation>> singletonList(annotation), validation);
154+
}
155+
152156
public void enclosingElementHasEBeanAnnotation(Element element, ElementValidation valid) {
153-
Element enclosingElement = element.getEnclosingElement();
154-
hasClassAnnotation(element, enclosingElement, EBean.class, valid);
157+
enclosingElementHasAnnotation(EBean.class, element, valid);
155158
}
156159

157160
public void enclosingElementHasEActivity(Element element, ElementValidation valid) {
158-
Element enclosingElement = element.getEnclosingElement();
159-
hasClassAnnotation(element, enclosingElement, EActivity.class, valid);
161+
enclosingElementHasAnnotation(EActivity.class, element, valid);
160162
}
161163

162164
public void enclosingElementHasEActivityOrEFragment(Element element, ElementValidation valid) {
163-
Element enclosingElement = element.getEnclosingElement();
164165
List<Class<? extends Annotation>> validAnnotations = asList(EActivity.class, EFragment.class);
165-
hasOneOfClassAnnotations(element, enclosingElement, validAnnotations, valid);
166+
enclosingElementHasOneOfAnnotations(element, validAnnotations, valid);
166167
}
167168

168169
public void enclosingElementHasEActivityOrEFragmentOrEServiceOrEIntentService(Element element, ElementValidation valid) {
169-
Element enclosingElement = element.getEnclosingElement();
170170
List<Class<? extends Annotation>> validAnnotations = asList(EActivity.class, EFragment.class, EService.class, EIntentService.class);
171-
hasOneOfClassAnnotations(element, enclosingElement, validAnnotations, valid);
171+
enclosingElementHasOneOfAnnotations(element, validAnnotations, valid);
172172
}
173173

174174
public void enclosingElementHasEFragment(Element element, ElementValidation valid) {
175-
Element enclosingElement = element.getEnclosingElement();
176-
hasClassAnnotation(element, enclosingElement, EFragment.class, valid);
175+
enclosingElementHasAnnotation(EFragment.class, element, valid);
177176
}
178177

179178
public void enclosingElementHasEIntentService(Element element, ElementValidation valid) {
180-
Element enclosingElement = element.getEnclosingElement();
181-
hasClassAnnotation(element, enclosingElement, EIntentService.class, valid);
179+
enclosingElementHasAnnotation(EIntentService.class, element, valid);
182180
}
183181

184182
public void enclosingElementHasEReceiver(Element element, ElementValidation valid) {
185-
Element enclosingElement = element.getEnclosingElement();
186-
hasClassAnnotation(element, enclosingElement, EReceiver.class, valid);
183+
enclosingElementHasAnnotation(EReceiver.class, element, valid);
187184
}
188185

189186
public void hasEActivity(Element element, ElementValidation valid) {
190-
hasClassAnnotation(element, element, EActivity.class, valid);
187+
hasAnnotation(element, element, EActivity.class, valid);
191188
}
192189

193190
public void hasEActivityOrEFragment(Element element, ElementValidation valid) {
194191
List<Class<? extends Annotation>> validAnnotations = asList(EActivity.class, EFragment.class);
195-
hasOneOfClassAnnotations(element, element, validAnnotations, valid);
192+
hasOneOfAnnotations(element, element, validAnnotations, valid);
196193
}
197194

198195
public void enclosingElementHasEnhancedViewSupportAnnotation(Element element, ElementValidation valid) {
199-
Element enclosingElement = element.getEnclosingElement();
200-
hasOneOfClassAnnotations(element, enclosingElement, VALID_ENHANCED_VIEW_SUPPORT_ANNOTATIONS, valid);
196+
enclosingElementHasOneOfAnnotations(element, VALID_ENHANCED_VIEW_SUPPORT_ANNOTATIONS, valid);
201197
}
202198

203199
public void enclosingElementHasEnhancedComponentAnnotation(Element element, ElementValidation valid) {
204-
Element enclosingElement = element.getEnclosingElement();
205-
hasOneOfClassAnnotations(element, enclosingElement, VALID_ENHANCED_COMPONENT_ANNOTATIONS, valid);
200+
enclosingElementHasOneOfAnnotations(element, VALID_ENHANCED_COMPONENT_ANNOTATIONS, valid);
206201
}
207202

208203
public void enclosingElementHasAndroidAnnotation(Element element, ElementValidation valid) {
209-
Element enclosingElement = element.getEnclosingElement();
210-
hasOneOfClassAnnotations(element, enclosingElement, environment().getGeneratingAnnotations(), valid);
204+
enclosingElementHasOneOfAnnotations(element, environment().getGeneratingAnnotations(), valid);
211205
}
212206

213-
private void hasClassAnnotation(Element reportElement, Element element, Class<? extends Annotation> validAnnotation, ElementValidation valid) {
207+
private void hasAnnotation(Element element, Element reportElement, Class<? extends Annotation> validAnnotation, ElementValidation valid) {
214208
ArrayList<Class<? extends Annotation>> validAnnotations = new ArrayList<>();
215209
validAnnotations.add(validAnnotation);
216-
hasOneOfClassAnnotations(reportElement, element, validAnnotations, valid);
210+
hasOneOfAnnotations(element, reportElement, validAnnotations, valid);
217211
}
218212

219-
public void hasOneOfClassAnnotations(Element reportElement, Element element, List<Class<? extends Annotation>> validAnnotations, ElementValidation valid) {
213+
public void enclosingElementHasOneOfAnnotations(Element element, List<Class<? extends Annotation>> validAnnotations, ElementValidation validation) {
214+
hasOneOfAnnotations(element, element.getEnclosingElement(), validAnnotations, validation);
215+
}
216+
217+
private void hasOneOfAnnotations(Element reportElement, Element element, List<Class<? extends Annotation>> validAnnotations, ElementValidation validation) {
218+
checkAnnotations(reportElement, element, validAnnotations, true, validation);
219+
}
220+
221+
public void doesNotHaveOneOfAnnotations(Element element, List<Class<? extends Annotation>> validAnnotations, ElementValidation validation) {
222+
checkAnnotations(element, element, validAnnotations, false, validation);
223+
}
224+
225+
public void doesNotHaveAnnotation(Element element, Class<? extends Annotation> annotation, ElementValidation validation) {
226+
doesNotHaveOneOfAnnotations(element, Collections.<Class<? extends Annotation>> singletonList(annotation), validation);
227+
}
228+
229+
private void checkAnnotations(Element reportElement, Element element, List<Class<? extends Annotation>> validAnnotations, boolean shouldFind, ElementValidation validation) {
230+
220231
boolean foundAnnotation = false;
221232
for (Class<? extends Annotation> validAnnotation : validAnnotations) {
222233
if (element.getAnnotation(validAnnotation) != null) {
223234
foundAnnotation = true;
224235
break;
225236
}
226237
}
227-
if (!foundAnnotation) {
228-
valid.addError(reportElement, "%s can only be used in a class annotated with " + getFormattedValidEnhancedBeanAnnotationTypes(validAnnotations) + ".");
238+
239+
if (shouldFind != foundAnnotation) {
240+
String not = shouldFind ? "" : " not";
241+
242+
validation.addError(reportElement, "%s can only be used in a " + element.getKind().toString().toLowerCase() + not + " annotated with "
243+
+ getFormattedValidEnhancedBeanAnnotationTypes(validAnnotations) + ".");
229244
}
230245
}
231246

@@ -248,11 +263,6 @@ public void hasViewByIdAnnotation(Element element, ElementValidation valid) {
248263
elementHasAnnotation(ViewById.class, element, valid, error);
249264
}
250265

251-
public void enclosingMethodHasAnnotation(Class<? extends Annotation> annotation, Element element, ElementValidation valid) {
252-
String error = "can only be used with a method annotated with";
253-
enclosingElementHasAnnotation(annotation, element, valid, error);
254-
}
255-
256266
public void enclosingElementHasAnnotation(Class<? extends Annotation> annotation, Element element, ElementValidation valid, String error) {
257267
Element enclosingElement = element.getEnclosingElement();
258268
elementHasAnnotation(annotation, enclosingElement, valid, error);

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/ExtraParameterHandler.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public ExtraParameterHandler(Class<? extends Annotation> targetClass, Class<? ex
5151

5252
@Override
5353
protected void validate(Element element, ElementValidation valid) {
54-
validatorHelper.enclosingMethodHasAnnotation(methodAnnotationClass, element, valid);
54+
validatorHelper.enclosingElementHasAnnotation(methodAnnotationClass, element, valid);
5555

5656
validatorHelper.canBePutInABundle(element, valid);
5757
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.rest.spring.annotations;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* This annotation can be used to add a form-encoded parameter to the request
25+
* from a method parameter. The annotation value should be the name of the
26+
* form-encoded parameter, if not specified, the method parameter name will be
27+
* used as the name of the form-encoded parameter. This annotation only can be
28+
* used with POST requests, hence the method must be annotated with {@link Post}
29+
* . To use this annotation, you must add <code>FormHttpMessageConverter</code>
30+
* to the list of converters.
31+
*
32+
* <blockquote>
33+
*
34+
* <b>Example :</b>
35+
*
36+
* <pre>
37+
* &#064;Rest(rootUrl = &quot;http://myserver&quot;, converters = FormHttpMessageConverter.class)
38+
* public interface RestClient {
39+
*
40+
* &#064;Post(&quot;/events/{id}&quot;)
41+
* EventList addEvent(String id, <b>&#064;Field</b> String eventName);
42+
* }
43+
* </pre>
44+
*
45+
* </blockquote>
46+
*
47+
* @see Rest
48+
* @see Post
49+
* @see Part
50+
*/
51+
@Retention(RetentionPolicy.CLASS)
52+
@Target(ElementType.PARAMETER)
53+
public @interface Field {
54+
55+
/**
56+
* Name of the form-encoded parameter.
57+
*
58+
* @return name of the parameter
59+
*/
60+
String value() default "";
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.rest.spring.annotations;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* This annotation can be used to add a multi-part parameter to the POST request
25+
* from a method parameter. The annotation value should be the name of the
26+
* multi-part parameter, if not specified, the method parameter name will be
27+
* used as the name of the multi-part parameter. This annotation only can be
28+
* used with POST requests, hence the method must be annotated with {@link Post}
29+
* . To use this annotation, you must add <code>FormHttpMessageConverter</code>
30+
* to the list of converters.
31+
*
32+
* <blockquote>
33+
*
34+
* <b>Example :</b>
35+
*
36+
* <pre>
37+
* &#064;Rest(rootUrl = &quot;http://myserver&quot;, converters = FormHttpMessageConverter.class)
38+
* public interface RestClient {
39+
*
40+
* &#064;Post(&quot;/events/{id}&quot;)
41+
* EventList addEvent(String id, <b>&#064;Part</b> FileResource image);
42+
* }
43+
* </pre>
44+
*
45+
* </blockquote>
46+
*
47+
* @see Post
48+
* @see Part
49+
* @see Rest
50+
*/
51+
@Retention(RetentionPolicy.CLASS)
52+
@Target(ElementType.PARAMETER)
53+
public @interface Part {
54+
55+
/**
56+
* Name of the post parameter.
57+
*
58+
* @return name of the parameter
59+
*/
60+
String value() default "";
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.rest.spring.annotations;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* This annotation can be used to mark a method parameter to be an url variable.
25+
* This annotation is optional, because method parameters which are not
26+
* annotated with any annotation or not correspond to the request entity,
27+
* implicitly interpreted as url variables. However with this annotation you can
28+
* explicitly mark them. Also, with the annotation value, you can add another
29+
* name to the url variable. If an url variable method parameter does not have
30+
* the {@link Path} annotation, or the annotation value is not specified, the
31+
* method parameter name will be used as the url variable name. The url in the
32+
* {@link Get} etc. annotation must contain an url variable, which the
33+
* corresponding method variable will be substituted into.
34+
*
35+
* <blockquote>
36+
*
37+
* <b>Example :</b>
38+
*
39+
* <pre>
40+
* &#064;Rest(converters = MappingJacksonHttpMessageConverter.class)
41+
* public interface MyRestClient {
42+
*
43+
* &#064;Get(&quot;/event/<b>{id}</b>&quot;)
44+
* Event getEvent(String <b>id</b>);
45+
*
46+
* &#064;Get(&quot;/event/<b>{id}</b>&quot;)
47+
* Event getEventWithPathAnnotation(&#064;Path String <b>id</b>);
48+
*
49+
* &#064;Get(&quot;/event/<b>{id}</b>&quot;)
50+
* Event getEventWithPathAnnotationValue(&#064;Path(&quot;<b>id</b>&quot;) String identifier);
51+
* }
52+
* </pre>
53+
*
54+
* </blockquote>
55+
*
56+
* @see Rest
57+
*/
58+
@Retention(RetentionPolicy.CLASS)
59+
@Target(ElementType.PARAMETER)
60+
public @interface Path {
61+
62+
/**
63+
* Name of the url variable.
64+
*
65+
* @return the url variable name
66+
*/
67+
String value() default "";
68+
}

0 commit comments

Comments
 (0)