Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -66,7 +66,7 @@ public class ValidatorHelper {

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

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

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

public void enclosingElementHasAnnotation(Class<? extends Annotation> annotation, Element element, ElementValidation validation) {
enclosingElementHasOneOfAnnotations(element, Collections.<Class<? extends Annotation>> singletonList(annotation), validation);
}

public void enclosingElementHasEBeanAnnotation(Element element, ElementValidation valid) {
Element enclosingElement = element.getEnclosingElement();
hasClassAnnotation(element, enclosingElement, EBean.class, valid);
enclosingElementHasAnnotation(EBean.class, element, valid);
}

public void enclosingElementHasEActivity(Element element, ElementValidation valid) {
Element enclosingElement = element.getEnclosingElement();
hasClassAnnotation(element, enclosingElement, EActivity.class, valid);
enclosingElementHasAnnotation(EActivity.class, element, valid);
}

public void enclosingElementHasEActivityOrEFragment(Element element, ElementValidation valid) {
Element enclosingElement = element.getEnclosingElement();
List<Class<? extends Annotation>> validAnnotations = asList(EActivity.class, EFragment.class);
hasOneOfClassAnnotations(element, enclosingElement, validAnnotations, valid);
enclosingElementHasOneOfAnnotations(element, validAnnotations, valid);
}

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

public void enclosingElementHasEFragment(Element element, ElementValidation valid) {
Element enclosingElement = element.getEnclosingElement();
hasClassAnnotation(element, enclosingElement, EFragment.class, valid);
enclosingElementHasAnnotation(EFragment.class, element, valid);
}

public void enclosingElementHasEIntentService(Element element, ElementValidation valid) {
Element enclosingElement = element.getEnclosingElement();
hasClassAnnotation(element, enclosingElement, EIntentService.class, valid);
enclosingElementHasAnnotation(EIntentService.class, element, valid);
}

public void enclosingElementHasEReceiver(Element element, ElementValidation valid) {
Element enclosingElement = element.getEnclosingElement();
hasClassAnnotation(element, enclosingElement, EReceiver.class, valid);
enclosingElementHasAnnotation(EReceiver.class, element, valid);
}

public void hasEActivity(Element element, ElementValidation valid) {
hasClassAnnotation(element, element, EActivity.class, valid);
hasAnnotation(element, element, EActivity.class, valid);
}

public void hasEActivityOrEFragment(Element element, ElementValidation valid) {
List<Class<? extends Annotation>> validAnnotations = asList(EActivity.class, EFragment.class);
hasOneOfClassAnnotations(element, element, validAnnotations, valid);
hasOneOfAnnotations(element, element, validAnnotations, valid);
}

public void enclosingElementHasEnhancedViewSupportAnnotation(Element element, ElementValidation valid) {
Element enclosingElement = element.getEnclosingElement();
hasOneOfClassAnnotations(element, enclosingElement, VALID_ENHANCED_VIEW_SUPPORT_ANNOTATIONS, valid);
enclosingElementHasOneOfAnnotations(element, VALID_ENHANCED_VIEW_SUPPORT_ANNOTATIONS, valid);
}

public void enclosingElementHasEnhancedComponentAnnotation(Element element, ElementValidation valid) {
Element enclosingElement = element.getEnclosingElement();
hasOneOfClassAnnotations(element, enclosingElement, VALID_ENHANCED_COMPONENT_ANNOTATIONS, valid);
enclosingElementHasOneOfAnnotations(element, VALID_ENHANCED_COMPONENT_ANNOTATIONS, valid);
}

public void enclosingElementHasAndroidAnnotation(Element element, ElementValidation valid) {
Element enclosingElement = element.getEnclosingElement();
hasOneOfClassAnnotations(element, enclosingElement, environment().getGeneratingAnnotations(), valid);
enclosingElementHasOneOfAnnotations(element, environment().getGeneratingAnnotations(), valid);
}

private void hasClassAnnotation(Element reportElement, Element element, Class<? extends Annotation> validAnnotation, ElementValidation valid) {
private void hasAnnotation(Element element, Element reportElement, Class<? extends Annotation> validAnnotation, ElementValidation valid) {
ArrayList<Class<? extends Annotation>> validAnnotations = new ArrayList<>();
validAnnotations.add(validAnnotation);
hasOneOfClassAnnotations(reportElement, element, validAnnotations, valid);
hasOneOfAnnotations(element, reportElement, validAnnotations, valid);
}

public void hasOneOfClassAnnotations(Element reportElement, Element element, List<Class<? extends Annotation>> validAnnotations, ElementValidation valid) {
public void enclosingElementHasOneOfAnnotations(Element element, List<Class<? extends Annotation>> validAnnotations, ElementValidation validation) {
hasOneOfAnnotations(element, element.getEnclosingElement(), validAnnotations, validation);
}

private void hasOneOfAnnotations(Element reportElement, Element element, List<Class<? extends Annotation>> validAnnotations, ElementValidation validation) {
checkAnnotations(reportElement, element, validAnnotations, true, validation);
}

public void doesNotHaveOneOfAnnotations(Element element, List<Class<? extends Annotation>> validAnnotations, ElementValidation validation) {
checkAnnotations(element, element, validAnnotations, false, validation);
}

public void doesNotHaveAnnotation(Element element, Class<? extends Annotation> annotation, ElementValidation validation) {
doesNotHaveOneOfAnnotations(element, Collections.<Class<? extends Annotation>> singletonList(annotation), validation);
}

private void checkAnnotations(Element reportElement, Element element, List<Class<? extends Annotation>> validAnnotations, boolean shouldFind, ElementValidation validation) {

boolean foundAnnotation = false;
for (Class<? extends Annotation> validAnnotation : validAnnotations) {
if (element.getAnnotation(validAnnotation) != null) {
foundAnnotation = true;
break;
}
}
if (!foundAnnotation) {
valid.addError(reportElement, "%s can only be used in a class annotated with " + getFormattedValidEnhancedBeanAnnotationTypes(validAnnotations) + ".");

if (shouldFind != foundAnnotation) {
String not = shouldFind ? "" : " not";

validation.addError(reportElement, "%s can only be used in a " + element.getKind().toString().toLowerCase() + not + " annotated with "
+ getFormattedValidEnhancedBeanAnnotationTypes(validAnnotations) + ".");
}
}

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

public void enclosingMethodHasAnnotation(Class<? extends Annotation> annotation, Element element, ElementValidation valid) {
String error = "can only be used with a method annotated with";
enclosingElementHasAnnotation(annotation, element, valid, error);
}

public void enclosingElementHasAnnotation(Class<? extends Annotation> annotation, Element element, ElementValidation valid, String error) {
Element enclosingElement = element.getEnclosingElement();
elementHasAnnotation(annotation, enclosingElement, valid, error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ExtraParameterHandler(Class<? extends Annotation> targetClass, Class<? ex

@Override
protected void validate(Element element, ElementValidation valid) {
validatorHelper.enclosingMethodHasAnnotation(methodAnnotationClass, element, valid);
validatorHelper.enclosingElementHasAnnotation(methodAnnotationClass, element, valid);

validatorHelper.canBePutInABundle(element, valid);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
*
* 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.androidannotations.rest.spring.annotations;

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

/**
* This annotation can be used to add a form-encoded parameter to the request
* from a method parameter. The annotation value should be the name of the
* form-encoded parameter, if not specified, the method parameter name will be
* used as the name of the form-encoded parameter. This annotation only can be
* used with POST requests, hence the method must be annotated with {@link Post}
* . To use this annotation, you must add <code>FormHttpMessageConverter</code>
* to the list of converters.
*
* <blockquote>
*
* <b>Example :</b>
*
* <pre>
* &#064;Rest(rootUrl = &quot;http://myserver&quot;, converters = FormHttpMessageConverter.class)
* public interface RestClient {
*
* &#064;Post(&quot;/events/{id}&quot;)
* EventList addEvent(String id, <b>&#064;Field</b> String eventName);
* }
* </pre>
*
* </blockquote>
*
* @see Rest
* @see Post
* @see Part
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.PARAMETER)
public @interface Field {

/**
* Name of the form-encoded parameter.
*
* @return name of the parameter
*/
String value() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
*
* 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.androidannotations.rest.spring.annotations;

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

/**
* This annotation can be used to add a multi-part parameter to the POST request
* from a method parameter. The annotation value should be the name of the
* multi-part parameter, if not specified, the method parameter name will be
* used as the name of the multi-part parameter. This annotation only can be
* used with POST requests, hence the method must be annotated with {@link Post}
* . To use this annotation, you must add <code>FormHttpMessageConverter</code>
* to the list of converters.
*
* <blockquote>
*
* <b>Example :</b>
*
* <pre>
* &#064;Rest(rootUrl = &quot;http://myserver&quot;, converters = FormHttpMessageConverter.class)
* public interface RestClient {
*
* &#064;Post(&quot;/events/{id}&quot;)
* EventList addEvent(String id, <b>&#064;Part</b> FileResource image);
* }
* </pre>
*
* </blockquote>
*
* @see Post
* @see Part
* @see Rest
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.PARAMETER)
public @interface Part {

/**
* Name of the post parameter.
*
* @return name of the parameter
*/
String value() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
*
* 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.androidannotations.rest.spring.annotations;

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

/**
* This annotation can be used to mark a method parameter to be an url variable.
* This annotation is optional, because method parameters which are not
* annotated with any annotation or not correspond to the request entity,
* implicitly interpreted as url variables. However with this annotation you can
* explicitly mark them. Also, with the annotation value, you can add another
* name to the url variable. If an url variable method parameter does not have
* the {@link Path} annotation, or the annotation value is not specified, the
* method parameter name will be used as the url variable name. The url in the
* {@link Get} etc. annotation must contain an url variable, which the
* corresponding method variable will be substituted into.
*
* <blockquote>
*
* <b>Example :</b>
*
* <pre>
* &#064;Rest(converters = MappingJacksonHttpMessageConverter.class)
* public interface MyRestClient {
*
* &#064;Get(&quot;/event/<b>{id}</b>&quot;)
* Event getEvent(String <b>id</b>);
*
* &#064;Get(&quot;/event/<b>{id}</b>&quot;)
* Event getEventWithPathAnnotation(&#064;Path String <b>id</b>);
*
* &#064;Get(&quot;/event/<b>{id}</b>&quot;)
* Event getEventWithPathAnnotationValue(&#064;Path(&quot;<b>id</b>&quot;) String identifier);
* }
* </pre>
*
* </blockquote>
*
* @see Rest
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.PARAMETER)
public @interface Path {

/**
* Name of the url variable.
*
* @return the url variable name
*/
String value() default "";
}
Loading