/** * Creates an invocation (can be a statement or an expression). * * @param <T> * the return type of the invoked method * @param target * the target expression (may be null for static methods) * @param executable * the invoked executable * @param arguments * the argument list * @return the new invocation */ public <T> CtInvocation<T> createInvocation(CtExpression<?> target, CtExecutableReference<T> executable, List<CtExpression<?>> arguments) { return factory.Core().<T>createInvocation().<CtInvocation<T>>setTarget(target).<CtInvocation<T>>setExecutable(executable).setArguments(arguments); }
@Override public <T> void visitCtInvocation(CtInvocation<T> invocation) { if (childJDT instanceof TypeReference && child instanceof CtTypeAccess) { invocation.getExecutable().addActualTypeArgument(((CtTypeAccess) child).getAccessedType()); return; } else if (child instanceof CtExpression) { if (hasChildEqualsToReceiver(invocation) || hasChildEqualsToQualification(invocation)) { if (child instanceof CtThisAccess) { final CtTypeReference<?> declaringType = invocation.getExecutable().getDeclaringType(); if (declaringType != null && invocation.getExecutable().isStatic() && child.isImplicit()) { invocation.setTarget(jdtTreeBuilder.getFactory().Code().createTypeAccess(declaringType, declaringType.isAnonymous())); } else { invocation.setTarget((CtThisAccess<?>) child); } } else { invocation.setTarget((CtExpression<?>) child); } } else { invocation.addArgument((CtExpression<?>) child); } return; } super.visitCtInvocation(invocation); }
@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; }
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; }
i.setTypeCasts(invocation.getTypeCasts()); boolean constant = true; i.setTarget(evaluate(invocation.getTarget())); if ((i.getTarget() != null) && !(i.getTarget() instanceof CtLiteral)) { constant = false;
@Nullable private static CtExpression buildConstructorWithFactory(CtTypeReference type, CtExpression<?>[] expressionsToAvoid) { final List<CtExpression<?>> constructorWithFactoryMethod = generateConstructorUsingFactory(type); if (!constructorWithFactoryMethod.isEmpty()) { CtExpression<?> selectedConstructor = constructorWithFactoryMethod .remove(RandomHelper.getRandom().nextInt(constructorWithFactoryMethod.size())); while (!constructorWithFactoryMethod.isEmpty() && Arrays.stream(expressionsToAvoid).anyMatch(selectedConstructor::equals)) { selectedConstructor = constructorWithFactoryMethod .remove(RandomHelper.getRandom().nextInt(constructorWithFactoryMethod.size())); } final CtTypeReference<?> declaringType = ((CtInvocation<?>) selectedConstructor).getExecutable().getDeclaringType(); ((CtInvocation<?>) selectedConstructor).setTarget(selectedConstructor.getFactory().createTypeAccess(declaringType)); return selectedConstructor; } return type.getFactory().createLiteral(null); }
@Override public <T> void visitCtInvocation(CtInvocation<T> invocation) { if (this.jdtTreeBuilder.context.isArgument(invocation) && child instanceof CtExpression) { invocation.addArgument((CtExpression<?>) child); return; } else if (child instanceof CtExpression) { invocation.setTarget((CtExpression<?>) child); return; } super.visitCtInvocation(invocation); }
/** * Creates an invocation (can be a statement or an expression). * * @param <T> * the return type of the invoked method * @param target * the target expression (may be null for static methods) * @param executable * the invoked executable * @param arguments * the argument list * @return the new invocation */ public <T> CtInvocation<T> createInvocation(CtExpression<?> target, CtExecutableReference<T> executable, List<CtExpression<?>> arguments) { return factory.Core().<T>createInvocation().<CtInvocation<T>>setTarget(target).<CtInvocation<T>>setExecutable(executable).setArguments(arguments); }
@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; }
private static <T> void processOverridden(CtClass<?> mergeInto, CtClass<?> toMerge, final CtMethod<T> methodToMerge) { List<CtInvocation<T>> superInvocations = mergeInto.getElements( new Filter<CtInvocation<T>>() { @Override public boolean matches(CtInvocation<T> invocation) { if (!(invocation.getTarget() instanceof CtSuperAccess)) return false; CtExecutable<?> m = invocation.getExecutable().getDeclaration(); return m != null && MethodNode.overrides((CtMethod<?>) m, methodToMerge); } }); methodToMerge.setSimpleName(classPrefixedName(toMerge, methodToMerge)); methodToMerge.setVisibility(PRIVATE); removeAnnotation(methodToMerge, Override.class); for (CtInvocation<T> superInvocation : superInvocations) { superInvocation.setTarget(null); superInvocation.setExecutable(methodToMerge.getReference()); } add(mergeInto, methodToMerge, mergeInto::addMethod); }
@SuppressWarnings("unchecked") private CtInvocation<?> createAssertThat(CtExpression<?> actual) { final Factory factory = InputConfiguration.get().getFactory(); final CtInvocation invocation = factory.createInvocation(); final CtExecutableReference executableReference = factory.Core().createExecutableReference(); executableReference.setStatic(true); executableReference.setSimpleName(ASSERT_THAT); executableReference.setDeclaringType(factory.Type().createReference(this.qualifiedNameOfAssertClass)); invocation.setExecutable(executableReference); invocation.setArguments(Collections.singletonList(actual)); invocation.setType(factory.Type().voidPrimitiveType()); invocation.setTarget(factory.createTypeAccess(factory.Type().createReference(this.qualifiedNameOfAssertClass))); invocation.putMetadata(METADATA_ASSERT_AMPLIFICATION, true); return invocation; }
final CtTypeAccess ta = factory.Core().createTypeAccess(); ta.setType(inv.getExecutable().getDeclaringType()); inv.setTarget(ta);
/** * Builds an invocation to <code>methodName</code> of {@link org.junit.Assert}. * This should be a correct method name such as assertEquals, assertTrue... * * @param assertion the type of the assertion method * @param arguments the arguments of the assertion, <i>e.g.</i> the two element to be compared in {@link org.junit.Assert#assertEquals(Object, Object)} * @return a spoon node representing the invocation to the assertion, ready to be inserted in a test method */ @Override public CtInvocation<?> buildInvocationToAssertion(CtMethod<?> testMethod, AssertEnum assertion, List<CtExpression> arguments) { final Factory factory = InputConfiguration.get().getFactory(); final CtInvocation invocation = factory.createInvocation(); final CtExecutableReference<?> executableReference = factory.Core().createExecutableReference(); executableReference.setStatic(true); executableReference.setSimpleName(assertion.toStringAccordingToClass(JUnitSupport.class)); executableReference.setDeclaringType(factory.Type().createReference(this.qualifiedNameOfAssertClass)); invocation.setExecutable(executableReference); invocation.setArguments(arguments); // TODO invocation.setType(factory.Type().voidPrimitiveType()); invocation.setTarget(factory.createTypeAccess(factory.Type().createReference(this.qualifiedNameOfAssertClass))); invocation.putMetadata(METADATA_ASSERT_AMPLIFICATION, true); return invocation; }
i.setExecutable(invocation.getExecutable()); boolean constant = true; i.setTarget(evaluate(i, invocation.getTarget())); if ((i.getTarget() != null) && !(i.getTarget() instanceof CtLiteral)) { constant = false;
invocation.setExecutable(executableReference); invocation.setType(factory.Type().voidPrimitiveType()); invocation.setTarget(factory.createTypeAccess(factory.Type().createReference(this.qualifiedNameOfAssertClass)));