Search in sources :

Example 31 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project storio by pushtorefresh.

the class StorIOContentResolverProcessor method processAnnotatedExecutables.

/**
     * Processes factory methods or constructors annotated with {@link StorIOContentResolverCreator}.
     *
     * @param roundEnvironment current processing environment
     * @param annotatedClasses map of classes annotated with {@link StorIOContentResolverType}
     */
@Override
protected void processAnnotatedExecutables(@NotNull RoundEnvironment roundEnvironment, @NotNull Map<TypeElement, StorIOContentResolverTypeMeta> annotatedClasses) {
    final Set<? extends Element> elementsAnnotatedWithStorIOContentResolverCreator = roundEnvironment.getElementsAnnotatedWith(StorIOContentResolverCreator.class);
    for (final Element annotatedElement : elementsAnnotatedWithStorIOContentResolverCreator) {
        final ExecutableElement annotatedExecutableElement = (ExecutableElement) annotatedElement;
        validateAnnotatedExecutable(annotatedExecutableElement);
        final StorIOContentResolverCreatorMeta storIOContentResolverCreatorMeta = new StorIOContentResolverCreatorMeta(annotatedExecutableElement.getEnclosingElement(), annotatedExecutableElement, annotatedExecutableElement.getAnnotation(StorIOContentResolverCreator.class));
        final StorIOContentResolverTypeMeta storIOContentResolverTypeMeta = annotatedClasses.get(storIOContentResolverCreatorMeta.enclosingElement);
        // If class already contains another creator -> throw exception.
        if (storIOContentResolverTypeMeta.creator == null) {
            storIOContentResolverTypeMeta.creator = annotatedExecutableElement;
        } else {
            throw new ProcessingException(annotatedExecutableElement, "Only one creator method or constructor is allowed: " + annotatedExecutableElement.getEnclosingElement().getSimpleName());
        }
    }
}
Also used : StorIOContentResolverTypeMeta(com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverTypeMeta) StorIOContentResolverCreatorMeta(com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverCreatorMeta) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) StorIOContentResolverCreator(com.pushtorefresh.storio.contentresolver.annotations.StorIOContentResolverCreator) ProcessingException(com.pushtorefresh.storio.common.annotations.processor.ProcessingException)

Example 32 with ExecutableElement

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

the class APTCodeModelHelper method getMethod.

private ExecutableElement getMethod(List<ExecutableElement> methods, String methodName, String returnType, String... parameterTypes) {
    for (ExecutableElement method : methods) {
        List<? extends VariableElement> parameters = method.getParameters();
        // Get the method return type or "VOID" if none.
        String methodReturnType = method.getReturnType().getKind() == TypeKind.VOID ? TypeKind.VOID.toString() : method.getReturnType().toString();
        if (parameters.size() == parameterTypes.length && methodReturnType.equals(returnType)) {
            if (methodName == null || method.getSimpleName().toString().equals(methodName)) {
                // At this point, method name, return type and number of
                // parameters are correct. Now we need to validate the
                // parameter types.
                boolean validMethod = true;
                for (int i = 0; i < parameters.size(); i++) {
                    VariableElement param = parameters.get(i);
                    if (!param.asType().toString().equals(parameterTypes[i])) {
                        // Parameter type does not match, this is not the
                        // correct method.
                        validMethod = false;
                        break;
                    }
                }
                if (validMethod) {
                    return method;
                }
            }
        }
    }
    return null;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement)

Example 33 with ExecutableElement

use of javax.lang.model.element.ExecutableElement 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 34 with ExecutableElement

use of javax.lang.model.element.ExecutableElement 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 35 with ExecutableElement

use of javax.lang.model.element.ExecutableElement 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)

Aggregations

ExecutableElement (javax.lang.model.element.ExecutableElement)345 TypeElement (javax.lang.model.element.TypeElement)158 TypeMirror (javax.lang.model.type.TypeMirror)97 VariableElement (javax.lang.model.element.VariableElement)85 Element (javax.lang.model.element.Element)72 Test (org.junit.Test)41 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)32 DeclaredType (javax.lang.model.type.DeclaredType)31 ArrayList (java.util.ArrayList)26 JBlock (com.helger.jcodemodel.JBlock)25 JInvocation (com.helger.jcodemodel.JInvocation)20 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)18 JVar (com.helger.jcodemodel.JVar)18 Map (java.util.Map)18 HashSet (java.util.HashSet)17 IJExpression (com.helger.jcodemodel.IJExpression)16 ElementValidation (org.androidannotations.ElementValidation)15 Block (com.google.devtools.j2objc.ast.Block)13 JMethod (com.helger.jcodemodel.JMethod)13 Elements (javax.lang.model.util.Elements)13