2019-08-15 关于Constants类

2019-08-15  本文已影响0人  百炼

Constants类

说明: Constants类对类的公共静态变量做一些转化

Constants.png

Constants.java构造方法

   public Constants(Class<?> clazz) {
       Assert.notNull(clazz);
       this.className = clazz.getName();
       Field[] fields = clazz.getFields();
       for (Field field : fields) {
           if (ReflectionUtils.isPublicStaticFinal(field)) {
               String name = field.getName();
               try {
                   Object value = field.get(null);
                   this.fieldCache.put(name, value);
               }
               catch (IllegalAccessException ex) {
                   // just leave this field and continue
               }
           }
       }
   }

Object value = field.get(null);

在java的反射中,通过字段获取对象,是通过public Object get(Object obj)

  1. 字段不是静态字段的话,要传入反射类的对象.如果传null是会报
    java.lang.NullPointerException
  2. 字段是静态字段的话,传入任何对象都是可以的,包括null

ConstantsTest测试用例:

    @SuppressWarnings("unused")
    private static final class A {

        public static final int DOG = 0;
        public static final int CAT = 66;
        public static final String S1 = "";

        public static final int PREFIX_NO = 1;
        public static final int PREFIX_YES = 2;

        public static final int MY_PROPERTY_NO = 1;
        public static final int MY_PROPERTY_YES = 2;

        public static final int NO_PROPERTY = 3;
        public static final int YES_PROPERTY = 4;

        /** ignore these */
        protected static final int P = -1;
        protected boolean f;
        static final Object o = new Object();
    }
public class ConstantsTests extends TestCase {
    public void testConstants() {
        Constants c = new Constants(A.class);
        assertEquals(A.class.getName(), c.getClassName());
        assertEquals(9, c.getSize());

        assertEquals(c.asNumber("DOG").intValue(), A.DOG);
        assertEquals(c.asNumber("dog").intValue(), A.DOG);
        assertEquals(c.asNumber("cat").intValue(), A.CAT);

        try {
            c.asNumber("bogus");
            fail("Can't get bogus field");
        }
        catch (ConstantException expected) {
        }

        assertTrue(c.asString("S1").equals(A.S1));
        try {
            c.asNumber("S1");
            fail("Wrong type");
        }
        catch (ConstantException expected) {
        }
    }

    public void testGetNames() {
        Constants c = new Constants(A.class);

        Set<?> names = c.getNames("");
        assertEquals(c.getSize(), names.size());
        assertTrue(names.contains("DOG"));
        assertTrue(names.contains("CAT"));
        assertTrue(names.contains("S1"));

        names = c.getNames("D");
        assertEquals(1, names.size());
        assertTrue(names.contains("DOG"));

        names = c.getNames("d");
        assertEquals(1, names.size());
        assertTrue(names.contains("DOG"));
    }

    public void testGetValues() {
        Constants c = new Constants(A.class);

        Set<?> values = c.getValues("");
        assertEquals(7, values.size());
        assertTrue(values.contains(new Integer(0)));
        assertTrue(values.contains(new Integer(66)));
        assertTrue(values.contains(""));

        values = c.getValues("D");
        assertEquals(1, values.size());
        assertTrue(values.contains(new Integer(0)));

        values = c.getValues("prefix");
        assertEquals(2, values.size());
        assertTrue(values.contains(new Integer(1)));
        assertTrue(values.contains(new Integer(2)));

        values = c.getValuesForProperty("myProperty");
        assertEquals(2, values.size());
        assertTrue(values.contains(new Integer(1)));
        assertTrue(values.contains(new Integer(2)));
    }

    public void testGetValuesInTurkey() {
        Locale oldLocale = Locale.getDefault();
        Locale.setDefault(new Locale("tr", ""));
        try {
            Constants c = new Constants(A.class);

            Set<?> values = c.getValues("");
            assertEquals(7, values.size());
            assertTrue(values.contains(new Integer(0)));
            assertTrue(values.contains(new Integer(66)));
            assertTrue(values.contains(""));

            values = c.getValues("D");
            assertEquals(1, values.size());
            assertTrue(values.contains(new Integer(0)));

            values = c.getValues("prefix");
            assertEquals(2, values.size());
            assertTrue(values.contains(new Integer(1)));
            assertTrue(values.contains(new Integer(2)));

            values = c.getValuesForProperty("myProperty");
            assertEquals(2, values.size());
            assertTrue(values.contains(new Integer(1)));
            assertTrue(values.contains(new Integer(2)));
        }
        finally {
            Locale.setDefault(oldLocale);
        }
    }

    public void testSuffixAccess() {
        Constants c = new Constants(A.class);

        Set<?> names = c.getNamesForSuffix("_PROPERTY");
        assertEquals(2, names.size());
        assertTrue(names.contains("NO_PROPERTY"));
        assertTrue(names.contains("YES_PROPERTY"));

        Set<?> values = c.getValuesForSuffix("_PROPERTY");
        assertEquals(2, values.size());
        assertTrue(values.contains(new Integer(3)));
        assertTrue(values.contains(new Integer(4)));
    }

    public void testToCode() {
        Constants c = new Constants(A.class);

        assertEquals(c.toCode(new Integer(0), ""), "DOG");
        assertEquals(c.toCode(new Integer(0), "D"), "DOG");
        assertEquals(c.toCode(new Integer(0), "DO"), "DOG");
        assertEquals(c.toCode(new Integer(0), "DoG"), "DOG");
        assertEquals(c.toCode(new Integer(0), null), "DOG");
        assertEquals(c.toCode(new Integer(66), ""), "CAT");
        assertEquals(c.toCode(new Integer(66), "C"), "CAT");
        assertEquals(c.toCode(new Integer(66), "ca"), "CAT");
        assertEquals(c.toCode(new Integer(66), "cAt"), "CAT");
        assertEquals(c.toCode(new Integer(66), null), "CAT");
        assertEquals(c.toCode("", ""), "S1");
        assertEquals(c.toCode("", "s"), "S1");
        assertEquals(c.toCode("", "s1"), "S1");
        assertEquals(c.toCode("", null), "S1");
        try {
            c.toCode("bogus", "bogus");
            fail("Should have thrown ConstantException");
        }
        catch (ConstantException expected) {
        }
        try {
            c.toCode("bogus", null);
            fail("Should have thrown ConstantException");
        }
        catch (ConstantException expected) {
        }

        assertEquals(c.toCodeForProperty(new Integer(1), "myProperty"), "MY_PROPERTY_NO");
        assertEquals(c.toCodeForProperty(new Integer(2), "myProperty"), "MY_PROPERTY_YES");
        try {
            c.toCodeForProperty("bogus", "bogus");
            fail("Should have thrown ConstantException");
        }
        catch (ConstantException expected) {
        }

        assertEquals(c.toCodeForSuffix(new Integer(0), ""), "DOG");
        assertEquals(c.toCodeForSuffix(new Integer(0), "G"), "DOG");
        assertEquals(c.toCodeForSuffix(new Integer(0), "OG"), "DOG");
        assertEquals(c.toCodeForSuffix(new Integer(0), "DoG"), "DOG");
        assertEquals(c.toCodeForSuffix(new Integer(0), null), "DOG");
        assertEquals(c.toCodeForSuffix(new Integer(66), ""), "CAT");
        assertEquals(c.toCodeForSuffix(new Integer(66), "T"), "CAT");
        assertEquals(c.toCodeForSuffix(new Integer(66), "at"), "CAT");
        assertEquals(c.toCodeForSuffix(new Integer(66), "cAt"), "CAT");
        assertEquals(c.toCodeForSuffix(new Integer(66), null), "CAT");
        assertEquals(c.toCodeForSuffix("", ""), "S1");
        assertEquals(c.toCodeForSuffix("", "1"), "S1");
        assertEquals(c.toCodeForSuffix("", "s1"), "S1");
        assertEquals(c.toCodeForSuffix("", null), "S1");
        try {
            c.toCodeForSuffix("bogus", "bogus");
            fail("Should have thrown ConstantException");
        }
        catch (ConstantException expected) {
        }
        try {
            c.toCodeForSuffix("bogus", null);
            fail("Should have thrown ConstantException");
        }
        catch (ConstantException expected) {
        }
    }

    public void testGetValuesWithNullPrefix() throws Exception {
        Constants c = new Constants(A.class);
        Set<?> values = c.getValues(null);
        assertEquals("Must have returned *all* public static final values", 7, values.size());
    }

    public void testGetValuesWithEmptyStringPrefix() throws Exception {
        Constants c = new Constants(A.class);
        Set<Object> values = c.getValues("");
        assertEquals("Must have returned *all* public static final values", 7, values.size());
    }

    public void testGetValuesWithWhitespacedStringPrefix() throws Exception {
        Constants c = new Constants(A.class);
        Set<?> values = c.getValues(" ");
        assertEquals("Must have returned *all* public static final values", 7, values.size());
    }

    public void testWithClassThatExposesNoConstants() throws Exception {
        Constants c = new Constants(NoConstants.class);
        assertEquals(0, c.getSize());
        final Set<?> values = c.getValues("");
        assertNotNull(values);
        assertEquals(0, values.size());
    }

    public void testCtorWithNullClass() throws Exception {
        try {
            new Constants(null);
            fail("Must have thrown IllegalArgumentException");
        }
        catch (IllegalArgumentException expected) {}
    }
    private static final class NoConstants {
    }
上一篇下一篇

猜你喜欢

热点阅读