/** * Creates a new invocation filter. * * @param method * the executable to be tested for being invoked. */ public InvocationFilter(CtMethod<?> method) { this(method.getReference()); }
@Override public Collection<CtExecutableReference<?>> getAllExecutables() { Set<CtExecutableReference<?>> l = new SignatureBasedSortedSet(); for (CtMethod<?> m : getAllMethods()) { l.add(m.getReference()); } return l; }
@Override @SuppressWarnings("unchecked") public <S extends T> CtExecutableReference<S> getOverridingExecutable(CtTypeReference<?> subType) { if ((subType == null) || subType.equals(getDeclaringType())) { return null; } CtType<?> t = subType.getDeclaration(); if (t == null) { return null; } if (!(t instanceof CtClass)) { return null; } CtClass<?> c = (CtClass<?>) t; for (CtMethod<?> m : c.getMethods()) { if (m.getReference().isOverriding(this)) { return (CtExecutableReference<S>) m.getReference(); } } return getOverridingExecutable(c.getSuperclass()); }
@SuppressWarnings("unchecked") private static List<CtInvocation<?>> buildSnippetAssertCollection(Factory factory, CtMethod<?> testMethod, String expression, Collection value) { final CtVariableAccess variableRead = factory.createVariableRead( factory.createLocalVariableReference().setSimpleName(expression), false ); final CtExecutableReference contains = factory.Type().get(Collection.class).getMethodsByName("contains").get(0).getReference(); return (List<CtInvocation<?>>) value.stream() .limit(Math.min(value.size(), MAX_NUMBER_OF_CHECKED_ELEMENT_IN_LIST)) .map(factory::createLiteral) .map(o -> TestFramework.get().buildInvocationToAssertion( testMethod, AssertEnum.ASSERT_TRUE, Collections.singletonList(factory.createInvocation(variableRead, contains, (CtLiteral) o ) ) ) ) .collect(Collectors.toList()); }
@Override @SuppressWarnings("unchecked") public <S extends T> CtExecutableReference<S> getOverridingExecutable( CtTypeReference<?> subType) { if ((subType == null) || subType.equals(getDeclaringType())) { return null; } CtType<?> t = subType.getDeclaration(); if (t == null) { return null; } if (!(t instanceof CtClass)) { return null; } CtClass<?> c = (CtClass<?>) t; for (CtMethod<?> m : c.getMethods()) { if (m.getReference().isOverriding(this)) { return (CtExecutableReference<S>) m.getReference(); } } return getOverridingExecutable(c.getSuperclass()); }
public <T> CtTargetedExpression<T, CtExpression<?>> fieldAccess( CtExpression<?> target, CtField<T> field) { if (!fields.containsKey(field)) throw new StageGraphCompilationException(field + " doesn't belong to " + this); return f().Code().createInvocation(target,fieldAccess(field).getReference()); }
public CtExpression<?> buildInvocationFromString(String invocationAsString) { final CtExpression<?> invocation = this.buildInvocationFromString(invocationAsString, null); if (invocation instanceof CtInvocation<?> && "isEmpty".equals(((CtInvocation) invocation).getExecutable().getSimpleName())) { return invocation; } if (invocationAsString.endsWith("isEmpty()")) { final CtType<?> listCtType = factory.Type() .get(java.util.List.class); final CtMethod<?> isEmpty = listCtType.getMethodsByName("isEmpty").get(0); return factory.createInvocation( invocation, isEmpty.getReference() ); } else { return invocation; } }
protected Optional<CtMethod<Void>> getCloseDependantsMethod() { if (closeDependantsMethod != null) return of(closeDependantsMethod); if (dependantsVia.isEmpty()) return empty(); if (!dependantsVia.stream() .map(DependencyNode::getCloseMethod) .anyMatch(Optional::isPresent)) { return empty(); } closeDependantsMethod = createSimpleMethod(f().Type().VOID_PRIMITIVE, "close" + name + "Dependants"); cxt.bindCloseDependants(closeDependantsMethod, this); for (DependencyNode dependant : topologicallySortedDependants()) { Optional<CtMethod<Void>> dependantCloseMethod = dependant.getCloseMethod(); if (dependantCloseMethod.isPresent()) { net.openhft.sg.CompilationNode dependantNode = cxt.getCompilationNode(dependant.declaringType); net.openhft.sg.CompilationNode thisNode = cxt.getCompilationNode(declaringType); CtExpression<?> dependantAccess = thisNode.access(dependantNode, AccessType.Read); closeDependantsMethod.getBody().addStatement(f().Code().createInvocation( dependantAccess, dependantCloseMethod.get().getReference())); } } return of(closeDependantsMethod); }
.map(method -> factory.createInvocation(factory.createTypeAccess(method.getParent(CtType.class).getReference(), true), method.getReference(), method.getParameters().stream() .map(parameter -> ValueCreator.generateRandomValue(parameter.getType(), 0))
void generateGlobalClose() { Factory f = root.f; CtBlock<Void> closeBody = f.Core().createBlock(); List<StageModel> stageModels = topologicallySorted(cxt.allStageModels().collect(Collectors.toList())); Collections.reverse(stageModels); // close dependant stages first stageModels.forEach(stage -> { CompilationNode refNode = cxt.getCompilationNode(stage.declaringType); CtExpression<?> access = root.access(refNode, AccessType.Read); closeBody.addStatement( f.Code().createInvocation(access, stage.getDoCloseMethod().getReference())); }); CtClass rootClass = root.classesToMerge.get(0); rootClass.addSuperInterface(f.Type().createReference(AutoCloseable.class)); f.Method().create(rootClass, EnumSet.of(PUBLIC), f.Type().VOID_PRIMITIVE, "close", emptyList(), Collections.emptySet(), closeBody); }
@SuppressWarnings("unchecked") private static List<CtInvocation<?>> buildSnippetAssertMap(Factory factory, CtMethod<?> testMethod, String expression, Map value) { final CtVariableAccess variableRead = factory.createVariableRead( factory.createLocalVariableReference().setSimpleName(expression), false ); final CtExecutableReference containsKey = factory.Type().get(Map.class).getMethodsByName("containsKey").get(0).getReference(); final CtExecutableReference get = factory.Type().get(Map.class).getMethodsByName("get").get(0).getReference(); return (List<CtInvocation<?>>) value.keySet().stream() .flatMap(key -> Arrays.stream(new CtInvocation<?>[]{ TestFramework.get().buildInvocationToAssertion(testMethod, AssertEnum.ASSERT_TRUE, Collections.singletonList(factory.createInvocation(variableRead, containsKey, factory.createLiteral(key) ) ) ), TestFramework.get().buildInvocationToAssertion(testMethod, AssertEnum.ASSERT_EQUALS, Arrays.asList(factory.createLiteral(value.get(key)), factory.createInvocation(variableRead, get, factory.createLiteral(key)) ) ) } ) ).collect(Collectors.toList()); }
private CtInvocation<Boolean> createStageInitInvocation() { return f().Code().createInvocation(thisAccess(), getStageInitMethod().getReference()); }
@Override public void generateAfterClassToSaveObservations(CtType<?> testClass, List<CtMethod<?>> testsToRun) { // get AfterClassMethod is exist otherwise use initAfterClassMethod final Factory factory = testClass.getFactory(); final CtMethod<?> afterClassMethod = testClass.getMethods() .stream() .filter(method -> method.getAnnotations() .stream() .anyMatch(ctAnnotation -> this.getFullQualifiedNameOfAnnotationAfterClass().equals(ctAnnotation.getAnnotationType().getQualifiedName()) ) ).findFirst() .orElse(initAfterClassMethod(factory)); final CtTypeReference<?> ctTypeReference = factory.createCtTypeReference(ObjectLog.class); final CtExecutableReference<?> reference = ctTypeReference .getTypeDeclaration() .getMethodsByName("save") .get(0) .getReference(); afterClassMethod.getBody().insertEnd( factory.createInvocation(factory.createTypeAccess(ctTypeReference), reference) ); testClass.addMethod(afterClassMethod); }
private static CtMethod<?> initializeTestDownAfterClassMethod(Factory factory, CtClass<?> testClass) { final CtType<?> loggerType = factory.Type().get(Logger.class); final CtMethod<?> save = loggerType.getMethodsByName("save").get(0); final CtMethod tearDownAfterClass = factory.createMethod( testClass, new HashSet<>(Arrays.asList(ModifierKind.PUBLIC, ModifierKind.STATIC)), factory.Type().VOID_PRIMITIVE, "tearDownAfterClass", Collections.emptyList(), Collections.emptySet(), factory.createCtBlock( factory.createInvocation(factory.Code().createTypeAccess(loggerType.getReference()), save.getReference())) ); final CtAnnotation annotation = factory.createAnnotation(); final CtTypeReference reference = factory.Type().createReference(AfterClass.class); annotation.setAnnotationType(reference); tearDownAfterClass.addAnnotation(annotation); return tearDownAfterClass; } }
public <T> CtTargetedExpression<T, CtExpression<?>> guardedStageMethodCall( CtInvocation<T> invocation, CtMethod<T> stageMethod) { if (!stageMethods.containsKey(stageMethod)) throw new StageGraphCompilationException(stageMethod + " doesn't belong to " + this); @SuppressWarnings("unchecked") Map<CtMethod<T>, CtMethod<T>> stageMethods = (Map<CtMethod<T>, CtMethod<T>>) (Map) this.stageMethods; return f().Code().createInvocation(null, stageMethods.computeIfAbsent(stageMethod, m -> { CtMethod<T> guarded = createSimpleMethod(m.getType(), m.getSimpleName() + "Guarded"); addGuardingPrologue(guarded); guarded.setParameters(new ArrayList<>(m.getParameters())); List<CtExpression<?>> arguments = m.getParameters().stream() .map(p -> p.getReference()) .map(pr -> f().Code().createVariableRead(pr, false)) .collect(toList()); CtInvocation<T> innerInvocation = f().Code().createInvocation(null, m.getReference(), arguments); if (m.getType().equals(f().Type().VOID_PRIMITIVE)) { guarded.getBody().addStatement(innerInvocation); } else { CtReturn<T> ctReturn = f().Core().createReturn(); ctReturn.setReturnedExpression(innerInvocation); guarded.getBody().addStatement(ctReturn); } return guarded; }).getReference(), invocation.getArguments()); }
private void addAssertionOnException(CtMethod<?> testMethod, CtCatch ctCatch, Failure failure) { final Factory factory = ctCatch.getFactory(); final CtCatchVariable<? extends Throwable> parameter = ctCatch.getParameter(); final CtInvocation<?> getMessage = factory.createInvocation( factory.createVariableRead(parameter.getReference(), false), factory.Class().get(java.lang.Throwable.class).getMethodsByName("getMessage").get(0).getReference() ); if (!AssertGeneratorHelper.containsObjectReferences(failure.messageOfFailure)) { ctCatch.getBody().addStatement( this.buildInvocationToAssertion( testMethod, AssertEnum.ASSERT_EQUALS, Arrays.asList(factory.createLiteral(failure.messageOfFailure), getMessage) ) ); } }
CtInvocation newInvocation = factory.Code().createInvocation(targetClone, methodToInvokeToAdd.getReference(), arguments);
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 static CtElement addSaveStatementInTearDownAfterClass(CtClass<?> testClass) { final Factory factory = testClass.getFactory(); CtMethod<?> testDownAfterClass = testClass.filterChildren(new TypeFilter<CtMethod>(CtMethod.class) { @Override public boolean matches(CtMethod element) { return element.getAnnotations().contains(factory.Annotation().get(AfterClass.class)); } }).first(); boolean methodCreated = false; if (testDownAfterClass == null) { methodCreated = true; testDownAfterClass = initializeTestDownAfterClassMethod(factory, testClass); } final CtType<?> loggerType = factory.Type().get(Logger.class); final CtMethod<?> save = loggerType.getMethodsByName("save").get(0); CtInvocation invocation = factory.createInvocation(factory.Code().createTypeAccess(loggerType.getReference()), save.getReference()); testDownAfterClass.getBody().insertEnd( invocation ); if (methodCreated) { return testDownAfterClass; } else { return invocation; } }
private <T> void addGuardingPrologue(CtMethod<T> proxy) { if (noArgInitStageMethod != null) { CtIf ctIf = createNotInitIf(); ctIf.setThenStatement(f().Code().createInvocation(thisAccess(), noArgInitStageMethod.getReference())); proxy.getBody().addStatement(ctIf); } else { CtAssert<String> ctAssert = f().Core().createAssert(); ctAssert.setAssertExpression(createStageInitInvocation()); ctAssert.setExpression(f().Code().createLiteral(name + " should be init")); proxy.getBody().addStatement(ctAssert); } }