2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package libcore.java.lang.reflect;
19 import java.lang.reflect.Method;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashSet;
25 import junit.framework.TestCase;
27 public class MethodOverridesTest extends TestCase {
29 public void testName() throws NoSuchMethodException {
30 Method method = StringBuilder.class.getMethod("append", char.class);
31 assertEquals("append", method.getName());
34 public void testParameterTypes() throws NoSuchMethodException {
35 Method method = StringBuilder.class.getMethod("append", char.class);
36 assertEquals(Arrays.<Class<?>>asList(char.class),
37 Arrays.asList(method.getParameterTypes()));
40 public void testDeclaringClass() throws NoSuchMethodException {
41 Method method = StringBuilder.class.getMethod("append", char.class);
42 assertEquals(StringBuilder.class, method.getDeclaringClass());
45 public void testReturnType() throws NoSuchMethodException {
46 Method method = StringBuilder.class.getMethod("append", char.class);
47 assertEquals(StringBuilder.class, method.getReturnType());
50 public void testThrownExceptions() throws NoSuchMethodException {
51 Method method = StringBuilder.class.getMethod("append", char.class);
52 assertEquals(Collections.<Class<?>>emptyList(), Arrays.asList(method.getExceptionTypes()));
55 public void testGetMethodsIncludesInheritedMethods() {
56 Set<String> signatures = signatures(Sub.class.getMethods());
57 assertContains(signatures, "void notOverridden[] throws []");
60 public void testGetDeclaredMethodsDoesNotIncludeInheritedMethods() {
61 Set<String> signatures = signatures(Sub.class.getDeclaredMethods());
62 assertFalse(signatures.contains("void notOverridden[] throws []"));
65 public void testGetDeclaringClassReturnsOverridingClass() throws NoSuchMethodException {
66 assertEquals(Sub.class, Sub.class.getMethod("unchanged").getDeclaringClass());
67 assertEquals(Sub.class, Sub.class.getDeclaredMethod("unchanged").getDeclaringClass());
70 public void testGetMethodsDoesNotIncludeExceptionChanges() throws NoSuchMethodException {
71 Set<String> signatures = signatures(Sub.class.getMethods());
72 assertContains(signatures, "void thrower[] throws []");
73 assertFalse(signatures.contains("void thrower[] throws [java.lang.Exception]"));
74 assertEquals(Sub.class, Sub.class.getMethod("thrower").getDeclaringClass());
77 public void testGetMethodsIncludesSyntheticMethods() throws NoSuchMethodException {
78 Set<String> signatures = signatures(Sub.class.getMethods());
79 assertContains(signatures, "java.lang.String returner[] throws []");
80 assertContains(signatures, "java.lang.Object returner[] throws []");
82 Method method = Sub.class.getMethod("returner");
83 assertEquals(Sub.class, method.getDeclaringClass());
84 assertFalse(method.isSynthetic());
87 public void testGetDeclaredMethodsIncludesSyntheticMethods() throws NoSuchMethodException {
88 Set<String> signatures = signatures(Sub.class.getDeclaredMethods());
89 assertContains(signatures, "java.lang.String returner[] throws []");
90 assertContains(signatures, "java.lang.Object returner[] throws []");
92 Method method = Sub.class.getMethod("returner");
93 assertEquals(Sub.class, method.getDeclaringClass());
94 assertFalse(method.isSynthetic());
97 public void testSubclassChangesVisibility() throws NoSuchMethodException {
98 Method[] methods = Sub.class.getMethods();
100 for (Method method : methods) {
101 if (signature(method).equals("void visibility[] throws []")) {
102 assertEquals(Sub.class, method.getDeclaringClass());
103 assertFalse(method.isSynthetic());
107 assertEquals(1, count);
109 Method method = Sub.class.getMethod("visibility");
110 assertEquals(Sub.class, method.getDeclaringClass());
111 assertFalse(method.isSynthetic());
114 public void testMoreVisibleSubclassChangesVisibility() throws NoSuchMethodException {
115 Method[] methods = PublicSub.class.getMethods();
117 for (Method method : methods) {
118 if (signature(method).equals("void unchanged[] throws []")) {
119 assertEquals(PublicSub.class, method.getDeclaringClass());
120 assertTrue(method.isSynthetic());
124 assertEquals(1, count);
126 Method method = PublicSub.class.getMethod("unchanged");
127 assertEquals(PublicSub.class, method.getDeclaringClass());
128 assertTrue(method.isSynthetic());
131 public static class Super {
132 public void notOverridden() {}
133 public void unchanged() {}
134 public void thrower() throws Exception {}
135 public Object returner() {
138 protected void visibility() {}
141 public static class Sub extends Super {
142 @Override public void unchanged() {}
143 @Override public void thrower() {}
144 @Override public String returner() {
147 @Override public void visibility() {}
150 static class PackageSuper {
151 public void unchanged() {}
154 public static class PublicSub extends PackageSuper {}
157 * Returns a method signature of this form:
158 * {@code java.lang.String concat[class java.lang.String] throws []}.
160 private String signature(Method method) {
161 return method.getReturnType().getName() + " " + method.getName()
162 + Arrays.toString(method.getParameterTypes())
163 + " throws " + Arrays.toString(method.getExceptionTypes());
166 private Set<String> signatures(Method[] methods) {
167 Set<String> signatures = new HashSet<String>();
168 for (Method method : methods) {
169 signatures.add(signature(method));
174 private <T> void assertContains(Collection<T> elements, T value) {
175 assertTrue("Expected " + value + " in " + elements, elements.contains(value));