summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/libcore/java/lang/reflect/MethodTest.java
blob: a3f9065ae7064f8d5263b4f861077409aff304e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Copyright (C) 2009 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 libcore.java.lang.reflect;

import java.lang.reflect.Method;

import junit.framework.TestCase;

public final class MethodTest extends TestCase {
    public void test_getExceptionTypes() throws Exception {
        Method method = MethodTestHelper.class.getMethod("m1", new Class[0]);
        Class[] exceptions = method.getExceptionTypes();
        assertEquals(1, exceptions.length);
        assertEquals(IndexOutOfBoundsException.class, exceptions[0]);
        // Check that corrupting our array doesn't affect other callers.
        exceptions[0] = NullPointerException.class;
        exceptions = method.getExceptionTypes();
        assertEquals(1, exceptions.length);
        assertEquals(IndexOutOfBoundsException.class, exceptions[0]);
    }

    public void test_getParameterTypes() throws Exception {
        Class[] expectedParameters = new Class[] { Object.class };
        Method method = MethodTestHelper.class.getMethod("m2", expectedParameters);
        Class[] parameters = method.getParameterTypes();
        assertEquals(1, parameters.length);
        assertEquals(expectedParameters[0], parameters[0]);
        // Check that corrupting our array doesn't affect other callers.
        parameters[0] = String.class;
        parameters = method.getParameterTypes();
        assertEquals(1, parameters.length);
        assertEquals(expectedParameters[0], parameters[0]);
    }

    public void testGetMethodWithPrivateMethodAndInterfaceMethod() throws Exception {
        assertEquals(InterfaceA.class, Sub.class.getMethod("a").getDeclaringClass());
    }

    public void testGetMethodReturnsIndirectlyImplementedInterface() throws Exception {
        assertEquals(InterfaceA.class, ImplementsC.class.getMethod("a").getDeclaringClass());
        assertEquals(InterfaceA.class, ExtendsImplementsC.class.getMethod("a").getDeclaringClass());
    }

    public void testGetDeclaredMethodReturnsIndirectlyImplementedInterface() throws Exception {
        try {
            ImplementsC.class.getDeclaredMethod("a").getDeclaringClass();
            fail();
        } catch (NoSuchMethodException expected) {
        }
        try {
            ExtendsImplementsC.class.getDeclaredMethod("a").getDeclaringClass();
            fail();
        } catch (NoSuchMethodException expected) {
        }
    }

    public void testGetMethodWithConstructorName() throws Exception {
        try {
            MethodTestHelper.class.getMethod("<init>");
            fail();
        } catch (NoSuchMethodException expected) {
        }
    }

    public void testGetMethodWithNullName() throws Exception {
        try {
            MethodTestHelper.class.getMethod(null);
            fail();
        } catch (NullPointerException expected) {
        }
    }

    public void testGetMethodWithNullArgumentsArray() throws Exception {
        Method m1 = MethodTestHelper.class.getMethod("m1", (Class[]) null);
        assertEquals(0, m1.getParameterTypes().length);
    }

    public void testGetMethodWithNullArgument() throws Exception {
        try {
            MethodTestHelper.class.getMethod("m2", new Class[] { null });
            fail();
        } catch (NoSuchMethodException expected) {
        }
    }

    public void testGetMethodReturnsInheritedStaticMethod() throws Exception {
        Method b = Sub.class.getMethod("b");
        assertEquals(void.class, b.getReturnType());
    }

    public void testGetDeclaredMethodReturnsPrivateMethods() throws Exception {
        Method method = Super.class.getDeclaredMethod("a");
        assertEquals(void.class, method.getReturnType());
    }

    public void testGetDeclaredMethodDoesNotReturnSuperclassMethods() throws Exception {
        try {
            Sub.class.getDeclaredMethod("a");
            fail();
        } catch (NoSuchMethodException expected) {
        }
    }

    public void testGetDeclaredMethodDoesNotReturnImplementedInterfaceMethods() throws Exception {
        try {
            InterfaceB.class.getDeclaredMethod("a");
            fail();
        } catch (NoSuchMethodException expected) {
        }
    }

    public void testImplementedInterfaceMethodOfAnonymousClass() throws Exception {
        Object anonymous = new InterfaceA() {
            @Override public void a() {
            }
        };
        Method method = anonymous.getClass().getMethod("a");
        assertEquals(anonymous.getClass(), method.getDeclaringClass());
    }

    public void testPublicMethodOfAnonymousClass() throws Exception {
        Object anonymous = new Object() {
            public void a() {
            }
        };
        Method method = anonymous.getClass().getMethod("a");
        assertEquals(anonymous.getClass(), method.getDeclaringClass());
    }

    public void testGetMethodDoesNotReturnPrivateMethodOfAnonymousClass() throws Exception {
        Object anonymous = new Object() {
            private void a() {
            }
        };
        try {
            anonymous.getClass().getMethod("a");
            fail();
        } catch (NoSuchMethodException expected) {
        }
    }

    public void testGetDeclaredMethodReturnsPrivateMethodOfAnonymousClass() throws Exception {
        Object anonymous = new Object() {
            private void a() {
            }
        };
        Method method = anonymous.getClass().getDeclaredMethod("a");
        assertEquals(anonymous.getClass(), method.getDeclaringClass());
    }

    public void testEqualMethodEqualsAndHashCode() throws Exception {
        Method m1 = MethodTestHelper.class.getMethod("m1");
        Method m2 = MethodTestHelper.class.getMethod("m1");
        assertEquals(m1, m2);
        assertEquals(m1.hashCode(), m2.hashCode());
        assertEquals(MethodTestHelper.class.getName().hashCode() ^ "m1".hashCode(), m1.hashCode());
    }

    public void testHashCodeSpec() throws Exception {
        Method m1 = MethodTestHelper.class.getMethod("m1");
        assertEquals(MethodTestHelper.class.getName().hashCode() ^ "m1".hashCode(), m1.hashCode());
    }

    public void testDifferentMethodEqualsAndHashCode() throws Exception {
        Method m1 = MethodTestHelper.class.getMethod("m1");
        Method m2 = MethodTestHelper.class.getMethod("m2", Object.class);
        assertFalse(m1.equals(m2));
        assertFalse(m1.hashCode() == m2.hashCode());
    }

    // http://b/1045939
    public void testMethodToString() throws Exception {
        assertEquals("public final native void java.lang.Object.notify()",
                Object.class.getMethod("notify", new Class[] { }).toString());
        assertEquals("public java.lang.String java.lang.Object.toString()",
                Object.class.getMethod("toString", new Class[] { }).toString());
        assertEquals("public final native void java.lang.Object.wait(long,int)"
                + " throws java.lang.InterruptedException",
                Object.class.getMethod("wait", new Class[] { long.class, int.class }).toString());
        assertEquals("public boolean java.lang.Object.equals(java.lang.Object)",
                Object.class.getMethod("equals", new Class[] { Object.class }).toString());
        assertEquals("public static java.lang.String java.lang.String.valueOf(char[])",
                String.class.getMethod("valueOf", new Class[] { char[].class }).toString());
        assertEquals( "public java.lang.Process java.lang.Runtime.exec(java.lang.String[])"
                + " throws java.io.IOException",
                Runtime.class.getMethod("exec", new Class[] { String[].class }).toString());
        // http://b/18488857
        assertEquals(
                "public int java.lang.String.compareTo(java.lang.Object)",
                String.class.getMethod("compareTo", Object.class).toString());
    }

    // Tests that the "varargs" modifier is handled correctly.
    // The underlying constant value for it is the same as for the "transient" field modifier.
    // http://b/18488857
    public void testVarargsModifier() throws NoSuchMethodException {
        Method stringFormatMethod = String.class.getMethod(
                "format", new Class[] { String.class, Object[].class });
        assertTrue(stringFormatMethod.isVarArgs());
        assertEquals(
                "public static java.lang.String java.lang.String.format("
                        + "java.lang.String,java.lang.Object[])",
                stringFormatMethod.toString());
    }

    public static class MethodTestHelper {
        public void m1() throws IndexOutOfBoundsException { }
        public void m2(Object o) { }
    }

    public static class Super {
        private void a() {}
        public static void b() {}
    }
    public static interface InterfaceA {
        void a();
    }
    public static abstract class Sub extends Super implements InterfaceA {
    }

    public static interface InterfaceB extends InterfaceA {}
    public static interface InterfaceC extends InterfaceB {}
    public static abstract class ImplementsC implements InterfaceC {}
    public static abstract class ExtendsImplementsC extends ImplementsC {}
}