public void testGetSql() throws Exception { BeanDesc beanDesc = BeanDescFactory.getBeanDesc(getDaoClass("AnnotationTestDaoImpl")); DaoAnnotationReader reader = readerFactory.createDaoAnnotationReader(beanDesc); Method method1 = beanDesc.getMethods("subclassMethod2")[0]; String sql = reader.getSQL(method1, "mysql"); assertEquals("1", "SELECT * FROM emp", sql); }
/** * COOL deployかどうか返します。 * * @param container * @return COOL deployかどうか */ public static boolean isCooldeployMode(S2Container container) { BeanDesc bd = BeanDescFactory.getBeanDesc(S2ContainerFactory.class); S2ContainerFactory.Provider provider = (Provider) FieldUtil.get(bd.getField("provider"), null); if (provider instanceof S2ContainerFactoryCoolProvider) { return true; } return false; }
public String getPath(Method method) { Class clazz = method.getDeclaringClass(); BeanDesc beanDesc = BeanDescFactory.getBeanDesc(clazz); String fieldName = method.getName() + BINDING_METHOD_SUFFIX; if (beanDesc.hasField(fieldName)) { String value = (String) beanDesc.getFieldValue(fieldName, null); return value.replaceFirst("path\\s*=\\s*", ""); } return null; }
/** @throws Exception */ public void testBindWarning() throws Exception { BeanDesc beanDesc = BeanDescFactory.getBeanDesc(A.class); PropertyDesc propDesc = beanDesc.getPropertyDesc("hoge"); S2Container container = new S2ContainerImpl(); ComponentDefImpl cd = new ComponentDefImpl(A.class); PropertyDef propDef = new PropertyDefImpl("hoge"); cd.addPropertyDef(propDef); container.register(cd); A a = new A(); BindingTypeDefFactory.MAY.bind(cd, propDef, propDesc, a); }
public ActionPropertyConfig createActionPropertyConfig( BeanDesc beanDesc, PropertyDesc propertyDesc) { String fieldName = propertyDesc.getPropertyName() + EXPORT_SUFFIX; if (!beanDesc.hasField(fieldName)) { return new ActionPropertyConfigImpl(); } if (!ConstantAnnotationUtil.isConstantAnnotationStringField(beanDesc.getField(fieldName))) { return new ActionPropertyConfigImpl(); } String value = (String) beanDesc.getFieldValue(fieldName, null); return new ActionPropertyConfigImpl(value); }
/** * アノテーションの要素を名前と値の{@link Map}として返します。 * * @param annotation アノテーション * @return アノテーションの要素の名前と値からなる{@link Map} */ public static Map<String, Object> getProperties(Annotation annotation) { Map<String, Object> map = new HashMap<String, Object>(); BeanDesc beanDesc = BeanDescFactory.getBeanDesc(annotation.annotationType()); String[] names = beanDesc.getMethodNames(); for (int i = 0; i < names.length; i++) { String name = names[i]; Object v = getProperty(beanDesc, annotation, name); if (v != null) { map.put(name, v); } } return map; }
/** * JavaBeansの値をマップにコピーします。 * * @param src ソース * @param dest あて先 */ public static void copyProperties(Object src, Map dest) { if (src == null || dest == null) { return; } final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(src.getClass()); final int size = beanDesc.getPropertyDescSize(); for (int i = 0; i < size; ++i) { final PropertyDesc pd = beanDesc.getPropertyDesc(i); if (pd.isReadable() && pd.isWritable()) { final Object value = pd.getValue(src); dest.put(pd.getPropertyName(), value); } } }
public void testGetQuery() throws Exception { BeanDesc beanDesc = BeanDescFactory.getBeanDesc(getDaoClass("AnnotationTestDaoImpl")); DaoAnnotationReader reader = readerFactory.createDaoAnnotationReader(beanDesc); Method method1 = beanDesc.getMethods("withQueryAnnotaion")[0]; String queryq = reader.getQuery(method1); assertEquals("1", "arg1 = /*arg1*/'dummy'", queryq); // return null if QUERY annotation not found Method method2 = beanDesc.getMethods("withNoAnnotaion")[0]; String query2 = reader.getQuery(method2); assertNull("1", query2); // annotationReader must read subclass annotation Method method3 = beanDesc.getMethods("subclassMethod")[0]; String[] names3 = reader.getArgNames(method3); assertEquals("3", 1, names3.length); }
/** * マップの値をJavaBeansにコピーします。 * * @param src ソース * @param dest あて先 */ public static void copyProperties(Map src, Object dest) { if (src == null || dest == null) { return; } BeanDesc beanDesc = BeanDescFactory.getBeanDesc(dest.getClass()); for (Iterator i = src.keySet().iterator(); i.hasNext(); ) { String key = (String) i.next(); if (!beanDesc.hasPropertyDesc(key)) { continue; } PropertyDesc pd = beanDesc.getPropertyDesc(key); if (pd.isWritable()) { pd.setValue(dest, src.get(key)); } } }
public void testGetArgNames() throws Exception { BeanDesc beanDesc = BeanDescFactory.getBeanDesc(getDaoClass("AnnotationTestDaoImpl")); DaoAnnotationReader reader = readerFactory.createDaoAnnotationReader(beanDesc); assertEquals("1", Employee.class, reader.getBeanClass()); Method method = beanDesc.getMethods("withArgumentAnnotaion")[0]; String[] names = reader.getArgNames(method); assertEquals("2", 2, names.length); assertEquals("2", "arg1", names[0]); assertEquals("2", "arg2", names[1]); // getArgNames return 0 length array if args annotation is not // specified. Method method2 = beanDesc.getMethods("withNoAnnotaion")[0]; String[] names2 = reader.getArgNames(method2); assertEquals("3", 0, names2.length); // annotationReader must read subclass annotation Method method3 = beanDesc.getMethods("subclassMethod")[0]; String[] names3 = reader.getArgNames(method3); assertEquals("3", 1, names3.length); }
public void testGetNoPersistentProps() throws Exception { BeanDesc beanDesc = BeanDescFactory.getBeanDesc(getDaoClass("AnnotationTestDaoImpl")); DaoAnnotationReader reader = readerFactory.createDaoAnnotationReader(beanDesc); Method method1 = beanDesc.getMethods("withNoPersistentProps")[0]; String[] props1 = reader.getNoPersistentProps(method1); assertEquals("1", 2, props1.length); assertEquals("1", "prop1", props1[0]); assertEquals("1", "prop2", props1[1]); // return null if QUERY annotation not found Method method2 = beanDesc.getMethods("withNoAnnotaion")[0]; String[] props2 = reader.getNoPersistentProps(method2); assertNull("2", props2); // annotationReader must read subclass annotation Method method3 = beanDesc.getMethods("subclassMethod2")[0]; String[] props3 = reader.getNoPersistentProps(method3); assertEquals("1", 2, props3.length); assertEquals("1", "prop1", props3[0]); assertEquals("1", "prop2", props3[1]); }
/** * アノテーションの要素の値を返します。 * * @param beanDesc アノテーションを表す{@link BeanDesc} * @param annotation アノテーション * @param name 要素の名前 * @return アノテーションの要素の値 */ public static Object getProperty(BeanDesc beanDesc, Annotation annotation, String name) { Method m = beanDesc.getMethodNoException(name); if (m == null) { return null; } Object v = MethodUtil.invoke(m, annotation, null); if (v != null && !"".equals(v)) { return v; } return null; }
/** * パラメータを準備します。 * * @param parameter パラメータ */ protected void prepareParameter(T parameter) { sqlContext = new SqlContextImpl(); if (parameter != null) { Class<?> clazz = parameter.getClass(); if (ValueTypes.isSimpleType(clazz) || TemporalParameter.class == clazz || LobParameter.class == clazz) { sqlContext.addArg("$1", parameter, clazz); } else if (parameter instanceof Map) { @SuppressWarnings("unchecked") Map<Object, Object> paramMap = (Map<Object, Object>) parameter; Set<Entry<Object, Object>> entrySet = paramMap.entrySet(); for (Map.Entry<Object, Object> entry : entrySet) { Object value = entry.getValue(); Object key = entry.getKey(); if (key == null || !(key instanceof CharSequence)) { continue; } String name = ((CharSequence) key).toString(); sqlContext.addArg(name, value, (value == null ? String.class : value.getClass())); } } else { BeanDesc beanDesc = BeanDescFactory.getBeanDesc(clazz); for (int i = 0; i < beanDesc.getPropertyDescSize(); i++) { PropertyDesc pd = beanDesc.getPropertyDesc(i); if (!pd.isReadable()) { continue; } Object value = Parameter.wrapIfNecessary(pd, pd.getValue(parameter)); sqlContext.addArg(pd.getPropertyName(), value, pd.getPropertyType()); } } } node.accept(sqlContext); Object[] vars = sqlContext.getBindVariables(); Class<?>[] types = sqlContext.getBindVariableTypes(); int size = vars.length; for (int i = 0; i < size; i++) { addParam(vars[i], types[i]); } }
// ----------------------------------------------------- // Validation // ---------- protected void doSetupValidationConfig( S2ExecuteConfig executeConfig, S2ActionMapping actionMapping, Class<?> actionClass, Method method, Execute execute, String input) { final List<S2ValidationConfig> validationConfigs = new ArrayList<S2ValidationConfig>(); final String validate = execute.validate(); boolean validator = false; if (!StringUtil.isEmpty(validate)) { final BeanDesc actionBeanDesc = actionMapping.getActionBeanDesc(); final BeanDesc actionFormBeanDesc = actionMapping.getActionFormBeanDesc(); for (String name : StringUtil.split(validate, ", ")) { if (VALIDATOR.equals(name)) { if (!execute.validator()) { throw new UnmatchValidatorAndValidateRuntimeException(actionClass, method.getName()); } validationConfigs.add(createValidationConfig()); validator = true; } else if (actionFormBeanDesc.hasMethod(name)) { final Method validateMethod = actionFormBeanDesc.getMethod(name); checkValidateMethod(actionClass, validateMethod); validationConfigs.add(createValidationConfig(validateMethod)); } else { final Method validateMethod = actionBeanDesc.getMethod(name); checkValidateMethod(actionClass, validateMethod); validationConfigs.add(createValidationConfig(validateMethod)); } } } if (!validator && execute.validator()) { validationConfigs.add(0, createValidationConfig()); } if (!validationConfigs.isEmpty() && input == null) { throw new IllegalValidatorOfExecuteMethodRuntimeException(actionClass, method.getName()); } executeConfig.setValidationConfigs(validationConfigs); }
/** * JavaBeansの値からマップを作成します。 * * @param src ソース * @param prefix プレフィックス * @return JavaBeansの値を持つマップ */ public static Map createProperties(Object src, String prefix) { Map map = new HashMap(); if (src == null) { return map; } final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(src.getClass()); final int size = beanDesc.getPropertyDescSize(); for (int i = 0; i < size; ++i) { final PropertyDesc pd = beanDesc.getPropertyDesc(i); if (pd.isReadable()) { if (prefix == null) { final Object value = pd.getValue(src); String name = pd.getPropertyName().replace('$', '.'); map.put(name, value); } else if (pd.getPropertyName().startsWith(prefix)) { final Object value = pd.getValue(src); String name = pd.getPropertyName().substring(prefix.length()).replace('$', '.'); map.put(name, value); } } } return map; }
/** * JavaBeansの値をJavaBeansにコピーします。 * * @param src ソース * @param dest あて先 * @param includeNull <code>null</code>を含めるかどうか */ public static void copyProperties( final Object src, final Object dest, final boolean includeNull) { final BeanDesc srcBeanDesc = BeanDescFactory.getBeanDesc(src.getClass()); final BeanDesc destBeanDesc = BeanDescFactory.getBeanDesc(dest.getClass()); final int propertyDescSize = destBeanDesc.getPropertyDescSize(); for (int i = 0; i < propertyDescSize; i++) { final PropertyDesc destPropertyDesc = destBeanDesc.getPropertyDesc(i); final String propertyName = destPropertyDesc.getPropertyName(); if (srcBeanDesc.hasPropertyDesc(propertyName)) { final PropertyDesc srcPropertyDesc = srcBeanDesc.getPropertyDesc(propertyName); if (destPropertyDesc.isWritable() && srcPropertyDesc.isReadable()) { final Object value = srcPropertyDesc.getValue(src); if (includeNull || value != null) { destPropertyDesc.setValue(dest, srcPropertyDesc.getValue(src)); } } } } }
public Object getValue() { Object value = super.getValue(); if (value instanceof SelectItem[] || value instanceof SelectItem) { return value; } List list = new ArrayList(); boolean nullLabelSet = false; if (nullLabelRequired) { SelectItem si = new SelectItem(); si.setValue(""); final FacesContext context = getFacesContext(); FacesMessage mes = FacesMessageUtil.getMessage(context, NULL_LABEL_MESSAGE_CODE, null); si.setLabel(mes.getSummary()); list.add(si); nullLabelSet = true; } if (value != null && value.getClass().isArray()) { value = Arrays.asList((Object[]) value); } if (value instanceof Collection) { final Collection valueCollection = (Collection) value; if (!nullLabelSet && valueCollection.size() == 0) { list.add(BLANK_SELECT_ITEM); } else { for (Iterator it = valueCollection.iterator(); it.hasNext(); ) { Object item = it.next(); if (item instanceof SelectItem) { list.add(item); } else if (item instanceof Map) { Map map = (Map) item; SelectItem si = new SelectItem(); Object itemValueValue = map.get(itemValue); if (itemValueValue != null) { si.setValue(itemValueValue); } Object itemLabelValue = map.get(itemLabel); if (itemLabelValue == null) { itemLabelValue = itemValueValue; } if (itemLabelValue != null) { si.setLabel(itemLabelValue.toString()); } list.add(si); } else { SelectItem si = new SelectItem(); BeanDesc bd = BeanDescFactory.getBeanDesc(item.getClass()); PropertyDesc pd = bd.getPropertyDesc(itemValue); Object itemValueValue = (pd.isReadable()) ? pd.getValue(item) : null; if (itemValueValue != null) { si.setValue(itemValueValue); } Object itemLabelValue = null; if (bd.hasPropertyDesc(itemLabel)) { pd = bd.getPropertyDesc(itemLabel); itemLabelValue = (pd.isReadable()) ? pd.getValue(item) : null; } if (itemLabelValue == null) { itemLabelValue = itemValueValue; } if (itemLabelValue != null) { si.setLabel(itemLabelValue.toString()); } list.add(si); } } } } else if (value instanceof Map) { final Map map = (Map) value; if (!nullLabelSet && map.size() == 0) { list.add(BLANK_SELECT_ITEM); } else { for (Iterator it = map.keySet().iterator(); it.hasNext(); ) { Object key = it.next(); Object val = map.get(key); SelectItem si = new SelectItem(); if (key != null) { si.setLabel(key.toString()); } if (val != null) { si.setValue(val); } list.add(si); } } } else { if (!nullLabelSet && value == null) { list.add(BLANK_SELECT_ITEM); } } return list; }