1 28 29 package org.objectweb.openccm.uml.transformation.ast; 30 31 import ispuml.mdaTransformation.utils.PropertyUtils; 32 33 import java.util.Collection ; 34 import java.util.List ; 35 import java.util.Iterator ; 36 import java.util.ArrayList ; 37 import java.util.Arrays ; 38 import java.lang.reflect.InvocationTargetException ; 39 40 45 public class CCMPropertyUtils extends PropertyUtils { 46 47 66 public static void setProperty(Object bean, String name, Object value) 67 throws IllegalAccessException , InvocationTargetException , NoSuchMethodException { 68 if (isPropertyMultiple(name)) { 69 setCollectionProperty(bean, name, value); 70 return; 71 } 72 setNestedProperty(bean, name, value); 73 } 74 75 94 public static void setCollectionProperty(Object bean, String name, Object value) 95 throws IllegalAccessException , InvocationTargetException , NoSuchMethodException { 96 if (bean == null) { 97 throw new IllegalArgumentException ("No bean specified"); 98 } 99 if (name == null) { 100 throw new IllegalArgumentException ("No name specified"); 101 } 102 103 int multipleIndex = name.indexOf(MULTIPLE_KEY); 106 if (multipleIndex < 0) { 107 throw new IllegalArgumentException ("No multivalue property specified (by use of [" + MULTIPLE_KEY + "] in '" + name + "'."); 108 } 109 int lastDelim = name.substring(0, multipleIndex).lastIndexOf(INDEXED_DELIM); 110 String startName = name.substring(0, lastDelim); 111 int closeDelimIndex = name.indexOf(INDEXED_DELIM2, multipleIndex); 112 if (closeDelimIndex < 0) { 113 throw new IllegalArgumentException ("Missing closing ']' in '" + name + "'"); 114 } 115 int nextNameIndex = name.indexOf(NESTED_DELIM, closeDelimIndex); 116 if (nextNameIndex >= 0) { 117 throw new UnsupportedOperationException ("Multiple multivalue not supported yet."); 118 } 119 120 List list; 122 if (value.getClass().isArray()) { 123 list = Arrays.asList((Object []) value); 124 } else if (value instanceof List ) { 125 list = (List ) value; 126 } else if (value instanceof Collection ) { 127 list = new ArrayList ((Collection ) value); 128 } else { 129 list = new ArrayList (1); 130 list.add(value); 131 } 132 133 Class type = getPropertyType(bean, startName); 137 138 java.lang.reflect.Method method; 139 140 if (Collection .class.isAssignableFrom(type)) { 142 Collection targetCollection = (Collection ) getProperty(bean, startName); 143 Iterator iter = list.iterator(); 144 while (iter.hasNext()) { 145 Object curBean = iter.next(); 146 targetCollection.add(curBean); 147 } } else if ((method = getAddMethod(list, type)) != null) { 149 Iterator iter = list.iterator(); 152 while (iter.hasNext()) { 153 Object target = getProperty(bean, startName); 154 Object curBean = iter.next(); 155 Object param[] = { curBean }; 156 method.invoke(target, param); 157 } 158 } else { throw new IllegalArgumentException ("Name '" + startName + "' doesn't denote a multivalue property."); 160 } 161 162 } 163 164 170 private static java.lang.reflect.Method getAddMethod(List list, Class type) { 171 if (list.size() > 0) { 172 java.lang.reflect.Method methods [] = type.getMethods(); 173 for (int i=0 ; i<methods.length ; i++) { 174 if (methods[i].getName().equals("add")) { 175 Class paramType [] = methods[i].getParameterTypes(); 176 if (paramType.length == 1 && paramType[0].isAssignableFrom(list.get(0).getClass())) 177 return methods[i]; 178 } 179 } 180 } else { 181 try { 182 return type.getMethod("add", new Class [0]); 183 } catch (Exception exception) { 184 return null; 185 } 186 } 187 return null; 188 } 189 190 } | Popular Tags |