@Override public <R> void visitCtBlock(CtBlock<R> block) { if (child instanceof CtStatement) { block.addStatement((CtStatement) child); return; } super.visitCtBlock(block); }
/** * Creates a block. * * @param element * Statement of the block. * @param <T> * Subclasses of CtStatement. * @return a block. */ public <T extends CtStatement> CtBlock<?> createCtBlock(T element) { return factory.Core().createBlock().addStatement(element); }
private CtBlock<?> insertNewBlock(CtStatement stat) { CtBlock<?> block = target.getFactory().Core().createBlock(); block.addStatement(stat); insertType.insertFromFirstStatement(block, target, statementsToBeInserted); return block; } }
@Override public <R> void visitCtBlock(CtBlock<R> block) { CtBlock<?> b = block.getFactory().Core().createBlock(); for (CtStatement s : block.getStatements()) { CtElement res = evaluate(s); if (res != null) { if (res instanceof CtStatement) { b.addStatement((CtStatement) res); } else { //the context expects statement. We cannot simplify in this case b.addStatement(s.clone()); } } // do not copy unreachable statements if (flowEnded) { break; } } setResult(b); }
private static String createWrapperContent(final CtElement element, final Factory f, final CtTypeReference returnType) { CtClass<?> w = f.Class().create(WRAPPER_CLASS_NAME); CtBlock body = f.Core().createBlock(); if (element instanceof CtStatement) { body.addStatement((CtStatement) element); } else if (element instanceof CtExpression) { CtReturn ret = f.Core().createReturn(); ret.setReturnedExpression((CtExpression) element); body.addStatement(ret); } Set<ModifierKind> modifiers = EnumSet.of(ModifierKind.STATIC); Set<CtTypeReference<? extends Throwable>> thrownTypes = new HashSet<>(); thrownTypes.add(f.Class().<Throwable>get(Throwable.class).getReference()); f.Method().create( w, modifiers, returnType, WRAPPER_METHOD_NAME, CtElementImpl.<CtParameter<?>>emptyList(), thrownTypes, body); String contents = w.toString(); // Clean up (delete wrapper from factory) after it is printed. The DefaultJavaPrettyPrinter needs w in model to be able to print it correctly w.getPackage().removeType(w); return contents; }
block.setImplicit(true); for (CtStatement statement : ((Iterable<CtStatement>) value)) { block.addStatement(statement);
@Override public <R> void visitCtBlock(CtBlock<R> block) { if (child instanceof CtStatement) { block.addStatement((CtStatement) child); return; } super.visitCtBlock(block); }
/** * Creates a block. * * @param element * Statement of the block. * @param <T> * Subclasses of CtStatement. * @return a block. */ public <T extends CtStatement> CtBlock<?> createCtBlock(T element) { return factory.Core().createBlock().addStatement(element); }
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); }
public CtMethod<Void> getDoCloseMethod() { if (doCloseMethod != null) return doCloseMethod; getCloseMethod(); // ensure closeMethodStatements list is init doCloseMethod = createSimpleMethod(f().Type().VOID_PRIMITIVE, "doClose" + name); for (CtStatement statement : closeMethodStatements) { doCloseMethod.getBody().addStatement(statement); } return doCloseMethod; }
public <R> void visitCtBlock(CtBlock<R> block) { CtBlock<?> b = block.getFactory().Core().createBlock(); for (CtStatement s : block.getStatements()) { CtStatement res = evaluate(b, s); if (res != null) { b.addStatement(res); } // do not copy unreachable statements if (flowEnded) { break; } } if ((b.getStatements().size() == 1) && (b.getStatements().get(0) instanceof CtBlock)) { setResult(b.getStatements().get(0)); } else { setResult(b); } }
@Override public List<OperatorInstance> createOperatorInstances(ModificationPoint modificationPoint) { List<OperatorInstance> opInstances = new ArrayList<>(); CtStatement statementPointed = (CtStatement) modificationPoint.getCodeElement(); CtTry tryNew = MutationSupporter.getFactory().createTry(); List<CtCatch> catchers = new ArrayList<>(); CtCatch catchEx1 = MutationSupporter.getFactory().createCtCatch("e", Exception.class, new CtBlockImpl()); catchers.add(catchEx1); tryNew.setCatchers(catchers); CtBlock tryBoddy = new CtBlockImpl(); tryNew.setBody(tryBoddy); CtStatement stmtC = statementPointed.clone(); MutationSupporter.clearPosition(stmtC); tryBoddy.addStatement(stmtC); OperatorInstance opInstace = new StatementOperatorInstance(modificationPoint, this, statementPointed, tryNew); opInstances.add(opInstace); return opInstances; }
@SuppressWarnings({ "static-access", "unchecked", "rawtypes" }) private void writeCondition(String type, CtBlock parent, int id) { CtExpression conditionExp = ExpressionGenerator.fetchEXP(this.mutSupporter, this.modificationPoint, type, querytype); CtIf ifStmt = this.mutSupporter.getFactory().Core().createIf(); ifStmt.setCondition(conditionExp); ifStmt.setThenStatement((CtStatement)this.modificationPoint.getCodeElement()); parent.addStatement(id, ifStmt); parent.removeStatement((CtStatement)this.modificationPoint.getCodeElement()); saveSketchAndSynthesize(); parent.addStatement(id, (CtStatement)this.modificationPoint.getCodeElement()); parent.removeStatement(ifStmt); resoreDiskFile(); }
private static CtClass<?> createWrapper(CtStatement st, Factory f) { CtClass<?> w = f.Class().create("Wrapper"); CtBlock<Void> body = f.Core().createBlock(); body.addStatement(st); Set<ModifierKind> x = EnumSet.noneOf(ModifierKind.class); f.Method().create( w, x, f.Type().createReference(void.class), "wrap", CtElementImpl.<CtParameter<?>>emptyList(), CtElementImpl .<CtTypeReference<? extends Throwable>>emptySet(), body); return w; }
@Override public void visitCtIf(CtIf element) { super.visitCtIf(element); if(!(element.getThenStatement() instanceof CtBlock)){ CtStatement c = element.getThenStatement() ; CtBlock nBlock = MutationSupporter.getFactory().Core().createBlock(); nBlock.addStatement(c); element.setThenStatement(nBlock); } if( element.getElseStatement() != null && !(element.getElseStatement() instanceof CtBlock)){ CtStatement c = element.getElseStatement() ; CtBlock nBlock = MutationSupporter.getFactory().Core().createBlock(); nBlock.addStatement(c); element.setElseStatement(nBlock); } }
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) ) ); } }
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); } }
@Override public Optional<CtMethod<Void>> getCloseMethod() { if (closeMethod != null && !closeMethod.hasModifier(ABSTRACT)) { initCloseMethodStatements(); return of(closeMethod); } assert initField != null; closeMethodGenerated = true; if (closeMethod == null) { closeMethod = createSimpleMethod(f().Type().VOID_PRIMITIVE, closeMethodName()); cxt.bindClose(closeMethod, this); } else { closeMethod.setBody(f().Core().createBlock()); closeMethod.removeModifier(ABSTRACT); } closeMethod.getBody().addStatement(net.openhft.sg.SpoonUtils.reassignDefault(initField)); initCloseMethodStatements(); return of(closeMethod); }
private static <R, B extends R> CtClass<?> createWrapper( CtExpression<B> st, Factory f) { CtClass<?> w = f.Class().create("Wrapper"); CtBlock<B> body = f.Core().createBlock(); CtReturn<B> ret = f.Core().createReturn(); ret.setReturnedExpression(st); body.addStatement(ret); Set<ModifierKind> x = EnumSet.noneOf(ModifierKind.class); f.Method().create( w, x, f.Type().createReference(Object.class), "wrap", CtElementImpl.<CtParameter<?>>emptyList(), CtElementImpl .<CtTypeReference<? extends Throwable>>emptySet(), body); return w; }
private <T> CtMethod<T> fieldAccess(CtField<T> field) { @SuppressWarnings("unchecked") Map<CtField<T>, CtMethod<T>> fields = (Map<CtField<T>, CtMethod<T>>) (Map) this.fields; return fields.compute(field, (f, proxy) -> { if (proxy == null) { proxy = createSimpleMethod(f.getType(), f.getSimpleName()); } else if (proxy.hasModifier(ABSTRACT)) { proxy.setBody(f().Core().createBlock()); proxy.removeModifier(ABSTRACT); } else { return proxy; } addGuardingPrologue(proxy); CtReturn<T> ret = f().Core().createReturn(); ret.setReturnedExpression(f().Code().createVariableRead(f.getReference(), false)); proxy.getBody().addStatement(ret); cxt.bindAccessMethod(proxy, StageModel.this); return proxy; }); }