private <T> boolean hasChildEqualsToReceiver(CtInvocation<T> ctInvocation) { if (!(jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof MessageSend)) { return false; } final MessageSend parent = (MessageSend) jdtTreeBuilder.getContextBuilder().stack.peek().node; // Receiver is equals to the jdt child. return parent.receiver != null && getFinalExpressionFromCast(parent.receiver).equals(childJDT) // Receiver not yet initialized. && !child.equals(ctInvocation.getTarget()); }
private <T> boolean hasChildEqualsToQualification(CtInvocation<T> ctInvocation) { if (!(jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof ExplicitConstructorCall)) { return false; } final ExplicitConstructorCall parent = (ExplicitConstructorCall) jdtTreeBuilder.getContextBuilder().stack.peek().node; // qualification is equals to the jdt child. return parent.qualification != null && getFinalExpressionFromCast(parent.qualification).equals(childJDT) // qualification not yet initialized. && !child.equals(ctInvocation.getTarget()); }
/** * 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()); }
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; }
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); }
printer.writeKeyword("this"); } else { if (invocation.getTarget() != null && !invocation.getTarget().isImplicit()) { scan(invocation.getTarget()); printer.writeSeparator("."); 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(".");
@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(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); }
@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; }
/** * 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<Object>(); for (CtExpression<?> e : i.getArguments()) { args.add(((CtLiteral<?>) e).getValue()); } Class<?> c = i.getExecutable().getDeclaringType().getActualClass(); ArrayList<Class<?>> argTypes = new ArrayList<Class<?>>(); for (CtTypeReference<?> type : i.getExecutable().getActualTypeArguments()) { argTypes.add(type.getActualClass()); } return (T) c.getMethod(i.getExecutable().getSimpleName(), argTypes.toArray(new Class[argTypes.size()])) .invoke(target, args.toArray()); }
private static List<CtFieldReference<?>> getVarargs(CtClass<? extends Template<?>> root, List<CtInvocation<?>> variables) { List<CtFieldReference<?>> fields = new ArrayList<CtFieldReference<?>>(); for (CtFieldReference<?> field : root.getReference().getAllFields()) { if (field.getType().getActualClass() == CtStatementList.class) { boolean alreadyAdded = false; for (CtInvocation<?> invocation : variables) { alreadyAdded |= ((CtFieldAccess<?>) invocation.getTarget()).getVariable().getDeclaration().equals(field); } if (!alreadyAdded) { fields.add(field); } } } return fields; }
private CtElement checkListStatements(List<?> teList) { for (Object tem : teList) { if (variables.contains(tem) && (tem instanceof CtInvocation)) { CtInvocation<?> listCand = (CtInvocation<?>) tem; boolean ok = listCand.getFactory().Type().createReference(TemplateParameter.class).isAssignableFrom(listCand.getTarget().getType()); return ok ? listCand : null; } if (tem instanceof CtVariable) { CtVariable<?> var = (CtVariable<?>) tem; String name = var.getSimpleName(); for (CtFieldReference<?> f : varArgs) { if (f.getSimpleName().equals(name)) { return f.getDeclaration(); } } } } return null; }
@SuppressWarnings({ "unchecked", "rawtypes" }) private boolean fetchInClassOverload(CtInvocation call, CtClass classname) { if(!call.getTarget().getType().getQualifiedName().equals(classname.getQualifiedName())) { return false; } String name = call.getExecutable().getSimpleName(); Set<CtMethod> methods=classname.getMethods(); List<CtMethod> overloadmethods = new ArrayList<CtMethod>(); for(CtMethod method: methods) { if(method.getSimpleName().equals(name)) overloadmethods.add(method); } if (overloadmethods.size() == 0) return false; if (overloadmethods.size()== 1) { return true; } List<String> origTypes = resolveTypes(call.getArguments()); if (origTypes == null) return true; for (CtMethod mtd : overloadmethods) { transformOneMethod(origTypes, resolveParams(mtd.getParameters()), call); } return true; }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public List<OperatorInstance> createOperatorInstances(ModificationPoint modificationPoint) { List<OperatorInstance> instances = new ArrayList<>(); CtInvocation invocation = (CtInvocation) modificationPoint.getCodeElement(); CtExpression targetExpresionOfMI = invocation.getTarget(); if (targetExpresionOfMI instanceof CtVariableAccess) { CtStatement parentStatement = modificationPoint.getCodeElement().getParent(CtStatement.class); CtExpression ifcondition = MutationSupporter.factory.createBinaryOperator(targetExpresionOfMI, MutationSupporter.factory.createCodeSnippetExpression("null").compile(), BinaryOperatorKind.NE); CtIf precodition = MutationSupporter.factory.createIf(); precodition.setCondition(ifcondition); precodition.setThenStatement(parentStatement); OperatorInstance operatorInstance = new StatementOperatorInstance(modificationPoint, this, parentStatement, precodition); instances.add(operatorInstance); } return instances; };
@Override public <T> void visitCtInvocation(CtInvocation<T> invocation) { enter(invocation); scan(invocation.getAnnotations()); scanReferences(invocation.getTypeCasts()); scan(invocation.getTarget()); scan(invocation.getArguments()); exit(invocation); }
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); } }); }); }
@SuppressWarnings({ "rawtypes", "unchecked" }) private void fetchLibOverload(CtInvocation call) { List<String> origTypes = resolveTypes(call.getArguments()); CtExpression exp = call.getTarget(); String typename=exp.getType().getQualifiedName(); if(typename==null||typename.isEmpty()) return; CtClass classname = parser.getClassMap().get(typename); if(classname!=null) { Set<CtMethod> overloadmethods=classname.getMethods(); for(CtMethod methodoverloaded : overloadmethods) { if(call.getExecutable().getSimpleName().equals(methodoverloaded.getSimpleName())) { List<CtParameter> paramlist=methodoverloaded.getParameters(); List<String> target=resolveParams(paramlist); transformOneMethod(origTypes,target, call); } } } else { List<Class[]> params = parser.fetchMethods(typename, call.getExecutable().getSimpleName()); for (Class[] p : params) transformOneConstructor(origTypes, call, p); } }
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); }