Skip to content

Commit a3c7200

Browse files
committed
added new rest features
1 parent 8663d98 commit a3c7200

8 files changed

Lines changed: 186 additions & 121 deletions

File tree

‎AndroidAnnotations/androidannotations-api/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
<version>1.6_r2</version>
2525
<scope>provided</scope>
2626
</dependency>
27+
<dependency>
28+
<!-- spring-android-rest-template is required to use the Rest API -->
29+
<groupId>org.springframework.android</groupId>
30+
<artifactId>spring-android-rest-template</artifactId>
31+
<version>1.0.0.RELEASE</version>
32+
<scope>test</scope>
33+
</dependency>
2734
</dependencies>
2835

2936
<build>

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/CanonicalNameConstants.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public final class CanonicalNameConstants {
127127
public static final String REST_TEMPLATE = "org.springframework.web.client.RestTemplate";
128128
public static final String HTTP_MESSAGE_CONVERTER = "org.springframework.http.converter.HttpMessageConverter";
129129
public static final String CLIENT_HTTP_REQUEST_INTERCEPTOR = "org.springframework.http.client.ClientHttpRequestInterceptor";
130+
public static final String REST_CLIENT_EXCEPTION = "org.springframework.web.client.RestClientException";
130131

131132
/*
132133
* RoboGuice

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.androidannotations.annotations.sharedpreferences.DefaultLong;
6969
import org.androidannotations.annotations.sharedpreferences.DefaultString;
7070
import org.androidannotations.annotations.sharedpreferences.SharedPref;
71+
import org.androidannotations.api.rest.EnhancedRestClient;
7172
import org.androidannotations.api.sharedpreferences.SharedPreferencesHelper;
7273
import org.androidannotations.model.AndroidSystemServices;
7374
import org.androidannotations.model.AnnotationElements;
@@ -131,10 +132,15 @@ public void isTopLevel(TypeElement element, IsValid valid) {
131132
}
132133
}
133134

134-
public void doesNotExtendOtherInterfaces(TypeElement element, IsValid valid) {
135+
public void doesNotExtendInvalidInterfaces(TypeElement element, IsValid valid) {
135136
if (element.getInterfaces().size() > 0) {
137+
if (element.getInterfaces().size() == 1 && element.getInterfaces().get(0).toString().equals(EnhancedRestClient.class.getName())) {
138+
// It's allowed to extend the EnhancedRestClient interface
139+
return;
140+
}
141+
136142
valid.invalidate();
137-
annotationHelper.printAnnotationError(element, "%s can only be used on an interface that does not extend other interfaces");
143+
annotationHelper.printAnnotationError(element, "%s interfaces can only extend EnhancedRestInterface");
138144
}
139145
}
140146

@@ -533,11 +539,12 @@ public void extendsOrmLiteDaoWithValidModelParameter(Element element, IsValid va
533539
DeclaredType daoParameterizedType = annotationHelper.getTypeUtils().getDeclaredType(daoTypeElement, modelTypeMirror, wildcardType);
534540
DeclaredType runtimeExceptionDaoParameterizedType = annotationHelper.getTypeUtils().getDeclaredType(runtimeExceptionDaoTypeElement, modelTypeMirror, wildcardType);
535541

536-
// Checks that elementType extends Dao<ModelType, ?> or RuntimeExceptionDao<ModelType, ?>
542+
// Checks that elementType extends Dao<ModelType, ?> or
543+
// RuntimeExceptionDao<ModelType, ?>
537544
if (!annotationHelper.isSubtype(elementType, daoParameterizedType) && !annotationHelper.isSubtype(elementType, runtimeExceptionDaoParameterizedType)) {
538545
valid.invalidate();
539546
annotationHelper.printAnnotationError(element, "%s can only be used on an element that extends " + daoParameterizedType.toString() //
540-
+ " or " + runtimeExceptionDaoParameterizedType.toString());
547+
+ " or " + runtimeExceptionDaoParameterizedType.toString());
541548
}
542549
}
543550
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EBeansHolder.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ public class Classes {
151151
public final JClass SCHEME_REGISTRY = refClass(CanonicalNameConstants.SCHEME_REGISTRY);
152152
public final JClass SINGLE_CLIENT_CONN_MANAGER = refClass(CanonicalNameConstants.SINGLE_CLIENT_CONN_MANAGER);
153153

154+
/*
155+
* Springframework
156+
*/
157+
public final JClass REST_CLIENT_EXCEPTION = refClass(CanonicalNameConstants.REST_CLIENT_EXCEPTION);
154158
}
155159

156160
private final Map<Element, EBeanHolder> eBeanHolders = new HashMap<Element, EBeanHolder>();

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/rest/MethodProcessor.java‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,18 @@
4040

4141
import com.sun.codemodel.JArray;
4242
import com.sun.codemodel.JBlock;
43+
import com.sun.codemodel.JCatchBlock;
4344
import com.sun.codemodel.JClass;
4445
import com.sun.codemodel.JCodeModel;
46+
import com.sun.codemodel.JConditional;
4547
import com.sun.codemodel.JExpr;
4648
import com.sun.codemodel.JExpression;
4749
import com.sun.codemodel.JForEach;
4850
import com.sun.codemodel.JInvocation;
4951
import com.sun.codemodel.JMethod;
5052
import com.sun.codemodel.JMod;
53+
import com.sun.codemodel.JOp;
54+
import com.sun.codemodel.JTryBlock;
5155
import com.sun.codemodel.JType;
5256
import com.sun.codemodel.JVar;
5357

@@ -204,13 +208,40 @@ protected JExpression addResultCallMethod(JExpression restCall, MethodProcessorH
204208

205209
private void insertRestCallInBody(JBlock body, JExpression restCall, MethodProcessorHolder methodHolder, boolean methodReturnVoid, boolean usesInstance) {
206210
if (methodReturnVoid && !usesInstance && restCall instanceof JInvocation) {
207-
body.add((JInvocation) restCall);
211+
insertRestTryCatchBlock(body, restCall, methodHolder, methodReturnVoid);
208212
} else if (!methodReturnVoid) {
209-
restCall = addResultCallMethod(restCall, methodHolder);
210-
body._return(restCall);
213+
insertRestTryCatchBlock(body, addResultCallMethod(restCall, methodHolder), methodHolder, methodReturnVoid);
211214
}
212215
}
213216

217+
private void insertRestTryCatchBlock(JBlock body, JExpression restCall, MethodProcessorHolder methodHolder, boolean methodReturnVoid) {
218+
RestImplementationHolder holder = restImplementationsHolder.getEnclosingHolder(methodHolder.getElement());
219+
220+
JTryBlock tryBlock = body._try();
221+
222+
if (methodReturnVoid) {
223+
tryBlock.body().add((JInvocation) restCall);
224+
} else {
225+
tryBlock.body()._return(restCall);
226+
}
227+
228+
JCatchBlock jcatch = tryBlock._catch(methodHolder.getHolder().classes().REST_CLIENT_EXCEPTION);
229+
230+
JBlock catchBlock = jcatch.body();
231+
JConditional con = catchBlock._if(JOp.ne(holder.restErrorHandlerField, JExpr._null()));
232+
JVar excParam = jcatch.param("e");
233+
234+
JBlock thenBlock = con._then();
235+
236+
thenBlock.add(holder.restErrorHandlerField.invoke("onRestClientExceptionThrown").arg(excParam));
237+
238+
if (!methodReturnVoid) {
239+
thenBlock._return(JExpr._null());
240+
}
241+
242+
con._else()._throw(excParam);
243+
}
244+
214245
private JVar generateHashMapVar(RestImplementationHolder holder, MethodProcessorHolder methodHolder) {
215246
ExecutableElement element = (ExecutableElement) methodHolder.getElement();
216247
JCodeModel codeModel = methodHolder.getCodeModel();

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/rest/RestImplementationHolder.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ public class RestImplementationHolder {
3232

3333
public JFieldVar authenticationField;
3434

35+
public JFieldVar restErrorHandlerField;
36+
3537
}

0 commit comments

Comments
 (0)