use of javax.lang.model.type.DeclaredType in project androidannotations by androidannotations.
the class APTCodeModelHelper method getMethods.
/**
* Gets all of the methods of the class and includes the methods of any
* implemented interfaces.
*
* @param typeElement
* @return full list of methods.
*/
public List<ExecutableElement> getMethods(TypeElement typeElement) {
List<? extends Element> enclosedElements = typeElement.getEnclosedElements();
List<ExecutableElement> methods = new ArrayList<>(ElementFilter.methodsIn(enclosedElements));
// through the validator.
for (TypeMirror iface : typeElement.getInterfaces()) {
DeclaredType dt = (DeclaredType) iface;
methods.addAll(ElementFilter.methodsIn(dt.asElement().getEnclosedElements()));
}
return methods;
}
use of javax.lang.model.type.DeclaredType in project androidannotations by androidannotations.
the class APTCodeModelHelper method overrideAnnotatedMethod.
public JMethod overrideAnnotatedMethod(ExecutableElement executableElement, GeneratedClassHolder holder) {
TypeMirror annotatedClass = holder.getAnnotatedElement().asType();
DeclaredType baseClass = (DeclaredType) executableElement.getEnclosingElement().asType();
Types typeUtils = environment.getProcessingEnvironment().getTypeUtils();
Map<String, TypeMirror> actualTypes = getActualTypes(typeUtils, baseClass, annotatedClass);
Map<String, List<AbstractJClass>> methodTypes = new LinkedHashMap<>();
for (TypeParameterElement typeParameter : executableElement.getTypeParameters()) {
List<? extends TypeMirror> bounds = typeParameter.getBounds();
List<AbstractJClass> addedBounds = typeBoundsToJClass(bounds, actualTypes);
methodTypes.put(typeParameter.toString(), addedBounds);
}
actualTypes.keySet().removeAll(methodTypes.keySet());
JMethod existingMethod = findAlreadyGeneratedMethod(executableElement, holder);
if (existingMethod != null) {
return existingMethod;
}
String methodName = executableElement.getSimpleName().toString();
AbstractJClass returnType = typeMirrorToJClass(executableElement.getReturnType(), actualTypes);
int modifier = elementVisibilityModifierToJMod(executableElement);
JMethod method = holder.getGeneratedClass().method(modifier, returnType, methodName);
copyNonAAAnnotations(method, executableElement.getAnnotationMirrors());
if (!hasAnnotation(method, Override.class)) {
method.annotate(Override.class);
}
for (Map.Entry<String, List<AbstractJClass>> typeDeclaration : methodTypes.entrySet()) {
List<AbstractJClass> bounds = typeDeclaration.getValue();
addTypeBounds(method, bounds, typeDeclaration.getKey());
}
int i = 0;
for (VariableElement parameter : executableElement.getParameters()) {
boolean varParam = i == executableElement.getParameters().size() - 1 && executableElement.isVarArgs();
addParamToMethod(method, parameter, JMod.FINAL, actualTypes, varParam);
i++;
}
for (TypeMirror superThrownType : executableElement.getThrownTypes()) {
AbstractJClass thrownType = typeMirrorToJClass(superThrownType, actualTypes);
method._throws(thrownType);
}
callSuperMethod(method, holder, method.body());
return method;
}
use of javax.lang.model.type.DeclaredType in project androidannotations by androidannotations.
the class APTCodeModelHelper method getActualTypes.
private Map<String, TypeMirror> getActualTypes(Types typeUtils, DeclaredType baseClass, TypeMirror annotatedClass) {
List<TypeMirror> superTypes = new ArrayList<>();
superTypes.add(annotatedClass);
while (!superTypes.isEmpty()) {
TypeMirror x = superTypes.remove(0);
if (typeUtils.isSameType(typeUtils.erasure(x), typeUtils.erasure(baseClass))) {
DeclaredType type = (DeclaredType) x;
Map<String, TypeMirror> actualTypes = new HashMap<>();
for (int i = 0; i < type.getTypeArguments().size(); i++) {
TypeMirror actualArg = type.getTypeArguments().get(i);
TypeMirror formalArg = baseClass.getTypeArguments().get(i);
if (!typeUtils.isSameType(actualArg, formalArg)) {
actualTypes.put(formalArg.toString(), actualArg);
}
}
return actualTypes;
}
superTypes.addAll(typeUtils.directSupertypes(x));
}
return Collections.emptyMap();
}
use of javax.lang.model.type.DeclaredType in project androidannotations by androidannotations.
the class APTCodeModelHelper method narrowGeneratedClass.
public AbstractJClass narrowGeneratedClass(AbstractJClass generatedClass, TypeMirror fromTypeArguments) {
DeclaredType type = (DeclaredType) fromTypeArguments;
for (TypeMirror param : type.getTypeArguments()) {
AbstractJClass paramClass = typeMirrorToJClass(param);
generatedClass = generatedClass.narrow(paramClass);
}
return generatedClass;
}
use of javax.lang.model.type.DeclaredType in project androidannotations by androidannotations.
the class AnnotationHelper method extractAnnotationClassArrayParameter.
public List<DeclaredType> extractAnnotationClassArrayParameter(Element element, String annotationName, String methodName) {
AnnotationMirror annotationMirror = findAnnotationMirror(element, annotationName);
Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = annotationMirror.getElementValues();
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : elementValues.entrySet()) {
/*
* "methodName" is unset when the default value is used
*/
if (methodName.equals(entry.getKey().getSimpleName().toString())) {
AnnotationValue annotationValue = entry.getValue();
@SuppressWarnings("unchecked") List<AnnotationValue> annotationClassArray = (List<AnnotationValue>) annotationValue.getValue();
List<DeclaredType> result = new ArrayList<>(annotationClassArray.size());
for (AnnotationValue annotationClassValue : annotationClassArray) {
result.add((DeclaredType) annotationClassValue.getValue());
}
return result;
}
}
return null;
}
Aggregations