use of com.squareup.javapoet.MethodSpec in project arez by arez.
the class ComponentDescriptor method buildHashcodeMethod.
@Nonnull
private MethodSpec buildHashcodeMethod() throws ArezProcessorException {
final String idMethod = getIdMethodName();
final MethodSpec.Builder method = MethodSpec.methodBuilder("hashCode").addModifiers(Modifier.PUBLIC, Modifier.FINAL).addAnnotation(Override.class).returns(TypeName.INT);
final TypeKind kind = null != _componentId ? _componentId.getReturnType().getKind() : TypeKind.LONG;
if (_requireEquals) {
if (kind == TypeKind.DECLARED || kind == TypeKind.TYPEVAR) {
method.addStatement("return null != $N() ? $N().hashCode() : $T.identityHashCode( this )", idMethod, idMethod, System.class);
} else if (kind == TypeKind.BYTE) {
method.addStatement("return $T.hashCode( $N() )", Byte.class, idMethod);
} else if (kind == TypeKind.CHAR) {
method.addStatement("return $T.hashCode( $N() )", Character.class, idMethod);
} else if (kind == TypeKind.SHORT) {
method.addStatement("return $T.hashCode( $N() )", Short.class, idMethod);
} else if (kind == TypeKind.INT) {
method.addStatement("return $T.hashCode( $N() )", Integer.class, idMethod);
} else if (kind == TypeKind.LONG) {
method.addStatement("return $T.hashCode( $N() )", Long.class, idMethod);
} else if (kind == TypeKind.FLOAT) {
method.addStatement("return $T.hashCode( $N() )", Float.class, idMethod);
} else if (kind == TypeKind.DOUBLE) {
method.addStatement("return $T.hashCode( $N() )", Double.class, idMethod);
} else {
// So very unlikely but will cover it for completeness
assert kind == TypeKind.BOOLEAN;
method.addStatement("return $T.hashCode( $N() )", Boolean.class, idMethod);
}
} else {
final CodeBlock.Builder guardBlock = CodeBlock.builder();
guardBlock.beginControlFlow("if ( $T.areNativeComponentsEnabled() )", GeneratorUtil.AREZ_CLASSNAME);
if (kind == TypeKind.DECLARED || kind == TypeKind.TYPEVAR) {
guardBlock.addStatement("return null != $N() ? $N().hashCode() : $T.identityHashCode( this )", idMethod, idMethod, System.class);
} else if (kind == TypeKind.BYTE) {
guardBlock.addStatement("return $T.hashCode( $N() )", Byte.class, idMethod);
} else if (kind == TypeKind.CHAR) {
guardBlock.addStatement("return $T.hashCode( $N() )", Character.class, idMethod);
} else if (kind == TypeKind.SHORT) {
guardBlock.addStatement("return $T.hashCode( $N() )", Short.class, idMethod);
} else if (kind == TypeKind.INT) {
guardBlock.addStatement("return $T.hashCode( $N() )", Integer.class, idMethod);
} else if (kind == TypeKind.LONG) {
guardBlock.addStatement("return $T.hashCode( $N() )", Long.class, idMethod);
} else if (kind == TypeKind.FLOAT) {
guardBlock.addStatement("return $T.hashCode( $N() )", Float.class, idMethod);
} else if (kind == TypeKind.DOUBLE) {
guardBlock.addStatement("return $T.hashCode( $N() )", Double.class, idMethod);
} else {
// So very unlikely but will cover it for completeness
assert kind == TypeKind.BOOLEAN;
guardBlock.addStatement("return $T.hashCode( $N() )", Boolean.class, idMethod);
}
guardBlock.nextControlFlow("else");
guardBlock.addStatement("return super.hashCode()");
guardBlock.endControlFlow();
method.addCode(guardBlock.build());
}
return method.build();
}
use of com.squareup.javapoet.MethodSpec in project arez by arez.
the class ComponentDescriptor method buildGetByIdMethod.
@Nonnull
private MethodSpec buildGetByIdMethod() {
final TypeName entityType = TypeName.get(getElement().asType());
final MethodSpec.Builder method = MethodSpec.methodBuilder("getBy" + getIdName()).addModifiers(Modifier.FINAL).addAnnotation(GeneratorUtil.NONNULL_CLASSNAME).addParameter(ParameterSpec.builder(getIdType(), "id", Modifier.FINAL).build()).returns(entityType).addStatement("return getByArezId( id )");
ProcessorUtil.copyAccessModifiers(getElement(), method);
return method.build();
}
use of com.squareup.javapoet.MethodSpec 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.MethodSpec 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.MethodSpec 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