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());
}
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();
}
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);
}
}
}
}
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();
}
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();
}
Aggregations