Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 @@ -109,7 +109,7 @@ private void createBuilderInjectionMethod(Element element, EFragmentHolder holde
TypeMirror type = codeModelHelper.getActualType(element, holder);
JClass paramClass = codeModelHelper.typeMirrorToJClass(type, holder);

JMethod method = builderClass.method(PUBLIC, builderClass, fieldName);
JMethod method = builderClass.method(PUBLIC, holder.narrow(builderClass), fieldName);
JVar arg = method.param(paramClass, fieldName);
method.body().invoke(builderArgsField, bundleHelper.getMethodNameToSave()).arg(argKeyStaticField).arg(arg);
method.body()._return(_this());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@
import org.androidannotations.annotations.rest.Rest;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.handler.BaseAnnotationHandler;
import org.androidannotations.helper.APTCodeModelHelper;
import org.androidannotations.holder.EComponentHolder;
import org.androidannotations.model.AnnotationElements;
import org.androidannotations.process.IsValid;

import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JFieldRef;

public class RestServiceHandler extends BaseAnnotationHandler<EComponentHolder> {

private APTCodeModelHelper codeModelHelper = new APTCodeModelHelper();

public RestServiceHandler(ProcessingEnvironment processingEnvironment) {
super(RestService.class, processingEnvironment);
}
Expand All @@ -52,14 +56,17 @@ public void process(Element element, EComponentHolder holder) {
String fieldName = element.getSimpleName().toString();

TypeMirror fieldTypeMirror = element.asType();
String interfaceName = fieldTypeMirror.toString();
TypeMirror erasedFieldTypeMirror = processingEnv.getTypeUtils().erasure(fieldTypeMirror);
String interfaceName = erasedFieldTypeMirror.toString();

String generatedClassName = interfaceName + classSuffix();

JClass clazz = codeModelHelper.narrowGeneratedClass(refClass(generatedClassName), fieldTypeMirror, holder);

JBlock methodBody = holder.getInitBody();

JFieldRef field = JExpr.ref(fieldName);

methodBody.assign(field, JExpr._new(refClass(generatedClassName)).arg(holder.getContextRef()));
methodBody.assign(field, JExpr._new(clazz).arg(holder.getContextRef()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,16 @@ public void generifyStaticHelper(GeneratedClassHolder holder, JMethod staticHelp
}
}

public JClass narrowGeneratedClass(JClass generatedClass, TypeMirror fromTypeArguments, GeneratedClassHolder holder) {
DeclaredType type = (DeclaredType) fromTypeArguments;

for (TypeMirror param : type.getTypeArguments()) {
JClass paramClass = typeMirrorToJClass(param, holder);
generatedClass = generatedClass.narrow(paramClass);
}
return generatedClass;
}

private JMethod findAlreadyGeneratedMethod(ExecutableElement executableElement, GeneratedClassHolder holder) {
JDefinedClass definedClass = holder.getGeneratedClass();
String methodName = executableElement.getSimpleName().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import static com.sun.codemodel.JMod.STATIC;
import static org.androidannotations.helper.ModelConstants.classSuffix;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
Expand All @@ -32,6 +35,7 @@
import com.sun.codemodel.JClass;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JTypeVar;

public abstract class BaseGeneratedClassHolder implements GeneratedClassHolder {

Expand Down Expand Up @@ -117,4 +121,15 @@ public JClass refClass(Class<?> clazz) {
public JDefinedClass definedClass(String fullyQualifiedClassName) {
return processHolder.definedClass(fullyQualifiedClassName);
}

public JClass narrow(JClass toNarrow) {
List<JClass> classes = new ArrayList<JClass>();
for (JTypeVar type : generatedClass.typeParams()) {
classes.add(codeModel().directClass(type.name()));
}
if (classes.isEmpty()) {
return toNarrow;
}
return toNarrow.narrow(classes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.androidannotations.process.ProcessHolder;

import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JVar;
Expand Down Expand Up @@ -83,7 +84,11 @@ public void invokeInitInConstructor() {

public void createFactoryMethod(boolean hasSingletonScope) {

JMethod factoryMethod = generatedClass.method(PUBLIC | STATIC, generatedClass, GET_INSTANCE_METHOD_NAME);
JClass narrowedGeneratedClass = codeModelHelper.narrowGeneratedClass(generatedClass, annotatedElement.asType(), this);

JMethod factoryMethod = generatedClass.method(PUBLIC | STATIC, narrowedGeneratedClass, GET_INSTANCE_METHOD_NAME);

codeModelHelper.generifyStaticHelper(this, factoryMethod, annotatedElement);

JVar factoryMethodContextParam = factoryMethod.param(classes().CONTEXT, "context");

Expand All @@ -100,13 +105,13 @@ public void createFactoryMethod(boolean hasSingletonScope) {
._if(instanceField.eq(_null())) //
._then();
JVar previousNotifier = viewNotifierHelper.replacePreviousNotifierWithNull(creationBlock);
creationBlock.assign(instanceField, _new(generatedClass).arg(factoryMethodContextParam.invoke("getApplicationContext")));
creationBlock.assign(instanceField, _new(narrowedGeneratedClass).arg(factoryMethodContextParam.invoke("getApplicationContext")));
creationBlock.invoke(instanceField, getInit());
viewNotifierHelper.resetPreviousNotifier(creationBlock, previousNotifier);

factoryMethodBody._return(instanceField);
} else {
factoryMethodBody._return(_new(generatedClass).arg(factoryMethodContextParam));
factoryMethodBody._return(_new(narrowedGeneratedClass).arg(factoryMethodContextParam));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
import static com.sun.codemodel.JMod.STATIC;
import static org.androidannotations.helper.ModelConstants.generationSuffix;

import java.util.ArrayList;
import java.util.List;

import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;

Expand Down Expand Up @@ -146,17 +143,6 @@ private void setFragmentBuilder() throws JClassAlreadyExistsException {
setFragmentBuilderCreate();
}

private JClass narrow(JClass toNarrow) {
List<JClass> classes = new ArrayList<>();
for (JTypeVar type : generatedClass.typeParams()) {
classes.add(codeModel().directClass(type.name()));
}
if (classes.isEmpty()) {
return toNarrow;
}
return toNarrow.narrow(classes);
}

private void setFragmentBuilderBuild() {
JMethod method = fragmentBuilderClass.method(PUBLIC, generatedClass._extends(), "build");
method.annotate(Override.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ private void createConstructorAndBuilder() {

JBlock body = copyConstructor.body();
JInvocation superCall = body.invoke("super");
JInvocation newInvocation = JExpr._new(generatedClass);
JClass narrowedGeneratedClass = narrow(generatedClass);

JInvocation newInvocation = JExpr._new(narrowedGeneratedClass);
for (VariableElement param : userConstructor.getParameters()) {
String paramName = param.getSimpleName().toString();
JClass paramType = codeModelHelper.typeMirrorToJClass(param.asType(), this);
Expand All @@ -97,7 +99,7 @@ private void createConstructorAndBuilder() {
newInvocation.arg(JExpr.ref(paramName));
}

JVar newCall = staticHelper.body().decl(generatedClass, "instance", newInvocation);
JVar newCall = staticHelper.body().decl(narrowedGeneratedClass, "instance", newInvocation);
staticHelper.body().invoke(newCall, getOnFinishInflate());
staticHelper.body()._return(newCall);
body.invoke(getInit());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public RestHolder(ProcessHolder processHolder, TypeElement annotatedElement) thr
protected void setExtends() {
String annotatedComponentQualifiedName = annotatedElement.getQualifiedName().toString();
JClass annotatedComponent = codeModel().directClass(annotatedComponentQualifiedName);
generatedClass._implements(annotatedComponent);
generatedClass._implements(narrow(annotatedComponent));
}

private void implementMethods() {
Expand Down Expand Up @@ -286,7 +286,8 @@ private void setAuthenticationField() {
}

public JFieldVar getRestErrorHandlerField() {
// restErrorHandlerField is created only if the method setRestErrorHandler is implemented
// restErrorHandlerField is created only if the method
// setRestErrorHandler is implemented
return restErrorHandlerField;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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;

import org.androidannotations.annotations.rest.Rest;
import org.springframework.http.converter.StringHttpMessageConverter;

@Rest(converters = { StringHttpMessageConverter.class })
public interface GenericClient<T> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.rest.RestService;

@EBean
public class GenericClientHolderBean {

@RestService
GenericClient<String> client;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 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;

import java.io.IOException;

import org.androidannotations.AndroidAnnotationProcessor;
import org.androidannotations.utils.AAProcessorTestHelper;
import org.junit.Before;
import org.junit.Test;

public class RestServiceTest extends AAProcessorTestHelper {

@Before
public void setUp() {
addManifestProcessorParameter(RestServiceTest.class);
addProcessor(AndroidAnnotationProcessor.class);
}

@Test
public void restServiceOnGenericField() throws IOException {
CompileResult result = compileFiles(GenericClient.class, GenericClientHolderBean.class);
assertCompilationSuccessful(result);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.