Code example for Field

Methods: get, getAnnotation, getModifiers, getName, getType, setAccessible

0
    } 
 
    //@formatter:off 
    private String getColumnSQLType(Field field) throws Exception {
        String colType = null;
        int fieldMods = field.getModifiers();
        String fieldType = field.getType().getSimpleName();
        boolean hasStaticMod = Modifier.isStatic(fieldMods);
        boolean hasFinalMod = Modifier.isFinal(fieldMods);
 
        if (hasStaticMod != hasFinalMod)
            throw new DataType.DataTypeException("Invalid field '" 
                    + field.getName()
                    + "'. fields should have both 'static' and 'final'" 
                    + " modifiers or not have both."); 
 
        if (hasStaticMod && hasFinalMod) {
            if (!fieldType.equals("String"))
                throw new DataType.DataTypeException( 
                        "Invalid field '" 
                                + field.getName()
                                + "'. static final fields needs to be of 'String' type."); 
 
            field.setAccessible(true);
            String fieldValue = (String) field.get(null);
            if (!fieldValue.equals(field.getName()))
                throw new DataType.DataTypeException("Invalid field '" 
                        + field.getName()
                        + "'. static final string fields need to have" 
                        + " matching value."); 
 
            Column cField = field.getAnnotation(Column.class);
            if (cField != null) colType = " TEXT";
 
            if (colType == null) {
                ColumnInt ciField = field.getAnnotation(ColumnInt.class);
                if (ciField != null) colType = " INTEGER";
            } 
 
            if (colType == null) {
                ColumnReal crField = field.getAnnotation(ColumnReal.class);
                if (crField != null) colType = " REAL";
            } 
        } else { 
 
            Column cField = field.getAnnotation(Column.class);
             
            if (cField == null){
                throw new DataType.DataTypeException( 
                        "Invalid annotation '" 
                                + field.getName()
                                + "'. Only 'Column' annotation is available" 
                                + " for instance members. Type is determined" 
                                + " automatically for instance members." 
                                + " Remove 'ColumnInt' or 'ColumnReal'" 
                                + " annotations.");  
            } 
             
            // if field is instance variable then figure out it's db type. 
            if (fieldType.equals("int") || fieldType.equals("long")) colType = " INTEGER";
            else if (fieldType.equals("float")
             || fieldType.equals("double")) colType = " REAL";
            else if (fieldType.equals("String")) colType = " TEXT";
 
        } 
         
        if (colType == null)
            throw new DataType.DataTypeException("Invalid field '" 
                    + field.getName() + "'.");
         
        return colType;
    } 
    //@formatter:on