Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 5 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
View
17 src/main/java/com/alta189/simplesave/exceptions/NotConnectedException.java
@@ -0,0 +1,17 @@
+package com.alta189.simplesave.exceptions;
+
+public class NotConnectedException extends RuntimeException {
+
+ public NotConnectedException(String message) {
+ super(message);
+ }
+
+ public NotConnectedException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public NotConnectedException(Throwable cause) {
+ super(cause);
+ }
+
+}
View
15 src/main/java/com/alta189/simplesave/internal/IdFactory.java
@@ -2,6 +2,7 @@
import com.alta189.simplesave.Id;
import com.alta189.simplesave.exceptions.TableRegistrationException;
+import com.alta189.simplesave.internal.reflection.EmptyInjector;
import java.lang.reflect.Field;
@@ -13,8 +14,18 @@ public static IdRegistration getId(Class<?> clazz) throws TableRegistrationExcep
continue;
Class<?> type = field.getType();
- if (type.equals(int.class))
- throw new TableRegistrationException("The id is not of type 'int'");
+ if (!type.equals(int.class))
+ throw new TableRegistrationException("The id is not of type 'int' its class is '" + type.getCanonicalName() + "'");
+
+ // Check if id defaults to 0
+ try {
+ Object o = new EmptyInjector().newInstance(clazz);
+ int id = ((Number)field.get(o)).intValue();
+ if (id != 0)
+ throw new TableRegistrationException("The id does not default to 0");
+ } catch (IllegalAccessException e) {
+ throw new TableRegistrationException(e);
+ }
return new IdRegistration(field.getName(), type);
}
View
4 src/main/java/com/alta189/simplesave/internal/TableFactory.java
@@ -39,6 +39,10 @@ public static TableRegistration buildTable(Class<?> clazz) throws TableRegistrat
// Create TableRegistration
TableRegistration tableRegistration = new TableRegistration(table.name(), clazz);
+ // Get Id
+ IdRegistration idRegistration = IdFactory.getId(clazz);
+ tableRegistration.setId(idRegistration);
+
// Register fields
try {
tableRegistration.addFields(FieldFactory.getFields(clazz));
View
9 src/main/java/com/alta189/simplesave/internal/TableRegistration.java
@@ -9,6 +9,7 @@
private final String name;
private final Class<?> clazz;
private final Map<String, FieldRegistration> fields = new HashMap<String, FieldRegistration>();
+ private IdRegistration id;
public TableRegistration(String name, Class<?> clazz) {
this.name = name;
@@ -23,6 +24,14 @@ public String getName() {
return clazz;
}
+ public IdRegistration getId() {
+ return id;
+ }
+
+ public void setId(IdRegistration id) {
+ this.id = id;
+ }
+
public Collection<FieldRegistration> getFields() {
return fields.values();
}
View
42 src/main/java/com/alta189/simplesave/internal/TableUtils.java
@@ -0,0 +1,42 @@
+package com.alta189.simplesave.internal;
+
+import java.lang.reflect.Field;
+
+public class TableUtils {
+
+ public static int getIdValue(TableRegistration table, Object o) {
+ IdRegistration idRegistration = table.getId();
+ try {
+ Field field = o.getClass().getDeclaredField(idRegistration.getName());
+ return ((Number)field.get(o)).intValue();
+ } catch (NoSuchFieldException e) {
+ throw new IllegalArgumentException(e);
+ } catch (IllegalAccessException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ public static String getValueAsString(FieldRegistration fieldRegistration, Object o) {
+ try {
+ Field field = o.getClass().getDeclaredField(fieldRegistration.getName());
+ return field.get(o).toString();
+ } catch (Exception e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ public static String serializeField(FieldRegistration fieldRegistration, Object tableObject) {
+ try {
+ Field field = tableObject.getClass().getDeclaredField(fieldRegistration.getName());
+ Object o = field.get(tableObject);
+ return SerializedClassBuilder.serialize(fieldRegistration.getClass(), o);
+ } catch (Exception e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ public static Object deserializeField(FieldRegistration fieldRegistration, String data) {
+ return SerializedClassBuilder.deserialize(fieldRegistration.getClass(), data);
+ }
+
+}
View
8 src/test/java/com/alta189/simplesave/internal/RegistrationTest.java
@@ -1,5 +1,6 @@
package com.alta189.simplesave.internal;
+import com.alta189.simplesave.exceptions.TableRegistrationException;
import com.alta189.simplesave.test.TestTable;
import org.junit.Test;
@@ -9,7 +10,12 @@
@Test
public void testRegistration() {
- TableRegistration test = TableFactory.buildTable(TestTable.class);
+ TableRegistration test = null;
+ try {
+ test = TableFactory.buildTable(TestTable.class);
+ } catch (TableRegistrationException e) {
+ e.printStackTrace();
+ }
assertNotNull("Registration failed", test);
}

No commit comments for this range

Something went wrong with that request. Please try again.