gra-m / DBServer

Database server project
0 stars 0 forks source link

Create Generic Implementation of Database Server (Required Reflection) #40

Closed gra-m closed 2 years ago

gra-m commented 2 years ago

Completed on locally merged branch, diffs are also a complete mess as there was no .gitignore adding pertinent code to Milestone and comments.

gra-m commented 2 years ago

d3c9c86769867a92f9fae6f9d1aad76517dd27eb TestSuite d3c9c86769867a92f9fae6f9d1aad76517dd27eb Access Private members of a Record Class

gra-m commented 2 years ago
    // @GenericBaseFileHandler
    public Object readFromByteStream(final DataInputStream stream) throws IOException {
        Object object = null;
        // Get empty object of class passed via reflection, via constructor now as safer.
        try {
            object = Class.forName(this.aClass.getCanonicalName()).getDeclaredConstructor().newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            LOGGER.severe("@GBFileHandler readObjFromByteStream(stream): " + this.aClass.getSimpleName());
            e.printStackTrace();
        }
gra-m commented 2 years ago

Access the private fields of a Record where necessary: https://github.com/gra-m/DBServer/commit/5210a74b49aa500ad849b3f221fce46375f737f6

private int getObjectLength(final Object obj, final LinkedList<SchemaField> linkedList) {
        LOGGER.severe("@GenericFileHandler getObjectLength(obj, LinkedList)" + obj.getClass().getSimpleName() + " " + linkedList.toString());
        int result = 0;
        int count = 0;

        try {
            for (SchemaField field : linkedList) {
                switch (field.fieldType) {
                    case "String" -> {
                        Class objClass = obj.getClass();
                        RecordComponent[] rcArray = objClass.getRecordComponents();
                        Field jReflectField =  objClass.getDeclaredField(rcArray[count].getAccessor().getName());
                        jReflectField.setAccessible(true);
                        LOGGER.severe("result " + jReflectField.get(obj));
                        result += String.valueOf(jReflectField.get(obj)).length();  // if I was using UTF-16 not recommended by IJ this would be 2x bytes per char lots of places on int == 2bytes but depends on encoding!
                        result += INTEGER_LENGTH_IN_BYTES;
                        count++;
                    //if Dog were a normal Class    String stringValue = (String) obj.getClass().getDeclaredField(field.fieldName).get(obj);
                        //result += stringValue.length(); // if I was using UTF-16 not recommended by IJ this would be 2x bytes per char lots of places on int == 2bytes but depends on encoding!
                        //result += INTEGER_LENGTH_IN_BYTES;
                    }
                    case "boolean" -> {
                        result += BOOLEAN_LENGTH_IN_BYTES;
                        count++;
                    }
                    case "int" -> {
                        result += INTEGER_LENGTH_IN_BYTES;
                        count++;
                    }
                    case "long" -> {
                        result += LONG_LENGTH_IN_BYTES;
                        count++;
                    }
                }
            }
    @@ -263,4 +276,5 @@ public Collection<Object> searchWithRegex(String regEx) {
        }
        return result;
    }

}
gra-m commented 2 years ago

For normal class field access:


String stringValue = (String) obj.getClass().getDeclaredField(field.fieldName).get(obj);