public static Object newInstance(String clazzName, String value) { try { Class<?> type = forName(clazzName); Class<?> enclosing = type.getEnclosingClass(); if (enclosing == null) { Constructor<?> c = type.getConstructor(String.class); c.setAccessible(true); return type.cast(c.newInstance(value)); } Object o = enclosing.newInstance(); Constructor<?> c = type.getDeclaredConstructor(enclosing, String.class); c.setAccessible(true); return type.cast(c.newInstance(o, value)); } catch (Exception e) { throw new RuntimeException(e); } }
public ClassIntrospector(Class<?> clazz) { this.clazz = clazz; fields = findFields(clazz); }
/** * Get all superclasses and interfaces recursively. * * @param clazz The class to start the search with. * * @return List of all super classes and interfaces of {@code clazz}. The list contains the class itself! The empty * list is returned if {@code clazz} is {@code null}. */ public static List<Class<?>> computeClassHierarchy(Class<?> clazz) { List<Class<?>> classes = new ArrayList<Class<?>>(); computeClassHierarchy(clazz, classes); return classes; }
@SuppressWarnings("unchecked") private BeanInstance(T instance) { this.type = (Class<T>) instance.getClass(); this.instance = instance; List<Field> fields = findFields(type); fieldAnnotations = findFieldsAnnotations(fields); }
private void convertPropertyRefList(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRefList prop : source.getSchema().get(SchemaPropertyRefList.class)) { List<BeanId> references = source.getReference(prop.getName()); if (references == null) { continue; } Collection<Object> c = newCollection(forName(prop.getCollectionType())); for (BeanId beanId : references) { Bean b = beanId.getBean(); if (b != null) { Object beanInstance = instanceCache.get(beanId); if (beanInstance == null) { String type = b.getSchema().getType(); try { beanInstance = newInstance(forName(type)); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(b, beanInstance, new HashMap<String, Object>(), instanceCache); } c.add(beanInstance); } } values.put(prop.getFieldName(), c); } }
public void add(Class<?> clazz) { List<Class<?>> classes = Reflections.computeClassHierarchy(clazz); for (Class<?> aClass : classes) { List<Class<?>> enclosings = Reflections.computeEnclosingClasses(clazz); for (Class<?> enclosing : enclosings) { if (shouldAdd(enclosing)) { dependencies.add(enclosing); } } if (shouldAdd(aClass)) { dependencies.add(aClass); } dependencies.addAll(getTransitiveDependencies(aClass)); } }
public static <T> BeanInstance<T> create(Class<T> elementType) { try { return new BeanInstance<T>(newInstance(elementType)); } catch (Exception e) { throw new RuntimeException(e); } }
public static List<Class<?>> computeEnclosingClasses(Class<?> clazz) { List<Class<?>> classes = new ArrayList<Class<?>>(); computeEnclosingClasses(clazz, classes); return classes; }
private static Class<?> extractClass(Class<?> ownerClass, Type arg) { if (arg instanceof ParameterizedType) { return extractClass(ownerClass, ((ParameterizedType) arg).getRawType()); } else if (arg instanceof GenericArrayType) { throw new UnsupportedOperationException("GenericArray types are not supported."); } else if (arg instanceof TypeVariable) { throw new UnsupportedOperationException("GenericArray types are not supported."); } return (arg instanceof Class ? (Class<?>) arg : Object.class); }
private void convertPropertyRefMap(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRefMap prop : source.getSchema().get(SchemaPropertyRefMap.class)) { List<BeanId> beans = source.getReference(prop.getName()); if (beans == null) { continue; } Map<Object, Object> c = newMap(forName(prop.getMapType())); for (BeanId beanId : beans) { Bean b = beanId.getBean(); if (b != null) { Object beanInstance = instanceCache.get(beanId); if (beanInstance == null) { try { beanInstance = newInstance(forName(b.getSchema().getType())); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(b, beanInstance, new HashMap<String, Object>(), instanceCache); } c.put(beanId.getInstanceId(), beanInstance); } } values.put(prop.getFieldName(), c); } }
@Override public Object convert(Bean source, Class<? extends Object> specificType) { Object instance = null; try { instance = newInstance(specificType); } catch (Exception e) { throw new UnsupportedOperationException(e); } Map<String, Object> valuesToInject = new HashMap<String, Object>(); Map<BeanId, Object> instanceCache = new HashMap<BeanId, Object>(); return convert(source, instance, valuesToInject, instanceCache); }
private static void computeEnclosingClasses(Class<?> clazz, List<Class<?>> classes) { for (Class<?> current = clazz; current != null; current = current.getEnclosingClass()) { if (classes.contains(current)) { return; } classes.add(current); for (Class<?> currentInterface : current.getInterfaces()) { computeEnclosingClasses(currentInterface, classes); } } }
classes.add(extractClass(ownerClass, aType));
public Class<?> loadClass(String className) { ClassLoader org = Thread.currentThread().getContextClassLoader(); try { ClassLoader cl = loadJars(jar); Thread.currentThread().setContextClassLoader(cl); try { return forName(className); } catch (Exception e) { throw new IllegalArgumentException("Could not load stub [" + className + "] from jar [" + jar.getAbsolutePath() + "].", e); } } finally { Thread.currentThread().setContextClassLoader(org); } }
private void convertPropertyRef(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRef prop : source.getSchema().get(SchemaPropertyRef.class)) { BeanId id = source.getFirstReference(prop.getName()); if (id == null) { continue; } Bean ref = id.getBean(); if (ref == null) { continue; } Schema refSchema = ref.getSchema(); SchemaPropertyRef schemaRef = source.getSchema().get(SchemaPropertyRef.class, prop.getName()); Object beanInstance = instanceCache.get(id); if (beanInstance == null) { try { beanInstance = newInstance(forName(refSchema.getType())); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(ref, beanInstance, new HashMap<String, Object>(), instanceCache); } values.put(schemaRef.getFieldName(), beanInstance); } }
private static String getIdField(Class<?> clazz) { for (Field field : findFields(clazz)) { field.setAccessible(true); Annotation annotation = field.getAnnotation(Id.class); if (annotation != null) { return field.getName(); } } throw new RuntimeException("Class [" + clazz + "] does not decalare @Id."); }
public Object getDefaultValue() { if (defaultDeclaringInstance == null) { try { defaultDeclaringInstance = newInstance(field.getDeclaringClass()); } catch (InstantiationException e) { throw new UnsupportedOperationException("Cannot access default values " + "from fields of class which cannot be constructed.", e); } catch (IllegalAccessException e) { throw new UnsupportedOperationException("Cannot access default values " + "from fields of class which cannot be constructed.", e); } catch (InvocationTargetException e) { throw new UnsupportedOperationException("Cannot access default values " + "from fields of class which cannot be constructed.", e); } catch (NoSuchMethodException e) { throw new UnsupportedOperationException("Cannot access default values " + "from fields of class which cannot be constructed.", e); } } try { return field.get(defaultDeclaringInstance); } catch (IllegalArgumentException e) { throw new UnsupportedOperationException("Cannot access default values " + "from fields of instances which cannot be accessed.", e); } catch (IllegalAccessException e) { throw new UnsupportedOperationException("Cannot access default values " + "from fields of class which cannot be accessed.", e); } }
/** * Get all superclasses and interfaces recursively. * * @param clazz The class to start the search with. * @param classes List of classes to which to add all found super classes and interfaces. */ private static void computeClassHierarchy(Class<?> clazz, List<Class<?>> classes) { for (Class<?> current = clazz; current != null; current = current.getSuperclass()) { if (classes.contains(current)) { return; } classes.add(current); for (Class<?> currentInterface : current.getInterfaces()) { computeClassHierarchy(currentInterface, classes); } } }
private Database(String dbProvider, List<String> installDdl, List<String> uninstallDdl) { this.dbProvider = dbProvider; this.installDdl = installDdl; this.uninstallDdl = uninstallDdl; this.username = System.getProperty("user.name"); this.password = System.getProperty("user.name"); this.host = PROPS.get(DB_HOST_TOOLS4J_CONFIG_PROPERTY); this.tablespace = System.getProperty("user.name"); if (DERBY.equals(dbProvider)) { driver = DERBY_DRIVER; forName(driver); url = "jdbc:derby:memory:" + tablespace + ";create=true"; } else if (MYSQL.equals(dbProvider)) { driver = MYSQL_DRIVER; url = "jdbc:mysql://" + host + ":3306/" + tablespace; } else if (POSTGRESQL.equals(dbProvider)) { driver = POSTGRESQL_DRIVER; url = "jdbc:postgresql://" + host + ":5432/" + tablespace; } else { throw new UnsupportedOperationException("DB provider not supported [" + dbProvider + "]"); } }
private void inject(Object instance, Map<String, Object> values) { List<Field> fields = findFields(instance.getClass()); for (Field field : fields) { field.setAccessible(true); Object value = values.get(field.getName()); if (value == null) { continue; } try { field.set(instance, value); } catch (IllegalArgumentException e) { throw new UnsupportedOperationException(e); } catch (IllegalAccessException e) { throw new UnsupportedOperationException(e); } } }