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 Alexey V. Varlamov
020: * @version $Revision$
021: */package java.lang.reflect;
022:
023: import org.apache.harmony.lang.AnnotatedElementTestFrame;
024: import org.apache.harmony.lang.AnnotatedElementTestFrame.MissingClassValAntn;
025: import org.apache.harmony.lang.AnnotatedElementTestFrame.MissingTypeAntn;
026: import org.apache.harmony.lang.AnnotatedElementTestFrame.TagAntn;
027: import org.apache.harmony.lang.AnnotatedElementTestFrame.ValAntn;
028:
029: class AnnotatedField {
030: @TagAntn
031: public Object foo;
032: @TagAntn
033: @ValAntn
034: public Object[] bar;
035: public static transient volatile TwoParamType<String, ?> buz;
036: @notfound.MissingAntn
037: public int i;
038: @MissingClassValAntn
039: public long l;
040: @MissingTypeAntn
041: public char ch;
042: }
043:
044: class TwoParamType<K, V> {
045: }
046:
047: class OneParamType<U> {
048: }
049:
050: public class Field5Test extends AnnotatedElementTestFrame {
051:
052: protected @Override
053: AnnotatedElement getElement1() throws Throwable {
054: return AnnotatedField.class.getField("foo");
055: }
056:
057: protected @Override
058: AnnotatedElement getElement2() throws Throwable {
059: return AnnotatedField.class.getField("bar");
060: }
061:
062: protected @Override
063: AnnotatedElement getElement3() throws Throwable {
064: return AnnotatedField.class.getField("buz");
065: }
066:
067: /**
068: * Provides an instance to be tested. The instance must be annotated
069: * by the notfound.MissingAntn.
070: */
071: protected @Override
072: AnnotatedElement getElement4() throws Throwable {
073: return AnnotatedField.class.getField("i");
074: }
075:
076: /**
077: * Provides an instance to be tested. The instance must be annotated
078: * by the MissingClassValAntn.
079: */
080: protected @Override
081: AnnotatedElement getElement5() throws Throwable {
082: return AnnotatedField.class.getField("l");
083: }
084:
085: /**
086: * Provides an instance to be tested. The instance must be annotated
087: * by the MissingTypeAntn.
088: */
089: protected @Override
090: AnnotatedElement getElement6() throws Throwable {
091: return AnnotatedField.class.getField("ch");
092: }
093:
094: public static void main(String[] args) {
095: junit.textui.TestRunner.run(Field5Test.class);
096: }
097:
098: class A {
099: {
100: assert true;
101: }
102: }
103:
104: enum E {
105: E1, E2, E3
106: }
107:
108: static class B {
109: public E foo;
110: }
111:
112: /**
113: * isSynthetic() should return true if and only if
114: * the target field does not appear in the source code.
115: */
116: public void testIsSynthetic() throws Exception {
117: assertFalse("case1.1: ordinary field", AnnotatedField.class
118: .getField("foo").isSynthetic());
119: assertFalse("case1.2: ordinary field", B.class.getField("foo")
120: .isSynthetic());
121:
122: Field[] fs = A.class.getDeclaredFields();
123: assertTrue(fs != null && fs.length > 0);
124: for (Field f : fs) {
125: assertTrue("case2: " + f.getName(), f.isSynthetic());
126: }
127:
128: fs = E.class.getFields();
129: assertTrue(fs != null && fs.length > 0);
130: for (Field f : fs) {
131: assertFalse("case3: " + f.getName(), f.isSynthetic());
132: }
133: }
134:
135: /**
136: * isEnumConstant() should return true if and only if
137: * the target field is an element of an enumeration.
138: */
139: public void testIsEnumConstant() throws Exception {
140: assertFalse("case1: ordinary field", AnnotatedField.class
141: .getField("foo").isEnumConstant());
142:
143: Field[] fs = E.class.getFields();
144: assertTrue(fs != null && fs.length > 0);
145: for (Field f : fs) {
146: assertTrue("case2: " + f.getName(), f.isEnumConstant());
147: }
148:
149: assertFalse("case3: enum-typed member field", B.class.getField(
150: "foo").isEnumConstant());
151: }
152:
153: enum E2 {
154: E1;
155: public int i;
156: }
157:
158: /**
159: * isEnumConstant() should not return true
160: * for ordinary members (not elements) of an enumeration.
161: */
162: public void testIsEnumConstant2() throws Exception {
163: assertFalse(E2.class.getField("i").isEnumConstant());
164: }
165:
166: /**
167: * toGenericString() should return a string exactly matching
168: * the API specification.
169: */
170: public void testToGenericString() throws Exception {
171: String s = AnnotatedField.class.getField("buz")
172: .toGenericString();
173: System.out.println(s);
174: assertEquals(
175: "public static transient volatile"
176: + " java.lang.reflect.TwoParamType<java.lang.String, ?>"
177: + " java.lang.reflect.AnnotatedField.buz", s);
178: }
179:
180: public final int INSTANCE_I = 10;
181:
182: /**
183: * Regression test for HARMONY-4927
184: */
185: public void testAccessFinalInstance() throws Throwable {
186: Field fi = this .getClass().getField("INSTANCE_I");
187: final Object oldVal = fi.get(this );
188: final Object newVal = new Integer(2134523);
189:
190: try {
191: fi.set(this , newVal);
192: fail("Should not modify final field");
193: } catch (IllegalAccessException expected) {
194: assertEquals(oldVal, fi.get(this ));
195: }
196:
197: fi.setAccessible(true);
198: fi.set(this , newVal);
199: assertEquals(newVal, fi.get(this ));
200: }
201:
202: public static final int STATIC_I = 10;
203:
204: public void testAccessFinalStatic() throws Throwable {
205: Field fi = this .getClass().getField("STATIC_I");
206: final Object oldVal = fi.get(null);
207: final Object newVal = new Integer(2134523);
208:
209: try {
210: fi.set(null, newVal);
211: fail("Should not modify final field");
212: } catch (IllegalAccessException expected) {
213: assertEquals(oldVal, fi.get(null));
214: }
215:
216: try {
217: fi.setAccessible(true);
218: fi.set(this , newVal);
219: fail("Should not modify final field");
220: } catch (IllegalAccessException expected) {
221: assertEquals(oldVal, fi.get(null));
222: }
223: }
224:
225: }
|