KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > Conventions


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.core;
18
19 import java.io.Externalizable JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.lang.reflect.Proxy JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import org.springframework.util.Assert;
28 import org.springframework.util.ClassUtils;
29 import org.springframework.util.StringUtils;
30
31 /**
32  * Provides methods to support various naming and other conventions used
33  * throughout the framework. Mainly for internal use within the framework.
34  *
35  * @author Rob Harrop
36  * @author Juergen Hoeller
37  * @since 2.0
38  */

39 public abstract class Conventions {
40
41     /**
42      * Suffix added to names when using arrays.
43      */

44     private static final String JavaDoc PLURAL_SUFFIX = "List";
45
46
47     /**
48      * Set of interfaces that are supposed to be ignored
49      * when searching for the 'primary' interface of a proxy.
50      */

51     private static final Set JavaDoc ignoredInterfaces = new HashSet JavaDoc();
52
53     static {
54         ignoredInterfaces.add(Serializable JavaDoc.class);
55         ignoredInterfaces.add(Externalizable JavaDoc.class);
56         ignoredInterfaces.add(Cloneable JavaDoc.class);
57         ignoredInterfaces.add(Comparable JavaDoc.class);
58     }
59
60
61     /**
62      * Determine the conventional variable name for the supplied
63      * code>Object</code> based on its concrete type. The convention
64      * used is to return the uncapitalized short name of the <code>Class</code>.
65      * So, <code>com.myapp.Product</code> becomes <code>product</code>.
66      * <p>For arrays, we use the pluralized version of the array component type.
67      * For <code>Collection</code>s we attempt to 'peek ahead' in the
68      * <code>Collection</code> to determine the component type and
69      * return the pluralized version of that component type.
70      */

71     public static String JavaDoc getVariableName(Object JavaDoc value) {
72         Assert.notNull(value, "Value must not be null");
73         Class JavaDoc valueClass = null;
74         boolean pluralize = false;
75
76         if (value.getClass().isArray()) {
77             valueClass = value.getClass().getComponentType();
78             pluralize = true;
79         }
80         else if (value instanceof Collection JavaDoc) {
81             Collection JavaDoc collection = (Collection JavaDoc) value;
82             if (collection.isEmpty()) {
83                 throw new IllegalArgumentException JavaDoc("Cannot generate variable name for an empty Collection");
84             }
85             Object JavaDoc valueToCheck = peekAhead(collection);
86             valueClass = getClassForValue(valueToCheck);
87             pluralize = true;
88         }
89         else {
90             valueClass = getClassForValue(value);
91         }
92
93         String JavaDoc name = StringUtils.uncapitalize(getShortName(valueClass));
94         return (pluralize ? pluralize(name) : name);
95     }
96
97     /**
98      * Convert <code>String</code>s in attribute name format (lowercase, hyphens separating words)
99      * into property name format (camel-cased). For example, <code>transaction-manager</code> is
100      * converted into <code>transactionManager</code>.
101      */

102     public static String JavaDoc attributeNameToPropertyName(String JavaDoc attributeName) {
103         Assert.notNull(attributeName, "'attributeName' must not be null");
104         if (attributeName.indexOf("-") == -1) {
105             return attributeName;
106         }
107         char[] chars = attributeName.toCharArray();
108         char[] result = new char[chars.length -1]; // not completely accurate but good guess
109
int currPos = 0;
110         boolean upperCaseNext = false;
111         for (int i = 0; i < chars.length; i++) {
112             char c = chars[i];
113             if (c == '-') {
114                 upperCaseNext = true;
115             }
116             else if (upperCaseNext) {
117                 result[currPos++] = Character.toUpperCase(c);
118                 upperCaseNext = false;
119             }
120             else {
121                 result[currPos++] = c;
122             }
123         }
124         return new String JavaDoc(result, 0, currPos);
125     }
126
127     /**
128      * Determine the class to use for naming a variable that contains
129      * the given value.
130      * <p>Will return the class of the given value, except when
131      * encountering a JDK proxy, in which case it will determine
132      * the 'primary' interface implemented by that proxy.
133      * @param value the value to check
134      * @return the class to use for naming a variable
135      */

136     private static Class JavaDoc getClassForValue(Object JavaDoc value) {
137         if (Proxy.isProxyClass(value.getClass())) {
138             Class JavaDoc[] ifcs = value.getClass().getInterfaces();
139             for (int i = 0; i < ifcs.length; i++) {
140                 Class JavaDoc ifc = ifcs[i];
141                 if (!ignoredInterfaces.contains(ifc)) {
142                     return ifc;
143                 }
144             }
145         }
146         return value.getClass();
147     }
148
149     /**
150      * Determine the short name of the given class: without package qualification,
151      * and without the outer class name in case of an inner class.
152      * @param valueClass the class to determine a name for
153      * @return the short name
154      */

155     private static String JavaDoc getShortName(Class JavaDoc valueClass) {
156         String JavaDoc shortName = ClassUtils.getShortName(valueClass);
157         int dotIndex = shortName.lastIndexOf('.');
158         return (dotIndex != -1 ? shortName.substring(dotIndex + 1) : shortName);
159     }
160
161     /**
162      * Pluralize the given name.
163      */

164     private static String JavaDoc pluralize(String JavaDoc name) {
165         return name + PLURAL_SUFFIX;
166     }
167
168     /**
169      * Retrieve the <code>Class</code> of an element in the <code>Collection</code>.
170      * The exact element for which the <code>Class</code> is retreived will depend
171      * on the concrete <code>Collection</code> implementation.
172      */

173     private static Object JavaDoc peekAhead(Collection JavaDoc collection) {
174         Iterator JavaDoc it = collection.iterator();
175         if (!it.hasNext()) {
176             throw new IllegalStateException JavaDoc(
177                     "Unable to peek ahead in non-empty collection - no element found");
178         }
179         Object JavaDoc value = it.next();
180         if (value == null) {
181             throw new IllegalStateException JavaDoc(
182                     "Unable to peek ahead in non-empty collection - only null element found");
183         }
184         return value;
185     }
186
187
188     /**
189      * Return an attribute name qualified by the supplied enclosing {@link Class}. For example,
190      * the attribute name '<code>foo</code>' qualified by {@link Class} '<code>com.myapp.SomeClass</code>'
191      * would be '<code>com.myapp.SomeClass.foo</code>'
192      */

193     public static String JavaDoc getQualifiedAttributeName(Class JavaDoc enclosingClass, String JavaDoc attributeName) {
194         Assert.notNull(enclosingClass, "'enclosingClass' must not be null");
195         Assert.notNull(attributeName, "'attributeName' must not be null");
196         return enclosingClass.getName() + "." + attributeName;
197     }
198
199 }
200
Popular Tags