/** * Verifies that {@code ctor} produces a {@link NullPointerException} or {@link * UnsupportedOperationException} when the parameter in position {@code paramIndex} is null. If * this parameter is marked nullable, this method does nothing. */ public void testConstructorParameter(Constructor<?> ctor, int paramIndex) { ctor.setAccessible(true); testParameter(null, Invokable.from(ctor), paramIndex, ctor.getDeclaringClass()); }
final Iterable<Method> getStaticMethods(Class<?> cls) { ImmutableList.Builder<Method> builder = ImmutableList.builder(); for (Method method : getVisibleMethods(cls)) { if (Invokable.from(method).isStatic()) { builder.add(method); } } return builder.build(); }
private static Invokable<?, ?> invokable(@Nullable Object instance, Method method) { if (instance == null) { return Invokable.from(method); } else { return TypeToken.of(instance.getClass()).method(method); } }
final Iterable<Method> getInstanceMethods(Class<?> cls) { ConcurrentMap<Signature, Method> map = Maps.newConcurrentMap(); for (Method method : getVisibleMethods(cls)) { if (!Invokable.from(method).isStatic()) { map.putIfAbsent(new Signature(method), method); } } return map.values(); }
static Invokable<Prepender, Object> method(String name, Class<?>... parameterTypes) { try { Method method = Prepender.class.getDeclaredMethod(name, parameterTypes); @SuppressWarnings("unchecked") // The method is from Prepender. Invokable<Prepender, Object> invokable = (Invokable<Prepender, Object>) Invokable.from(method); return invokable; } catch (NoSuchMethodException e) { throw new IllegalArgumentException(e); } }
private boolean hasInstanceMethodToTestNulls(Class<?> c, Visibility visibility) { for (Method method : nullPointerTester.getInstanceMethodsToTest(c, visibility)) { for (Parameter param : Invokable.from(method).getParameters()) { if (!NullPointerTester.isPrimitiveOrNullable(param)) { return true; } } } return false; }
public void testAnonymousClassWithTwoParametersConstructor() { abstract class Base { @SuppressWarnings("unused") // called by reflection Base(String s, int i) {} } Class<?> anonymous = new Base("test", 0) {}.getClass(); Constructor<?> constructor = anonymous.getDeclaredConstructors()[0]; assertEquals(2, Invokable.from(constructor).getParameters().size()); }
public void testLocalClassDefaultConstructor() { final int i = 1; final String s = "hello world"; class LocalWithDefaultConstructor implements Runnable { @Override public void run() { System.out.println(s + i); } } Constructor<?> constructor = LocalWithDefaultConstructor.class.getDeclaredConstructors()[0]; assertEquals(0, Invokable.from(constructor).getParameters().size()); }
public void testNestedInnerClassDefaultConstructor() { Constructor<?> constructor = InnerWithDefaultConstructor.NestedInner.class.getDeclaredConstructors()[0]; assertEquals(0, Invokable.from(constructor).getParameters().size()); }
public void testNulls() { for (Method method : ParameterTest.class.getDeclaredMethods()) { for (Parameter param : Invokable.from(method).getParameters()) { new NullPointerTester().testAllPublicInstanceMethods(param); } } }
public void testLocalClassWithAnnotatedConstructorParameter() throws Exception { class LocalWithAnnotatedConstructorParameter { @SuppressWarnings("unused") // called by reflection LocalWithAnnotatedConstructorParameter(@Nullable String s) {} } Constructor<?> constructor = LocalWithAnnotatedConstructorParameter.class.getDeclaredConstructors()[0]; Invokable<?, ?> invokable = Invokable.from(constructor); assertEquals(1, invokable.getParameters().size()); assertEquals(TypeToken.of(String.class), invokable.getParameters().get(0).getType()); }
public void testInnerClassWithOneParameterConstructor() { Constructor<?> constructor = InnerWithOneParameterConstructor.class.getDeclaredConstructors()[0]; Invokable<?, ?> invokable = Invokable.from(constructor); assertEquals(1, invokable.getParameters().size()); assertEquals(TypeToken.of(String.class), invokable.getParameters().get(0).getType()); }
public void testInnerClassWithAnnotatedConstructorParameter() { Constructor<?> constructor = InnerWithAnnotatedConstructorParameter.class.getDeclaredConstructors()[0]; Invokable<?, ?> invokable = Invokable.from(constructor); assertEquals(1, invokable.getParameters().size()); assertEquals(TypeToken.of(String.class), invokable.getParameters().get(0).getType()); }
public void testEquals() { EqualsTester tester = new EqualsTester(); for (Method method : ParameterTest.class.getDeclaredMethods()) { for (Parameter param : Invokable.from(method).getParameters()) { tester.addEqualityGroup(param); } } tester.testEquals(); }
public void testConstructor_isOverridablel() throws Exception { Invokable<?, ?> delegate = Invokable.from(Foo.class.getDeclaredConstructor()); assertFalse(delegate.isOverridable()); assertFalse(delegate.isVarArgs()); }
public void testNonFinalMethodInFinalClass_isOverridable() throws Exception { Invokable<?, ?> delegate = Invokable.from(FinalClass.class.getDeclaredMethod("notFinalMethod")); assertFalse(delegate.isOverridable()); assertFalse(delegate.isVarArgs()); }
public void testInnerClassWithGenericConstructorParameter() { Constructor<?> constructor = InnerWithGenericConstructorParameter.class.getDeclaredConstructors()[0]; Invokable<?, ?> invokable = Invokable.from(constructor); assertEquals(2, invokable.getParameters().size()); assertEquals(new TypeToken<Iterable<String>>() {}, invokable.getParameters().get(0).getType()); assertEquals(TypeToken.of(String.class), invokable.getParameters().get(1).getType()); }
public void testLocalClassWithGenericConstructorParameter() throws Exception { class LocalWithGenericConstructorParameter { @SuppressWarnings("unused") // called by reflection LocalWithGenericConstructorParameter(Iterable<String> it, String s) {} } Constructor<?> constructor = LocalWithGenericConstructorParameter.class.getDeclaredConstructors()[0]; Invokable<?, ?> invokable = Invokable.from(constructor); assertEquals(2, invokable.getParameters().size()); assertEquals(new TypeToken<Iterable<String>>() {}, invokable.getParameters().get(0).getType()); assertEquals(TypeToken.of(String.class), invokable.getParameters().get(1).getType()); }
public void testConstructor_equals() throws NoSuchMethodException { Constructor<?> defaultConstructor = ArrayList.class.getConstructor(); Constructor<?> oneArgConstructor = ArrayList.class.getConstructor(int.class); new EqualsTester() .addEqualityGroup(Invokable.from(defaultConstructor), Invokable.from(defaultConstructor)) .addEqualityGroup(Invokable.from(oneArgConstructor)) .addEqualityGroup(new TypeToken<ArrayList<Integer>>() {}.constructor(defaultConstructor)) .addEqualityGroup(new TypeToken<ArrayList<String>>() {}.constructor(defaultConstructor)) .addEqualityGroup(new TypeToken<ArrayList<Integer>>() {}.constructor(oneArgConstructor)) .addEqualityGroup(new TypeToken<ArrayList<String>>() {}.constructor(oneArgConstructor)) .testEquals(); }
public void testMethod_equals() throws NoSuchMethodException { Method getMethod = List.class.getMethod("get", int.class); Method setMethod = List.class.getMethod("set", int.class, Object.class); new EqualsTester() .addEqualityGroup(Invokable.from(getMethod), Invokable.from(getMethod)) .addEqualityGroup(Invokable.from(setMethod)) .addEqualityGroup(new TypeToken<List<Integer>>() {}.method(getMethod)) .addEqualityGroup(new TypeToken<List<String>>() {}.method(getMethod)) .addEqualityGroup(new TypeToken<List<Integer>>() {}.method(setMethod)) .addEqualityGroup(new TypeToken<List<String>>() {}.method(setMethod)) .testEquals(); }