diff --git a/core/java/com/android/internal/util/MessageUtils.java b/core/java/com/android/internal/util/MessageUtils.java index 1014bfd6955d1..184245ef538dc 100644 --- a/core/java/com/android/internal/util/MessageUtils.java +++ b/core/java/com/android/internal/util/MessageUtils.java @@ -21,6 +21,7 @@ import android.util.Log; import android.util.SparseArray; import java.lang.reflect.Field; +import java.lang.reflect.Modifier; /** * Static utility class for dealing with {@link Message} objects. @@ -62,8 +63,12 @@ public class MessageUtils { } for (Field field : fields) { - String name = field.getName(); + int modifiers = field.getModifiers(); + if (!Modifier.isStatic(modifiers) | !Modifier.isFinal(modifiers)) { + continue; + } + String name = field.getName(); for (String prefix : prefixes) { // Does this look like a constant? if (!name.startsWith(prefix)) { diff --git a/core/tests/utiltests/src/com/android/internal/util/MessageUtilsTest.java b/core/tests/utiltests/src/com/android/internal/util/MessageUtilsTest.java new file mode 100644 index 0000000000000..32b969a8d1b51 --- /dev/null +++ b/core/tests/utiltests/src/com/android/internal/util/MessageUtilsTest.java @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.internal.util; + +import static org.junit.Assert.*; + +import com.android.internal.util.MessageUtils; +import android.test.suitebuilder.annotation.SmallTest; +import android.util.SparseArray; + +import org.junit.Test; + + +class A { + // Should not see these. + private int mMember; + public final int CMD_NOT_STATIC = 7; + private static final String CMD_NOT_INT = "not an integer"; + public static int CMD_NOT_FINAL = 34; + public static final int kWrongPrefix = 99; + + // Should see these. + private static final int CMD_DO_SOMETHING = 12; + public static final int EVENT_SOMETHING_HAPPENED = 45; +} + +class B { + public static final int CMD_FOO = 56; + public static final int EVENT_BAR = 55; + public static final int NOTIFICATION_BAZ = 12; +} + +/** + * Unit tests for {@link com.android.util.MessageUtils}. + */ +@SmallTest +public class MessageUtilsTest { + + private static final Class[] CLASSES = { A.class, B.class }; + + private SparseArray makeSparseArray(int[] keys, String[] values) throws Exception { + assertEquals("Must specify same number of keys and values", keys.length, values.length); + SparseArray out = new SparseArray<>(); + for (int i = 0; i < keys.length; i++) { + out.put(keys[i], values[i]); + } + return out; + } + + private void assertSparseArrayEquals( + SparseArray a1, SparseArray a2) throws Exception { + String msg = String.format("%s != %s", a1.toString(), a2.toString()); + assertEquals(msg, a1.size(), a2.size()); + int size = a1.size(); + for (int i = 0; i < size; i++) { + assertEquals(msg, a1.keyAt(i), a2.keyAt(i)); + assertEquals(msg, a1.valueAt(i), a2.valueAt(i)); + } + } + + @Test + public void basicOperation() throws Exception { + SparseArray expected = makeSparseArray( + new int[]{12, 45, 55, 56}, + new String[]{"CMD_DO_SOMETHING", "EVENT_SOMETHING_HAPPENED", "EVENT_BAR", "CMD_FOO"}); + assertSparseArrayEquals(expected, MessageUtils.findMessageNames(CLASSES)); + } + + @Test + public void withPrefixes() throws Exception { + SparseArray expected = makeSparseArray( + new int[]{45, 55}, + new String[]{"EVENT_SOMETHING_HAPPENED", "EVENT_BAR"}); + assertSparseArrayEquals(expected, MessageUtils.findMessageNames(CLASSES, + new String[]{"EVENT_"})); + } + + @Test(expected=MessageUtils.DuplicateConstantError.class) + public void duplicateConstants() { + MessageUtils.findMessageNames(CLASSES, new String[]{"CMD_", "NOTIFICATION_"}); + } + +}