public static void insertAfter(CtStatement target, CtStatement statement) throws ParentNotInitializedException { CtStatementList sts = target.getFactory().Core().createStatementList(); sts.addStatement(statement); insertAfter(target, sts); }
/** insert `statement` just before target */ public static void insertBefore(CtStatement target, CtStatement statement) throws ParentNotInitializedException { CtStatementList sts = target.getFactory().Core().createStatementList(); sts.addStatement(statement); insertBefore(target, sts); }
private CtBlock<?> insertNewBlock(CtStatement stat) { CtBlock<?> block = target.getFactory().Core().createBlock(); block.addStatement(stat); insertType.insertFromFirstStatement(block, target, statementsToBeInserted); return block; } }
private static CtStatement internalCompileStatement(CtElement st, CtTypeReference returnType) { Factory f = st.getFactory(); String contents = createWrapperContent(st, f, returnType); build(f, contents); CtType<?> c = f.Type().get(WRAPPER_CLASS_NAME); // Get the part we want CtMethod<?> wrapper = c.getMethod(WRAPPER_METHOD_NAME); List<CtStatement> statements = wrapper.getBody().getStatements(); CtStatement ret = statements.get(statements.size() - 1); // Clean up c.getPackage().removeType(c); //disconnect element from the parent, so it can be added to another model ret.delete(); ret.setParent(null); if (ret instanceof CtClass) { CtClass klass = (CtClass) ret; ret.getFactory().Package().getRootPackage().addType(klass); klass.setSimpleName(klass.getSimpleName().replaceAll("^[0-9]*", "")); } return ret; }
public static CtBlock<?> asBlock(CtStatement statement, CtElement parent) { CtBlock<?> block; if (isBlock(statement)) { block = (CtBlock<?>) statement; } else { block = newBlock(statement.getFactory(), statement); } setParent(parent, block); return block; }
public static void insertBefore(CtStatement target, CtStatement statement) throws ParentNotInitializedException { CtStatementList sts = target.getFactory().Core().createStatementList(); sts.addStatement(statement); insertBefore(target, sts); }
@Override public void process(CtStatement element) { String oldStatement = element.toString(); CtCodeSnippetExpression<Boolean> snippet = null; if (getValue() != null) { if (getValue().equals("1")) { snippet = element.getFactory().Code() .createCodeSnippetExpression("true"); } else if (getValue().equals("0")) { snippet = element.getFactory().Code() .createCodeSnippetExpression("false"); } else { snippet = element.getFactory().Code() .createCodeSnippetExpression(getValue()); } } else { snippet = element .getFactory() .Code() .createCodeSnippetExpression( Debug.class.getCanonicalName() + ".makeSymbolicBoolean(\"guess_fix\")"); } CtExpression<Boolean> condition = getCondition(element); condition.replace(snippet); logger.debug("Replacing:\n{}\nby:\n{}", oldStatement, element); }
public static void insertAfter(CtStatement target, CtStatement statement) throws ParentNotInitializedException { CtStatementList sts = target.getFactory().Core().createStatementList(); sts.addStatement(statement); insertAfter(target, sts); }
@Override public CtIf processCondition(CtStatement element, String newCondition) { CtCodeSnippetExpression<Boolean> snippet = element.getFactory().Core().createCodeSnippetExpression(); snippet.setValue(newCondition); CtExpression<Boolean> condition = getCondition(element); condition.replace(snippet); return (CtIf) element; } }
private static CtStatement internalCompileStatement(CtStatement st) { Factory f = st.getFactory(); CtClass<?> w = createWrapper(st, f); compile(f, w); CtType<?> c = f.Type().get("Wrapper"); // Get the part we want CtMethod<?> wrapper = Query.getElements(c, new Filter<CtMethod<?>>() { public boolean matches(CtMethod<?> element) { return element.getSimpleName().equals("wrap"); } }).get(0); CtStatement ret = wrapper.getBody().getStatements().get(0); // Clean up c.getPackage().getTypes().remove(c); // check typing? return ret; }
@Override public void process(CtStatement statement) { String evaluationAccess = "runtimeAngelicValue"; // create the angelic value String type = subprocessor.getType().getCanonicalName(); CtLocalVariable<?> evaluation = newLocalVariableDeclaration( statement.getFactory(), subprocessor.getType(), evaluationAccess, "(" + type + ")" + this.getClass().getCanonicalName() + ".getValue(" + subprocessor.getDefaultValue() + ")"); // insert angelic value before the statement insertBeforeUnderSameParent(evaluation, statement); // collect values of the statement appendValueCollection(statement, evaluationAccess); // insert angelic value in the condition subprocessor.setValue("runtimeAngelicValue"); subprocessor().process(statement); }
private static <T> CtIf collectionWrappingIf(List<CtStatement> collectingStatements, RuntimeValues<T> runtimeValues, CtStatement insertionPoint) { Factory factory = insertionPoint.getFactory(); CtStatement newBlock = newBlock(factory, collectingStatements); CtExpression<Boolean> isEnabled = newExpressionFromSnippet(factory, runtimeValues.isEnabledInquiry(), Boolean.class); CtIf newIf = newIf(factory, isEnabled, newBlock); insertBeforeUnderSameParent(newIf, insertionPoint); return newIf; }
public void process(CtStatement element) { logger.debug("##### {} ##### Before:\n{}", element, element.getParent()); CtElement parent = element.getParent(); CtIf newIf = element.getFactory().Core().createIf(); CtCodeSnippetExpression<Boolean> condition; if (getValue() != null) { switch (getValue()) { case "1": condition = element.getFactory().Code() .createCodeSnippetExpression("true"); break; case "0": condition = element.getFactory().Code() .createCodeSnippetExpression("false"); break; default: condition = element.getFactory().Code() .createCodeSnippetExpression(getValue()); .getFactory() .Code() .createCodeSnippetExpression(
final CtTypeAccess<ObjectLog> typeAccess = stmt.getFactory().createTypeAccess( stmt.getFactory().Type().createReference(ObjectLog.class) ); final CtExecutableReference objectLogExecRef = stmt.getFactory().createExecutableReference() .setStatic(true) .setDeclaringType(stmt.getFactory().Type().createReference(ObjectLog.class)) .setSimpleName("log"); objectLogExecRef.setType(stmt.getFactory().Type().voidPrimitiveType()); final CtInvocation invocationToObjectLog = stmt.getFactory().createInvocation(typeAccess, objectLogExecRef); } else if (stmt instanceof CtLocalVariable) { CtLocalVariable localVar = (CtLocalVariable) stmt; final CtVariableAccess variableRead = stmt.getFactory().createVariableRead(localVar.getReference(), false);// TODO checks static invocationToObjectLog.addArgument(variableRead); invocationToObjectLog.addArgument(stmt.getFactory().createLiteral(localVar.getSimpleName())); insertAfter = stmt; } else if (stmt instanceof CtAssignment) { CtAssignment localVar = (CtAssignment) stmt; invocationToObjectLog.addArgument(localVar.getAssigned()); invocationToObjectLog.addArgument(stmt.getFactory().createLiteral(localVar.getAssigned().toString())); insertAfter = stmt; } else if (stmt instanceof CtInvocation) { if (isVoidReturn(invocation)) { invocationToObjectLog.addArgument(invocation.getTarget()); invocationToObjectLog.addArgument(stmt.getFactory().createLiteral( invocation.getTarget().toString().replace("\"", "\\\""))
@Override public CtIf processCondition(CtStatement element, String newCondition) { //logger.debug("##### {} ##### Before:\n{}", element, element.getParent()); // if the element is not a line if (!new LineFilter().matches(element)) { element = element.getParent(new LineFilter()); } CtElement parent = element.getParent(); CtIf newIf = element.getFactory().Core().createIf(); CtCodeSnippetExpression<Boolean> condition = element.getFactory().Core().createCodeSnippetExpression(); condition.setValue(newCondition); newIf.setCondition(condition); // Fix : warning: ignoring inconsistent parent for [CtElem1] ( [CtElem2] != [CtElem3] ) newIf.setParent(parent); element.replace(newIf); // this should be after the replace to avoid an StackOverflowException caused by the circular reference. newIf.setThenStatement(element); //logger.debug("##### {} ##### After:\n{}", element, element.getParent().getParent()); return newIf; } }
public static <T> CtStatement runtimeCollectionBefore(CtStatement location, Map<String, String> inputs, Multimap<String, String> getters, String output, RuntimeValues<T> runtime) { Factory factory = location.getFactory(); List<CtStatement> newStatements = MetaList.newLinkedList(); addCollectionStatementFor(newStatements, factory, runtime.invocationOnCollectionStart()); addVariableCollection(inputs, runtime, factory, newStatements); addGetterCollection(getters, runtime, factory, newStatements); addCollectionStatementFor(newStatements, factory, runtime.invocationOnOutputCollection(output)); addCollectionStatementFor(newStatements, factory, runtime.invocationOnCollectionEnd()); return collectionWrappingIf(newStatements, runtime, location); }
return; } else { CtBlock<?> block = target.getFactory().Core().createBlock(); target.setParent(block); block.addStatement(stat); return; } else { CtBlock<?> block = target.getFactory().Core().createBlock(); block.getStatements().add(stat); ((CtLoop) targetParent).setBody(block);
@Override public void process(CtStatement statement) { String evaluationAccess = "runtimeAngelicValue"; CtLocalVariable defaultValue = newLocalVariableDeclarationString(statement.getFactory(), cl, "spoonDefaultValue", "false"); insertBeforeUnderSameParent(defaultValue, statement); CtTry aTry = getFactory().Core().createTry(); CtAssignment variableAssignment = getFactory().Code().createVariableAssignment(defaultValue.getReference(), false, getFactory().Code().createCodeSnippetExpression(subprocessor().getDefaultValue())); aTry.setBody(getFactory().Code().createCtBlock(variableAssignment)); CtCatch aCatch = getFactory().Core().createCatch(); CtCatchVariable<Exception> nopolProcessorException = getFactory().Code().createCatchVariable(getFactory().Type().createReference(Exception.class), "__NopolProcessorException"); aCatch.setParameter(nopolProcessorException); aCatch.setBody(getFactory().Core().createBlock()); aTry.addCatcher(aCatch); insertBeforeUnderSameParent(aTry, statement); String evaluationValue = angelicInvocation("spoonDefaultValue"); CtLocalVariable<T> evaluation = newLocalVariableDeclarationString(statement.getFactory(), cl, evaluationAccess, evaluationValue); insertBeforeUnderSameParent(evaluation, statement); ((ConditionalProcessor) subprocessor()).processCondition(statement, evaluationAccess); }
@Override public void process(CtStatement statement) { String evaluationAccess = "runtimeAngelicValue"; CtLocalVariable defaultValue = newLocalVariableDeclarationString(statement.getFactory(), Boolean.class, "spoonDefaultValue", "false"); insertBeforeUnderSameParent(defaultValue, statement); CtTry aTry = getFactory().Core().createTry(); CtAssignment variableAssignment = getFactory().Code().createVariableAssignment(defaultValue.getReference(), false, getFactory().Code().createCodeSnippetExpression(subprocessor().getDefaultValue())); aTry.setBody(getFactory().Code().createCtBlock(variableAssignment)); CtCatch aCatch = getFactory().Core().createCatch(); CtCatchVariable<Exception> nopolProcessorException = getFactory().Code().createCatchVariable(getFactory().Type().createReference(Exception.class), "__NopolProcessorException"); aCatch.setParameter(nopolProcessorException); aCatch.setBody(getFactory().Core().createBlock()); aTry.addCatcher(aCatch); insertBeforeUnderSameParent(aTry, statement); String evaluationValue = angelicInvocation("spoonDefaultValue"); CtLocalVariable<Boolean> evaluation = newLocalVariableDeclaration(statement.getFactory(), Boolean.class, evaluationAccess, evaluationValue); insertBeforeUnderSameParent(evaluation, statement); appendValueCollection(statement, evaluationAccess, "spoonDefaultValue"); ((ConditionalProcessor) subprocessor()).processCondition(statement, evaluationAccess); }