List of usage examples for java.lang ClassLoader loadClass
public Class<?> loadClass(String name) throws ClassNotFoundException
From source file:org.jsonschema2pojo.integration.EnumIT.java
@BeforeClass @SuppressWarnings("unchecked") public static void generateAndCompileEnum() throws ClassNotFoundException { ClassLoader resultsClassLoader = classSchemaRule.generateAndCompile( "/schema/enum/typeWithEnumProperty.json", "com.example", config("propertyWordDelimiters", "_")); parentClass = resultsClassLoader.loadClass("com.example.TypeWithEnumProperty"); enumClass = (Class<Enum>) resultsClassLoader.loadClass("com.example.TypeWithEnumProperty$EnumProperty"); }
From source file:com.amalto.core.storage.hibernate.FlatTypeMapping.java
@SuppressWarnings("rawtypes") private static Serializable createCompositeId(ClassLoader classLoader, Class<?> clazz, List<Object> compositeIdValues) { try {/*from w w w .j a v a2s. co m*/ Class<?> idClass = classLoader.loadClass(clazz.getName() + "_ID"); //$NON-NLS-1$ Class[] parameterClasses = new Class[compositeIdValues.size()]; int i = 0; for (Object o : compositeIdValues) { parameterClasses[i++] = o.getClass(); } Constructor<?> constructor = idClass.getConstructor(parameterClasses); return (Serializable) constructor.newInstance(compositeIdValues.toArray()); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:corner.orm.gae.GaeModule.java
public static void contributeValueEncoderSource(MappedConfiguration<Class, ValueEncoderFactory> configuration, final TypeCoercer typeCoercer, final PropertyAccess propertyAccess, final LoggerSource loggerSource, final EntityPackageManager packageManager, final ClassNameLocator classNameLocator, @Local final EntityManager entityManager) { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); for (String packageName : packageManager.getPackageNames()) { for (String className : classNameLocator.locateClassNames(packageName)) { try { final Class<?> entityClass = contextClassLoader.loadClass(className); if (entityClass.isAnnotationPresent(Entity.class)) { ValueEncoderFactory factory = new ValueEncoderFactory() { /** * @see org.apache.tapestry5.services.ValueEncoderFactory#create(java.lang.Class) *//*from w w w. j av a 2 s. com*/ @Override public ValueEncoder create(Class type) { return new JpaEntityValueEncoder(entityClass, propertyAccess, typeCoercer, loggerSource.getLogger(entityClass), entityManager); } }; configuration.add(entityClass, factory); } } catch (ClassNotFoundException ex) { throw new RuntimeException(ex); } } } }
From source file:com.mani.cucumber.ReflectionUtils.java
public static <T> Class<T> loadClass(ClassLoader classloader, String name) { try {/*from www. j av a2 s .c om*/ @SuppressWarnings("unchecked") Class<T> loaded = (Class<T>) classloader.loadClass(name); return loaded; } catch (ClassNotFoundException exception) { throw new IllegalStateException("Cannot find class " + name, exception); } }
From source file:com.smart.utils.ReflectionUtils.java
/** * beanClass?Annotation// w ww . ja v a 2 s .c o m * * @return */ public static boolean isAnnotationPresent(String beanClass, Class annotation) { ClassLoader classLoader = annotation.getClassLoader(); Class clz = null; try { clz = classLoader.loadClass(beanClass); } catch (ClassNotFoundException e) { logger.warn("" + beanClass); return false; } return clz.isAnnotationPresent(annotation); }
From source file:android.reflect.ClazzLoader.java
/** * // w ww . j a va2 s .c om */ public static <Z> Class<Z> forName(String className) { Class<Z> clazz = null; if (Assert.notEmpty(className)) { try { Class<?> tempClazz = null; ClassLoader classLoader = ClazzLoader.class.getClassLoader(); if (classLoader != null) { tempClazz = classLoader.loadClass(className); } else { tempClazz = Class.forName(className); } if (tempClazz != null) { clazz = (Class<Z>) tempClazz; } } catch (Throwable t) { Log.e(TAG, t); } } return clazz; }
From source file:com.norconex.commons.lang.ClassFinder.java
private static String resolveName(ClassLoader loader, String rawName, Class<?> superClass) { String className = rawName;/*from w ww .j av a 2 s . c o m*/ if (!rawName.endsWith(".class") || className.contains("$")) { return null; } className = className.replaceAll("[\\\\/]", "."); className = StringUtils.removeStart(className, "."); className = StringUtils.removeEnd(className, ".class"); try { Class<?> clazz = loader.loadClass(className); // load only concrete implementations if (!clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers()) && superClass.isAssignableFrom(clazz)) { return clazz.getName(); } } catch (ClassNotFoundException e) { LOG.error("Invalid class: " + className, e); } catch (NoClassDefFoundError e) { LOG.debug("Invalid class: " + className, e); } return null; }
From source file:org.jberet.support.io.MappingJsonFactoryObjectFactory.java
static void configureCustomSerializersAndDeserializers(final ObjectMapper objectMapper, final String customSerializers, final String customDeserializers, final String customDataTypeModules, final ClassLoader classLoader) throws Exception { if (customDeserializers != null || customSerializers != null) { final SimpleModule simpleModule = new SimpleModule("custom-serializer-deserializer-module"); if (customSerializers != null) { final StringTokenizer st = new StringTokenizer(customSerializers, ", "); while (st.hasMoreTokens()) { final Class<?> aClass = classLoader.loadClass(st.nextToken()); simpleModule.addSerializer(aClass, (JsonSerializer) aClass.newInstance()); }/* w ww .jav a 2 s. c o m*/ } if (customDeserializers != null) { final StringTokenizer st = new StringTokenizer(customDeserializers, ", "); while (st.hasMoreTokens()) { final Class<?> aClass = classLoader.loadClass(st.nextToken()); simpleModule.addDeserializer(aClass, (JsonDeserializer) aClass.newInstance()); } } objectMapper.registerModule(simpleModule); } if (customDataTypeModules != null) { final StringTokenizer st = new StringTokenizer(customDataTypeModules, ", "); while (st.hasMoreTokens()) { final Class<?> aClass = classLoader.loadClass(st.nextToken()); objectMapper.registerModule((Module) aClass.newInstance()); } } }
From source file:azkaban.utils.Utils.java
public static Object invokeStaticMethod(ClassLoader loader, String className, String methodName, Object... args) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class<?> clazz = loader.loadClass(className); Class<?>[] argTypes = new Class[args.length]; for (int i = 0; i < args.length; ++i) { // argTypes[i] = args[i].getClass(); argTypes[i] = args[i].getClass(); }/*from w w w . ja v a 2 s . c o m*/ Method method = clazz.getDeclaredMethod(methodName, argTypes); return method.invoke(null, args); }
From source file:org.paxml.util.ReflectUtils.java
/** * Load class from class name, swallowing possible ClassNotFoundException. * // w w w .j av a 2 s. c om * @param clazz * the class name * @param cl * the classloader, set to null to use the current thread context * class loader. * @return the loaded class, or null if class not found. */ public static Class loadClass(String clazz, ClassLoader cl) { cl = cl == null ? Thread.currentThread().getContextClassLoader() : cl; try { return cl.loadClass(clazz); } catch (ClassNotFoundException e) { return null; } }