Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No boxing #621

Merged
merged 2 commits into from
Jan 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.acra.config.ACRAConfigurationException;
import org.acra.config.ClassValidator;
import org.acra.definition.Field;
import org.acra.definition.Type;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -58,7 +59,7 @@ class BuildMethodCreator {

void addValidation(Field field, ExecutableElement method) {
if (!field.hasDefault()) {
methodBuilder.beginControlFlow("if ($L == null)", field.getName())
methodBuilder.beginControlFlow("if ($L == $L)", field.getName(), getDefault(field.getType()))
.addStatement("throw new $T(\"$L has to be set\")", ACRAConfigurationException.class, field.getName())
.endControlFlow();
}
Expand All @@ -75,6 +76,29 @@ void addValidation(Field field, ExecutableElement method) {
}
}

private String getDefault(Type type) {
switch (type.getMirror().getKind()) {
case BOOLEAN:
return "false";
case BYTE:
return "0";
case SHORT:
return "0";
case INT:
return "0";
case LONG:
return "0L";
case CHAR:
return "\u0000";
case FLOAT:
return "0.0f";
case DOUBLE:
return "0.0d";
default:
return "null";
}
}

void addMethodCall(String delegate, String methodName) {
statements.add(CodeBlock.builder().addStatement("$L.$L()", delegate, methodName).build());
}
Expand All @@ -87,7 +111,7 @@ MethodSpec build() {
.endControlFlow();
}
methodBuilder.endControlFlow();
for (CodeBlock s : statements){
for (CodeBlock s : statements) {
methodBuilder.addCode(s);
}
methodBuilder.addStatement("return new $T(this)", config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.acra.definition.FieldGetter;
import org.acra.definition.FieldSetter;
import org.acra.definition.Method;
import org.acra.definition.Transformable;
import org.acra.definition.Transformer;
import org.acra.definition.Type;

Expand Down Expand Up @@ -86,7 +85,7 @@ class BuilderCreator {
.addJavadoc("@param $L class annotated with {@link $T}\n", PARAM_0, baseAnnotation.getName());
build = new BuildMethodCreator(utils.getOnlyMethod(configurationBuilder.getElement()), config);
final Field enabled = new Field(ENABLED, utils.getBooleanType(), Collections.emptyList(), null, null);
enabled.addTo(classBuilder);
enabled.addTo(classBuilder, utils);
methods.add(new FieldGetter(enabled));
methods.add(new FieldSetter(enabled, builder));
}
Expand All @@ -106,7 +105,7 @@ private void handleAnnotationMethods() {
.beginControlFlow("if ($L)", ENABLED);
for (ExecutableElement method : utils.getMethods(baseAnnotation.getElement())) {
final Field field = Field.from(method, utils);
field.addTo(classBuilder);
field.addTo(classBuilder, utils);
methods.add(new FieldGetter(field));
methods.add(new FieldSetter(field, builder));
build.addValidation(field, method);
Expand Down Expand Up @@ -138,7 +137,7 @@ private void handleBaseBuilderMethods() {
build.addMethodCall(FIELD_0, method.getSimpleName().toString());
} else if (method.getAnnotation(Transform.class) != null) {
final String transform = method.getAnnotation(Transform.class).methodName();
methods.stream().filter(m -> m instanceof Transformable).map(Transformable.class::cast).filter(m -> m.getName().equals(transform)).findAny()
methods.stream().filter(m -> m instanceof FieldGetter).map(FieldGetter.class::cast).filter(m -> m.getName().equals(transform)).findAny()
.ifPresent(m -> methods.set(methods.indexOf(m), Transformer.from(method, FIELD_0, m, utils)));
} else {
methods.add(DelegateMethod.from(method, FIELD_0, builder, utils));
Expand Down
22 changes: 13 additions & 9 deletions annotationprocessor/src/main/java/org/acra/definition/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.type.TypeKind;

/**
* The minimal Definition needed to create a getter
Expand Down Expand Up @@ -60,7 +61,7 @@ public String getName() {
return name;
}

Type getType() {
public Type getType() {
return type;
}

Expand All @@ -72,15 +73,18 @@ public boolean hasDefault() {
return defaultValue != null;
}

AnnotationValue getDefaultValue() {
return defaultValue;
}

public void addTo(TypeSpec.Builder builder) {
public void addTo(TypeSpec.Builder builder, ModelUtils utils) {
annotations.removeIf(a -> a.type.equals(TypeName.get(NonNull.class)));
builder.addField(FieldSpec.builder(type.getName().box(), name, Modifier.PRIVATE)
.addAnnotations(annotations)
.build());
final FieldSpec.Builder field = FieldSpec.builder(type.getName(), name, Modifier.PRIVATE)
.addAnnotations(annotations);
if(defaultValue != null){
if (type.getMirror().getKind() == TypeKind.ARRAY) {
field.initializer("new $T$L", utils.erasure(type.getMirror()), defaultValue);
}else {
field.initializer("$L", defaultValue);
}
}
builder.addField(field.build());
}

public String getJavadoc() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,15 @@
import com.squareup.javapoet.TypeSpec;

import org.acra.ModelUtils;
import org.apache.commons.lang3.tuple.Pair;

import java.util.ArrayList;
import java.util.List;

import javax.lang.model.type.TypeKind;

/**
* @author F43nd1r
* @since 12.06.2017
*/

public class FieldGetter extends FieldMethod implements org.acra.definition.Transformable {
public class FieldGetter extends FieldMethod {
public FieldGetter(Field field) {
super(field);
}
Expand All @@ -46,11 +42,10 @@ public boolean shouldPropagate() {

@Override
public void writeTo(TypeSpec.Builder builder, ModelUtils utils) {
final Pair<String, List<Object>> statement = getStatementWithParams(utils);
builder.addMethod(MethodSpec.methodBuilder(getField().getName())
.addAnnotations(getField().getAnnotations())
.returns(getField().getType().getName())
.addStatement("return " + statement.getKey(), statement.getValue().toArray())
.addStatement("return $L", getField().getName())
.build());
}

Expand All @@ -69,21 +64,4 @@ public TypeName getReturnType() {
return getField().getType().getName();
}

@Override
public Pair<String, List<Object>> getStatementWithParams(ModelUtils utils) {
final List<Object> params = new ArrayList<>();
params.add(getField().getName());
String result = "$" + params.size() + "L";
final Object defaultValue = getField().getDefaultValue();
if (defaultValue != null) {
result = "$" + params.size() + "L != null ? " + result + " : ";
if (getField().getType().getMirror().getKind() == TypeKind.ARRAY) {
params.add(utils.erasure(getField().getType().getMirror()));
result += "new $" + params.size() + "T";
}
params.add(defaultValue);
result += "$" + params.size() + "L";
}
return Pair.of(result, params);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.squareup.javapoet.TypeSpec;

import org.acra.ModelUtils;
import org.apache.commons.lang3.tuple.Pair;

import java.util.List;

Expand All @@ -37,13 +36,13 @@ public class Transformer implements Method {
private final String name;
private final String delegate;
private final Type returnType;
private final Transformable transformable;
private final FieldGetter transformable;

public static Transformer from(ExecutableElement method, String field, Transformable transformable, ModelUtils utils) {
public static Transformer from(ExecutableElement method, String field, FieldGetter transformable, ModelUtils utils) {
return new Transformer(method.getSimpleName().toString(), field, utils.getType(method.getReturnType()), transformable);
}

private Transformer(String name, String delegate, Type returnType, Transformable transformable) {
private Transformer(String name, String delegate, Type returnType, FieldGetter transformable) {
this.name = name;
this.delegate = delegate;
this.returnType = returnType;
Expand All @@ -58,15 +57,10 @@ public boolean shouldPropagate() {

@Override
public void writeTo(TypeSpec.Builder builder, ModelUtils utils) {
final Pair<String, List<Object>> baseStatement = transformable.getStatementWithParams(utils);
final List<Object> params = baseStatement.getValue();
params.add(delegate);
params.add(name);
final String statement = "return $" + (params.size() - 1) + "L.$" + params.size() + "L(" + baseStatement.getKey() + ")";
builder.addMethod(MethodSpec.methodBuilder(transformable.getName())
.addAnnotations(transformable.getAnnotations())
.returns(returnType.getName())
.addStatement(statement, params.toArray())
.addStatement("return $L.$L($L)", delegate, name, transformable.getField().getName())
.build());
}

Expand Down