@Override public boolean matches(CtInvocation<?> invocation) { return invocation.getExecutable().isOverriding(executable); } }
/** * 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()); }
private boolean shouldInsertAfterSuper() { try { if (getParent() != null && getParent() instanceof CtConstructor && !getStatements().isEmpty()) { CtStatement first = getStatements().get(0); if (first instanceof CtInvocation && ((CtInvocation<?>) first).getExecutable().isConstructor()) { return true; } } } catch (ParentNotInitializedException ignore) { // CtBlock hasn't a parent. So, it isn't in a constructor. } return false; }
@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; }
@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()); }
/** * @return an invocation of {@link TemplateParameter#S()} if it is parent of `element` */ private ParameterElementPair transformTemplateParameterInvocationOfS(ParameterElementPair pep) { CtElement element = pep.element; if (element.isParentInitialized()) { CtElement parent = element.getParent(); if (parent instanceof CtInvocation<?>) { CtInvocation<?> invocation = (CtInvocation<?>) parent; CtExecutableReference<?> executableRef = invocation.getExecutable(); if ("S".equals(executableRef.getSimpleName())) { if (TemplateParameter.class.getName().equals(executableRef.getDeclaringType().getQualifiedName())) { /* * the invocation of TemplateParameter#S() has to be substituted */ return pep.copyAndSet(invocation); } } } } return pep; }
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; }
CtReference parentType; if (parent instanceof CtInvocation) { parentType = ((CtInvocation) parent).getExecutable(); } else { parentType = (CtReference) parent; typeReference = ((CtExecutableReference) parent).getDeclaringType(); } else { typeReference = ((CtInvocation) parent).getExecutable().getDeclaringType();
@Override public boolean visit(ExplicitConstructorCall explicitConstructor, BlockScope scope) { CtInvocation<Object> inv = factory.Core().createInvocation(); inv.setImplicit(explicitConstructor.isImplicitSuper()); inv.setExecutable(references.getExecutableReference(explicitConstructor.binding)); CtTypeReference<?> declaringType = inv.getExecutable().getDeclaringType(); inv.getExecutable().setType(declaringType == null ? null : (CtTypeReference<Object>) declaringType.clone()); context.enter(inv, explicitConstructor); return true; }
/** inserts all statements of `statementsToBeInserted` just before `target` */ public static void insertBefore(CtStatement target, CtStatementList statementsToBeInserted) throws ParentNotInitializedException { CtElement targetParent = target.getParent(); if (targetParent instanceof CtExecutable) { throw new SpoonException("cannot insert in this context (use insertEnd?)"); } try { if (target.getParent(CtConstructor.class) != null) { if (target instanceof CtInvocation && ((CtInvocation<?>) target).getExecutable().getSimpleName().startsWith(CtExecutableReference.CONSTRUCTOR_NAME)) { throw new SpoonException("cannot insert a statement before a super or this invocation."); } } } catch (ParentNotInitializedException ignore) { // no parent set somewhere } new InsertVisitor(target, statementsToBeInserted, InsertType.BEFORE).scan(targetParent); }
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); }
@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); }
return (T) ((Class) value).getSimpleName(); } else if (value instanceof CtInvocation) { return (T) getShortSignatureForJavadoc(((CtInvocation<?>) value).getExecutable()); } else if (value instanceof CtExecutableReference) { return (T) getShortSignatureForJavadoc((CtExecutableReference<?>) value);
/** * variable read/write of `variable` of type {@link TemplateParameter} * @param variable a variable whose references will be substituted * @return {@link PatternParameterConfigurator} to support fluent API */ public PatternParameterConfigurator byTemplateParameterReference(CtVariable<?> variable) { ParameterInfo pi = getCurrentParameter(); queryModel().map(new VariableReferenceFunction(variable)) .forEach((CtVariableReference<?> varRef) -> { /* * the target of substitution is always the invocation of TemplateParameter#S() */ CtVariableAccess<?> varAccess = (CtVariableAccess<?>) varRef.getParent(); CtElement invocationOfS = varAccess.getParent(); if (invocationOfS instanceof CtInvocation<?>) { CtInvocation<?> invocation = (CtInvocation<?>) invocationOfS; if ("S".equals(invocation.getExecutable().getSimpleName())) { addSubstitutionRequest(pi, invocation); return; } } throw new SpoonException("TemplateParameter reference is NOT used as target of invocation of TemplateParameter#S()"); }); return this; }
enterCtStatement(invocation); enterCtExpression(invocation); if (invocation.getExecutable().isConstructor()) { elementPrinterHelper.writeActualTypeArguments(invocation.getExecutable()); CtType<?> parentType; try { parentType = null; if (parentType != null && parentType.getQualifiedName() != null && parentType.getQualifiedName().equals(invocation.getExecutable().getDeclaringType().getQualifiedName())) { printer.writeKeyword("this"); } else { boolean isImported = this.isImported(invocation.getExecutable()); if (!isImported) { try (Writable _context = context.modify()) { getPrinterHelper().adjustStartPosition(invocation); printer.writeIdentifier(invocation.getExecutable().getSimpleName());
@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; 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();
public <T> void visitCtInvocation(final CtInvocation<T> invocation) { enter(invocation); scan(CtRole.ANNOTATION, invocation.getAnnotations()); scan(CtRole.CAST, invocation.getTypeCasts()); scan(CtRole.TARGET, invocation.getTarget()); scan(CtRole.EXECUTABLE_REF, invocation.getExecutable()); scan(CtRole.ARGUMENT, invocation.getArguments()); scan(CtRole.COMMENT, invocation.getComments()); exit(invocation); }
} else if (parent instanceof CtInvocation) { CtInvocation invo = (CtInvocation) parent; CtExecutableReference localReference = invo.getExecutable(); declaringType = localReference.getDeclaringType(); reference = localReference;
@java.lang.Override public <T> void visitCtInvocation(final spoon.reflect.code.CtInvocation<T> invocation) { replaceInListIfExist(invocation.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(invocation)); replaceInListIfExist(invocation.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(invocation)); replaceElementIfExist(invocation.getTarget(), new spoon.support.visitor.replace.ReplacementVisitor.CtTargetedExpressionTargetReplaceListener(invocation)); replaceElementIfExist(invocation.getExecutable(), new spoon.support.visitor.replace.ReplacementVisitor.CtAbstractInvocationExecutableReplaceListener(invocation)); replaceInListIfExist(invocation.getArguments(), new spoon.support.visitor.replace.ReplacementVisitor.CtAbstractInvocationArgumentsReplaceListener(invocation)); replaceInListIfExist(invocation.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(invocation)); }
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; }