Source Code Cross Referenced for Annotations.java in  » Net » Terracotta » com » tc » backport175 » 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.backport175 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************************
002:         * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved.                      *
003:         * http://backport175.codehaus.org                                                         *
004:         * --------------------------------------------------------------------------------------- *
005:         * The software in this package is published under the terms of Apache License Version 2.0 *
006:         * a copy of which has been included with this distribution in the license.txt file.       *
007:         *******************************************************************************************/package com.tc.backport175;
008:
009:        import com.tc.backport175.bytecode.AnnotationReader;
010:
011:        import java.lang.reflect.Constructor;
012:        import java.lang.reflect.Field;
013:        import java.lang.reflect.Method;
014:        import java.util.ArrayList;
015:        import java.util.List;
016:
017:        /**
018:         * Helper class for reader retrieval of strongly typed JavaDoc annotations (as well as regular Java 5 {@link
019:         * java.lang.reader.RetentionPolicy.RUNTIME} annotations  when running Java 1.5.x).
020:         *
021:         * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
022:         * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
023:         */
024:        public final class Annotations {
025:
026:            /**
027:             * Checks if an annotation is present at a specific class.
028:             *
029:             * @param annotationType the annotation type
030:             * @param target the annotated type
031:             * @return true if the annotation is present else false
032:             */
033:            public static boolean isAnnotationPresent(
034:                    final Class annotationType, final Class target) {
035:                boolean isPresent = AnnotationReader
036:                        .getReaderFor(target)
037:                        .isAnnotationPresent(getAnnnotationName(annotationType));
038:                if (!isPresent && isInherited(annotationType)) {
039:                    if (target.getSuperclass() == null) {
040:                        return isPresent;
041:                    } else {
042:                        return isAnnotationPresent(annotationType, target
043:                                .getSuperclass());
044:                    }
045:                }
046:                return isPresent;
047:            }
048:
049:            /**
050:             * Return all the annotations for a specific class.
051:             *
052:             * @param target          the java.lang.Class object to find the annotations on.
053:             * @return an array with the annotations
054:             */
055:            public static Annotation[] getAnnotations(final Class target) {
056:                Annotation[] declaredAnnotations = AnnotationReader
057:                        .getReaderFor(target).getAnnotations();
058:                if (target.getSuperclass() == null) {
059:                    return declaredAnnotations;
060:                } else {
061:                    List annotations = new ArrayList(declaredAnnotations.length);
062:                    Annotation[] parents = getAnnotations(target
063:                            .getSuperclass());
064:                    for (int i = 0; i < parents.length; i++) {
065:                        if (isInherited(parents[i].annotationType())) {
066:                            annotations.add(parents[i]);
067:                        }
068:                    }
069:                    for (int i = 0; i < declaredAnnotations.length; i++) {
070:                        annotations.add(declaredAnnotations[i]);
071:                    }
072:                    return (Annotation[]) annotations
073:                            .toArray(new Annotation[] {});
074:                }
075:            }
076:
077:            /**
078:             * Return the annotation with a specific name for a specific class.
079:             *
080:             * @param annotationType the annotation class
081:             * @param target      the java.lang.Class object to find the annotation on.
082:             * @return the annotation or null
083:             */
084:            public static Annotation getAnnotation(final Class annotationType,
085:                    final Class target) {
086:                final AnnotationReader reader = AnnotationReader
087:                        .getReaderFor(target);
088:                Annotation annotation = reader
089:                        .getAnnotation(getAnnnotationName(annotationType));
090:                if (annotation == null && isInherited(annotationType)) {
091:                    if (target.getSuperclass() == null) {
092:                        return annotation;
093:                    } else {
094:                        return getAnnotation(annotationType, target
095:                                .getSuperclass());
096:                    }
097:                }
098:                return annotation;
099:            }
100:
101:            /**
102:             * Checks if an annotation is present at a specific method.
103:             *
104:             * @param annotationType the annotation type
105:             * @param method the annotated type
106:             * @return true if the annotation is present else false
107:             */
108:            public static boolean isAnnotationPresent(
109:                    final Class annotationType, final Method method) {
110:                final AnnotationReader reader = AnnotationReader
111:                        .getReaderFor(method.getDeclaringClass());
112:                return reader.isAnnotationPresent(
113:                        getAnnnotationName(annotationType), method);
114:            }
115:
116:            /**
117:             * Return all the annotations for a specific method.
118:             *
119:             * @param method the java.lang.reflect.Method object to find the annotations on.
120:             * @return an array with the annotations
121:             */
122:            public static Annotation[] getAnnotations(final Method method) {
123:                return AnnotationReader
124:                        .getReaderFor(method.getDeclaringClass())
125:                        .getAnnotations(method);
126:            }
127:
128:            /**
129:             * Return the annotation with a specific name for a specific method.
130:             *
131:             * @param annotationType the annotation class
132:             * @param method     the java.lang.refect.Method object to find the annotation on.
133:             * @return the annotation or null
134:             */
135:            public static Annotation getAnnotation(final Class annotationType,
136:                    final Method method) {
137:                final AnnotationReader reader = AnnotationReader
138:                        .getReaderFor(method.getDeclaringClass());
139:                return reader.getAnnotation(getAnnnotationName(annotationType),
140:                        method);
141:            }
142:
143:            /**
144:             * Checks if an annotation is present at a specific method.
145:             *
146:             * @param annotationType the annotation type
147:             * @param constructor the annotated type
148:             * @return true if the annotation is present else false
149:             */
150:            public static boolean isAnnotationPresent(
151:                    final Class annotationType, final Constructor constructor) {
152:                final AnnotationReader reader = AnnotationReader
153:                        .getReaderFor(constructor.getDeclaringClass());
154:                return reader.isAnnotationPresent(
155:                        getAnnnotationName(annotationType), constructor);
156:            }
157:
158:            /**
159:             * Return all the annotations for a specific constructor.
160:             *
161:             * @param constructor the java.lang.reflect.Constructor object to find the annotations on.
162:             * @return an array with the annotations
163:             */
164:            public static Annotation[] getAnnotations(
165:                    final Constructor constructor) {
166:                return AnnotationReader.getReaderFor(
167:                        constructor.getDeclaringClass()).getAnnotations(
168:                        constructor);
169:            }
170:
171:            /**
172:             * Return the annotation with a specific name for a specific constructor.
173:             *
174:             * @param annotationType  the annotation class
175:             * @param constructor the java.lang.refect.Constructor object to find the annotation on.
176:             * @return the annotation or null
177:             */
178:            public static Annotation getAnnotation(final Class annotationType,
179:                    final Constructor constructor) {
180:                final AnnotationReader reader = AnnotationReader
181:                        .getReaderFor(constructor.getDeclaringClass());
182:                return reader.getAnnotation(getAnnnotationName(annotationType),
183:                        constructor);
184:            }
185:
186:            /**
187:             * Checks if an annotation is present at a specific field.
188:             *
189:             * @param annotationType the annotation type
190:             * @param field the annotated type
191:             * @return true if the annotation is present else false
192:             */
193:            public static boolean isAnnotationPresent(
194:                    final Class annotationType, final Field field) {
195:                final AnnotationReader reader = AnnotationReader
196:                        .getReaderFor(field.getDeclaringClass());
197:                return reader.isAnnotationPresent(
198:                        getAnnnotationName(annotationType), field);
199:            }
200:
201:            /**
202:             * Return all the annotations for a specific field.
203:             *
204:             * @param field the java.lang.reflect.Field object to find the annotations on.
205:             * @return an array with the annotations
206:             */
207:            public static Annotation[] getAnnotations(final Field field) {
208:                return AnnotationReader.getReaderFor(field.getDeclaringClass())
209:                        .getAnnotations(field);
210:            }
211:
212:            /**
213:             * Return the annotation with a specific name for a specific field.
214:             *
215:             * @param annotationType the annotation class
216:             * @param field      the java.lang.reflect.Field object to find the annotation on.
217:             * @return the annotation or null
218:             */
219:            public static Annotation getAnnotation(final Class annotationType,
220:                    final Field field) {
221:                final AnnotationReader reader = AnnotationReader
222:                        .getReaderFor(field.getDeclaringClass());
223:                return reader.getAnnotation(getAnnnotationName(annotationType),
224:                        field);
225:            }
226:
227:            /**
228:             * Returns the annotation class name in Java style.
229:             *
230:             * @param annotationType
231:             * @return
232:             */
233:            private static String getAnnnotationName(final Class annotationType) {
234:                return annotationType.getName().replace('/', '.');
235:            }
236:
237:            /**
238:             * Returns true if the annotation is @Inherited
239:             * @param annotationType
240:             * @return
241:             */
242:            private static boolean isInherited(final Class annotationType) {
243:                return AnnotationReader.getReaderFor(annotationType)
244:                        .isAnnotationPresent("java.lang.annotation.Inherited");
245:            }
246:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.