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 @@ -961,6 +961,33 @@ public void notAlreadyValidated(Element element, AnnotationElements validatedEle
}
}

public void hasEmptyOrContextConstructor(Element element, IsValid valid) {
List<ExecutableElement> constructors = ElementFilter.constructorsIn(element.getEnclosedElements());

if (constructors.size() == 1) {
ExecutableElement constructor = constructors.get(0);

if (!annotationHelper.isPrivate(constructor)) {
if (constructor.getParameters().size() > 1) {
annotationHelper.printAnnotationError(element, "%s annotated element should have a constructor with one parameter max, of type " + CanonicalNameConstants.CONTEXT);
valid.invalidate();
} else if (constructor.getParameters().size() == 1) {
VariableElement parameter = constructor.getParameters().get(0);
if (!parameter.asType().toString().equals(CanonicalNameConstants.CONTEXT)) {
annotationHelper.printAnnotationError(element, "%s annotated element should have a constructor with one parameter max, of type " + CanonicalNameConstants.CONTEXT);
valid.invalidate();
}
}
} else {
annotationHelper.printAnnotationError(element, "%s annotated element should not have a private constructor");
valid.invalidate();
}
} else {
annotationHelper.printAnnotationError(element, "%s annotated element should have only one constructor");
valid.invalidate();
}
}

public void hasEmptyConstructor(Element element, IsValid valid) {

List<ExecutableElement> constructors = ElementFilter.constructorsIn(element.getEnclosedElements());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
import static com.sun.codemodel.JMod.STATIC;

import java.lang.annotation.Annotation;
import java.util.List;

import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;

import com.googlecode.androidannotations.annotations.EBean;
import com.googlecode.androidannotations.api.Scope;
Expand Down Expand Up @@ -125,6 +128,14 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo

JBlock constructorBody = constructor.body();

List<ExecutableElement> constructors = ElementFilter.constructorsIn(element.getEnclosedElements());

ExecutableElement superConstructor = constructors.get(0);

if (superConstructor.getParameters().size() == 1) {
constructorBody.invoke("super").arg(constructorContextParam);
}

constructorBody.assign(contextField, constructorContextParam);

constructorBody.invoke(holder.init);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public boolean validate(Element element, AnnotationElements validatedElements) {

validatorHelper.isNotPrivate(element, valid);

validatorHelper.hasEmptyConstructor(element, valid);
validatorHelper.hasEmptyOrContextConstructor(element, valid);

return valid.isValid();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.googlecode.androidannotations.test15.ebean;

import android.content.Context;
import android.widget.ArrayAdapter;

import com.googlecode.androidannotations.annotations.EBean;

@EBean
public class SomeArrayAdapter extends ArrayAdapter<String> {

public SomeArrayAdapter(Context context) {
super(context, android.R.layout.simple_list_item_1);
}

}