Java Code Examples for spoon.reflect.code.CtStatement#getParent()

The following examples show how to use spoon.reflect.code.CtStatement#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: SpoonStatementLibrary.java    From nopol with GNU General Public License v2.0 6 votes vote down vote up
public static boolean isLastStatementOfMethod(CtStatement statement) {
    CtElement statementParent = statement.getParent();
    if (!isStatementList(statementParent)) {
        return isLastStatementOfMethod((CtStatement) statementParent);
    }
    CtStatementList block = (CtStatementList) statementParent;
    if (isLastStatementOf(block, statement)) {
        CtElement blockParent = block.getParent();
        if (isStatement(blockParent)) {
            return isLastStatementOfMethod((CtStatement) blockParent);
        } else {
            return isMethod(blockParent);
        }
    }
    return false;
}
 
Example 2
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 3
Source File: ConditionalAdder.java    From nopol with GNU General Public License v2.0 6 votes vote down vote up
@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;
}
 
Example 4
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 5
Source File: VariabletoNullMetaMutator.java    From metamutator with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isToBeProcessed(CtStatement element) {
	// if (element.getParent(CtAnonymousExecutable.class)!=null) {
			// System.out.println(element.getParent(CtAnonymousExecutable.class));
			// }
	
			if(!(element instanceof CtRHSReceiver))
				return false;
			try {
				Selector.getTopLevelClass(element);
			} catch (Exception e) {
				return false;
			}

			// not in constructors because we use static fields
			if (element.getParent(CtConstructor.class) != null) {
				return false;
			}
			
			if (((CtRHSReceiver)element).getAssignment() == null)
				return false;
			
			CtTypeReference type = ((CtRHSReceiver)element).getAssignment().getType();
			
			if (type == null)
				return false;
			
			if (element.toString().contains("java.lang.String.format"))
				return false;

			return !((CtRHSReceiver)element).getAssignment().getType().isPrimitive() 
					&& (element.getParent(CtAnonymousExecutable.class) == null);
}
 
Example 6
Source File: SpoonStatementLibrary.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
public static void insertBeforeUnderSameParent(CtStatement toBeInserted, CtStatement insertionPoint) {
    CtElement parent;
    if (isBlock(insertionPoint)) {
        CtBlock<?> block = (CtBlock<?>) insertionPoint;
        block.insertBegin(toBeInserted);
        parent = block;
    } else {
        insertionPoint.insertBefore(toBeInserted);
        parent = insertionPoint.getParent();
    }
    setParent(parent, toBeInserted);
}
 
Example 7
Source File: SpoonStatementLibrary.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
public static void insertAfterUnderSameParent(CtStatement toBeInserted, CtStatement insertionPoint) {
    CtElement parent;
    if (isBlock(insertionPoint)) {
        CtBlock<?> block = (CtBlock<?>) insertionPoint;
        block.insertEnd(toBeInserted);
        parent = block;
    } else {
        insertionPoint.insertAfter(toBeInserted);
        parent = insertionPoint.getParent();
    }
    setParent(parent, toBeInserted);
}
 
Example 8
Source File: MethodFromLocation.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isToBeProcessed(CtStatement candidate) {
    CtClass parent = candidate.getParent(CtClass.class);
    if (parent == null || !parent.getQualifiedName().equals(this.location.getContainingClassName())) {
        return false;
    }
    return parent.getPosition().getLine() == location.getLineNumber();
}
 
Example 9
Source File: SymbolicConditionalAdder.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
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());
        }
    } else {
        condition = element
                .getFactory()
                .Code()
                .createCodeSnippetExpression(
                        Debug.class.getCanonicalName()
                                + ".makeSymbolicBoolean(\"guess_fix\")");
    }
    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);
    // Fix : warning: ignoring inconsistent parent for [CtElem1] ( [CtElem2] != [CtElem3] )
    newIf.getThenStatement().setParent(newIf);
    logger.debug("##### {} ##### After:\n{}", element, element.getParent().getParent());
}
 
Example 10
Source File: AbstractCodeAnalyzer.java    From coming with MIT License 5 votes vote down vote up
public boolean isStatementInControl(CtStatement targetstatement, CtStatement statementtocompare) {
	CtElement parentelement = targetstatement.getParent();
	int layer = 0;
	CtElement parent;
	parent = statementtocompare;
	do {
		parent = parent.getParent();
		layer++;
	} while (parent != parentelement && parent != null);

	if (layer > 1 && parent != null)
		return true;
	else
		return false;
}
 
Example 11
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);
	}
}
 
Example 12
Source File: StatementFixSpaceProcessor.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void process(CtStatement element) {
	if (!(element instanceof CtClass || element instanceof CtMethod) && (element.getParent() instanceof CtBlock)) {
		add(element);
	}
}