private void handleStaticReplacementMethods(Class<? extends EvoSuiteMock> mockClass) { for (Method m : mockClass.getMethods()) { StaticReplacementMethod srm = m.getAnnotation(StaticReplacementMethod.class); if (srm == null) { continue; } if (!Modifier.isStatic(m.getModifiers())) { throw new RuntimeException("EvoSuite Bug: improper annotations in class " + mockClass.getName()); } String target; if (OverrideMock.class.isAssignableFrom(mockClass)) { target = mockClass.getSuperclass().getCanonicalName(); } else { throw new RuntimeException("EvoSuite Bug: StaticReplacementMethod can only be used in OverrideMock"); } String desc = Type.getMethodDescriptor(m); addSpecialReplacementCall( new MethodCallReplacement(target.replace('.', '/'), m.getName(), desc, Opcodes.INVOKESPECIAL, mockClass.getCanonicalName().replace('.', '/'), m.getName(), desc, false, false)); } }
/** * Replace all the methods of {@code target} with a method (with same input * parameters) of mock subclass {@code mockClass}. * * @param mockClass * @param target * @throws IllegalArgumentException */ private void replaceAllInvokeSpecial(Class<?> mockClass, Class<?> target) throws IllegalArgumentException { if (!target.isAssignableFrom(mockClass)) { throw new IllegalArgumentException("Method replacement can be done only for subclasses. Class " + mockClass + " is not an instance of " + target); } // logger.debug("Static Mock: " + mockClass.getCanonicalName() + " for " + target.getCanonicalName()); for (Method method : mockClass.getMethods()) { String desc = Type.getMethodDescriptor(method); addSpecialReplacementCall(new MethodCallReplacement(target.getCanonicalName().replace('.', '/'), method.getName(), desc, Opcodes.INVOKESPECIAL, mockClass.getCanonicalName().replace('.', '/'), method.getName(), desc, false, false)); } }
/** * Replace all the constructors of {@code target} with a constructor (with * same input parameters) of mock subclass {@code mockClass}. * * @param mockClass * @param target * @throws IllegalArgumentException */ private void replaceAllConstructors(Class<?> mockClass, Class<?> target) throws IllegalArgumentException { if (!target.isAssignableFrom(mockClass)) { throw new IllegalArgumentException("Constructor replacement can be done only for subclasses. Class " + mockClass + " is not an instance of " + target); } for (Constructor<?> constructor : ReflectionUtils.getDeclaredConstructors(mockClass)) { String desc = Type.getConstructorDescriptor(constructor); addSpecialReplacementCall(new MethodCallReplacement(target.getCanonicalName().replace('.', '/'), "<init>", desc, Opcodes.INVOKESPECIAL, mockClass.getCanonicalName().replace('.', '/'), "<init>", desc, false, false)); } }