Skip to content

Commit

Permalink
Merge pull request #498 from pushtorefresh/storio-sqlite-annotations-…
Browse files Browse the repository at this point in the history
…blob

Add support for byte[] into StorIOSQLite annotation processor
  • Loading branch information
nikitin-da committed Aug 29, 2015
2 parents cf99382 + 31901c2 commit 1eb3a2a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.pushtorefresh.storio.sample.db.entities;

import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteColumn;
import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteType;

/**
* Just a sample entity with all supported types of type mapping via annotation processor.
*/
@StorIOSQLiteType(table = "there_is_no_such_table")
public class AllSupportedTypes {

@StorIOSQLiteColumn(name = "some_boolean")
boolean someBoolean;

@StorIOSQLiteColumn(name = "some_boolean_object")
Boolean someBooleanObject;

@StorIOSQLiteColumn(name = "some_short")
short someShort;

@StorIOSQLiteColumn(name = "some_short_object")
Short someShortObject;

@StorIOSQLiteColumn(name = "some_integer", key = true) // One field should be key
int someInteger;

@StorIOSQLiteColumn(name = "some_integer_object")
Integer someIntegerObject;

@StorIOSQLiteColumn(name = "some_long")
long someLong;

@StorIOSQLiteColumn(name = "some_long_object")
Long someLongObject;

@StorIOSQLiteColumn(name = "some_float")
float someFloat;

@StorIOSQLiteColumn(name = "some_float_object")
Float someFloatObject;

@StorIOSQLiteColumn(name = "some_double")
double someDouble;

@StorIOSQLiteColumn(name = "some_double_object")
Double someDoubleObject;

@StorIOSQLiteColumn(name = "some_string")
String someString;

@StorIOSQLiteColumn(name = "some_byte_array")
byte[] someByteArray;

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ public class Tweet {
@StorIOSQLiteColumn(name = TweetsTable.COLUMN_CONTENT)
String content;

@NonNull
@StorIOSQLiteColumn(name = "some_bytes")
byte[] someBytes;

// leave default constructor for AutoGenerated code!
Tweet() {

}

private Tweet(@Nullable Long id, @NonNull String author, @NonNull String content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static com.pushtorefresh.storio.sqlite.annotations.processor.generate.Common.INDENT;
import static com.pushtorefresh.storio.sqlite.annotations.processor.introspection.JavaType.BOOLEAN;
import static com.pushtorefresh.storio.sqlite.annotations.processor.introspection.JavaType.BOOLEAN_OBJECT;
import static com.pushtorefresh.storio.sqlite.annotations.processor.introspection.JavaType.BYTE_ARRAY;
import static com.pushtorefresh.storio.sqlite.annotations.processor.introspection.JavaType.DOUBLE;
import static com.pushtorefresh.storio.sqlite.annotations.processor.introspection.JavaType.DOUBLE_OBJECT;
import static com.pushtorefresh.storio.sqlite.annotations.processor.introspection.JavaType.FLOAT;
Expand Down Expand Up @@ -84,12 +85,13 @@ MethodSpec createMapFromCursorMethodSpec(@NotNull StorIOSQLiteTypeMeta storIOSQL
getFromCursor = "getDouble(" + columnIndex + ")";
} else if (javaType == STRING) {
getFromCursor = "getString(" + columnIndex + ")";
} else if (javaType == BYTE_ARRAY) {
getFromCursor = "getBlob(" + columnIndex + ")";
} else {
throw new ProcessingException(columnMeta.element, "Can not generate GetResolver for field");
}

builder
.addStatement("object.$L = cursor.$L", columnMeta.fieldName, getFromCursor);
builder.addStatement("object.$L = cursor.$L", columnMeta.fieldName, getFromCursor);
}

return builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public enum JavaType {
FLOAT_OBJECT,
DOUBLE,
DOUBLE_OBJECT,
STRING;
STRING,
BYTE_ARRAY;

@NotNull
public static JavaType from(@NotNull TypeMirror typeMirror) {
Expand All @@ -28,30 +29,32 @@ public static JavaType from(@NotNull TypeMirror typeMirror) {

if (typeKind == TypeKind.BOOLEAN) {
return BOOLEAN;
} else if ("java.lang.Boolean".equals(typeName)) {
} else if (Boolean.class.getCanonicalName().equals(typeName)) {
return BOOLEAN_OBJECT;
} else if (typeKind == TypeKind.SHORT) {
return SHORT;
} else if ("java.lang.Short".equals(typeName)) {
} else if (Short.class.getCanonicalName().equals(typeName)) {
return SHORT_OBJECT;
} else if (typeKind == TypeKind.INT) {
return INTEGER;
} else if ("java.lang.Integer".equals(typeName)) {
} else if (Integer.class.getCanonicalName().equals(typeName)) {
return INTEGER_OBJECT;
} else if (typeKind == TypeKind.LONG) {
return LONG;
} else if ("java.lang.Long".equals(typeName)) {
} else if (Long.class.getCanonicalName().equals(typeName)) {
return LONG_OBJECT;
} else if (typeKind == TypeKind.FLOAT) {
return FLOAT;
} else if ("java.lang.Float".equals(typeName)) {
} else if (Float.class.getCanonicalName().equals(typeName)) {
return FLOAT_OBJECT;
} else if (typeKind == TypeKind.DOUBLE) {
return DOUBLE;
} else if ("java.lang.Double".equals(typeName)) {
} else if (Double.class.getCanonicalName().equals(typeName)) {
return DOUBLE_OBJECT;
} else if ("java.lang.String".equals(typeName)) {
} else if (String.class.getCanonicalName().equals(typeName)) {
return STRING;
} else if (byte[].class.getCanonicalName().equals(typeName)) {
return BYTE_ARRAY;
} else {
throw new IllegalArgumentException("Unsupported type: " + typeMirror);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.pushtorefresh.storio.sqlite.annotations.processor.introspection;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.Test;

import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;

import static com.pushtorefresh.storio.sqlite.annotations.processor.introspection.JavaType.BOOLEAN;
import static com.pushtorefresh.storio.sqlite.annotations.processor.introspection.JavaType.BOOLEAN_OBJECT;
import static com.pushtorefresh.storio.sqlite.annotations.processor.introspection.JavaType.BYTE_ARRAY;
import static com.pushtorefresh.storio.sqlite.annotations.processor.introspection.JavaType.DOUBLE;
import static com.pushtorefresh.storio.sqlite.annotations.processor.introspection.JavaType.DOUBLE_OBJECT;
import static com.pushtorefresh.storio.sqlite.annotations.processor.introspection.JavaType.FLOAT;
Expand All @@ -24,7 +27,8 @@

public class JavaTypeTest {

private static TypeMirror mockTypeMirror(TypeKind typeKind, String typeName) {
@NotNull
private static TypeMirror mockTypeMirror(@Nullable TypeKind typeKind, @Nullable String typeName) {
final TypeMirror typeMirror = mock(TypeMirror.class);

when(typeMirror.getKind())
Expand All @@ -44,7 +48,7 @@ public void fromBoolean() {

@Test
public void fromBooleanObject() {
final TypeMirror typeMirror = mockTypeMirror(null, "java.lang.Boolean");
final TypeMirror typeMirror = mockTypeMirror(null, Boolean.class.getCanonicalName());
assertThat(JavaType.from(typeMirror)).isEqualTo(BOOLEAN_OBJECT);
}

Expand All @@ -56,7 +60,7 @@ public void fromShort() {

@Test
public void fromShortObject() {
final TypeMirror typeMirror = mockTypeMirror(null, "java.lang.Short");
final TypeMirror typeMirror = mockTypeMirror(null, Short.class.getCanonicalName());
assertThat(JavaType.from(typeMirror)).isEqualTo(SHORT_OBJECT);
}

Expand All @@ -68,7 +72,7 @@ public void fromInteger() {

@Test
public void fromIntegerObject() {
final TypeMirror typeMirror = mockTypeMirror(null, "java.lang.Integer");
final TypeMirror typeMirror = mockTypeMirror(null, Integer.class.getCanonicalName());
assertThat(JavaType.from(typeMirror)).isEqualTo(INTEGER_OBJECT);
}

Expand All @@ -80,7 +84,7 @@ public void fromLong() {

@Test
public void fromLongObject() {
final TypeMirror typeMirror = mockTypeMirror(null, "java.lang.Long");
final TypeMirror typeMirror = mockTypeMirror(null, Long.class.getCanonicalName());
assertThat(JavaType.from(typeMirror)).isEqualTo(LONG_OBJECT);
}

Expand All @@ -92,7 +96,7 @@ public void fromFloat() {

@Test
public void fromFloatObject() {
final TypeMirror typeMirror = mockTypeMirror(null, "java.lang.Float");
final TypeMirror typeMirror = mockTypeMirror(null, Float.class.getCanonicalName());
assertThat(JavaType.from(typeMirror)).isEqualTo(FLOAT_OBJECT);
}

Expand All @@ -104,13 +108,19 @@ public void fromDouble() {

@Test
public void fromDoubleObject() {
final TypeMirror typeMirror = mockTypeMirror(null, "java.lang.Double");
final TypeMirror typeMirror = mockTypeMirror(null, Double.class.getCanonicalName());
assertThat(JavaType.from(typeMirror)).isEqualTo(DOUBLE_OBJECT);
}

@Test
public void fromString() {
final TypeMirror typeMirror = mockTypeMirror(null, "java.lang.String");
final TypeMirror typeMirror = mockTypeMirror(null, String.class.getCanonicalName());
assertThat(JavaType.from(typeMirror)).isEqualTo(STRING);
}

@Test
public void fromByteArray() {
final TypeMirror typeMirror = mockTypeMirror(null, byte[].class.getCanonicalName());
assertThat(JavaType.from(typeMirror)).isEqualTo(BYTE_ARRAY);
}
}

0 comments on commit 1eb3a2a

Please sign in to comment.