Skip to content
Browse files

Initial Commit

  • Loading branch information...
0 parents commit 2914d0253d94c39b20d360b853fa6bb586cbad85 @alta189 committed Apr 11, 2012
Showing with 658 additions and 0 deletions.
  1. +64 −0 .gitignore
  2. 0 README.md
  3. +33 −0 pom.xml
  4. +39 −0 src/main/java/com/alta189/simplesave/Configuration.java
  5. +10 −0 src/main/java/com/alta189/simplesave/Database.java
  6. +9 −0 src/main/java/com/alta189/simplesave/DatabaseFactory.java
  7. +16 −0 src/main/java/com/alta189/simplesave/Driver.java
  8. +12 −0 src/main/java/com/alta189/simplesave/Field.java
  9. +11 −0 src/main/java/com/alta189/simplesave/Id.java
  10. +15 −0 src/main/java/com/alta189/simplesave/SerializeException.java
  11. +14 −0 src/main/java/com/alta189/simplesave/Table.java
  12. +65 −0 src/main/java/com/alta189/simplesave/internal/FieldFactory.java
  13. +31 −0 src/main/java/com/alta189/simplesave/internal/FieldRegistration.java
  14. +17 −0 src/main/java/com/alta189/simplesave/internal/FieldRegistrationException.java
  15. +20 −0 src/main/java/com/alta189/simplesave/internal/IdFactory.java
  16. +8 −0 src/main/java/com/alta189/simplesave/internal/IdRegistration.java
  17. +76 −0 src/main/java/com/alta189/simplesave/internal/SerializedClassBuilder.java
  18. +38 −0 src/main/java/com/alta189/simplesave/internal/TableFactory.java
  19. +39 −0 src/main/java/com/alta189/simplesave/internal/TableRegistration.java
  20. +17 −0 src/main/java/com/alta189/simplesave/internal/TableRegistrationException.java
  21. +16 −0 src/main/java/com/alta189/simplesave/internal/reflection/EmptyInjector.java
  22. +7 −0 src/main/java/com/alta189/simplesave/internal/reflection/Injector.java
  23. +16 −0 src/main/java/com/alta189/simplesave/internal/reflection/InjectorException.java
  24. +34 −0 src/main/java/com/alta189/simplesave/internal/reflection/SimpleInjector.java
  25. +12 −0 src/main/java/com/alta189/simplesave/mysql/MySQLConfiguration.java
  26. +4 −0 src/main/java/com/alta189/simplesave/mysql/MySQLDatabase.java
  27. +16 −0 src/test/java/com/alta189/simplesave/internal/RegistrationTest.java
  28. +19 −0 src/test/java/com/alta189/simplesave/test/TestTable.java
64 .gitignore
@@ -0,0 +1,64 @@
+# Compiled source #
+###################
+bin
+lib
+out
+target
+*.com
+*.class
+*.dll
+*.exe
+*.o
+*.so
+
+# Git #
+#######
+.git
+
+# Packages #
+############
+*.7z
+*.dmg
+*.gz
+*.iso
+*.jar
+*.rar
+*.tar
+*.zip
+
+# Logs and databases #
+######################
+*.log
+*.sql
+*.sqlite
+
+# OS generated files #
+######################
+.DS_Store
+ehthumbs.db
+Icon?
+Thumbs.db
+
+# Project files #
+#################
+.classpath
+.externalToolBuilders
+.project
+.settings
+MANIFEST.MF
+
+# Netbeans files #
+##################
+build
+dist
+nbproject
+build.xml
+
+# IntelliJ IDEA files #
+#######################
+.idea
+*.iml
+
+# JRebel files #
+################
+rebel.xml
0 README.md
No changes.
33 pom.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>com.alta189</groupId>
+ <artifactId>simplesave</artifactId>
+ <version>1.0.0</version>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.10</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <!-- Compile plugin -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.3.2</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
39 src/main/java/com/alta189/simplesave/Configuration.java
@@ -0,0 +1,39 @@
+package com.alta189.simplesave;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class Configuration {
+
+ private Map<String, String> properties = new HashMap<String, String>();
+ private final Driver driver;
+
+ public Configuration(Driver driver) {
+ this.driver = driver;
+ }
+
+ public Driver getDriver() {
+ return driver;
+ }
+
+ public Map<String, String> getProperties() {
+ return properties;
+ }
+
+ public String getProperty(String property) {
+ return properties.get(property);
+ }
+
+ public void setProperty(String property, String value) {
+ properties.put(property, value);
+ }
+
+ public void removeProperty(String property) {
+ properties.remove(property);
+ }
+
+ public boolean containsProperty(String property) {
+ return properties.containsKey(property);
+ }
+
+}
10 src/main/java/com/alta189/simplesave/Database.java
@@ -0,0 +1,10 @@
+package com.alta189.simplesave;
+
+public class Database {
+
+ protected Database() {
+
+ }
+
+
+}
9 src/main/java/com/alta189/simplesave/DatabaseFactory.java
@@ -0,0 +1,9 @@
+package com.alta189.simplesave;
+
+public class DatabaseFactory {
+
+ public static void createNewDatabase(Configuration configuration) {
+
+ }
+
+}
16 src/main/java/com/alta189/simplesave/Driver.java
@@ -0,0 +1,16 @@
+package com.alta189.simplesave;
+
+public enum Driver {
+
+ MYSQL;
+
+ public static Driver getDriver(String value) {
+ Driver result = null;
+ try {
+ result = Driver.valueOf(value.toUpperCase());
+ } catch (Exception ignored) {
+ }
+ return result;
+ }
+
+}
12 src/main/java/com/alta189/simplesave/Field.java
@@ -0,0 +1,12 @@
+package com.alta189.simplesave;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface Field {
+
+}
11 src/main/java/com/alta189/simplesave/Id.java
@@ -0,0 +1,11 @@
+package com.alta189.simplesave;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface Id {
+}
15 src/main/java/com/alta189/simplesave/SerializeException.java
@@ -0,0 +1,15 @@
+package com.alta189.simplesave;
+
+public class SerializeException extends RuntimeException {
+ public SerializeException(String message) {
+ super(message);
+ }
+
+ public SerializeException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public SerializeException(Throwable cause) {
+ super(cause);
+ }
+}
14 src/main/java/com/alta189/simplesave/Table.java
@@ -0,0 +1,14 @@
+package com.alta189.simplesave;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface Table {
+
+ public String name();
+
+}
65 src/main/java/com/alta189/simplesave/internal/FieldFactory.java
@@ -0,0 +1,65 @@
+package com.alta189.simplesave.internal;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+
+public class FieldFactory {
+
+ public static List<FieldRegistration> getFields(Class<?> clazz) {
+ final List<FieldRegistration> fields = new ArrayList<FieldRegistration>();
+ for (Field field : clazz.getDeclaredFields()) {
+ FieldRegistration fieldRegistration = getField(field);
+ if (fieldRegistration != null) {
+ fields.add(fieldRegistration);
+ }
+ }
+ return fields;
+ }
+
+ public static FieldRegistration getField(Field field) {
+ // Check if the field has the Field annotation
+ com.alta189.simplesave.Field fieldAnnotation = getFieldAnnotation(field);
+ if (fieldAnnotation == null) {
+ return null;
+ }
+
+ Class<?> type = field.getType();
+
+ // Check if the field has a valid type
+ if (validType(type)) {
+ return new FieldRegistration(field.getName(), type);
+ } else if (SerializedClassBuilder.validClass(type)) {
+ return new FieldRegistration(field.getName(), type, true);
+ } else {
+ throw new FieldRegistrationException("The type '" + type.getCanonicalName() + "' is not a valid type");
+ }
+ }
+
+ public static com.alta189.simplesave.Field getFieldAnnotation(Field field) {
+ com.alta189.simplesave.Field fieldAnnotation = null;
+ for (Annotation annotation : field.getDeclaredAnnotations()) {
+ if (annotation instanceof com.alta189.simplesave.Field) {
+ fieldAnnotation = (com.alta189.simplesave.Field) annotation;
+ break;
+ }
+ }
+ return fieldAnnotation;
+ }
+
+ public static boolean hasFieldAnnotation(Field field) {
+ for (Annotation annotation : field.getDeclaredAnnotations()) {
+ if (annotation.getClass().equals(com.alta189.simplesave.Field.class)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static boolean validType(Class<?> type) {
+ return true;
+ }
+
+}
31 src/main/java/com/alta189/simplesave/internal/FieldRegistration.java
@@ -0,0 +1,31 @@
+package com.alta189.simplesave.internal;
+
+public class FieldRegistration {
+
+ private final String name;
+ private final Class<?> type;
+ private final boolean serializable;
+
+ public FieldRegistration(String name, Class<?> type) {
+ this(name, type, false);
+ }
+
+ public FieldRegistration(String name, Class<?> type, boolean serializable) {
+ this.name = name;
+ this.type = type;
+ this.serializable = serializable;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public Class<?> getType() {
+ return type;
+ }
+
+ public boolean isSerializable() {
+ return serializable;
+ }
+
+}
17 src/main/java/com/alta189/simplesave/internal/FieldRegistrationException.java
@@ -0,0 +1,17 @@
+package com.alta189.simplesave.internal;
+
+public class FieldRegistrationException extends RuntimeException {
+
+ public FieldRegistrationException(String message) {
+ super(message);
+ }
+
+ public FieldRegistrationException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public FieldRegistrationException(Throwable cause) {
+ super(cause);
+ }
+
+}
20 src/main/java/com/alta189/simplesave/internal/IdFactory.java
@@ -0,0 +1,20 @@
+package com.alta189.simplesave.internal;
+
+import com.alta189.simplesave.Id;
+
+import java.lang.reflect.Field;
+
+public class IdFactory {
+
+ public static IdRegistration getId(Class<?> clazz) {
+ for (Field field : clazz.getDeclaredFields()) {
+ if (!field.isAnnotationPresent(Id.class))
+ continue;
+
+ Class<?> type = field.getType();
+
+ }
+ return null;
+ }
+
+}
8 src/main/java/com/alta189/simplesave/internal/IdRegistration.java
@@ -0,0 +1,8 @@
+package com.alta189.simplesave.internal;
+
+public class IdRegistration extends FieldRegistration {
+
+ public IdRegistration(String name, Class<?> type) {
+ super(name, type);
+ }
+}
76 src/main/java/com/alta189/simplesave/internal/SerializedClassBuilder.java
@@ -0,0 +1,76 @@
+package com.alta189.simplesave.internal;
+
+import com.alta189.simplesave.SerializeException;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+public class SerializedClassBuilder {
+
+ public static boolean validClass(Class<?> clazz) {
+ try {
+ Method serialize = clazz.getDeclaredMethod("serialize");
+ if (!serialize.getReturnType().equals(String.class)) {
+ System.out.println("Class '" + clazz.getCanonicalName() + "' is not serializable because it does not return a String");
+ return false;
+ }
+ if (!Modifier.isPublic(serialize.getModifiers())) {
+ System.out.println("Class '" + clazz.getCanonicalName() + "' is not serializable because the method 'serialize' is not public");
+ return false;
+ }
+ if (!Modifier.isStatic(serialize.getModifiers())) {
+ System.out.println("Class '" + clazz.getCanonicalName() + "' is not serializable because the method 'serialize' is static");
+ return false;
+ }
+
+ Method deserialize = clazz.getDeclaredMethod("deserialize", String.class);
+ if (!deserialize.getReturnType().equals(clazz)) {
+ System.out.println("Class '" + clazz.getCanonicalName() + "' is not deserializable because the method 'deserialize' does not return the class '" + clazz.getCanonicalName() + "'");
+ return false;
+ }
+
+ if (!Modifier.isStatic(deserialize.getModifiers())) {
+ System.out.println("Class '" + clazz.getCanonicalName() + "' is not deserializable because the method 'deserialize' is not static");
+ return false;
+ }
+ if (!Modifier.isPublic(deserialize.getModifiers())) {
+ System.out.println("Class '" + clazz.getCanonicalName() + "' is not deserializable because the method 'deserialize' is not public");
+ return false;
+ }
+ } catch (NoSuchMethodException e) {
+ System.out.println("Class '" + clazz.getCanonicalName() + "' is not deserializable because the method 'deserialize' is does not have either the serialize and/or deserialize method(s)");
+ return false;
+ }
+ return true;
+ }
+
+ public static Object deserialize(Class<?> clazz, String data) {
+ try {
+ Method deserialize = clazz.getDeclaredMethod("deserialize", String.class);
+ return deserialize.invoke(null, data);
+ } catch (NoSuchMethodException e) {
+ throw new SerializeException("Could not deserialize data", e.getCause());
+ } catch (InvocationTargetException e) {
+ throw new SerializeException("Could not deserialize data", e.getCause());
+ } catch (IllegalAccessException e) {
+ throw new SerializeException("Could not deserialize data", e.getCause());
+ }
+ }
+
+ public static String serialize(Class<?> clazz, Object object) {
+ try {
+ Method serialize = clazz.getDeclaredMethod("serialize");
+ return (String) serialize.invoke(object);
+ } catch (NoSuchMethodException e) {
+ throw new SerializeException("Could not serialize Class '" + clazz.getCanonicalName() + "'", e.getCause());
+ } catch (InvocationTargetException e) {
+ throw new SerializeException("Could not serialize Class '" + clazz.getCanonicalName() + "'", e.getCause());
+ } catch (IllegalAccessException e) {
+ throw new SerializeException("Could not serialize Class '" + clazz.getCanonicalName() + "'", e.getCause());
+ } catch (ClassCastException e) {
+ throw new SerializeException("Could not serialize Class '" + clazz.getCanonicalName() + "'", e.getCause());
+ }
+ }
+
+}
38 src/main/java/com/alta189/simplesave/internal/TableFactory.java
@@ -0,0 +1,38 @@
+package com.alta189.simplesave.internal;
+
+import com.alta189.simplesave.Table;
+import com.alta189.simplesave.internal.reflection.EmptyInjector;
+import com.alta189.simplesave.internal.reflection.Injector;
+
+public class TableFactory {
+
+ public static TableRegistration buildTable(Class<?> clazz) {
+ // Make sure that the class has the Table annotation
+ if (!clazz.isAnnotationPresent(Table.class)) {
+ throw new TableRegistrationException("Class '" + clazz.getCanonicalName() + "' does not have the Table annotation");
+ }
+
+ // Get the annotation and make sure that 'name' is defined
+ Table table = clazz.getAnnotation(Table.class);
+ if (table.name() == null || table.name().isEmpty()) {
+ throw new TableRegistrationException("Class '\" + clazz.getCanonicalName() + \"' is missing a table name");
+ }
+
+ // Check that the class has an empty constructor
+ Injector injector = new EmptyInjector();
+ try {
+ injector.newInstance(clazz);
+ } catch (Exception e) {
+ throw new TableRegistrationException("Class '" + clazz.getCanonicalName() + "' does not have an empty constructor", e);
+ }
+
+ // Create TableRegistration
+ TableRegistration tableRegistration = new TableRegistration(table.name(), clazz);
+
+ // Register fields
+ tableRegistration.addFields(FieldFactory.getFields(clazz));
+
+ return tableRegistration;
+ }
+
+}
39 src/main/java/com/alta189/simplesave/internal/TableRegistration.java
@@ -0,0 +1,39 @@
+package com.alta189.simplesave.internal;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Collection;
+
+public class TableRegistration {
+
+ private final String name;
+ private final Class<?> clazz;
+ private final Map<String, FieldRegistration> fields = new HashMap<String, FieldRegistration>();
+
+ public TableRegistration(String name, Class<?> clazz) {
+ this.name = name;
+ this.clazz = clazz;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public Class<?> getTableClass() {
+ return clazz;
+ }
+
+ public Collection<FieldRegistration> getFields() {
+ return fields.values();
+ }
+
+ public void addFields(Collection<FieldRegistration> fields) {
+ for (FieldRegistration field : fields) {
+ addField(field.getName(), field);
+ }
+ }
+
+ public void addField(String name, FieldRegistration field) {
+ fields.put(name.toLowerCase(), field);
+ }
+}
17 src/main/java/com/alta189/simplesave/internal/TableRegistrationException.java
@@ -0,0 +1,17 @@
+package com.alta189.simplesave.internal;
+
+public class TableRegistrationException extends RuntimeException {
+
+ public TableRegistrationException(String message) {
+ super(message);
+ }
+
+ public TableRegistrationException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public TableRegistrationException(Throwable cause) {
+ super(cause);
+ }
+
+}
16 src/main/java/com/alta189/simplesave/internal/reflection/EmptyInjector.java
@@ -0,0 +1,16 @@
+package com.alta189.simplesave.internal.reflection;
+
+public class EmptyInjector implements Injector {
+
+ @Override
+ public Object newInstance(Class<?> clazz) {
+ try {
+ return clazz.newInstance();
+ } catch (InstantiationException e) {
+ throw new InjectorException("Could not create a new instance of class '" + clazz.getCanonicalName() + "'", e.getCause());
+ } catch (IllegalAccessException e) {
+ throw new InjectorException("Could not create a new instance of class '" + clazz.getCanonicalName() + "'", e.getCause());
+ }
+ }
+
+}
7 src/main/java/com/alta189/simplesave/internal/reflection/Injector.java
@@ -0,0 +1,7 @@
+package com.alta189.simplesave.internal.reflection;
+
+public interface Injector {
+
+ public Object newInstance(Class<?> clazz);
+
+}
16 src/main/java/com/alta189/simplesave/internal/reflection/InjectorException.java
@@ -0,0 +1,16 @@
+package com.alta189.simplesave.internal.reflection;
+
+public class InjectorException extends RuntimeException {
+
+ public InjectorException(String message) {
+ super(message);
+ }
+
+ public InjectorException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public InjectorException(Throwable cause) {
+ super(cause);
+ }
+}
34 src/main/java/com/alta189/simplesave/internal/reflection/SimpleInjector.java
@@ -0,0 +1,34 @@
+package com.alta189.simplesave.internal.reflection;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+public class SimpleInjector implements Injector {
+
+ private Object[] args;
+ private Class<?>[] argClasses;
+
+ public SimpleInjector(Object... args) {
+ this.args = args;
+ argClasses = new Class[args.length];
+ for (int i = 0; i < args.length; ++i) {
+ argClasses[i] = args[i].getClass();
+ }
+ }
+
+ public Object newInstance(Class<?> clazz) {
+ try {
+ Constructor<?> ctr = clazz.getConstructor(argClasses);
+ return ctr.newInstance(args);
+ } catch (NoSuchMethodException e) {
+ throw new InjectorException("Could not create a new instance of class '" + clazz.getCanonicalName() + "'", e.getCause());
+ } catch (InvocationTargetException e) {
+ throw new InjectorException("Could not create a new instance of class '" + clazz.getCanonicalName() + "'", e.getCause());
+ } catch (InstantiationException e) {
+ throw new InjectorException("Could not create a new instance of class '" + clazz.getCanonicalName() + "'", e.getCause());
+ } catch (IllegalAccessException e) {
+ throw new InjectorException("Could not create a new instance of class '" + clazz.getCanonicalName() + "'", e.getCause());
+ }
+ }
+
+}
12 src/main/java/com/alta189/simplesave/mysql/MySQLConfiguration.java
@@ -0,0 +1,12 @@
+package com.alta189.simplesave.mysql;
+
+import com.alta189.simplesave.Configuration;
+import com.alta189.simplesave.Driver;
+
+public class MySQLConfiguration extends Configuration {
+
+ public MySQLConfiguration() {
+ super(Driver.MYSQL);
+ }
+
+}
4 src/main/java/com/alta189/simplesave/mysql/MySQLDatabase.java
@@ -0,0 +1,4 @@
+package com.alta189.simplesave.mysql;
+
+public class MySQLDatabase {
+}
16 src/test/java/com/alta189/simplesave/internal/RegistrationTest.java
@@ -0,0 +1,16 @@
+package com.alta189.simplesave.internal;
+
+import com.alta189.simplesave.test.TestTable;
+import org.junit.Test;
+
+import static org.junit.Assert.assertNotNull;
+
+public class RegistrationTest {
+
+ @Test
+ public void testRegistration() {
+ TableRegistration test = TableFactory.buildTable(TestTable.class);
+ assertNotNull("Registration failed", test);
+ }
+
+}
19 src/test/java/com/alta189/simplesave/test/TestTable.java
@@ -0,0 +1,19 @@
+package com.alta189.simplesave.test;
+
+import com.alta189.simplesave.Field;
+import com.alta189.simplesave.Id;
+import com.alta189.simplesave.Table;
+
+@Table(name = "Test")
+public class TestTable {
+
+ @Id
+ private int id;
+
+ @Field
+ private String data;
+
+ @Field
+ private double d;
+
+}

0 comments on commit 2914d02

Please sign in to comment.
Something went wrong with that request. Please try again.