/** * 根据namespace查找需要的信息,避免每次反射的耗时操作 * * @param conf * @param statementId * @return * @throws ClassNotFoundException */ private MapperMeta getMapperMeta(Configuration conf, final String statementId) throws ClassNotFoundException { MapperMeta meta = CACHED.get(statementId); if (meta == null) { int pos = statementId.lastIndexOf('.'); String namespace = statementId.substring(0, pos); // mapper类名 String methodName = statementId.substring(pos + 1); Class<?> mapperClass; mapperClass = Class.forName(namespace); synchronized (this) { meta = CACHED.get(statementId); if (meta == null) { meta = buildMapperMeta(conf, namespace, methodName, mapperClass); CACHED.put(statementId, meta); } } } return meta; }
private Class<?> getEntityClass(String namespace) { Class<?> entityClazz = ENTITY_CACHE.get(namespace); if (entityClazz == null) { try { Type[] genTypes = Class.forName(namespace).getGenericInterfaces(); if (genTypes.length == 0) { return null; } Type type = genTypes[0]; if (!(type instanceof ParameterizedType)) { return null; } Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments(); logger.info("namespace:{}, EntityClass: {}", namespace, actualTypeArguments); entityClazz = (Class<?>) actualTypeArguments[0]; } catch (ClassNotFoundException e) { logger.error("getEntityClass({})", namespace, e); entityClazz = Object.class; } ENTITY_CACHE.put(namespace, entityClazz); } return entityClazz; }