public <T> void visitCtInvocation(final spoon.reflect.code.CtInvocation<T> invocation) { spoon.reflect.code.CtInvocation<T> aCtInvocation = invocation.getFactory().Core().createInvocation(); this.builder.copy(invocation, aCtInvocation); aCtInvocation.setAnnotations(this.cloneHelper.clone(invocation.getAnnotations())); aCtInvocation.setTypeCasts(this.cloneHelper.clone(invocation.getTypeCasts())); aCtInvocation.setTarget(this.cloneHelper.clone(invocation.getTarget())); aCtInvocation.setExecutable(this.cloneHelper.clone(invocation.getExecutable())); aCtInvocation.setArguments(this.cloneHelper.clone(invocation.getArguments())); aCtInvocation.setComments(this.cloneHelper.clone(invocation.getComments())); this.cloneHelper.tailor(invocation, aCtInvocation); this.other = aCtInvocation; }
@Override public <T> void visitCtInvocation(CtInvocation<T> invocation) { CtInvocation<T> i = invocation.getFactory().Core().createInvocation(); i.setExecutable(invocation.getExecutable()); i.setTypeCasts(invocation.getTypeCasts()); boolean constant = true; i.setTarget(evaluate(invocation.getTarget())); if ((i.getTarget() != null) && !(i.getTarget() instanceof CtLiteral)) { constant = false; for (CtExpression<?> e : invocation.getArguments()) { CtExpression<?> re = evaluate(e); if (!(re instanceof CtLiteral)) { constant = false; i.addArgument(re); if (i.getExecutable().getSimpleName().equals(CtExecutableReference.CONSTRUCTOR_NAME)) { setResult(i); return; CtExecutable<?> executable = invocation.getExecutable().getDeclaration(); CtType<?> aType = invocation.getParent(CtType.class); CtTypeReference<?> execDeclaringType = invocation.getExecutable().getDeclaringType(); if (executable != null && aType != null && invocation.getType() != null && execDeclaringType != null && execDeclaringType.isSubtypeOf(aType.getReference())) { CtBlock<?> b = evaluate(executable.getBody()); r = RtHelper.invoke(i);
protected void removeInvocationArgument(CtInvocation<?> invocation) { invocation.removeArgument(invocation.getArguments().get(this.parameterIndex)); }
public <T> void visitCtInvocation(final spoon.reflect.code.CtInvocation<T> invocation) { spoon.reflect.code.CtInvocation other = ((spoon.reflect.code.CtInvocation) (this.stack.peek())); enter(invocation); biScan(spoon.reflect.path.CtRole.ANNOTATION, invocation.getAnnotations(), other.getAnnotations()); biScan(spoon.reflect.path.CtRole.CAST, invocation.getTypeCasts(), other.getTypeCasts()); biScan(spoon.reflect.path.CtRole.TARGET, invocation.getTarget(), other.getTarget()); biScan(spoon.reflect.path.CtRole.EXECUTABLE_REF, invocation.getExecutable(), other.getExecutable()); biScan(spoon.reflect.path.CtRole.ARGUMENT, invocation.getArguments(), other.getArguments()); biScan(spoon.reflect.path.CtRole.COMMENT, invocation.getComments(), other.getComments()); exit(invocation); }
public <T> void visitCtInvocation(CtInvocation<T> invocation) { CtInvocation<T> i = invocation.getFactory().Core().createInvocation(); i.setType(invocation.getType()); i.setExecutable(invocation.getExecutable()); boolean constant = true; i.setTarget(evaluate(i, invocation.getTarget())); if ((i.getTarget() != null) && !(i.getTarget() instanceof CtLiteral)) { constant = false; for (CtExpression<?> e : invocation.getArguments()) { CtExpression<?> re = evaluate(i, e); if (!(re instanceof CtLiteral)) { constant = false; i.addArgument(re); if (i.getExecutable().getSimpleName().equals("<init>")) { setResult(i); return; CtExecutable<?> executable = invocation.getExecutable().getDeclaration(); if ((executable != null) && (invocation.getType() != null) && invocation.getExecutable().getDeclaringType() .isAssignableFrom(((CtType<?>) invocation.getParent(CtType.class)).getReference())) { CtBlock<?> b = evaluate(invocation.getParent(), executable.getBody()); flowEnded = false; CtStatement last = b.getStatements().get(b.getStatements().size() - 1); CtLiteral<T> l = invocation.getFactory().Core().createLiteral(); l.setValue(r);
/** * Actually invokes from a compile-time invocation (by using runtime * reflection). */ @SuppressWarnings("unchecked") public static <T> T invoke(CtInvocation<T> i) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Object target = i.getTarget() == null ? null : ((CtLiteral<?>) i.getTarget()).getValue(); List<Object> args = new ArrayList<>(); for (CtExpression<?> e : i.getArguments()) { args.add(((CtLiteral<?>) e).getValue()); } Class<?> c = i.getExecutable().getDeclaringType().getActualClass(); ArrayList<Class<?>> argTypes = new ArrayList<>(); for (CtTypeReference<?> type : i.getExecutable().getActualTypeArguments()) { argTypes.add(type.getActualClass()); } return (T) c.getMethod(i.getExecutable().getSimpleName(), argTypes.toArray(new Class[0])) .invoke(target, args.toArray()); }
@Override public boolean matches(CtInvocation<?> invocation) { return invocation.getExecutable().isOverriding(executable); } }
enterCtStatement(invocation); enterCtExpression(invocation); if (invocation.getExecutable().isConstructor()) { elementPrinterHelper.writeActualTypeArguments(invocation.getExecutable()); CtType<?> parentType; try { parentType = invocation.getParent(CtType.class); } catch (ParentNotInitializedException e) { parentType = null; if (parentType != null && parentType.getQualifiedName() != null && parentType.getQualifiedName().equals(invocation.getExecutable().getDeclaringType().getQualifiedName())) { printer.writeKeyword("this"); } else { if (invocation.getTarget() != null && !invocation.getTarget().isImplicit()) { scan(invocation.getTarget()); printer.writeSeparator("."); boolean isImported = this.isImported(invocation.getExecutable()); if (!isImported) { try (Writable _context = context.modify()) { if (invocation.getTarget() instanceof CtTypeAccess) { _context.ignoreGenerics(true); if (invocation.getTarget() != null && !invocation.getTarget().isImplicit()) { scan(invocation.getTarget()); printer.writeSeparator("."); printer.writeIdentifier(invocation.getExecutable().getSimpleName());
@Override public CtInvocation<?> buildInvocationToAssertion(CtMethod<?> testMethod, AssertEnum assertion, List<CtExpression> arguments) { final Factory factory = InputConfiguration.get().getFactory(); //assertThat(actual) final CtInvocation<?> assertThat = createAssertThat(arguments.get(arguments.size() - 1)); //isXXX(expected) final CtInvocation invocation = factory.createInvocation(); final CtExecutableReference<?> executableReference = factory.Core().createExecutableReference(); executableReference.setSimpleName(assertion.toStringAccordingToClass(this.getClass())); executableReference.setDeclaringType(factory.Type().createReference(assertion.toStringAccordingToClass(this.getClass()))); invocation.setExecutable(executableReference); if (arguments.size() > 1) { invocation.setArguments(Collections.singletonList(arguments.get(0))); } invocation.setType(factory.Type().voidPrimitiveType()); invocation.setTarget(assertThat); invocation.putMetadata(METADATA_ASSERT_AMPLIFICATION, true); return invocation; }
@Override public void visitCtInvocation(CtInvocation invocation) { this.scan(invocation.getTypeCasts()); this.scan(invocation.getExecutable()); if (!this.isImportedInMethodImports(invocation.getExecutable())) { this.scan(invocation.getTarget()); } this.scan(invocation.getArguments()); }
public <T> void visitCtInvocation(CtInvocation<T> invocation) { enter(invocation); scan(invocation.getAnnotations()); scan(invocation.getType()); scanReferences(invocation.getTypeCasts()); scan(invocation.getTarget()); scan(invocation.getExecutable()); scan(invocation.getArguments()); exit(invocation); }
static List<Integer> replaceByLogStatement(CtMethod<?> clone) { final List<CtInvocation> assertionsToBeReplaced = clone.getElements(new TypeFilter<CtInvocation>(CtInvocation.class) { @Override public boolean matches(CtInvocation element) { return Util.isAssert.test(element) && super.matches(element); } }).stream() .sorted(Comparator.comparingInt(ctInvocation -> ctInvocation.getPosition().getLine())) .collect(Collectors.toList()); final List<Integer> indices = new ArrayList<>(); assertionsToBeReplaced.forEach(assertionToBeReplaced -> { final int index = assertionToBeReplaced.getParent(CtBlock.class).getStatements().indexOf(assertionToBeReplaced); indices.add(index); String snippet = "eu.stamp.project.assertfixer.asserts.log.Logger.log("; if (assertionToBeReplaced.getExecutable().getSimpleName().endsWith("True") || assertionToBeReplaced.getExecutable().getSimpleName().endsWith("False")) { assertionToBeReplaced.replace( clone.getFactory().createCodeSnippetStatement(snippet + index + "," + assertionToBeReplaced.getArguments().get(0) + ")") ); } else { assertionToBeReplaced.replace( clone.getFactory().createCodeSnippetStatement(snippet + index + "," + assertionToBeReplaced.getArguments().get(1) + ")") ); } }); return indices; }
private static boolean isGetter(CtInvocation invocation) { return invocation.getArguments().isEmpty() && MethodsHandler.isASupportedMethodName(invocation.getExecutable().getSimpleName()); }
boolean replaced = false; final CtElement valueToReplace = (CtElement) ((CtInvocation) testCaseToBeFix.getBody() .getStatement(index)).getArguments().get(0); final CtComment comment = factory.createComment("AssertFixer: old assertion " + testCaseToBeFix.getBody().getStatement(index).toString(), CtComment.CommentType.INLINE); String snippet = ((CtInvocation) testCaseToBeFix.getBody().getStatement(index)).getTarget().toString() + ".assert" + Logger.observations.get(index).toString().toUpperCase().substring(0, 1) + Logger.observations.get(index).toString().substring(1) + "(" + valueToReplace + ")"; testCaseToBeFix.getBody().getStatement(index).replace(factory.createCodeSnippetStatement(snippet)); } else if ("assertSame".equals(((CtInvocation) valueToReplace.getParent()).getExecutable().getSimpleName())) { ((CtInvocation) valueToReplace.getParent()).replace(factory.createCodeSnippetStatement( valueToReplace.getParent().toString().replace("assertSame", "assertNotSame") } else if ("assertNotSame".equals(((CtInvocation) valueToReplace.getParent()).getExecutable().getSimpleName())) { ((CtInvocation) valueToReplace.getParent()).replace(factory.createCodeSnippetStatement( valueToReplace.getParent().toString().replace("assertSame", "assertSame")
@Override public <T> void visitCtInvocation(CtInvocation<T> invocation) { if (invocation.getExecutable().isOverriding(S)) { CtFieldAccess<?> fa = null; if ((invocation.getTarget() instanceof CtFieldAccess)) { fa = (CtFieldAccess<?>) invocation.getTarget(); if (((invocation.getTarget() instanceof CtArrayAccess) && (((CtArrayAccess<?, CtExpression<?>>) invocation.getTarget()) .getTarget() instanceof CtFieldAccess))) { fa = (CtFieldAccess<?>) ((CtArrayAccess<?, CtExpression<?>>) invocation.getTarget()).getTarget(); if ((invocation.getParent() instanceof CtReturn) && (r instanceof CtBlock)) { ((CtReturn<?>) invocation.getParent()).replace((CtStatement) r); } else { invocation.replace((CtExpression) r);
public MethodTypingContext setInvocation(CtInvocation<?> invocation) { if (classTypingContext == null) { CtExpression<?> target = invocation.getTarget(); if (target != null) { CtTypeReference<?> targetTypeRef = target.getType(); if (targetTypeRef != null) { classTypingContext = new ClassTypingContext(targetTypeRef); } } } setExecutableReference(invocation.getExecutable()); return this; }
@Override public boolean visit(MessageSend messageSend, BlockScope scope) { CtInvocation<Object> inv = factory.Core().createInvocation(); inv.setExecutable(references.getExecutableReference(messageSend)); if (messageSend.binding instanceof ProblemMethodBinding) { // We are in a static complex in noclasspath mode. if (inv.getExecutable() != null && inv.getExecutable().getDeclaringType() != null) { inv.setTarget(factory.Code().createTypeAccess(inv.getExecutable().getDeclaringType(), inv.getExecutable().getDeclaringType().isAnonymous())); } if (messageSend.expectedType() != null) { inv.getExecutable().setType(references.getTypeReference(messageSend.expectedType())); } } context.enter(inv, messageSend); return true; }
/** * replace assertNotNull * * @param ctInvocation * @return */ private CtStatement replaceAssertNotNull(CtInvocation<?> ctInvocation) { List<?> arguments = ctInvocation.getArguments(); CtIf newIf = ctInvocation.getFactory().Core().createIf(); Object elem1 = arguments.get(0); CtExpression<Boolean> condition = ctInvocation.getFactory().Code() .createCodeSnippetExpression("(" + elem1 + ") != null"); newIf.setCondition(condition); // newIf.setThenStatement(getFactory().Code().createCodeSnippetStatement( // Debug.class.getCanonicalName() + ".printPC(\"Path Condition: \")")); /* * CtBlock<Object> thenStatement = ctInvocation.getFactory().Core() * .createBlock(); thenStatement .addStatement((getFactory().Code() * .createCodeSnippetStatement("System.out.println(\"Then...\")"))); * thenStatement.addStatement((getFactory().Code() * .createCodeSnippetStatement("System.out.println(" + * Debug.class.getCanonicalName() + ".getSolvedPC())"))); * newIf.setThenStatement(thenStatement); */ newIf.setThenStatement(createThen(ctInvocation)); return newIf; }
private void guardStageMethodCalls() { cxt.allNodes().forEach(node -> { List<CtInvocation<?>> invocations = node.filterBlocksForBuildingDeps((CtInvocation<?> inv) -> true); invocations.forEach(invocation -> { // the case when the declaring type is out of classpath, // to avoid NPE on the next line if (invocation.getExecutable().getDeclaringType() == null) return; CtExecutable<?> declaration = invocation.getExecutable().getDeclaration(); if (declaration == null || !(declaration instanceof CtMethod)) return; CtMethod<?> method = (CtMethod<?>) declaration; StageModel stage = cxt.getStageModelByStageMethod(method); if (stage != null && stage != node) { CtTargetedExpression guardedInvocation = stage.guardedStageMethodCall((CtInvocation) invocation, method); CtExpression<?> target = invocation.getTarget(); guardedInvocation.setTarget(target); if (target != null) target.setParent(guardedInvocation); invocation.replace(guardedInvocation); } }); }); }
final Factory factory = invocation.getFactory(); final TypeFilter<CtStatement> statementTypeFilter = new TypeFilter<CtStatement>(CtStatement.class) { @Override if (!(invocation.getMetadata(AssertGeneratorHelper.METADATA_ASSERT_AMPLIFICATION) != null && (boolean) invocation.getMetadata(AssertGeneratorHelper.METADATA_ASSERT_AMPLIFICATION))) { for (CtExpression<?> argument : invocation.getArguments()) { CtExpression clone = ((CtExpression) argument).clone(); if (clone instanceof CtUnaryOperator) { invocation.getParent(CtStatementList.class).insertBefore(statementTypeFilter, (CtStatement) clone); } else if (!(clone instanceof CtLiteral || clone instanceof CtVariableRead)) { CtTypeReference<?> typeOfParameter = clone.getType(); clone ); invocation.getParent(CtStatementList.class).insertBefore(statementTypeFilter, localVariable); } else if (clone instanceof CtVariableRead && !(clone instanceof CtFieldRead)) { final CtVariableReference variable = ((CtVariableRead) clone).getVariable(); final List<CtLocalVariable> assertedVariables = invocation.getParent(CtBlock.class).getElements( localVariable -> localVariable.getSimpleName().equals(variable.getSimpleName())