Source Code Cross Referenced for ClassTestGetField.java in  » Apache-Harmony-Java-SE » java-package » java » lang » 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 » Apache Harmony Java SE » java package » java.lang 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        /**
019:         * @author Evgueni V. Brevnov, Roman S. Bushmanov
020:         * @version $Revision$
021:         */package java.lang;
022:
023:        import java.lang.reflect.Field;
024:
025:        import junit.framework.TestCase;
026:
027:        /**
028:         * tested class: java.lang.Class
029:         * tested method: getDeclaredField
030:         */
031:        @SuppressWarnings(value={"all"})
032:        public class ClassTestGetField extends TestCase {
033:
034:            /**
035:             * the length field should not be reflected.
036:             */
037:            public void test1() {
038:                try {
039:                    final String name = "length";
040:                    Class c = new int[0].getClass();
041:                    c.getField(name);
042:                } catch (NoSuchFieldException e) {
043:                    return;
044:                }
045:                fail("NoSuchFieldException exception expected");
046:            }
047:
048:            /**
049:             * if name is null NullPoinerexception exception should be thrown.
050:             */
051:            public void test2() {
052:                try {
053:                    final String name = null;
054:                    getClass().getField(name);
055:                } catch (NullPointerException e) {
056:                    return;
057:                } catch (NoSuchFieldException e) {
058:                }
059:                fail("NullPointerException exception expected");
060:            }
061:
062:            /**
063:             * checks whether public field is reflected
064:             */
065:            public void test3() {
066:                try {
067:                    final String name = "i";
068:                    Field f = A.class.getField(name);
069:                    assertEquals("incorrect name", name, f.getName());
070:                } catch (Exception e) {
071:                    fail(e.toString());
072:                }
073:            }
074:
075:            /**
076:             * Private field should not be reflected.
077:             */
078:            public void test4() {
079:                try {
080:                    final String name = "s";
081:                    A.class.getField(name);
082:                } catch (NoSuchFieldException e) {
083:                    return;
084:                }
085:                fail("NoSuchFieldException exception expected");
086:            }
087:
088:            /**
089:             * Class B redefines field s of A class. Field of the B class should be
090:             * reflected in this case.
091:             */
092:            public void test5() {
093:                try {
094:                    final String name = "s";
095:                    Field f = B.class.getField(name);
096:                    assertEquals("incorrect name", name, f.getName());
097:                    assertSame("objects differ", B.class, f.getDeclaringClass());
098:                } catch (Exception e) {
099:                    fail(e.toString());
100:                }
101:            }
102:
103:            /**
104:             * Class B does not define field k. But its super interface J defines field
105:             * with such name. The field of the super interface should be returned.
106:             */
107:            public void test6() {
108:                try {
109:                    final String name = "k";
110:                    Field f = B.class.getField(name);
111:                    assertNotNull("unexpected null", f);
112:                    assertEquals("incorrect name", name, f.getName());
113:                    assertSame("objects differ", J.class, f.getDeclaringClass());
114:                } catch (Exception e) {
115:                    fail(e.toString());
116:                }
117:            }
118:
119:            /**
120:             * Class B does not define field i. But its super interface I and super
121:             * class A define field with such name. The field of the super interface
122:             * should be returned.
123:             */
124:            public void test7() {
125:                try {
126:                    final String name = "i";
127:                    Field f = B.class.getField(name);
128:                    assertNotNull("unexpected null", f);
129:                    assertEquals("incorrect name", name, f.getName());
130:                    assertSame("objects differ", I.class, f.getDeclaringClass());
131:                } catch (Exception e) {
132:                    fail(e.toString());
133:                }
134:            }
135:
136:            /**
137:             * Class B does not define field j. But its super class A defines field with
138:             * such name. The field of the super class should be returned.
139:             */
140:            public void test8() {
141:                try {
142:                    final String name = "j";
143:                    Field f = B.class.getField(name);
144:                    assertNotNull("unexpected null", f);
145:                    assertEquals("incorrect name", name, f.getName());
146:                    assertSame("objects differ", A.class, f.getDeclaringClass());
147:                } catch (Exception e) {
148:                    fail(e.toString());
149:                }
150:            }
151:
152:            /**
153:             * Class B does not define field o. Its super class A defines field with
154:             * such name. Sice this field has private accessibility an
155:             * NoSuchFieldException exception should be thrown.
156:             */
157:            public void test9() {
158:                try {
159:                    final String name = "o";
160:                    B.class.getField(name);
161:                } catch (NoSuchFieldException e) {
162:                    return;
163:                }
164:                fail("NoSuchFieldException exception expected");
165:            }
166:
167:            private static class A {
168:
169:                public final static int i = 0;
170:
171:                public final static int j = 1;
172:
173:                Object o;
174:
175:                private String s;
176:            }
177:
178:            interface I {
179:
180:                public final static int i = 0;
181:            }
182:
183:            interface J extends I {
184:
185:                int k = 0;
186:            }
187:
188:            private class B extends A implements  J {
189:
190:                public String s;
191:            }
192:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.