/** See doc in {@link CtMethod#copyMethod()} */ public static CtMethod<?> copyMethod(final CtMethod<?> method) { CtMethod<?> clone = method.clone(); StringBuilder tentativeTypeName = new StringBuilder(method.getSimpleName() + "Copy"); CtType parent = method.getParent(CtType.class); while (!parent.getMethodsByName(tentativeTypeName.toString()).isEmpty()) { tentativeTypeName.append("X"); } final String cloneMethodName = tentativeTypeName.toString(); clone.setSimpleName(cloneMethodName); parent.addMethod(clone); new CtScanner() { @Override public <T> void visitCtExecutableReference(CtExecutableReference<T> reference) { CtExecutable<T> declaration = reference.getDeclaration(); if (declaration == null) { return; } if (declaration == method) { reference.setSimpleName(cloneMethodName); } if (reference.getDeclaration() != clone) { throw new SpoonException("post condition broken " + reference); } super.visitCtExecutableReference(reference); } }.scan(clone); return clone; }
/** * Creates a method by copying an existing method. * * @param <T> * the type of the method * @param target * the target type where the new method has to be inserted to * @param source * the source method to be copied * @param redirectReferences * tells if all the references to the owning type of the source * method should be redirected to the target type (true is * recommended for most uses) * @return the newly created method */ public <T> CtMethod<T> create(CtType<?> target, CtMethod<T> source, boolean redirectReferences) { CtMethod<T> newMethod = source.clone(); if (redirectReferences && (source.getDeclaringType() != null)) { Substitution.redirectTypeReferences(newMethod, source.getDeclaringType().getReference(), target.getReference()); } target.addMethod(newMethod); return newMethod; }
/** * Creates a constructor into a target class by copying it from a source * method. * * @param target * the target class * @param source * the method to be copied * @return the new constructor */ @SuppressWarnings("unchecked") public <T> CtConstructor<T> create(CtClass<T> target, CtMethod<?> source) { CtMethod<T> method = (CtMethod<T>) source.clone(); CtConstructor<T> newConstructor = factory.Core().createConstructor(); newConstructor.setAnnotations(method.getAnnotations()); newConstructor.setBody(method.getBody()); newConstructor.setDocComment(method.getDocComment()); newConstructor.setFormalCtTypeParameters(method.getFormalCtTypeParameters()); newConstructor.setModifiers(method.getModifiers()); newConstructor.setParameters(method.getParameters()); target.addConstructor(newConstructor); return newConstructor; }
/** * Clones a method. * * @param method Method to be cloned * @param suffix Suffix for the cloned method's name * @return The cloned method */ public static CtMethod cloneMethod(CtMethod<?> method, String suffix) { CtMethod cloned_method = method.clone(); //rename the clone cloned_method.setSimpleName(method.getSimpleName() + (suffix.isEmpty() ? "" : suffix + cloneNumber)); cloneNumber++; CtAnnotation toRemove = cloned_method.getAnnotations().stream() .filter(annotation -> annotation.toString().contains("Override")) .findFirst().orElse(null); if (toRemove != null) { cloned_method.removeAnnotation(toRemove); } return cloned_method; }
@Override public CtMethod<?> minimize(CtMethod<?> amplifiedTestToBeMinimized) { final CtMethod<?> clone = amplifiedTestToBeMinimized.clone(); final long time = System.currentTimeMillis(); LOGGER.info("Inlining one time used variables..."); inlineLocalVariable(clone); LOGGER.info("Remove redundant assertions..."); removeRedundantAssertions(clone); LOGGER.info("Reduce {}, {} statements to {} statements in {} ms.", amplifiedTestToBeMinimized.getSimpleName(), amplifiedTestToBeMinimized.getBody().getStatements().size(), clone.getBody().getStatements().size(), System.currentTimeMillis() - time ); return clone; }
@Override public CtMethod<?> minimize(CtMethod<?> amplifiedTestToBeMinimized) { final CtMethod<?> clone = amplifiedTestToBeMinimized.clone(); final long time = System.currentTimeMillis(); LOGGER.info("Inlining one time used variables..."); inlineLocalVariable(clone); LOGGER.info("Remove redundant assertions..."); removeRedundantAssertions(clone); LOGGER.info("Reduce {}, {} statements to {} statements in {} ms.", amplifiedTestToBeMinimized.getSimpleName(), amplifiedTestToBeMinimized.getBody().getStatements().size(), clone.getBody().getStatements().size(), System.currentTimeMillis() - time ); return clone; }
@Override public CtMethod<?> minimize(CtMethod<?> amplifiedTestToBeMinimized) { final CtMethod<?> generalMinimize = super.minimize(amplifiedTestToBeMinimized); final CtMethod<?> changeMinimize = generalMinimize.clone(); final long time = System.currentTimeMillis(); final Failure failureToKeep = this.failurePerAmplifiedTest.get(amplifiedTestToBeMinimized); final List<CtInvocation> assertions = changeMinimize.filterChildren(TestFramework.ASSERTIONS_FILTER).list(); LOGGER.info("Minimizing {} assertions.", assertions.size()); assertions.forEach(invocation -> { DSpotUtils.printProgress(assertions.indexOf(invocation), assertions.size()); tryToRemoveAssertion(changeMinimize, invocation, failureToKeep ); } ); LOGGER.info("Reduce {}, {} statements to {} statements in {} ms.", amplifiedTestToBeMinimized.getSimpleName(), generalMinimize.getBody().getStatements().size(), changeMinimize.getBody().getStatements().size(), System.currentTimeMillis() - time ); // now that we reduce the amplified test, we must update the stack trace updateStackTrace(amplifiedTestToBeMinimized, changeMinimize); return changeMinimize; }
@Override public CtMethod<?> minimize(CtMethod<?> amplifiedTestToBeMinimized) { final CtMethod<?> generalMinimize = super.minimize(amplifiedTestToBeMinimized); final CtMethod<?> changeMinimize = generalMinimize.clone(); final long time = System.currentTimeMillis(); final Failure failureToKeep = this.failurePerAmplifiedTest.get(amplifiedTestToBeMinimized); final List<CtInvocation> assertions = changeMinimize.filterChildren(TestFramework.ASSERTIONS_FILTER).list(); LOGGER.info("Minimizing {} assertions.", assertions.size()); assertions.forEach(invocation -> { DSpotUtils.printProgress(assertions.indexOf(invocation), assertions.size()); tryToRemoveAssertion(changeMinimize, invocation, failureToKeep ); } ); LOGGER.info("Reduce {}, {} statements to {} statements in {} ms.", amplifiedTestToBeMinimized.getSimpleName(), generalMinimize.getBody().getStatements().size(), changeMinimize.getBody().getStatements().size(), System.currentTimeMillis() - time ); // now that we reduce the amplified test, we must update the stack trace updateStackTrace(amplifiedTestToBeMinimized, changeMinimize); return changeMinimize; }
private void tryToRemoveAssertion(CtMethod<?> amplifiedTestToBeMinimized, CtInvocation<?> invocation, Failure failureToKeep) { final CtMethod<?> clone = amplifiedTestToBeMinimized.clone(); clone.getBody().removeStatement(invocation); if (checkIfMinimizationIsOk(clone, failureToKeep)) { amplifiedTestToBeMinimized.getBody().removeStatement(invocation); } }
private void tryToRemoveAssertion(CtMethod<?> amplifiedTestToBeMinimized, CtInvocation<?> invocation, Failure failureToKeep) { final CtMethod<?> clone = amplifiedTestToBeMinimized.clone(); clone.getBody().removeStatement(invocation); if (checkIfMinimizationIsOk(clone, failureToKeep)) { amplifiedTestToBeMinimized.getBody().removeStatement(invocation); } }
IntStream.range(0, 2) .forEach(index -> { final CtMethod<?> clone = ctMethod.clone(); clone.setSimpleName(clone.getSimpleName() + "_" + index); testClass.addMethod(clone);
Counter.incNumberOfFailingAssertion(); Counter.incNumberOfFailingTestFromAssertion(); CtMethod<?> clone = testCaseToBeFix.clone(); classTestToBeFixed.removeMethod(testCaseToBeFix); classTestToBeFixed.addMethod(clone);