Source Code Cross Referenced for ClassUtils.java in  » Net » Terracotta » com » tc » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Terracotta » com.tc.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003:         * notice. All rights reserved.
004:         */
005:        package com.tc.util;
006:
007:        import java.lang.reflect.Constructor;
008:        import java.lang.reflect.Field;
009:        import java.lang.reflect.Method;
010:        import java.text.ParseException;
011:
012:        /**
013:         * Class utility methods
014:         */
015:        public class ClassUtils {
016:
017:            private static final Class METHOD_CLASS = Method.class;
018:            private static final Class CONSTRUCTOR_CLASS = Constructor.class;
019:            private static final Class FIELD_CLASS = Field.class;
020:
021:            /**
022:             * Convert fully-qualified field name like "mypackage.MyClass.myField" into a specification which 
023:             * contains the fully-qualified class name and the field name.  
024:             * @param fieldName Fully-qualified field name 
025:             * @return Specification of class/field names
026:             * @throws ParseException If the fieldName is not properly formatted
027:             */
028:            public static ClassSpec parseFullyQualifiedFieldName(
029:                    String fieldName) throws ParseException {
030:                ClassSpecImpl rv = new ClassSpecImpl();
031:                rv.parseFullyQualifiedFieldName(fieldName);
032:                return rv;
033:            }
034:
035:            /**
036:             * Get the dimension of an array
037:             * @param arrayClass The array class
038:             * @return Dimension, >= 0
039:             * @throws NullPointerException If arrayClass is null
040:             * @throws IllegalArgumentException If arrayClass is not an array class
041:             */
042:            public static int arrayDimensions(Class arrayClass) {
043:                verifyIsArray(arrayClass); // guarantees c is non-null and an array class
044:                return arrayClass.getName().lastIndexOf("[") + 1;
045:            }
046:
047:            /**
048:             * If c is an array, return the reifiable type of the array element
049:             * @param c Array class
050:             * @return Type of an array element
051:             * @throws NullPointerException If arrayClass is null
052:             * @throws IllegalArgumentException If arrayClass is not an array class
053:             */
054:            public static Class baseComponentType(Class c) {
055:                verifyIsArray(c); // guarantees c is non-null and an array class
056:                while (c.isArray()) {
057:                    c = c.getComponentType();
058:                }
059:                return c;
060:            }
061:
062:            private static void verifyIsArray(Class arrayClass) {
063:                if (arrayClass == null) {
064:                    throw new NullPointerException();
065:                }
066:                if (!arrayClass.isArray()) {
067:                    throw new IllegalArgumentException(arrayClass
068:                            + " is not an array type");
069:                }
070:            }
071:
072:            /**
073:             * Determine whether test is a primitive array
074:             * @param test The object
075:             * @return True if test is a non-null primitive array
076:             */
077:            public static boolean isPrimitiveArray(Object test) {
078:                if (test == null) {
079:                    return false;
080:                }
081:                Class c = test.getClass();
082:                if (!c.isArray()) {
083:                    return false;
084:                }
085:                return c.getComponentType().isPrimitive();
086:            }
087:
088:            /**
089:             * Determine whether c is an enum (JDK 1.4 friendly)
090:             * @param c Class  
091:             * @return True if enum
092:             */
093:            public static boolean isEnum(Class c) {
094:                // a jdk1.4 friendly (but still fast) check for enums
095:                Class super Class = c.getSuperclass();
096:                if (super Class == null)
097:                    return false;
098:                if (((c.getModifiers() & 0x00004000) != 0)
099:                        && isSubclassOfEnum(super Class)) {
100:                    return true;
101:                }
102:                return false;
103:            }
104:
105:            private static boolean isSubclassOfEnum(Class c) {
106:                String name = c.getName();
107:                while (!"java.lang.Enum".equals(name)) {
108:                    c = c.getSuperclass();
109:                    if (c == null) {
110:                        return false;
111:                    }
112:                    name = c.getName();
113:                }
114:                return true;
115:            }
116:
117:            /**
118:             * Check whether c is a portable java reflection class like Method, Constructor, or Field
119:             * @param c Class
120:             * @return True if portable
121:             */
122:            public static boolean isPortableReflectionClass(Class c) {
123:                return METHOD_CLASS == c || CONSTRUCTOR_CLASS == c
124:                        || FIELD_CLASS == c;
125:            }
126:
127:            /**
128:             * Holder for a class name and field name which together fully identify a field
129:             * @see ClassUtils#parseFullyQualifiedFieldName(String)
130:             */
131:            public interface ClassSpec {
132:                /**
133:                 * @return Full class name
134:                 */
135:                public String getFullyQualifiedClassName();
136:
137:                /**
138:                 * @return Short field name
139:                 */
140:                public String getShortFieldName();
141:            }
142:
143:            private static class ClassSpecImpl implements  ClassSpec {
144:
145:                private String fullyQualifiedClassName;
146:                private String shortFieldName;
147:
148:                private void parseFullyQualifiedFieldName(String fieldName)
149:                        throws ParseException {
150:                    if (fieldName == null)
151:                        throwNotFullyQualifiedFieldName(fieldName, 0);
152:                    int lastDot = fieldName.lastIndexOf('.');
153:                    if (lastDot <= 0)
154:                        throwNotFullyQualifiedFieldName(fieldName, 0);
155:                    if (lastDot + 1 == fieldName.length())
156:                        throwNotFullyQualifiedFieldName(fieldName, lastDot);
157:                    fullyQualifiedClassName = fieldName.substring(0, lastDot);
158:                    shortFieldName = fieldName.substring(lastDot + 1);
159:                }
160:
161:                private void throwNotFullyQualifiedFieldName(String fieldName,
162:                        int position) throws ParseException {
163:                    throw new ParseException(
164:                            "Not a fully qualified fieldname: " + fieldName,
165:                            position);
166:                }
167:
168:                public String getFullyQualifiedClassName() {
169:                    return this .fullyQualifiedClassName;
170:                }
171:
172:                public String getShortFieldName() {
173:                    return this .shortFieldName;
174:                }
175:
176:                public String toString() {
177:                    return "ClassSpec[classname=" + fullyQualifiedClassName
178:                            + ", shortFieldName=" + shortFieldName + "]";
179:                }
180:            }
181:
182:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.