Search in sources :

Example 11 with ExecutableType

use of javax.lang.model.type.ExecutableType in project arez by arez.

the class ComponentDescriptor method buildRepository.

/**
 * Build the enhanced class for the component.
 */
@Nonnull
TypeSpec buildRepository(@Nonnull final Types typeUtils) throws ArezProcessorException {
    assert null != _repositoryExtensions;
    final TypeElement element = getElement();
    final ClassName arezType = ClassName.get(getPackageName(), getArezClassName());
    final TypeSpec.Builder builder = TypeSpec.classBuilder(getRepositoryName()).addTypeVariables(ProcessorUtil.getTypeArgumentsAsNames(asDeclaredType()));
    builder.addAnnotation(AnnotationSpec.builder(Generated.class).addMember("value", "$S", ArezProcessor.class.getName()).build());
    final boolean addSingletonAnnotation = "ENABLE".equals(_repositoryInjectConfig) || ("AUTODETECT".equals(_repositoryInjectConfig) && _injectClassesPresent);
    final AnnotationSpec.Builder arezComponent = AnnotationSpec.builder(ClassName.bestGuess(Constants.COMPONENT_ANNOTATION_CLASSNAME));
    if (!addSingletonAnnotation) {
        arezComponent.addMember("nameIncludesId", "false");
    }
    if (!"AUTODETECT".equals(_repositoryInjectConfig)) {
        arezComponent.addMember("inject", "$T.$N", GeneratorUtil.INJECTIBLE_CLASSNAME, _repositoryInjectConfig);
    }
    if (!"AUTODETECT".equals(_repositoryDaggerConfig)) {
        arezComponent.addMember("dagger", "$T.$N", GeneratorUtil.INJECTIBLE_CLASSNAME, _repositoryDaggerConfig);
    }
    builder.addAnnotation(arezComponent.build());
    if (addSingletonAnnotation) {
        builder.addAnnotation(GeneratorUtil.SINGLETON_CLASSNAME);
    }
    builder.superclass(ParameterizedTypeName.get(GeneratorUtil.ABSTRACT_REPOSITORY_CLASSNAME, getIdType().box(), ClassName.get(element), ClassName.get(getPackageName(), getRepositoryName())));
    _repositoryExtensions.forEach(e -> builder.addSuperinterface(TypeName.get(e.asType())));
    ProcessorUtil.copyAccessModifiers(element, builder);
    builder.addModifiers(Modifier.ABSTRACT);
    // Add the default access, no-args constructor
    builder.addMethod(MethodSpec.constructorBuilder().build());
    // Add the factory method
    builder.addMethod(buildFactoryMethod());
    for (final ExecutableElement constructor : ProcessorUtil.getConstructors(element)) {
        final ExecutableType methodType = (ExecutableType) typeUtils.asMemberOf((DeclaredType) _element.asType(), constructor);
        builder.addMethod(buildRepositoryCreate(constructor, methodType, arezType));
    }
    if (null != _componentId) {
        builder.addMethod(buildFindByIdMethod());
        builder.addMethod(buildGetByIdMethod());
    }
    return builder.build();
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) Generated(javax.annotation.Generated) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ClassName(com.squareup.javapoet.ClassName) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) TypeSpec(com.squareup.javapoet.TypeSpec) DeclaredType(javax.lang.model.type.DeclaredType) Nonnull(javax.annotation.Nonnull)

Example 12 with ExecutableType

use of javax.lang.model.type.ExecutableType in project arez by arez.

the class ComponentDescriptor method buildConstructors.

/**
 * Build all constructors as they appear on the ArezComponent class.
 * Arez Observable fields are populated as required and parameters are passed up to superclass.
 */
private void buildConstructors(@Nonnull final TypeSpec.Builder builder, @Nonnull final Types typeUtils) {
    final boolean requiresDeprecatedSuppress = hasDeprecatedElements();
    for (final ExecutableElement constructor : ProcessorUtil.getConstructors(getElement())) {
        final ExecutableType methodType = (ExecutableType) typeUtils.asMemberOf((DeclaredType) _element.asType(), constructor);
        builder.addMethod(buildConstructor(constructor, methodType, requiresDeprecatedSuppress));
    }
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) ExecutableElement(javax.lang.model.element.ExecutableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 13 with ExecutableType

use of javax.lang.model.type.ExecutableType in project arez by arez.

the class ProcessorUtil method processMethod.

private static void processMethod(@Nonnull final Elements elementUtils, @Nonnull final Types typeUtils, @Nonnull final TypeElement typeElement, @Nonnull final Map<String, ArrayList<ExecutableElement>> methods, @Nonnull final ExecutableElement method) {
    final ExecutableType methodType = (ExecutableType) typeUtils.asMemberOf((DeclaredType) typeElement.asType(), method);
    final String key = method.getSimpleName().toString();
    final ArrayList<ExecutableElement> elements = methods.computeIfAbsent(key, k -> new ArrayList<>());
    boolean found = false;
    final int size = elements.size();
    for (int i = 0; i < size; i++) {
        final ExecutableElement executableElement = elements.get(i);
        if (method.equals(executableElement)) {
            found = true;
            break;
        } else if (isSubsignature(typeUtils, typeElement, methodType, executableElement)) {
            if (!isAbstractInterfaceMethod(method)) {
                elements.set(i, method);
            }
            found = true;
            break;
        } else if (elementUtils.overrides(method, executableElement, typeElement)) {
            elements.set(i, method);
            found = true;
            break;
        }
    }
    if (!found) {
        elements.add(method);
    }
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) ExecutableElement(javax.lang.model.element.ExecutableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 14 with ExecutableType

use of javax.lang.model.type.ExecutableType in project arez by arez.

the class ProcessorUtil method isSubsignature.

private static boolean isSubsignature(@Nonnull final Types typeUtils, @Nonnull final TypeElement typeElement, @Nonnull final ExecutableType methodType, @Nonnull final ExecutableElement candidate) {
    final ExecutableType candidateType = (ExecutableType) typeUtils.asMemberOf((DeclaredType) typeElement.asType(), candidate);
    final boolean isEqual = methodType.equals(candidateType);
    final boolean isSubsignature = typeUtils.isSubsignature(methodType, candidateType);
    return isSubsignature || isEqual;
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) DeclaredType(javax.lang.model.type.DeclaredType)

Example 15 with ExecutableType

use of javax.lang.model.type.ExecutableType in project ceylon by eclipse.

the class P method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (!ran) {
        ran = true;
        ExecutableElement m = getFirstMethodIn("C");
        System.err.println("method: " + m);
        TypeMirror type = (DeclaredType) m.getParameters().get(0).asType();
        System.err.println("parameters[0]: " + type);
        if (!isParameterized(type))
            throw new AssertionError(type);
        type = ((ExecutableType) m.asType()).getParameterTypes().get(0);
        System.err.println("parameterTypes[0]: " + type);
        if (!isParameterized(type))
            throw new AssertionError(type);
        System.err.println();
    }
    return true;
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

ExecutableType (javax.lang.model.type.ExecutableType)68 ExecutableElement (javax.lang.model.element.ExecutableElement)53 DeclaredType (javax.lang.model.type.DeclaredType)41 TypeElement (javax.lang.model.element.TypeElement)39 TypeMirror (javax.lang.model.type.TypeMirror)39 Element (javax.lang.model.element.Element)29 VariableElement (javax.lang.model.element.VariableElement)27 TypeSpec (com.squareup.javapoet.TypeSpec)13 Nonnull (javax.annotation.Nonnull)13 PackageElement (javax.lang.model.element.PackageElement)13 HashMap (java.util.HashMap)12 Map (java.util.Map)12 MethodSpec (com.squareup.javapoet.MethodSpec)11 AnnotationMirror (javax.lang.model.element.AnnotationMirror)11 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 HashSet (java.util.HashSet)10 Nullable (javax.annotation.Nullable)10 JavaFile (com.squareup.javapoet.JavaFile)9 List (java.util.List)9