spoon.reflect.code.CtBlock Java Examples

The following examples show how to use spoon.reflect.code.CtBlock. 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: SingleWrapIfOperator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean applyModification() {

	CtStatement original = (CtStatement) MetaGenerator.geOriginalElement(statementToWrap);
	this.setParentBlock(original.getParent(CtBlock.class));
	//
	CtIf ifNew = MutationSupporter.getFactory().createIf();
	ifNew.setParent(original.getParent());
	CtStatement originalCloned = original.clone();
	MutationSupporter.clearPosition(originalCloned);
	ifNew.setThenStatement(originalCloned);

	// as difference with the meta, here we put the ingredient evaluated in the
	// meta.
	ifNew.setCondition((CtExpression<Boolean>) ifcondition);

	//

	super.setOriginal(original);
	super.setModified(ifNew);
	//

	return super.applyModification();
}
 
Example #2
Source File: MultiPatchTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private OperatorInstance createSecondPatch(ProgramVariant variant, ModificationPoint modificationPoint,
		CtElement fix, int currentGeneration) {

	CtElement targetStmt = modificationPoint.getCodeElement();

	StatementOperatorInstance operation = new StatementOperatorInstance();

	operation.setOperationApplied(new ReplaceOp());
	operation.setModificationPoint(modificationPoint);
	operation.setParentBlock((CtBlock) targetStmt.getParent());
	operation.setOriginal(targetStmt);
	operation.setModified(fix);
	operation.defineParentInformation(modificationPoint);

	variant.putModificationInstance(currentGeneration, operation);
	operation.setModificationPoint(modificationPoint);

	return operation;
}
 
Example #3
Source File: StatamentTransformer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static boolean undoRemoveStatement(OperatorInstance operation) {
	StatementOperatorInstance stmtoperator = (StatementOperatorInstance) operation;
	CtStatement ctst = (CtStatement) operation.getOriginal();
	CtBlock<?> parentBlock = stmtoperator.getParentBlock();
	if (parentBlock != null) {
		if ((parentBlock.getStatements().isEmpty() && stmtoperator.getLocationInParent() == 0)
				|| (parentBlock.getStatements().size() >= stmtoperator.getLocationInParent())) {

			parentBlock.getStatements().add(stmtoperator.getLocationInParent(), ctst);
			return true;
		} else {
			log.error(
					"Problems to recover, re-adding " + ctst + " at location " + stmtoperator.getLocationInParent()
							+ " from parent size " + parentBlock.getStatements().size());
			throw new IllegalStateException("Undo:Not valid index");
		}

	}
	return false;
}
 
Example #4
Source File: PatchValidationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private OperatorInstance createDummyOperation1(ProgramVariant variant, int currentGeneration) {

		SuspiciousModificationPoint genSusp = searchSuspiciousElement(variant, "n += 3", " ", 93);
		assertNotNull(genSusp);

		CtElement targetStmt = genSusp.getCodeElement();
		CtElement fix = createFix1();
		assertEquals(fix.toString(), "n += 2");

		StatementOperatorInstance operation = new StatementOperatorInstance();

		operation.setOperationApplied(new ReplaceOp());
		operation.setModificationPoint(genSusp);
		operation.setParentBlock((CtBlock) targetStmt.getParent());
		operation.setOriginal(targetStmt);
		operation.setModified(fix);

		variant.putModificationInstance(currentGeneration, operation);
		operation.setModificationPoint(genSusp);

		return operation;
	}
 
Example #5
Source File: PatchGenerator.java    From nopol with GNU General Public License v2.0 6 votes vote down vote up
private boolean isElseIf(CtStatement original, CtStatement parentLine) {
	if (parentLine.getParent() instanceof CtIf) {
		CtStatement elseStatement = ((CtIf) parentLine.getParent()).getElseStatement();
		if (elseStatement == original) {
			return true;
		} else if (elseStatement instanceof CtBlock) {
			CtBlock block = (CtBlock) elseStatement;
			if (block.isImplicit() && block.getStatement(0) == original) {
				return true;
			}
		}
	}
	if (parentLine.getParent() instanceof CtBlock) {
		return isElseIf(original, (CtStatement) parentLine.getParent());
	}
	return false;
}
 
Example #6
Source File: StatamentTransformer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static boolean doReplaceStatement(OperatorInstance operation) {
	StatementOperatorInstance stmtoperator = (StatementOperatorInstance) operation;
	boolean successful = false;
	CtStatement ctst = (CtStatement) operation.getOriginal();
	CtStatement fix = (CtStatement) operation.getModified();

	CtBlock parentBlock = stmtoperator.getParentBlock();

	if (parentBlock != null) {

		try {
			ctst.replace((CtStatement) fix);
			fix.setParent(parentBlock);
			successful = true;
			operation.setSuccessfulyApplied(successful);
		} catch (Exception ex) {
			log.error("Error applying an operation, exception: " + ex.getMessage());
			operation.setExceptionAtApplied(ex);
			operation.setSuccessfulyApplied(false);
		}
	} else {
		log.error("Operation not applied. Parent null ");
	}
	return successful;
}
 
Example #7
Source File: StatamentTransformer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static boolean doRemoveStatement(OperatorInstance operation) {
	boolean successful = false;
	CtStatement ctst = (CtStatement) operation.getOriginal();
	StatementOperatorInstance stmtoperator = (StatementOperatorInstance) operation;
	CtBlock parentBlock = stmtoperator.getParentBlock();

	if (parentBlock != null) {

		try {
			parentBlock.getStatements().remove(ctst);
			successful = true;
			operation.setSuccessfulyApplied(successful);
		} catch (Exception ex) {
			log.error("Error applying an operation, exception: " + ex.getMessage());
			operation.setExceptionAtApplied(ex);
			operation.setSuccessfulyApplied(false);
		}
	} else {
		log.error("Operation not applied. Parent null ");
	}
	return successful;
}
 
Example #8
Source File: SpecialStatementFixSpaceProcessor.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void process(CtStatement element) {

	if (element instanceof CtIf) {
		add(((CtIf) element).getCondition());
	} else if (element instanceof CtFor) {
		add(((CtFor) element).getExpression());
	} else if (element instanceof CtWhile) {
		add(((CtWhile) element).getLoopingExpression());
	} else if (element instanceof CtDo) {
		add(((CtDo) element).getLoopingExpression());
	} else if (element instanceof CtThrow) {
		add(((CtThrow) element).getThrownExpression());
	} else if (element instanceof CtInvocation && (element.getParent() instanceof CtBlock)) {
		add(element);
	} else if (element instanceof CtAssignment || element instanceof CtConstructorCall
			|| element instanceof CtCFlowBreak || element instanceof CtLocalVariable) {
		add(element);
	}

}
 
Example #9
Source File: AbstractCodeAnalyzer.java    From coming with MIT License 6 votes vote down vote up
public static boolean whethereffectiveguard(CtIf ifstatement, CtStatement targetstatement) {
	CtBlock thenBlock = ifstatement.getThenStatement();
	CtBlock elseBlock = ifstatement.getElseStatement();

	if (thenBlock != null) {
		List<CtStatement> thenstatements = thenBlock.getStatements();
		if (thenstatements.size() > 0 && thenstatements.get(0) == targetstatement)
			return true;
	}

	if (elseBlock != null) {
		List<CtStatement> elsestatements = elseBlock.getStatements();
		if (elsestatements.size() > 0 && elsestatements.get(0) == targetstatement)
			return true;
	}

	return false;
}
 
Example #10
Source File: StatementOperatorInstance.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public boolean defineParentInformation(ModificationPoint genSusp) {
	CtElement targetStmt = genSusp.getCodeElement();
	CtElement cparent = targetStmt.getParent();
	if ((cparent != null && (cparent instanceof CtBlock))) {
		CtBlock parentBlock = (CtBlock) cparent;
		int location = locationInParent(parentBlock, targetStmt);
		if (location >= 0) {
			this.setParentBlock(parentBlock);
			this.setLocationInParent(location);
			return true;
		}

	} else {
		log.error("Parent null or it is not a block");
	}
	return false;
}
 
Example #11
Source File: BoundTemplateProcessor.java    From spoon-examples with GNU General Public License v2.0 6 votes vote down vote up
public void process(Bound annotation, CtParameter<?> element) {
	// Is to be process?
	CtExecutable<?> e = element.getParent();
	if (e.getBody() == null) {
		return;
	}

	// Use template.
	CtClass<?> type = e.getParent(CtClass.class);
	Template t = new BoundTemplate(getFactory().Type().createReference(Double.class), element.getSimpleName(), annotation.min(), annotation.max());
	final CtBlock apply = (CtBlock) t.apply(type);

	// Apply transformation.
	for (int i = apply.getStatements().size() - 1; i >= 0; i--) {
		final CtStatement statement = apply.getStatement(i);
		statement.delete();
		e.getBody().insertBegin(statement);
	}
}
 
Example #12
Source File: NullPreconditionWithExpressionOperator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public boolean applyChangesInModel(OperatorInstance operation, ProgramVariant p) {
	StatementOperatorInstance stmtoperator = (StatementOperatorInstance) operation;
	boolean successful = false;
	CtStatement ctst = (CtStatement) operation.getOriginal();
	CtStatement fix = (CtStatement) operation.getModified();

	CtBlock parentBlock = stmtoperator.getParentBlock();

	if (parentBlock != null) {

		try {
			ctst.replace((CtStatement) fix);
			fix.setParent(parentBlock);
			successful = true;
			operation.setSuccessfulyApplied(successful);
		} catch (Exception ex) {
			log.error("Error applying an operation, exception: " + ex.getMessage());
			operation.setExceptionAtApplied(ex);
			operation.setSuccessfulyApplied(false);
		}
	} else {
		log.error("Operation not applied. Parent null ");
	}
	return successful;
}
 
Example #13
Source File: NullPreconditionOperatorMI.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public boolean applyChangesInModel(OperatorInstance operation, ProgramVariant p) {
	StatementOperatorInstance stmtoperator = (StatementOperatorInstance) operation;
	boolean successful = false;
	CtStatement ctst = (CtStatement) operation.getOriginal();
	CtStatement fix = (CtStatement) operation.getModified();

	CtBlock parentBlock = stmtoperator.getParentBlock();

	if (parentBlock != null) {

		try {
			ctst.replace((CtStatement) fix);
			fix.setParent(parentBlock);
			successful = true;
			operation.setSuccessfulyApplied(successful);
		} catch (Exception ex) {
			log.error("Error applying an operation, exception: " + ex.getMessage());
			operation.setExceptionAtApplied(ex);
			operation.setSuccessfulyApplied(false);
		}
	} else {
		log.error("Operation not applied. Parent null ");
	}
	return successful;
}
 
Example #14
Source File: NullPreconditionOperator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public boolean applyChangesInModel(OperatorInstance operation, ProgramVariant p) {
	StatementOperatorInstance stmtoperator = (StatementOperatorInstance) operation;
	boolean successful = false;
	CtStatement ctst = (CtStatement) operation.getOriginal();
	CtStatement fix = (CtStatement) operation.getModified();

	CtBlock parentBlock = stmtoperator.getParentBlock();

	if (parentBlock != null) {

		try {
			ctst.replace((CtStatement) fix);
			fix.setParent(parentBlock);
			successful = true;
			operation.setSuccessfulyApplied(successful);
		} catch (Exception ex) {
			log.error("Error applying an operation, exception: " + ex.getMessage());
			operation.setExceptionAtApplied(ex);
			operation.setSuccessfulyApplied(false);
		}
	} else {
		log.error("Operation not applied. Parent null ");
	}
	return successful;
}
 
Example #15
Source File: InsertBeforeOp.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean applyChangesInModel(OperatorInstance operation, ProgramVariant p) {
	boolean successful = false;
	CtStatement ctst = (CtStatement) operation.getOriginal();
	CtStatement fix = (CtStatement) operation.getModified();
	StatementOperatorInstance stmtoperator = (StatementOperatorInstance) operation;
	CtBlock parentBlock = stmtoperator.getParentBlock();

	if (parentBlock != null) {
		ctst.insertBefore((CtStatement) fix);
		fix.setParent(parentBlock);
		successful = true;
		operation.setSuccessfulyApplied(successful);
		StatementSupporter.updateBlockImplicitly(parentBlock, true);
	} else {
		log.error("Operation not applied. Parent null ");
	}
	return successful;

}
 
Example #16
Source File: InsertAfterOp.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean applyChangesInModel(OperatorInstance operation, ProgramVariant p) {

	StatementOperatorInstance stmtoperator = (StatementOperatorInstance) operation;

	boolean successful = false;
	CtStatement ctst = (CtStatement) operation.getOriginal();
	CtStatement fix = (CtStatement) operation.getModified();

	CtBlock parentBlock = stmtoperator.getParentBlock();

	if (parentBlock != null) {
		ctst.insertAfter((CtStatement) fix);
		fix.setParent(parentBlock);
		successful = true;
		operation.setSuccessfulyApplied(successful);
		StatementSupporter.updateBlockImplicitly(parentBlock, true);
	} else {
		log.error("Operation not applied. Parent null");
	}
	return successful;
}
 
Example #17
Source File: ReplaceOp.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public boolean applyChangesInModel(OperatorInstance operation, ProgramVariant p) {
	StatementOperatorInstance stmtoperator = (StatementOperatorInstance) operation;
	boolean successful = false;
	CtStatement ctst = (CtStatement) operation.getOriginal();
	CtStatement fix = (CtStatement) operation.getModified();

	CtBlock parentBlock = stmtoperator.getParentBlock();

	if (parentBlock != null) {

		try {
			ctst.replace((CtStatement) fix);
			fix.setParent(parentBlock);
			successful = true;
			operation.setSuccessfulyApplied(successful);
		} catch (Exception ex) {
			log.error("Error applying an operation, exception: " + ex.getMessage());
			operation.setExceptionAtApplied(ex);
			operation.setSuccessfulyApplied(false);
		}
	} else {
		log.error("Operation not applied. Parent null ");
	}
	return successful;
}
 
Example #18
Source File: RemoveOp.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean applyChangesInModel(OperatorInstance operation, ProgramVariant p) {
	StatementOperatorInstance stmtoperator = (StatementOperatorInstance) operation;
	boolean successful = false;
	CtStatement ctst = (CtStatement) operation.getOriginal();
	CtBlock parentBlock = stmtoperator.getParentBlock();

	if (parentBlock != null) {

		try {
			parentBlock.getStatements().remove(stmtoperator.getLocationInParent());
			successful = true;
			operation.setSuccessfulyApplied(successful);
			StatementSupporter.updateBlockImplicitly(parentBlock, false);
		} catch (Exception ex) {
			log.error("Error applying an operation, exception: " + ex.getMessage());
			operation.setExceptionAtApplied(ex);
			operation.setSuccessfulyApplied(false);
		}
	} else {
		log.error("Operation not applied. Parent null ");
	}
	return successful;
}
 
Example #19
Source File: RemoveOp.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean undoChangesInModel(OperatorInstance operation, ProgramVariant p) {
	StatementOperatorInstance stmtoperator = (StatementOperatorInstance) operation;
	CtStatement ctst = (CtStatement) operation.getOriginal();
	CtBlock<?> parentBlock = stmtoperator.getParentBlock();
	if (parentBlock != null) {
		if ((parentBlock.getStatements().isEmpty() && stmtoperator.getLocationInParent() == 0)
				|| (parentBlock.getStatements().size() >= stmtoperator.getLocationInParent())) {
			parentBlock.getStatements().add(stmtoperator.getLocationInParent(), ctst);
			parentBlock.setImplicit(stmtoperator.isParentBlockImplicit());
			return true;
		} else {
			log.error(
					"Problems to recover, re-adding " + ctst + " at location " + stmtoperator.getLocationInParent()
							+ " from parent size " + parentBlock.getStatements().size());
			throw new IllegalStateException("Undo:Not valid index");
		}

	}
	return false;
}
 
Example #20
Source File: StatementSupporter.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Updates the implicitly of a block. Workarround for Spoon 5.4.0
 * 
 * @param block
 * @param isInsert
 */
public static void updateBlockImplicitly(CtBlock block, boolean isInsert) {

	if (!block.isImplicit() && block.getStatements().size() == 1 && !(block.getParent() instanceof CtExecutable)) {
		block.setImplicit(true);
	} else {
		if (isInsert) {
			if (block.isImplicit() && block.getStatements().size() > 1) {
				block.setImplicit(false);
			}
		} else {// Delete
			if (block.isImplicit() && block.getStatements().size() == 0) {
				block.setImplicit(false);
			}
		}
	}
}
 
Example #21
Source File: StatamentTransformer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public static boolean undoReplaceStatement(OperatorInstance operation) {
	StatementOperatorInstance stmtoperator = (StatementOperatorInstance) operation;
	CtStatement ctst = (CtStatement) operation.getOriginal();
	CtStatement fix = (CtStatement) operation.getModified();
	CtBlock<?> parentBlock = stmtoperator.getParentBlock();
	if (parentBlock != null) {
		fix.replace((CtStatement) ctst);
		return true;

	}
	return false;
}
 
Example #22
Source File: ConditionAddTransform.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public <R> void visitCtBlock(CtBlock<R> block) {
	int id = 0;
	List<CtStatement>  statlist = block.getStatements();
	boolean flag = false;
	for (; id < statlist.size(); id++)
		if (statlist.get(id).equals((CtStatement) this.modificationPoint.getCodeElement())) {
			flag = true;
			break;
		}
	if (!flag)
		return;
	
	List<CtVariable> varsavilable = modificationPoint.getContextOfModificationPoint();
	PriorityQueue<CtVariable> queue = new PriorityQueue<CtVariable>(new Comparator<CtVariable>() {
		@Override
		public int compare(CtVariable o1, CtVariable o2) {
			return o2.getPosition().getLine()-o1.getPosition().getLine();
		}
	});	
	queue.addAll(varsavilable);
	
	List<String> types = new ArrayList<String>();
	while (!queue.isEmpty()) {
		CtVariable var = queue.poll();
	    String type = var.getType().getQualifiedName();
		type = type.replaceAll("\\d","");
		if (!types.contains(type)) {
			types.add(type);
			log.info(type);
			writeCondition(type, block, id);
			writeIfReturn(type, block, id);
		}
	}
}
 
Example #23
Source File: StatementSupporter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public static boolean remove(CtBlock parentBlock, CtStatement fixStatement, int pos) {

		CtStatement s = parentBlock.getStatement(pos);
		// To be sure that the position has the element we
		// want to remove
		if (fixStatement.equals(s)) {
			parentBlock.getStatements().remove(pos);
			return true;
		} else {
			System.out.println("\n fx: " + fixStatement + "\n" + (s));
			throw new IllegalStateException("Undo: Not valid fix position");
		}
	}
 
Example #24
Source File: MethodInvocationFixSpaceProcessor.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void process(CtInvocation element) {
	
	if(element.getParent() instanceof CtBlock){
		if(!element.toString().equals("super()"))
			super.add((CtStatement)element);
		
	}
}
 
Example #25
Source File: ConditionAddTransform.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public List<String> transform() {
	
	try {
		CtStatement targetStmt = (CtStatement) this.modificationPoint.getCodeElement().getParent();
		if (targetStmt instanceof CtBlock)
			visitCtBlock((CtBlock) targetStmt);
	 }catch (Exception e) {
			log.debug("Exception happens when geting the parent statement of the suspicious statement");
	 }
	 return list;
}
 
Example #26
Source File: LabelFinder.java    From gumtree-spoon-ast-diff with Apache License 2.0 5 votes vote down vote up
@Override
public <R> void visitCtBlock(CtBlock<R> e) {
	if (e.getRoleInParent() == CtRole.ELSE) {
		label = "ELSE";
	} else if (e.getRoleInParent() == CtRole.THEN) {
		label = "THEN";
	} else {
		label = "{";
	}
}
 
Example #27
Source File: SingleUnWrapIfOperator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private CtStatement getBlock(CtStatement thenStatement) {
	if (thenStatement instanceof CtBlock) {
		return getBlock(((CtBlock) thenStatement).getStatement(0));
	}

	return thenStatement;
}
 
Example #28
Source File: SingleTryOperator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean applyModification() {

	CtElement el = MetaGenerator.geOriginalElement(statementToWrap);
	if (!(el instanceof CtStatement)) {
		return false;
	}

	CtStatement original = (CtStatement) el;
	this.setParentBlock(original.getParent(CtBlock.class));

	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 = statementToWrap.clone();

	MutationSupporter.clearPosition(stmtC);
	tryBoddy.addStatement(stmtC);

	//
	super.setOriginal(original);
	super.setModified(tryNew);
	//

	return super.applyModification();
}
 
Example #29
Source File: TreeScanner.java    From gumtree-spoon-ast-diff with Apache License 2.0 5 votes vote down vote up
private String getNodeType(CtElement element) {
	String nodeTypeName = NOTYPE;
	if (element != null) {
		nodeTypeName = getTypeName(element.getClass().getSimpleName());
	}
	if (element instanceof CtBlock) {
		nodeTypeName = element.getRoleInParent().toString();
	}
	return nodeTypeName;
}
 
Example #30
Source File: SingleStatementFixSpaceProcessor.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void process(CtStatement element) {
	if (!(element instanceof CtBlock || element instanceof CtClass || element instanceof CtMethod
			|| element instanceof CtTry || element instanceof CtCatch) && 
			(element.getParent() instanceof CtBlock) && 
			(!(element.toString().startsWith("super"))
					|| ConfigurationProperties.getPropertyBool("manipulatesuper"))) {
		add(element);
	}
}