Java Code Examples for org.apache.ibatis.io.Resources#classForName()

The following examples show how to use org.apache.ibatis.io.Resources#classForName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XMLMapperBuilder.java    From Shop-for-JavaWeb with MIT License 6 votes vote down vote up
private void bindMapperForNamespace() {
	String namespace = builderAssistant.getCurrentNamespace();
	if (namespace != null) {
		Class<?> boundType = null;
		try {
			boundType = Resources.classForName(namespace);
		} catch (ClassNotFoundException e) {
			// ignore, bound type is not required
		}
		if (boundType != null) {
			if (!configuration.hasMapper(boundType)) {
				// Spring may not know the real resource name so we set a
				// flag
				// to prevent loading again this resource from the mapper
				// interface
				// look at MapperAnnotationBuilder#loadXmlResource
				configuration.addLoadedResource("namespace:" + namespace);
				configuration.addMapper(boundType);
			}
		}
	}
}
 
Example 2
Source File: UnpooledDataSource.java    From mybaties with Apache License 2.0 6 votes vote down vote up
private synchronized void initializeDriver() throws SQLException {
 //这里便是大家熟悉的初学JDBC时的那几句话了 Class.forName newInstance()
  if (!registeredDrivers.containsKey(driver)) {
    Class<?> driverType;
    try {
      if (driverClassLoader != null) {
        driverType = Class.forName(driver, true, driverClassLoader);
      } else {
        driverType = Resources.classForName(driver);
      }
      // DriverManager requires the driver to be loaded via the system ClassLoader.
      // http://www.kfu.com/~nsayer/Java/dyn-jdbc.html
      Driver driverInstance = (Driver)driverType.newInstance();
      DriverManager.registerDriver(new DriverProxy(driverInstance));
      registeredDrivers.put(driver, driverInstance);
    } catch (Exception e) {
      throw new SQLException("Error setting driver on UnpooledDataSource. Cause: " + e);
    }
  }
}
 
Example 3
Source File: OgnlClassResolver.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Override
public Class classForName(String className, Map context) throws ClassNotFoundException {
  Class<?> result = null;
  if ((result = classes.get(className)) == null) {
    try {
      result = Resources.classForName(className);
    } catch (ClassNotFoundException e1) {
      if (className.indexOf('.') == -1) {
        result = Resources.classForName("java.lang." + className);
        classes.put("java.lang." + className, result);
      }
    }
    classes.put(className, result);
  }
  return result;
}
 
Example 4
Source File: XMLMapperBuilder.java    From mybaties with Apache License 2.0 6 votes vote down vote up
private void bindMapperForNamespace() {
  String namespace = builderAssistant.getCurrentNamespace();
  if (namespace != null) {
    Class<?> boundType = null;
    try {
      boundType = Resources.classForName(namespace);
    } catch (ClassNotFoundException e) {
      //ignore, bound type is not required
    }
    if (boundType != null) {
      if (!configuration.hasMapper(boundType)) {
        // Spring may not know the real resource name so we set a flag
        // to prevent loading again this resource from the mapper interface
        // look at MapperAnnotationBuilder#loadXmlResource
        configuration.addLoadedResource("namespace:" + namespace);
        configuration.addMapper(boundType);
      }
    }
  }
}
 
Example 5
Source File: XMLMapperBuilder.java    From mybatis with Apache License 2.0 6 votes vote down vote up
private void bindMapperForNamespace() {
  String namespace = builderAssistant.getCurrentNamespace();
  if (namespace != null) {
    Class<?> boundType = null;
    try {
      boundType = Resources.classForName(namespace);
    } catch (ClassNotFoundException e) {
      //ignore, bound type is not required
    }
    if (boundType != null) {
      if (!configuration.hasMapper(boundType)) {
        // Spring may not know the real resource name so we set a flag
        // to prevent loading again this resource from the mapper interface
        // look at MapperAnnotationBuilder#loadXmlResource
        configuration.addLoadedResource("namespace:" + namespace);
        configuration.addMapper(boundType);
      }
    }
  }
}
 
Example 6
Source File: OgnlClassResolver.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Override
public Class classForName(String className, Map context) throws ClassNotFoundException {
  Class<?> result = null;
  if ((result = classes.get(className)) == null) {
    try {
      result = Resources.classForName(className);
    } catch (ClassNotFoundException e1) {
      if (className.indexOf('.') == -1) {
        result = Resources.classForName("java.lang." + className);
        classes.put("java.lang." + className, result);
      }
    }
    classes.put(className, result);
  }
  return result;
}
 
Example 7
Source File: UnpooledDataSource.java    From mybatis with Apache License 2.0 6 votes vote down vote up
private synchronized void initializeDriver() throws SQLException {
 //这里便是大家熟悉的初学JDBC时的那几句话了 Class.forName newInstance()
  if (!registeredDrivers.containsKey(driver)) {
    Class<?> driverType;
    try {
      if (driverClassLoader != null) {
        driverType = Class.forName(driver, true, driverClassLoader);
      } else {
        driverType = Resources.classForName(driver);
      }
      // DriverManager requires the driver to be loaded via the system ClassLoader.
      // http://www.kfu.com/~nsayer/Java/dyn-jdbc.html
      Driver driverInstance = (Driver)driverType.newInstance();
      DriverManager.registerDriver(new DriverProxy(driverInstance));
      registeredDrivers.put(driver, driverInstance);
    } catch (Exception e) {
      throw new SQLException("Error setting driver on UnpooledDataSource. Cause: " + e);
    }
  }
}
 
Example 8
Source File: XMLConfigBuilder.java    From QuickProject with Apache License 2.0 6 votes vote down vote up
private void typeAliasesElement(XNode parent) {
  if (parent != null) {
    for (XNode child : parent.getChildren()) {
      if ("package".equals(child.getName())) {
        String typeAliasPackage = child.getStringAttribute("name");
        configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
      } else {
        String alias = child.getStringAttribute("alias");
        String type = child.getStringAttribute("type");
        try {
          Class<?> clazz = Resources.classForName(type);
          if (alias == null) {
            typeAliasRegistry.registerAlias(clazz);
          } else {
            typeAliasRegistry.registerAlias(alias, clazz);
          }
        } catch (ClassNotFoundException e) {
          throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e);
        }
      }
    }
  }
}
 
Example 9
Source File: CglibProxyFactory.java    From mybaties with Apache License 2.0 5 votes vote down vote up
public CglibProxyFactory() {
  try {
    //先检查是否有Cglib
    Resources.classForName("net.sf.cglib.proxy.Enhancer");
  } catch (Throwable e) {
    throw new IllegalStateException("Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.", e);
  }
}
 
Example 10
Source File: JavassistProxyFactory.java    From mybatis with Apache License 2.0 5 votes vote down vote up
public JavassistProxyFactory() {
  try {
    //先检查是否有javassist
    Resources.classForName("javassist.util.proxy.ProxyFactory");
  } catch (Throwable e) {
    throw new IllegalStateException("Cannot enable lazy loading because Javassist is not available. Add Javassist to your classpath.", e);
  }
}
 
Example 11
Source File: ResultSetWrapper.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private Class<?> resolveClass(String className) {
  try {
    return Resources.classForName(className);
  } catch (ClassNotFoundException e) {
    return null;
  }
}
 
Example 12
Source File: TypeAliasRegistry.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
 // throws class cast exception as well if types cannot be assigned
//解析类型别名
 public <T> Class<T> resolveAlias(String string) {
   try {
     if (string == null) {
       return null;
     }
     // issue #748
     //先转成小写再解析
     //这里转个小写也有bug?见748号bug(在google code上)
     //https://code.google.com/p/mybatis/issues
     //比如如果本地语言是Turkish,那i转成大写就不是I了,而是另外一个字符(İ)。这样土耳其的机器就用不了mybatis了!这是一个很大的bug,但是基本上每个人都会犯......
     String key = string.toLowerCase(Locale.ENGLISH);
     Class<T> value;
     //原理就很简单了,从HashMap里找对应的键值,找到则返回类型别名对应的Class
     if (TYPE_ALIASES.containsKey(key)) {
       value = (Class<T>) TYPE_ALIASES.get(key);
     } else {
       //找不到,再试着将String直接转成Class(这样怪不得我们也可以直接用java.lang.Integer的方式定义,也可以就int这么定义)
       value = (Class<T>) Resources.classForName(string);
     }
     return value;
   } catch (ClassNotFoundException e) {
     throw new TypeException("Could not resolve type alias '" + string + "'.  Cause: " + e, e);
   }
 }
 
Example 13
Source File: TypeAliasRegistry.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
 // throws class cast exception as well if types cannot be assigned
//解析类型别名
 public <T> Class<T> resolveAlias(String string) {
   try {
     if (string == null) {
       return null;
     }
     // issue #748
     //先转成小写再解析
     //这里转个小写也有bug?见748号bug(在google code上)
     //https://code.google.com/p/mybatis/issues
     //比如如果本地语言是Turkish,那i转成大写就不是I了,而是另外一个字符(İ)。这样土耳其的机器就用不了mybatis了!这是一个很大的bug,但是基本上每个人都会犯......
     String key = string.toLowerCase(Locale.ENGLISH);
     Class<T> value;
     //原理就很简单了,从HashMap里找对应的键值,找到则返回类型别名对应的Class
     if (TYPE_ALIASES.containsKey(key)) {
       value = (Class<T>) TYPE_ALIASES.get(key);
     } else {
       //找不到,再试着将String直接转成Class(这样怪不得我们也可以直接用java.lang.Integer的方式定义,也可以就int这么定义)
       value = (Class<T>) Resources.classForName(string);
     }
     return value;
   } catch (ClassNotFoundException e) {
     throw new TypeException("Could not resolve type alias '" + string + "'.  Cause: " + e, e);
   }
 }
 
Example 14
Source File: CglibProxyFactory.java    From mybatis with Apache License 2.0 5 votes vote down vote up
public CglibProxyFactory() {
  try {
    //先检查是否有Cglib
    Resources.classForName("net.sf.cglib.proxy.Enhancer");
  } catch (Throwable e) {
    throw new IllegalStateException("Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.", e);
  }
}
 
Example 15
Source File: JavassistProxyFactory.java    From mybaties with Apache License 2.0 5 votes vote down vote up
public JavassistProxyFactory() {
  try {
    //先检查是否有javassist
    Resources.classForName("javassist.util.proxy.ProxyFactory");
  } catch (Throwable e) {
    throw new IllegalStateException("Cannot enable lazy loading because Javassist is not available. Add Javassist to your classpath.", e);
  }
}
 
Example 16
Source File: ResultSetWrapper.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private Class<?> resolveClass(String className) {
  try {
    return Resources.classForName(className);
  } catch (ClassNotFoundException e) {
    return null;
  }
}
 
Example 17
Source File: XMLConfigBuilder.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private void typeAliasesElement(XNode parent) {
  if (parent != null) {
    for (XNode child : parent.getChildren()) {
      if ("package".equals(child.getName())) {
        //如果是package
        String typeAliasPackage = child.getStringAttribute("name");
        //(一)调用TypeAliasRegistry.registerAliases,去包下找所有类,然后注册别名(有@Alias注解则用,没有则取类的simpleName)
        configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
      } else {
        //如果是typeAlias
        String alias = child.getStringAttribute("alias");
        String type = child.getStringAttribute("type");
        try {
          Class<?> clazz = Resources.classForName(type);
          //根据Class名字来注册类型别名
          //(二)调用TypeAliasRegistry.registerAlias
          if (alias == null) {
            //alias可以省略
            typeAliasRegistry.registerAlias(clazz);
          } else {
            typeAliasRegistry.registerAlias(alias, clazz);
          }
        } catch (ClassNotFoundException e) {
          throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e);
        }
      }
    }
  }
}
 
Example 18
Source File: MapperScanApplication.java    From mumu with Apache License 2.0 5 votes vote down vote up
/**
 * 加载别名
 * @param configuration
 */
private void handleTypeAlias(Configuration configuration, XNode root) {
	log.info("load alias message...........................");
	TypeAliasRegistry typeAliasRegistry = configuration.getTypeAliasRegistry();
	XNode parent = root.evalNode("typeAliases");
	if(parent!=null){
		for (XNode child : parent.getChildren()) {
			if ("package".equals(child.getName())) {
				String typeAliasPackage = child.getStringAttribute("name");
				configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
				log.info("package:"+typeAliasPackage);
			} else {
				String alias = child.getStringAttribute("alias");
				String type = child.getStringAttribute("type");
				try {
					Class<?> clazz = Resources.classForName(type);
					if (alias == null) {
						typeAliasRegistry.registerAlias(clazz);
					} else {
						typeAliasRegistry.registerAlias(alias, clazz);
					}
					log.info("alias:"+alias+"   type:"+clazz);
				} catch (ClassNotFoundException e) {
					throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e);
				}
			}
		}
	}
}
 
Example 19
Source File: SerializedCache.java    From mybatis with Apache License 2.0 4 votes vote down vote up
@Override
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
  return Resources.classForName(desc.getName());
}
 
Example 20
Source File: SerializedCache.java    From mybaties with Apache License 2.0 4 votes vote down vote up
@Override
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
  return Resources.classForName(desc.getName());
}