Java Code Examples for spoon.reflect.code.CtIf#getParent()
The following examples show how to use
spoon.reflect.code.CtIf#getParent() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StatementDeletionMetaMutator.java From metamutator with GNU General Public License v3.0 | 5 votes |
private void SetElseStatementWithReturn(CtIf ifStatement){ //search the first parent method CtMethod parentMethod = ifStatement.getParent(CtMethod.class); //we go out of while with null of a CtMethod. If null, return without method in parents...? if(parentMethod == null){ return; } //create returned expression from template. CtLiteral returnedExpression = null; Class classOfReturn = parentMethod.getType().getActualClass(); if(PrimitiveTemplateExpressions.containsKey(classOfReturn)){ CtLiteral templateExpression = PrimitiveTemplateExpressions.get(classOfReturn); returnedExpression = getFactory().Core().clone(templateExpression); }else{ returnedExpression = new CtLiteralImpl().setValue(null); } CtReturn theReturn = getFactory().Core().createReturn(); theReturn.setReturnedExpression(returnedExpression); CtBlock elseBlock = ifStatement.getElseStatement(); if(elseBlock == null){ elseBlock = getFactory().Core().createBlock(); } elseBlock.addStatement(theReturn); ifStatement.setElseStatement(elseBlock); }
Example 2
Source File: StatementDeletionMetaMutator.java From metamutator with GNU General Public License v3.0 | 4 votes |
private void mutateOperator(final CtStatement expression) { /*if (alreadyInHotsSpot(expression)) { System.out .println(String .format("Expression '%s' ignored because it is included in previous hot spot", expression)); return; }*/ int thisIndex = ++selectorIndex; ACTIVABLE kind = ACTIVABLE.ENABLED; String expressionContent = String.format("("+ PREFIX + "%s.is(%s))", thisIndex, kind.getClass().getCanonicalName()+"."+kind.name()); //create IfChoice with right condition CtIf ifChoice = getFactory().Core().createIf(); CtCodeSnippetExpression expIf = getFactory().Code().createCodeSnippetExpression(expressionContent); ifChoice.setCondition(expIf); //create block from a clone of expression CtStatement exp2 = getFactory().Core().clone(expression); CtBlock thenBlock = getFactory().Code().createCtBlock(exp2); //set if and replace the expression with the new if ifChoice.setThenStatement(thenBlock); expression.replace(ifChoice); //to be sure ifChoice.getParent().updateAllParentsBelow(); //if there are return or throws, set else with value of return. Filter<CtCFlowBreak> filterReturn = new ReturnOrThrowFilter(); if(!thenBlock.getElements(filterReturn).isEmpty()){ SetElseStatementWithReturn(ifChoice); } //to avoid to delete assignement in statement, we assign a default value to all local variable. Filter<CtLocalVariable> filterLocalVariable = new TypeFilter<CtLocalVariable>(CtLocalVariable.class); CtMethod method = ifChoice.getParent(CtMethod.class); if(method != null && !method.getElements(filterLocalVariable).isEmpty()){ for(CtLocalVariable var : method.getElements(filterLocalVariable)){ if(var.getAssignment() == null){ //create right side expression from template. Class classOfAssignment = var.getType().getActualClass(); CtLiteral rightHand = null; //Particular case of ForEach (int x : numbers) can't be (int x = 0 : numbers) if(var.getParent() instanceof CtForEach){ continue; } if(PrimitiveTemplateExpressions.containsKey(classOfAssignment)){ CtLiteral templateExpression = PrimitiveTemplateExpressions.get(classOfAssignment); rightHand = getFactory().Core().clone(templateExpression); }else{ rightHand = getFactory().createLiteral().setValue(null); } var.setAssignment(rightHand); } } } Selector.generateSelector(expression, ACTIVABLE.ENABLED, thisIndex, ActivableSet, PREFIX); //hotSpots.add(expression); }
Example 3
Source File: jMutRepairEvolutionary.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Create a Gen Mutation for a given CtElement * * @param ctElementPointed * * @param className * @param suspValue * @return * @throws IllegalAccessException */ @Override public OperatorInstance createOperatorInstanceForPoint(ModificationPoint gen) throws IllegalAccessException { ModificationPoint genSusp = gen; AstorOperator operationType = new ReplaceOp(); if (!(genSusp.getCodeElement() instanceof CtIf)) { // logger.error(".....The pointed Element is Not a statement"); return null; } CtIf targetIF = (CtIf) genSusp.getCodeElement(); CtElement cpar = targetIF.getParent(); // TODO: the parent not always is a block.. and we should manage them... if ((cpar == null /* || !(cpar instanceof CtBlock) */)) { return null; } OperatorInstance operation = new OperatorInstance(); operation.setOriginal(targetIF.getCondition()); operation.setOperationApplied(operationType); operation.setModificationPoint(genSusp); List<MutantCtElement> mutations = getMutants(targetIF); // currentStat.sizeSpace.add(new StatSpaceSize(mutations.size(),0)); log.debug("mutations: (" + mutations.size() + ") " + mutations); if (mutations == null || mutations.size() == 0) { return null; } CtElement fix = null; int max = 0; boolean continueSearching = true; while (continueSearching && max < mutations.size()) { fix = getFixMutation(mutations); continueSearching = fix != null;// alreadyApplied(gen,fix, // operationType); max++; } if (continueSearching) { log.debug("All mutations were applied: no mutation left to apply"); return null; } operation.setModified(fix); return operation; }