Search in sources :

Example 31 with VariableElement

use of javax.lang.model.element.VariableElement in project androidannotations by androidannotations.

the class InjectHelper method processMethod.

private void processMethod(Element element, T holder) {
    ExecutableElement executableElement = (ExecutableElement) element;
    VariableElement param = executableElement.getParameters().get(0);
    String methodName = executableElement.getSimpleName().toString();
    JBlock block = createBlock(holder, true);
    AbstractJClass type = codeModelHelper.typeMirrorToJClass(param.asType());
    JVar fieldRef = block.decl(type, param.getSimpleName().toString(), getDefault(param.asType()));
    handler.assignValue(block, fieldRef, holder, element, param);
    block.add(JExpr.invoke(methodName).arg(fieldRef));
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) VariableElement(javax.lang.model.element.VariableElement) JVar(com.helger.jcodemodel.JVar)

Example 32 with VariableElement

use of javax.lang.model.element.VariableElement in project androidannotations by androidannotations.

the class InjectHelper method validate.

public void validate(Class<? extends Annotation> expectedAnnotation, Element element, ElementValidation valid) {
    Element enclosingElement = element.getEnclosingElement();
    if (element instanceof VariableElement && enclosingElement instanceof ExecutableElement) {
        validatorHelper.param.annotatedWith(expectedAnnotation).multiple().validate((ExecutableElement) enclosingElement, valid);
        validatorHelper.doesNotHaveAnyOfSupportedAnnotations(enclosingElement, valid);
        handler.validateEnclosingElement(enclosingElement, valid);
    } else if (element instanceof ExecutableElement) {
        handler.validateEnclosingElement(element, valid);
        validatorHelper.param.anyType().validate((ExecutableElement) element, valid);
        List<? extends VariableElement> parameters = ((ExecutableElement) element).getParameters();
        for (VariableElement param : parameters) {
            validatorHelper.doesNotHaveAnyOfSupportedAnnotations(param, valid);
        }
    } else {
        handler.validateEnclosingElement(element, valid);
    }
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) List(java.util.List) VariableElement(javax.lang.model.element.VariableElement)

Example 33 with VariableElement

use of javax.lang.model.element.VariableElement in project androidannotations by androidannotations.

the class APTCodeModelHelper method implementMethod.

public JMethod implementMethod(GeneratedClassHolder holder, List<ExecutableElement> methods, String methodName, String returnType, boolean finalParams, String... parameterTypes) {
    // First get the ExecutableElement method object from the util function.
    ExecutableElement method = getMethod(methods, methodName, returnType, parameterTypes);
    JMethod jmethod = null;
    if (method != null) {
        // Get the return type or VOID if none.
        AbstractJType jcReturnType = returnType.equals(TypeKind.VOID.toString()) ? environment.getCodeModel().VOID : environment.getJClass(returnType);
        // Create the implementation and annotate it with the Override
        // annotation.
        jmethod = holder.getGeneratedClass().method(JMod.PUBLIC, jcReturnType, method.getSimpleName().toString());
        jmethod.annotate(Override.class);
        // Create the parameters.
        int paramMods = finalParams ? JMod.FINAL : JMod.NONE;
        for (int i = 0; i < method.getParameters().size(); i++) {
            VariableElement param = method.getParameters().get(i);
            jmethod.param(paramMods, environment.getJClass(parameterTypes[i]), param.getSimpleName().toString());
        }
    }
    return jmethod;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) AbstractJType(com.helger.jcodemodel.AbstractJType) VariableElement(javax.lang.model.element.VariableElement) JMethod(com.helger.jcodemodel.JMethod)

Example 34 with VariableElement

use of javax.lang.model.element.VariableElement in project androidannotations by androidannotations.

the class ValidatorHelper method allowedType.

public void allowedType(Element element, List<String> allowedTypes, ElementValidation valid) {
    String qualifiedName;
    Element enclosingElement = element.getEnclosingElement();
    if (element instanceof VariableElement && enclosingElement instanceof ExecutableElement) {
        qualifiedName = element.asType().toString();
    } else if (element instanceof ExecutableElement) {
        element = ((ExecutableElement) element).getParameters().get(0);
        qualifiedName = element.asType().toString();
    } else {
        qualifiedName = element.asType().toString();
    }
    if (!allowedTypes.contains(qualifiedName)) {
        valid.addError("%s can only be used on a field which is a " + allowedTypes.toString() + ", not " + qualifiedName);
    }
}
Also used : VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement)

Example 35 with VariableElement

use of javax.lang.model.element.VariableElement in project androidannotations by androidannotations.

the class RestMethodHandler method addMethodParams.

protected SortedMap<String, JVar> addMethodParams(ExecutableElement executableElement, RestHolder restHolder, JMethod method) {
    List<? extends VariableElement> params = executableElement.getParameters();
    SortedMap<String, JVar> methodParams = new TreeMap<>();
    for (VariableElement parameter : params) {
        String paramName = parameter.getSimpleName().toString();
        String paramType = parameter.asType().toString();
        JVar param;
        if (parameter.asType().getKind().isPrimitive()) {
            param = method.param(getCodeModel().parseType(paramType), paramName);
        } else {
            AbstractJClass parameterClass = codeModelHelper.typeMirrorToJClass(parameter.asType());
            param = method.param(parameterClass, paramName);
        }
        methodParams.put(paramName, param);
    }
    return methodParams;
}
Also used : AbstractJClass(com.helger.jcodemodel.AbstractJClass) VariableElement(javax.lang.model.element.VariableElement) TreeMap(java.util.TreeMap) JVar(com.helger.jcodemodel.JVar)

Aggregations

VariableElement (javax.lang.model.element.VariableElement)248 ExecutableElement (javax.lang.model.element.ExecutableElement)75 TypeElement (javax.lang.model.element.TypeElement)75 TypeMirror (javax.lang.model.type.TypeMirror)72 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)35 Element (javax.lang.model.element.Element)35 Expression (com.google.devtools.j2objc.ast.Expression)30 SimpleName (com.google.devtools.j2objc.ast.SimpleName)28 JVar (com.helger.jcodemodel.JVar)25 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)23 ArrayList (java.util.ArrayList)23 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)16 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)16 JInvocation (com.helger.jcodemodel.JInvocation)16 DeclaredType (javax.lang.model.type.DeclaredType)16 JBlock (com.helger.jcodemodel.JBlock)15 AbstractJClass (com.helger.jcodemodel.AbstractJClass)14 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)13 Elements (javax.lang.model.util.Elements)13 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)12