KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > uml > transformation > ast > CCMPropertyUtils


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Pierre Carpentier.
23 Contributor(s): Philippe Merle.
24
25 ---------------------------------------------------------------------
26 $Id: CCMPropertyUtils.java,v 1.1 2004/05/26 11:25:35 carpentier Exp $
27 ====================================================================*/

28
29 package org.objectweb.openccm.uml.transformation.ast;
30
31 import ispuml.mdaTransformation.utils.PropertyUtils;
32
33 import java.util.Collection JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Arrays JavaDoc;
38 import java.lang.reflect.InvocationTargetException JavaDoc;
39
40 /**
41  * Extended version of ispuml.modelexplorer.utils.PropertyUtils.
42  * This version supports List defined in IDL file (AST).
43  * @author Pierre Carpentier
44  */

45 public class CCMPropertyUtils extends PropertyUtils {
46
47     /**
48      * Set the value of the specified property of the specified bean,
49      * no matter which property reference format is used, with no
50      * type conversions.
51      *
52      * @param bean Bean whose property is to be modified
53      * @param name Possibly indexed and/or nested name of the property
54      * to be modified
55      * @param value Value to which this property is to be set
56      *
57      * @exception IllegalAccessException if the caller does not have
58      * access to the property accessor method
59      * @exception IllegalArgumentException if <code>bean</code> or
60      * <code>name</code> is null
61      * @exception InvocationTargetException if the property accessor method
62      * throws an exception
63      * @exception NoSuchMethodException if an accessor method for this
64      * propety cannot be found
65      */

66     public static void setProperty(Object JavaDoc bean, String JavaDoc name, Object JavaDoc value)
67         throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc, NoSuchMethodException JavaDoc {
68         if (isPropertyMultiple(name)) {
69             setCollectionProperty(bean, name, value);
70             return;
71         }
72         setNestedProperty(bean, name, value);
73     }
74
75     /**
76      * Set the value of the specified property of the specified bean,
77      * no matter which property reference format is used, with no
78      * type conversions.
79      *
80      * @param bean Bean whose property is to be modified
81      * @param name Possibly indexed and/or nested name of the property
82      * to be modified
83      * @param value Value to which this property is to be set
84      *
85      * @exception IllegalAccessException if the caller does not have
86      * access to the property accessor method
87      * @exception IllegalArgumentException if <code>bean</code> or
88      * <code>name</code> is null
89      * @exception InvocationTargetException if the property accessor method
90      * throws an exception
91      * @exception NoSuchMethodException if an accessor method for this
92      * propety cannot be found
93      */

94     public static void setCollectionProperty(Object JavaDoc bean, String JavaDoc name, Object JavaDoc value)
95         throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc, NoSuchMethodException JavaDoc {
96         if (bean == null) {
97             throw new IllegalArgumentException JavaDoc("No bean specified");
98         }
99         if (name == null) {
100             throw new IllegalArgumentException JavaDoc("No name specified");
101         }
102
103         // Cut the name in two parts: the one before the first multiple key, and
104
// the one after the first multiple key: part1[*].part2
105
int multipleIndex = name.indexOf(MULTIPLE_KEY);
106         if (multipleIndex < 0) {
107             throw new IllegalArgumentException JavaDoc("No multivalue property specified (by use of [" + MULTIPLE_KEY + "] in '" + name + "'.");
108         }
109         int lastDelim = name.substring(0, multipleIndex).lastIndexOf(INDEXED_DELIM);
110         String JavaDoc startName = name.substring(0, lastDelim);
111         int closeDelimIndex = name.indexOf(INDEXED_DELIM2, multipleIndex);
112         if (closeDelimIndex < 0) {
113             throw new IllegalArgumentException JavaDoc("Missing closing ']' in '" + name + "'");
114         }
115         int nextNameIndex = name.indexOf(NESTED_DELIM, closeDelimIndex);
116         if (nextNameIndex >= 0) {
117             throw new UnsupportedOperationException JavaDoc("Multiple multivalue not supported yet.");
118         }
119
120         // Translate the provided value into a l=List if necessary
121
List JavaDoc list;
122         if (value.getClass().isArray()) {
123             list = Arrays.asList((Object JavaDoc[]) value);
124         } else if (value instanceof List JavaDoc) {
125             list = (List JavaDoc) value;
126         } else if (value instanceof Collection JavaDoc) {
127             list = new ArrayList JavaDoc((Collection JavaDoc) value);
128         } else {
129             list = new ArrayList JavaDoc(1);
130             list.add(value);
131         }
132
133         // Now set the requested bean property.
134
// First, check the bean property type, and set it according
135
// to this type
136
Class JavaDoc type = getPropertyType(bean, startName);
137
138         java.lang.reflect.Method JavaDoc method;
139
140         // Check bean type
141
if (Collection JavaDoc.class.isAssignableFrom(type)) {
142             Collection JavaDoc targetCollection = (Collection JavaDoc) getProperty(bean, startName);
143             Iterator JavaDoc iter = list.iterator();
144             while (iter.hasNext()) {
145                 Object JavaDoc curBean = iter.next();
146                 targetCollection.add(curBean);
147             } // end loop
148
} else if ((method = getAddMethod(list, type)) != null) {
149             // Checks if the type have a 'add' method with a parameter type
150
// got from the source list
151
Iterator JavaDoc iter = list.iterator();
152             while (iter.hasNext()) {
153                 Object JavaDoc target = getProperty(bean, startName);
154                 Object JavaDoc curBean = iter.next();
155                 Object JavaDoc param[] = { curBean };
156                 method.invoke(target, param);
157             }
158         } else { // error !
159
throw new IllegalArgumentException JavaDoc("Name '" + startName + "' doesn't denote a multivalue property.");
160         }
161
162     }
163
164     /**
165      * Get the 'add' method of a list. This methods is compliant with the CORBA list (sequence).
166      * @param list The list which contains the 'add' method.
167      * @param type The type of the object to add to the list.
168      * @return The method to call to add the new object to the list.
169      */

170     private static java.lang.reflect.Method JavaDoc getAddMethod(List JavaDoc list, Class JavaDoc type) {
171         if (list.size() > 0) {
172             java.lang.reflect.Method JavaDoc methods [] = type.getMethods();
173             for (int i=0 ; i<methods.length ; i++) {
174                 if (methods[i].getName().equals("add")) {
175                     Class JavaDoc 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 JavaDoc[0]);
183             } catch (Exception JavaDoc exception) {
184                 return null;
185             }
186         }
187         return null;
188     }
189
190 }
Popular Tags