Search in sources :

Example 31 with TypeName

use of com.squareup.javapoet.TypeName in project arez by arez.

the class ComputedDescriptor method buildFields.

void buildFields(@Nonnull final TypeSpec.Builder builder) {
    assert null != _computed;
    assert null != _computedType;
    final TypeName parameterType = _computed.getTypeParameters().isEmpty() ? TypeName.get(_computedType.getReturnType()).box() : WildcardTypeName.subtypeOf(TypeName.OBJECT);
    final ParameterizedTypeName typeName = ParameterizedTypeName.get(GeneratorUtil.COMPUTED_VALUE_CLASSNAME, parameterType);
    final FieldSpec.Builder field = FieldSpec.builder(typeName, getFieldName(), Modifier.FINAL, Modifier.PRIVATE).addAnnotation(GeneratorUtil.NONNULL_CLASSNAME);
    builder.addField(field.build());
}
Also used : WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) FieldSpec(com.squareup.javapoet.FieldSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 32 with TypeName

use of com.squareup.javapoet.TypeName in project arez by arez.

the class ComputedDescriptor method buildComputed.

/**
 * Generate the wrapper around Computed method.
 */
@Nonnull
private MethodSpec buildComputed() throws ArezProcessorException {
    assert null != _computed;
    assert null != _computedType;
    final MethodSpec.Builder builder = MethodSpec.methodBuilder(_computed.getSimpleName().toString());
    ProcessorUtil.copyAccessModifiers(_computed, builder);
    ProcessorUtil.copyExceptions(_computedType, builder);
    ProcessorUtil.copyTypeParameters(_computedType, builder);
    ProcessorUtil.copyDocumentedAnnotations(_computed, builder);
    builder.addAnnotation(Override.class);
    final TypeName returnType = TypeName.get(_computedType.getReturnType());
    builder.returns(returnType);
    GeneratorUtil.generateNotDisposedInvariant(_componentDescriptor, builder);
    if (_computed.getTypeParameters().isEmpty()) {
        builder.addStatement("return this.$N.get()", getFieldName());
    } else {
        builder.addStatement("return ($T) this.$N.get()", returnType.box(), getFieldName());
    }
    return builder.build();
}
Also used : WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) Nonnull(javax.annotation.Nonnull)

Example 33 with TypeName

use of com.squareup.javapoet.TypeName in project arez by arez.

the class ComputedDescriptor method validate.

void validate() throws ArezProcessorException {
    if (null == _computed) {
        if (null != _onActivate) {
            throw new ArezProcessorException("@OnActivate exists but there is no corresponding @Computed", _onActivate);
        } else if (null != _onDeactivate) {
            throw new ArezProcessorException("@OnDeactivate exists but there is no corresponding @Computed", _onDeactivate);
        } else if (null != _onDispose) {
            throw new ArezProcessorException("@OnDispose exists but there is no corresponding @Computed", _onDispose);
        } else if (null != _refMethod) {
            throw new ArezProcessorException("@ComputedValueRef exists but there is no corresponding @Computed", _refMethod);
        } else {
            final ExecutableElement onStale = _onStale;
            assert null != onStale;
            throw new ArezProcessorException("@OnStale exists but there is no corresponding @Computed", onStale);
        }
    }
    if (null != _refMethod) {
        final TypeName typeName = TypeName.get(_refMethod.getReturnType());
        if (typeName instanceof ParameterizedTypeName) {
            final ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName;
            final TypeName expectedType = parameterizedTypeName.typeArguments.get(0);
            final TypeName actual = TypeName.get(_computed.getReturnType());
            if (!actual.box().toString().equals(expectedType.toString())) {
                throw new ArezProcessorException("@ComputedValueRef target has a type parameter of " + expectedType + " but @Computed method returns type of " + actual, _refMethod);
            }
        }
    }
}
Also used : WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) ExecutableElement(javax.lang.model.element.ExecutableElement) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 34 with TypeName

use of com.squareup.javapoet.TypeName in project arez by arez.

the class MemoizeDescriptor method buildMemoize.

@Nonnull
private MethodSpec buildMemoize() throws ArezProcessorException {
    assert null != _memoize;
    assert null != _memoizeType;
    final MethodSpec.Builder builder = MethodSpec.methodBuilder(_memoize.getSimpleName().toString());
    ProcessorUtil.copyAccessModifiers(_memoize, builder);
    ProcessorUtil.copyExceptions(_memoizeType, builder);
    ProcessorUtil.copyTypeParameters(_memoizeType, builder);
    ProcessorUtil.copyDocumentedAnnotations(_memoize, builder);
    builder.addAnnotation(Override.class);
    final TypeName returnType = TypeName.get(_memoizeType.getReturnType());
    builder.returns(returnType);
    final boolean hasTypeParameters = !_memoize.getTypeParameters().isEmpty();
    if (hasTypeParameters) {
        builder.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unchecked").build());
    }
    {
        final List<? extends VariableElement> parameters = _memoize.getParameters();
        final int paramCount = parameters.size();
        for (int i = 0; i < paramCount; i++) {
            final VariableElement element = parameters.get(i);
            final TypeName parameterType = TypeName.get(_memoizeType.getParameterTypes().get(i));
            final ParameterSpec.Builder param = ParameterSpec.builder(parameterType, element.getSimpleName().toString(), Modifier.FINAL);
            ProcessorUtil.copyDocumentedAnnotations(element, param);
            builder.addParameter(param.build());
        }
    }
    GeneratorUtil.generateNotDisposedInvariant(_componentDescriptor, builder);
    final StringBuilder sb = new StringBuilder();
    final ArrayList<Object> parameters = new ArrayList<>();
    sb.append("return ");
    if (hasTypeParameters) {
        sb.append("($T) ");
        parameters.add(returnType.box());
    }
    sb.append("this.$N.get( ");
    parameters.add(getFieldName());
    boolean first = true;
    for (final VariableElement element : _memoize.getParameters()) {
        if (!first) {
            sb.append(", ");
        }
        first = false;
        sb.append("$N");
        parameters.add(element.getSimpleName().toString());
    }
    sb.append(" )");
    builder.addStatement(sb.toString(), parameters.toArray());
    return builder.build();
}
Also used : WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) VariableElement(javax.lang.model.element.VariableElement) Nonnull(javax.annotation.Nonnull)

Example 35 with TypeName

use of com.squareup.javapoet.TypeName in project arez by arez.

the class ObservableDescriptor method buildObservableSetter.

/**
 * Generate the setter that reports that ensures that the access is reported as Observable.
 */
@Nonnull
private MethodSpec buildObservableSetter() throws ArezProcessorException {
    assert null != _setter;
    assert null != _setterType;
    assert null != _getter;
    final MethodSpec.Builder builder = MethodSpec.methodBuilder(_setter.getSimpleName().toString());
    ProcessorUtil.copyAccessModifiers(_setter, builder);
    ProcessorUtil.copyExceptions(_setterType, builder);
    ProcessorUtil.copyTypeParameters(_setterType, builder);
    ProcessorUtil.copyDocumentedAnnotations(_setter, builder);
    builder.addAnnotation(Override.class);
    // actually changed
    if (null == _setter.getAnnotation(Deprecated.class) && null != _getter.getAnnotation(Deprecated.class)) {
        builder.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "deprecation").build());
    }
    final TypeMirror parameterType = _setterType.getParameterTypes().get(0);
    final VariableElement element = _setter.getParameters().get(0);
    final String paramName = element.getSimpleName().toString();
    final TypeName type = TypeName.get(parameterType);
    final ParameterSpec.Builder param = ParameterSpec.builder(type, paramName, Modifier.FINAL);
    ProcessorUtil.copyDocumentedAnnotations(element, param);
    builder.addParameter(param.build());
    GeneratorUtil.generateNotDisposedInvariant(_componentDescriptor, builder);
    final CodeBlock.Builder codeBlock = CodeBlock.builder();
    final boolean abstractObservables = getGetter().getModifiers().contains(Modifier.ABSTRACT);
    if (type.isPrimitive()) {
        if (abstractObservables) {
            codeBlock.beginControlFlow("if ( $N != this.$N )", paramName, getDataFieldName());
        } else {
            codeBlock.beginControlFlow("if ( $N != super.$N() )", paramName, _getter.getSimpleName());
        }
    } else {
        if (abstractObservables) {
            codeBlock.beginControlFlow("if ( !$T.equals( $N, this.$N ) )", Objects.class, paramName, getDataFieldName());
        } else {
            codeBlock.beginControlFlow("if ( !$T.equals( $N, super.$N() ) )", Objects.class, paramName, _getter.getSimpleName());
        }
    }
    codeBlock.addStatement("this.$N.preReportChanged()", getFieldName());
    if (abstractObservables) {
        codeBlock.addStatement("this.$N = $N", getDataFieldName(), paramName);
    } else {
        codeBlock.addStatement("super.$N($N)", _setter.getSimpleName(), paramName);
    }
    codeBlock.addStatement("this.$N.reportChanged()", getFieldName());
    codeBlock.endControlFlow();
    builder.addCode(codeBlock.build());
    return builder.build();
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) TypeMirror(javax.lang.model.type.TypeMirror) ParameterSpec(com.squareup.javapoet.ParameterSpec) CodeBlock(com.squareup.javapoet.CodeBlock) VariableElement(javax.lang.model.element.VariableElement) Nonnull(javax.annotation.Nonnull)

Aggregations

TypeName (com.squareup.javapoet.TypeName)296 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)232 MethodSpec (com.squareup.javapoet.MethodSpec)109 ClassName (com.squareup.javapoet.ClassName)82 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)78 TypeSpec (com.squareup.javapoet.TypeSpec)55 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)50 TypeElement (javax.lang.model.element.TypeElement)46 ArrayList (java.util.ArrayList)45 TypeMirror (javax.lang.model.type.TypeMirror)41 ExecutableElement (javax.lang.model.element.ExecutableElement)36 ParameterSpec (com.squareup.javapoet.ParameterSpec)26 Builder (com.squareup.javapoet.TypeSpec.Builder)22 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)21 CodeBlock (com.squareup.javapoet.CodeBlock)20 VariableElement (javax.lang.model.element.VariableElement)20 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)19 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)18 List (java.util.List)18 Map (java.util.Map)17