LocalVariableTableParameterNameDiscovererTests.java 9.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2002-2006 the original author or authors.
 *
 * 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 org.springframework.core;

19 20
import java.awt.Component;
import java.io.PrintStream;
21 22
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
23 24
import java.util.Arrays;
import java.util.Date;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

import junit.framework.TestCase;

import org.springframework.beans.TestBean;

/**
 * @author Adrian Colyer
 */
public class LocalVariableTableParameterNameDiscovererTests extends TestCase {

	private LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();

	public void testMethodParameterNameDiscoveryNoArgs() throws NoSuchMethodException {
		Method getName = TestBean.class.getMethod("getName", new Class[0]);
		String[] names = discoverer.getParameterNames(getName);
		assertNotNull("should find method info", names);
		assertEquals("no argument names", 0, names.length);
	}

	public void testMethodParameterNameDiscoveryWithArgs() throws NoSuchMethodException {
45
		Method setName = TestBean.class.getMethod("setName", new Class[] { String.class });
46 47 48 49 50 51 52 53 54 55 56 57 58 59
		String[] names = discoverer.getParameterNames(setName);
		assertNotNull("should find method info", names);
		assertEquals("one argument", 1, names.length);
		assertEquals("name", names[0]);
	}

	public void testConsParameterNameDiscoveryNoArgs() throws NoSuchMethodException {
		Constructor noArgsCons = TestBean.class.getConstructor(new Class[0]);
		String[] names = discoverer.getParameterNames(noArgsCons);
		assertNotNull("should find cons info", names);
		assertEquals("no argument names", 0, names.length);
	}

	public void testConsParameterNameDiscoveryArgs() throws NoSuchMethodException {
60
		Constructor twoArgCons = TestBean.class.getConstructor(new Class[] { String.class, int.class });
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
		String[] names = discoverer.getParameterNames(twoArgCons);
		assertNotNull("should find cons info", names);
		assertEquals("one argument", 2, names.length);
		assertEquals("name", names[0]);
		assertEquals("age", names[1]);
	}

	public void testStaticMethodParameterNameDiscoveryNoArgs() throws NoSuchMethodException {
		Method m = getClass().getMethod("staticMethodNoLocalVars", new Class[0]);
		String[] names = discoverer.getParameterNames(m);
		assertNotNull("should find method info", names);
		assertEquals("no argument names", 0, names.length);
	}

	public void testOverloadedStaticMethod() throws Exception {
		Class clazz = this.getClass();

78
		Method m1 = clazz.getMethod("staticMethod", new Class[] { Long.TYPE, Long.TYPE });
79 80 81 82 83 84
		String[] names = discoverer.getParameterNames(m1);
		assertNotNull("should find method info", names);
		assertEquals("two arguments", 2, names.length);
		assertEquals("x", names[0]);
		assertEquals("y", names[1]);

85
		Method m2 = clazz.getMethod("staticMethod", new Class[] { Long.TYPE, Long.TYPE, Long.TYPE });
86 87 88 89 90 91 92 93 94 95 96
		names = discoverer.getParameterNames(m2);
		assertNotNull("should find method info", names);
		assertEquals("three arguments", 3, names.length);
		assertEquals("x", names[0]);
		assertEquals("y", names[1]);
		assertEquals("z", names[2]);
	}

	public void testOverloadedStaticMethodInInnerClass() throws Exception {
		Class clazz = InnerClass.class;

97
		Method m1 = clazz.getMethod("staticMethod", new Class[] { Long.TYPE });
98 99 100 101 102
		String[] names = discoverer.getParameterNames(m1);
		assertNotNull("should find method info", names);
		assertEquals("one argument", 1, names.length);
		assertEquals("x", names[0]);

103
		Method m2 = clazz.getMethod("staticMethod", new Class[] { Long.TYPE, Long.TYPE });
104 105 106 107 108 109 110 111 112 113
		names = discoverer.getParameterNames(m2);
		assertNotNull("should find method info", names);
		assertEquals("two arguments", 2, names.length);
		assertEquals("x", names[0]);
		assertEquals("y", names[1]);
	}

	public void testOverloadedMethod() throws Exception {
		Class clazz = this.getClass();

114
		Method m1 = clazz.getMethod("instanceMethod", new Class[] { Double.TYPE, Double.TYPE });
115 116 117 118 119 120
		String[] names = discoverer.getParameterNames(m1);
		assertNotNull("should find method info", names);
		assertEquals("two arguments", 2, names.length);
		assertEquals("x", names[0]);
		assertEquals("y", names[1]);

121
		Method m2 = clazz.getMethod("instanceMethod", new Class[] { Double.TYPE, Double.TYPE, Double.TYPE });
122 123 124 125 126 127 128 129 130 131 132
		names = discoverer.getParameterNames(m2);
		assertNotNull("should find method info", names);
		assertEquals("three arguments", 3, names.length);
		assertEquals("x", names[0]);
		assertEquals("y", names[1]);
		assertEquals("z", names[2]);
	}

	public void testOverloadedMethodInInnerClass() throws Exception {
		Class clazz = InnerClass.class;

133
		Method m1 = clazz.getMethod("instanceMethod", new Class[] { String.class });
134 135 136 137 138
		String[] names = discoverer.getParameterNames(m1);
		assertNotNull("should find method info", names);
		assertEquals("one argument", 1, names.length);
		assertEquals("aa", names[0]);

139
		Method m2 = clazz.getMethod("instanceMethod", new Class[] { String.class, String.class });
140 141 142 143 144 145 146
		names = discoverer.getParameterNames(m2);
		assertNotNull("should find method info", names);
		assertEquals("two arguments", 2, names.length);
		assertEquals("aa", names[0]);
		assertEquals("bb", names[1]);
	}

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
	public void testGenerifiedClass() throws Exception {
		Class clazz = GenerifiedClass.class;

		Constructor ctor = clazz.getDeclaredConstructor(Object.class);
		String[] names = discoverer.getParameterNames(ctor);
		assertEquals(1, names.length);
		assertEquals("key", names[0]);

		ctor = clazz.getDeclaredConstructor(Object.class, Object.class);
		names = discoverer.getParameterNames(ctor);
		assertEquals(2, names.length);
		assertEquals("key", names[0]);
		assertEquals("value", names[1]);

		Method m = clazz.getMethod("generifiedStaticMethod", Object.class);
		names = discoverer.getParameterNames(m);
		assertEquals(1, names.length);
		assertEquals("param", names[0]);

		m = clazz.getMethod("generifiedMethod", Object.class, long.class, Object.class, Object.class);
		names = discoverer.getParameterNames(m);
		assertEquals(4, names.length);
		assertEquals("param", names[0]);
		assertEquals("x", names[1]);
		assertEquals("key", names[2]);
		assertEquals("value", names[3]);

		m = clazz.getMethod("voidStaticMethod", Object.class, long.class, int.class);
		names = discoverer.getParameterNames(m);
		assertEquals(3, names.length);
		assertEquals("obj", names[0]);
		assertEquals("x", names[1]);
		assertEquals("i", names[2]);

		m = clazz.getMethod("nonVoidStaticMethod", Object.class, long.class, int.class);
		names = discoverer.getParameterNames(m);
		assertEquals(3, names.length);
		assertEquals("obj", names[0]);
		assertEquals("x", names[1]);
		assertEquals("i", names[2]);

		m = clazz.getMethod("getDate", null);
		names = discoverer.getParameterNames(m);
		assertEquals(0, names.length);
	}

	public void testMemUsage() throws Exception {
		// JDK classes don't have debug information (usually)
		Class clazz = Component.class;
		String methodName = "list";

		Method m = clazz.getMethod(methodName, null);
		String[] names = discoverer.getParameterNames(m);
		assertNull(names);

		m = clazz.getMethod(methodName, PrintStream.class);
		names = discoverer.getParameterNames(m);
		assertNull(names);

		m = clazz.getMethod(methodName, PrintStream.class, int.class);
		names = discoverer.getParameterNames(m);
		assertNull(names);

		//System.in.read()
	}
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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

	public static void staticMethodNoLocalVars() {
	}

	public static long staticMethod(long x, long y) {
		long u = x * y;
		return u;
	}

	public static long staticMethod(long x, long y, long z) {
		long u = x * y * z;
		return u;
	}

	public double instanceMethod(double x, double y) {
		double u = x * y;
		return u;
	}

	public double instanceMethod(double x, double y, double z) {
		double u = x * y * z;
		return u;
	}

	public static class InnerClass {

		public int waz = 0;

		public InnerClass() {
		}

		public InnerClass(String firstArg, long secondArg, Object thirdArg) {
			long foo = 0;
			short bar = 10;
			this.waz = (int) (foo + bar);
		}

		public String instanceMethod(String aa) {
			return aa;
		}

		public String instanceMethod(String aa, String bb) {
			return aa + bb;
		}

		public static long staticMethod(long x) {
			long u = x;
			return u;
		}

		public static long staticMethod(long x, long y) {
			long u = x * y;
			return u;
		}
	}

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
	public static class GenerifiedClass<K, V> {
		private static long date;

		private K key;
		private V value;

		static {
			// some custom static bloc or <clinit>
			date = new Date().getTime();
		}

		public GenerifiedClass() {
			this(null, null);
		}

		public GenerifiedClass(K key) {
			this(key, null);
		}

		public GenerifiedClass(K key, V value) {
			this.key = key;
			this.value = value;
		}

		public static <P> long generifiedStaticMethod(P param) {
			return date;
		}

		public <P> void generifiedMethod(P param, long x, K key, V value) {
			// nothing
		}

		public static void voidStaticMethod(Object obj, long x, int i) {
			// nothing
		}

		public static long nonVoidStaticMethod(Object obj, long x, int i) {
			return date;
		}

		public static long getDate() {
			return date;
		}
	}
}