当前位置:网站首页>Greendao reported an error in qigsaw, could not init daoconfig

Greendao reported an error in qigsaw, could not init daoconfig

2022-07-05 09:15:00 Xu Jiajia 233

Report errors

org.greenrobot.greendao.DaoException: Could not init DAOConfig

scene

  • Use qigsaw
  • The host module depends on greenDAO
  • The plug-in module uses greenDAO
  • The plug-in module uses greenDAO Report errors :Could not init DAOConfig

reason

  • greenDAO The source code calls through reflection Properties class
  • Host module loading greenDAO Of ClassLoader yes PathClassLoader
  • Plug in module loading AbstractDao Implementation class ClassLoader yes QigsawClassLoader
  • because QigsawClassLoader Not loaded in DAOConfig, So it will report an error .

How to check it out

The way to check is to check greenDAO Source code , The crash caused by plug-in , So it can be quickly inferred that ClassLoader The problem of , After verification, it was found that .

    public DaoConfig(Database db, Class<? extends AbstractDao<?, ?>> daoClass) {
    
        this.db = db;
        try {
    
            this.tablename = (String) daoClass.getField("TABLENAME").get(null);
            Property[] properties = reflectProperties(daoClass);
            this.properties = properties;

            allColumns = new String[properties.length];

            List<String> pkColumnList = new ArrayList<String>();
            List<String> nonPkColumnList = new ArrayList<String>();
            Property lastPkProperty = null;
            for (int i = 0; i < properties.length; i++) {
    
                Property property = properties[i];
                String name = property.columnName;
                allColumns[i] = name;
                if (property.primaryKey) {
    
                    pkColumnList.add(name);
                    lastPkProperty = property;
                } else {
    
                    nonPkColumnList.add(name);
                }
            }
            String[] nonPkColumnsArray = new String[nonPkColumnList.size()];
            nonPkColumns = nonPkColumnList.toArray(nonPkColumnsArray);
            String[] pkColumnsArray = new String[pkColumnList.size()];
            pkColumns = pkColumnList.toArray(pkColumnsArray);

            pkProperty = pkColumns.length == 1 ? lastPkProperty : null;
            statements = new TableStatements(db, tablename, allColumns, pkColumns);

            if (pkProperty != null) {
    
                Class<?> type = pkProperty.type;
                keyIsNumeric = type.equals(long.class) || type.equals(Long.class) || type.equals(int.class)
                        || type.equals(Integer.class) || type.equals(short.class) || type.equals(Short.class)
                        || type.equals(byte.class) || type.equals(Byte.class);
            } else {
    
                keyIsNumeric = false;
            }

        } catch (Exception e) {
    
            throw new DaoException("Could not init DAOConfig", e);
        }
    }

    private static Property[] reflectProperties(Class<? extends AbstractDao<?, ?>> daoClass)
            throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException {
    
        Class<?> propertiesClass = Class.forName(daoClass.getName() + "$Properties");
        Field[] fields = propertiesClass.getDeclaredFields();

        ArrayList<Property> propertyList = new ArrayList<Property>();
        final int modifierMask = Modifier.STATIC | Modifier.PUBLIC;
        for (Field field : fields) {
    
            // There might be other fields introduced by some tools, just ignore them (see issue #28)
            if ((field.getModifiers() & modifierMask) == modifierMask) {
    
                Object fieldValue = field.get(null);
                if (fieldValue instanceof Property) {
    
                    propertyList.add((Property) fieldValue);
                }
            }
        }

        Property[] properties = new Property[propertyList.size()];
        for (Property property : propertyList) {
    
            if (properties[property.ordinal] != null) {
    
                throw new DaoException("Duplicate property ordinals");
            }
            properties[property.ordinal] = property;
        }
        return properties;
    }

Solution

There are still many solutions , Mainly based on business needs . The author will write some possible solutions :

  1. Add the plug-in to the greenDAO The relevant logic of is put into the host module
  2. If in the plug-in greenDAO There is not much logic in , As a temporary solution , Can be greenDAO The called class is defined again in the host .( It can only be a temporary plan )
  3. If not used in the host greenDao The logic of , It can be changed into a plug-in that directly depends on , Instead of loading in the host .
原网站

版权声明
本文为[Xu Jiajia 233]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140538085247.html