Home | History | Annotate | Download | only in reflect
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      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 libcore.java.lang.reflect;
     18 
     19 import java.lang.reflect.Field;
     20 import java.lang.reflect.Modifier;
     21 
     22 import junit.framework.TestCase;
     23 
     24 public final class FieldTest extends TestCase {
     25     private static final long MY_LONG = 5073258162644648461L;
     26 
     27     // Reflection for static long fields was broken http://b/1120750
     28     public void testLongFieldReflection() throws Exception {
     29         Field field = getClass().getDeclaredField("MY_LONG");
     30         assertEquals(5073258162644648461L, field.getLong(null));
     31     }
     32 
     33     public void testEqualConstructorEqualsAndHashCode() throws Exception {
     34         Field f1 = FieldTestHelper.class.getField("a");
     35         Field f2 = FieldTestHelper.class.getField("a");
     36         assertEquals(f1, f2);
     37         assertEquals(f1.hashCode(), f2.hashCode());
     38     }
     39 
     40     public void testHashCodeSpec() throws Exception {
     41         Field f1 = FieldTestHelper.class.getField("a");
     42         assertEquals(FieldTestHelper.class.getName().hashCode() ^ "a".hashCode(), f1.hashCode());
     43     }
     44 
     45     public void testDifferentConstructorEqualsAndHashCode() throws Exception {
     46         Field f1 = FieldTestHelper.class.getField("a");
     47         Field f2 = FieldTestHelper.class.getField("b");
     48         assertFalse(f1.equals(f2));
     49     }
     50 
     51     // Tests that the "synthetic" modifier is handled correctly.
     52     // It's supposed to be present but not shown in toString.
     53     public void testSyntheticModifier() throws NoSuchFieldException {
     54         Field valuesField = Thread.State.class.getDeclaredField("$VALUES");
     55         // Check that this test makes sense.
     56         assertTrue(valuesField.isSynthetic());
     57         assertEquals(Modifier.SYNTHETIC, valuesField.getModifiers() & Modifier.SYNTHETIC);
     58         assertEquals("private static final java.lang.Thread$State[] java.lang.Thread$State.$VALUES",
     59                 valuesField.toString());
     60     }
     61 
     62     // Ensure that the "enum constant" bit is not returned in toString.
     63     public void testEnumValueField() throws NoSuchFieldException {
     64         Field blockedField = Thread.State.class.getDeclaredField("BLOCKED");
     65         assertTrue(Thread.State.class.getDeclaredField("BLOCKED").isEnumConstant());
     66         assertEquals("public static final", Modifier.toString(blockedField.getModifiers()));
     67         assertEquals(
     68                 "public static final java.lang.Thread$State java.lang.Thread$State.BLOCKED",
     69                 blockedField.toString());
     70     }
     71 
     72     class ClassWithATransientField {
     73         private transient Class<String> transientField = String.class;
     74     }
     75 
     76     // Tests that the "transient" modifier is handled correctly.
     77     // The underlying constant value for it is the same as for the "varargs" method modifier.
     78     // http://b/18488857
     79     public void testTransientModifier() throws NoSuchFieldException {
     80         Field transientField = ClassWithATransientField.class.getDeclaredField("transientField");
     81         // Check that this test makes sense.
     82         assertEquals(Modifier.TRANSIENT, transientField.getModifiers() & Modifier.TRANSIENT);
     83         assertEquals(
     84                 "private transient java.lang.Class "
     85                         + "libcore.java.lang.reflect.FieldTest$ClassWithATransientField"
     86                         + ".transientField",
     87                 transientField.toString());
     88     }
     89 
     90     public void testToGenericString() throws NoSuchFieldException {
     91         Field transientField = ClassWithATransientField.class.getDeclaredField("transientField");
     92         // Check that this test makes sense.
     93         assertEquals(Modifier.TRANSIENT, transientField.getModifiers() & Modifier.TRANSIENT);
     94         assertEquals(
     95                 "private transient java.lang.Class<java.lang.String> "
     96                         + "libcore.java.lang.reflect.FieldTest$ClassWithATransientField"
     97                         + ".transientField",
     98                 transientField.toGenericString());
     99     }
    100 
    101     static class FieldTestHelper {
    102         public String a;
    103         public Object b;
    104     }
    105 }
    106