Java Code Examples for spoon.reflect.code.CtLocalVariable

The following examples show how to use spoon.reflect.code.CtLocalVariable. These examples are extracted from open source projects. 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 Project: nopol   Source File: LoggingInstrumenter.java    License: GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example 2
Source Project: nopol   Source File: LiteralReplacer.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public void process(CtStatement ctStatement) {
    if (!(ctStatement instanceof CtLocalVariable<?>) && !(ctStatement instanceof CtAssignment<?, ?>))
        return;

    Class<?> localVariableClass = ((CtTypedElement<?>) ctStatement).getType().getActualClass();
    if (localVariableClass.equals(Integer.class) || localVariableClass.equals(int.class)) {
        if (ctStatement instanceof CtAssignment<?, ?>) {
            replaceInteger(((CtAssignment<?, ?>) ctStatement).getAssignment());
        } else {
            replaceInteger(((CtLocalVariable<?>) ctStatement).getDefaultExpression());
        }
    } else if (localVariableClass.equals(Double.class) || localVariableClass.equals(double.class)) {
        if (ctStatement instanceof CtAssignment<?, ?>) {
            replaceDouble(((CtAssignment<?, ?>) ctStatement).getAssignment());
        } else {
            replaceDouble(((CtLocalVariable<?>) ctStatement).getDefaultExpression());
        }
    } else if (localVariableClass.equals(Boolean.class) || localVariableClass.equals(boolean.class)) {
        if (ctStatement instanceof CtAssignment<?, ?>) {
            replaceBoolean(((CtAssignment<?, ?>) ctStatement).getAssignment());
        } else {
            replaceBoolean(((CtLocalVariable<?>) ctStatement).getDefaultExpression());
        }
    }
}
 
Example 3
Source Project: nopol   Source File: LiteralReplacer.java    License: GNU General Public License v2.0 6 votes vote down vote up
private void replaceDouble(CtExpression ctElement) {
    if (getValue() == null) {
        CtLocalVariable<Double> evaluation = newLocalVariableDeclaration(
                ctElement.getFactory(), double.class, "guess_fix",
                Debug.class.getCanonicalName() + ".makeSymbolicReal(\"guess_fix\")");

        CtStatement firstStatement = getFirstStatement(ctElement);
        if (firstStatement == null) {
            return;
        }
        SpoonStatementLibrary.insertBeforeUnderSameParent(evaluation, firstStatement);
        // SpoonStatementLibrary.insertAfterUnderSameParent(getFactory().Code().createCodeSnippetStatement("System.out.println(\"guess_fix: \" + guess_fix)"),
        // getFirstStatement(ctElement));
        ctElement.replace(getFactory().Code().createCodeSnippetExpression("guess_fix"));
    } else {
        ctElement.replace(getFactory().Code().createCodeSnippetExpression(getValue()));
    }
}
 
Example 4
Source Project: nopol   Source File: LiteralReplacer.java    License: GNU General Public License v2.0 6 votes vote down vote up
private void replaceInteger(CtExpression ctElement) {
    if (getValue() == null) {
        CtLocalVariable<Integer> evaluation = newLocalVariableDeclaration(
                ctElement.getFactory(), int.class, "guess_fix",
                Debug.class.getCanonicalName()
                        + ".makeSymbolicInteger(\"guess_fix\")");

        CtStatement firstStatement = getFirstStatement(ctElement);
        if (firstStatement == null) {
            return;
        }
        SpoonStatementLibrary.insertBeforeUnderSameParent(evaluation, firstStatement);
        // SpoonStatementLibrary.insertAfterUnderSameParent(getFactory().Code().createCodeSnippetStatement("System.out.println(\"guess_fix: \" + guess_fix)"),
        // getFirstStatement(ctElement));

        ctElement.replace(getFactory().Code().createCodeSnippetExpression("guess_fix"));
    } else {
        ctElement.replace(getFactory().Code().createCodeSnippetExpression(getValue()));
    }
}
 
Example 5
@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 6
Source Project: nopol   Source File: LiteralReplacer.java    License: GNU General Public License v2.0 5 votes vote down vote up
public LiteralReplacer(Class<?> cl, CtStatement statement, RepairType repairType) {
    super(statement, repairType);
    if (statement instanceof CtAssignment<?, ?>) {
        super.setDefaultValue(((CtAssignment<?, ?>) statement).getAssignment().toString());
    } else if (statement instanceof CtLocalVariable<?>) {
        super.setDefaultValue(((CtLocalVariable<?>) statement).getDefaultExpression().toString());
    }
    super.setType(cl);
}
 
Example 7
Source Project: nopol   Source File: LiteralReplacer.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void replaceBoolean(CtExpression ctElement) {
    if (getValue() == null) {
        CtLocalVariable<Boolean> evaluation = newLocalVariableDeclaration(
                ctElement.getFactory(), boolean.class, "guess_fix",
                Debug.class.getCanonicalName() + ".makeSymbolicBoolean(\"guess_fix\")");
        CtStatement firstStatement = getFirstStatement(ctElement);
        if (firstStatement == null) {
            return;
        }
        SpoonStatementLibrary.insertBeforeUnderSameParent(evaluation,
                firstStatement);
        ctElement.replace(getFactory().Code().createCodeSnippetExpression(
                "guess_fix"));
    } else {
        switch (getValue()) {
            case "1":
                ctElement.replace(getFactory().Code().createCodeSnippetExpression("true"));
                break;
            case "0":
                ctElement.replace(getFactory().Code().createCodeSnippetExpression("false"));
                break;
            default:
                ctElement.replace(getFactory().Code().createCodeSnippetExpression(getValue()));
                break;
        }
    }
}
 
Example 8
Source Project: spoon-examples   Source File: Util.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static List<CtMethod> getGetters(CtLocalVariable localVariable) {
	return ((Set<CtMethod<?>>) localVariable.getType().getDeclaration().getMethods()).stream()
			.filter(method -> method.getParameters().isEmpty() &&
					method.getType() != localVariable.getFactory().Type().VOID_PRIMITIVE &&
					(method.getSimpleName().startsWith("get") ||
							method.getSimpleName().startsWith("is"))
			).collect(Collectors.toList());
}
 
Example 9
Source Project: spoon-examples   Source File: Collector.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void collect(Launcher launcher, CtMethod<?> testMethod, List<CtLocalVariable> localVariables) {
	CtClass testClass = testMethod.getParent(CtClass.class);
	testClass.removeMethod(testMethod);
	CtMethod<?> clone = testMethod.clone();
	instrument(clone, localVariables);
	testClass.addMethod(clone);
	System.out.println(clone);
	run(launcher, testClass, clone);
	testClass.removeMethod(clone);
	testClass.addMethod(testMethod);
}
 
Example 10
Source Project: spoon-examples   Source File: Collector.java    License: GNU General Public License v2.0 5 votes vote down vote up
void instrument(CtMethod testMethod, CtLocalVariable localVariable) {
	List<CtMethod> getters = getGetters(localVariable);
	getters.forEach(getter -> {
		CtInvocation invocationToGetter =
				invok(getter, localVariable);
		CtInvocation invocationToObserve =
				createObserve(getter, invocationToGetter);
		testMethod.getBody().insertEnd(invocationToObserve);
	});
}
 
Example 11
Source Project: spoon-examples   Source File: Analyzer.java    License: GNU General Public License v2.0 5 votes vote down vote up
List<CtLocalVariable> analyze(CtMethod testMethod) {
	TypeFilter filterLocalVar =
			new TypeFilter(CtLocalVariable.class) {
				public boolean matches(CtLocalVariable localVariable) {
					return !localVariable.getType().isPrimitive();
				}
			};
	return testMethod.getElements(filterLocalVar);
}
 
Example 12
Source Project: spoon-examples   Source File: Analyzer.java    License: GNU General Public License v2.0 5 votes vote down vote up
public Map<CtMethod, List<CtLocalVariable>> analyze(CtType<?> ctClass) {
	CtTypeReference<Test> reference =
			ctClass.getFactory().Type().createReference(Test.class);
	return ctClass.getMethods().stream()
			.filter(ctMethod -> ctMethod.getAnnotation(reference) != null)
			.collect(Collectors.toMap(
					ctMethod -> ctMethod,
					this::analyze)
			);
}
 
Example 13
@SuppressWarnings("unchecked")
void addAssertion(CtMethod testMethod, CtLocalVariable localVariable) {
	List<CtMethod> getters =
			getGetters(localVariable);
	getters.forEach(getter -> {
		String key = getKey(getter);
		CtInvocation invocationToGetter =
				invok(getter, localVariable);
		CtInvocation invocationToAssert = createAssert("assertEquals",
				factory.createLiteral(Logger.observations.get(key)), // expected
				invocationToGetter); // actual
		testMethod.getBody().insertEnd(invocationToAssert);
	});
}
 
Example 14
@Test
public void test() {
	Launcher launcher = new Launcher();
	launcher.getEnvironment().setAutoImports(true);
	launcher.getEnvironment().setLevel(Level.ALL.toString());
	launcher.addInputResource("src/test/resources/project/src/main/java/");
	launcher.addInputResource("src/test/resources/project/src/test/java/");
	launcher.addInputResource("src/main/java/fr/inria/gforge/spoon/assertgenerator/Logger.java");
	launcher.getEnvironment().setSourceClasspath(getPathToJunit());
	launcher.buildModel();

	final Analyzer analyzer = new Analyzer();
	final Collector collector = new Collector(launcher.getFactory());
	final AssertionAdder assertionAdder = new AssertionAdder(launcher.getFactory());

	launcher.getFactory().Class().getAll().forEach(ctClass -> {
		// Analyze
		Map<CtMethod, List<CtLocalVariable>> localVariablesPerTestMethod = analyzer.analyze(ctClass);
		localVariablesPerTestMethod.keySet().stream().forEach(key -> System.out.println("{"+ key.getParent(CtClass.class).getSimpleName() + "#" + key.getSimpleName() + "=["+ localVariablesPerTestMethod.get(key) +"]"));
		if (!localVariablesPerTestMethod.isEmpty()) {
			// Collect
			localVariablesPerTestMethod.keySet().forEach(ctMethod -> collector.collect(launcher, ctMethod, localVariablesPerTestMethod.get(ctMethod)));
			// Generate
			localVariablesPerTestMethod.keySet().forEach(ctMethod -> assertionAdder.addAssertion(ctMethod, localVariablesPerTestMethod.get(ctMethod)));
		}
	});
}
 
Example 15
Source Project: coming   Source File: VariableResolver.java    License: MIT License 5 votes vote down vote up
/**
 * Return the local variables of a block from the beginning until the element
 * located at positionEl.
 * 
 * @param positionEl analyze variables from the block until that position.
 * @param pb         a block to search the local variables
 * @return
 */
protected static List<CtLocalVariable> retrieveLocalVariables(int positionEl, CtBlock pb) {
	List stmt = pb.getStatements();
	List<CtLocalVariable> variables = new ArrayList<CtLocalVariable>();
	for (int i = 0; i < positionEl; i++) {
		CtElement ct = (CtElement) stmt.get(i);
		if (ct instanceof CtLocalVariable) {
			variables.add((CtLocalVariable) ct);
		}
	}
	CtElement beforei = pb;
	CtElement parenti = pb.getParent();
	boolean continueSearch = true;
	// We find the parent block
	while (continueSearch) {

		if (parenti == null) {
			continueSearch = false;
			parenti = null;
		} else if (parenti instanceof CtBlock) {
			continueSearch = false;
		} else {
			beforei = parenti;
			parenti = parenti.getParent();
		}
	}

	if (parenti != null) {
		int pos = ((CtBlock) parenti).getStatements().indexOf(beforei);
		variables.addAll(retrieveLocalVariables(pos, (CtBlock) parenti));
	}
	return variables;
}
 
Example 16
Source Project: astor   Source File: RemoveOp.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean canBeAppliedToPoint(ModificationPoint point) {
	if (!(point.getCodeElement() instanceof CtStatement))
		return false;
	// Do not remove local declaration
	if (point.getCodeElement() instanceof CtLocalVariable) {
		CtLocalVariable lv = (CtLocalVariable) point.getCodeElement();
		boolean shadow = false;
		CtClass parentC = point.getCodeElement().getParent(CtClass.class);
		List<CtField> ff = parentC.getFields();
		for (CtField<?> f : ff) {
			if (f.getSimpleName().equals(lv.getSimpleName()))
				shadow = true;
		}
		if (!shadow)
			return false;
	}
	// do not remove the last statement
	CtMethod parentMethd = point.getCodeElement().getParent(CtMethod.class);
	if (point.getCodeElement() instanceof CtReturn
			&& parentMethd.getBody().getLastStatement().equals(point.getCodeElement())) {
		return false;
	}

	// Otherwise, accept the element
	return true;
}
 
Example 17
Source Project: astor   Source File: VariableResolver.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static List<CtLocalVariable> getVarsInFor(CtElement element) {
	List<CtLocalVariable> variables = new ArrayList<CtLocalVariable>();
	CtElement currentElement = element;
	CtFor ff = currentElement.getParent(CtFor.class);
	while (ff != null) {
		variables.addAll(ff.getForInit().stream().filter(e -> e instanceof CtLocalVariable)
				.map(CtLocalVariable.class::cast).collect(Collectors.toList()));

		ff = ff.getParent(CtFor.class);

	}
	return variables;
}
 
Example 18
Source Project: astor   Source File: VariableResolver.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static List<CtLocalVariable> getVarsInForEach(CtElement element) {
	List<CtLocalVariable> variables = new ArrayList<CtLocalVariable>();
	CtElement currentElement = element;
	CtForEach ff = currentElement.getParent(CtForEach.class);
	while (ff != null) {
		variables.add((CtLocalVariable) ff.getVariable());
		ff = ff.getParent(CtForEach.class);

	}
	return variables;
}
 
Example 19
Source Project: astor   Source File: VariableResolver.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return the local variables of a block from the beginning until the element
 * located at positionEl.
 * 
 * @param positionEl analyze variables from the block until that position.
 * @param pb         a block to search the local variables
 * @return
 */
protected static List<CtLocalVariable> retrieveLocalVariables(int positionEl, CtBlock pb) {
	List stmt = pb.getStatements();
	List<CtLocalVariable> variables = new ArrayList<CtLocalVariable>();
	for (int i = 0; i < positionEl; i++) {
		CtElement ct = (CtElement) stmt.get(i);
		if (ct instanceof CtLocalVariable) {
			variables.add((CtLocalVariable) ct);
		}
	}
	CtElement beforei = pb;
	CtElement parenti = pb.getParent();
	boolean continueSearch = true;
	// We find the parent block
	while (continueSearch) {

		if (parenti == null) {
			continueSearch = false;
			parenti = null;
		} else if (parenti instanceof CtBlock) {
			continueSearch = false;
		} else {
			beforei = parenti;
			parenti = parenti.getParent();
		}
	}

	if (parenti != null) {
		int pos = ((CtBlock) parenti).getStatements().indexOf(beforei);
		variables.addAll(retrieveLocalVariables(pos, (CtBlock) parenti));
	}
	return variables;
}
 
Example 20
Source Project: gumtree-spoon-ast-diff   Source File: AstComparatorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test_t_211903() throws Exception {
	AstComparator diff = new AstComparator();
	// meld src/test/resources/examples/t_211903/left_MemberFilePersister_1.4.java
	// src/test/resources/examples/t_211903/right_MemberFilePersister_1.5.java
	File fl = new File("src/test/resources/examples/t_211903/left_MemberFilePersister_1.4.java");
	File fr = new File("src/test/resources/examples/t_211903/right_MemberFilePersister_1.5.java");
	Diff result = diff.compare(fl, fr);

	// result.debugInformation();

	CtElement ancestor = result.commonAncestor();
	assertTrue(ancestor instanceof CtConstructorCall);
	assertEquals(88, ancestor.getPosition().getLine());

	List<Operation> actions = result.getRootOperations();
	// result.debugInformation();
	assertTrue(
			result.containsOperation(OperationKind.Update, "ConstructorCall", "java.io.FileReader(java.io.File)"));
	assertTrue(result.containsOperation(OperationKind.Insert, "ConstructorCall",
			"java.io.InputStreamReader(java.io.InputStream,java.lang.String)"));

	// additional checks on low-level actions
	assertTrue(result.containsOperations(result.getAllOperations(), OperationKind.Insert, "Literal", "\"UTF-8\""));

	// the change is in the local variable declaration
	CtElement elem = actions.get(0).getNode();
	assertNotNull(elem);
	assertNotNull(elem.getParent(CtLocalVariable.class));
}
 
Example 21
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 22
@Override
public <T> void visitCtLocalVariable(CtLocalVariable<T> localVariable) {
    if (isReachable(localVariable)) {
        reachedVariables().add(localVariable);
    }
}
 
Example 23
Source Project: nopol   Source File: SpoonModelLibrary.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static <T> CtLocalVariable<T> newLocalVariableDeclaration(Factory factory, Class<T> type, String variableName, T defaultValue, CtElement parent) {
    CtLocalVariable<T> localVariable = newLocalVariableDeclaration(factory, type, variableName, defaultValue);
    setParent(parent, localVariable);
    return localVariable;
}
 
Example 24
Source Project: nopol   Source File: SpoonModelLibrary.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static <T> CtLocalVariable<T> newLocalVariableDeclaration(Factory factory, Class<T> type, String variableName, String defaultValue, CtElement parent) {
    CtLocalVariable<T> localVariable = newLocalVariableDeclaration(factory, type, variableName, defaultValue);
    setParent(parent, localVariable);
    return localVariable;
}
 
Example 25
Source Project: nopol   Source File: SpoonModelLibrary.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static <T> CtLocalVariable<T> newLocalVariableDeclaration(Factory factory, Class<T> type, String variableName, T defaultValue) {
    return newLocalVariable(factory, type, variableName, newLiteral(factory, defaultValue));
}
 
Example 26
Source Project: nopol   Source File: SpoonModelLibrary.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static <T> CtLocalVariable<T> newLocalVariableDeclarationString(Factory factory, Class<T> type, String variableName, String defaultValue) {
    return newLocalVariableDeclaration(factory, type, variableName, defaultValue);
}
 
Example 27
Source Project: nopol   Source File: SpoonModelLibrary.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static <T> CtLocalVariable<T> newLocalVariableDeclaration(Factory factory, Class<T> type, String variableName, String defaultValue) {
    return newLocalVariable(factory, type, variableName, newExpressionFromSnippet(factory, defaultValue, type));
}
 
Example 28
Source Project: nopol   Source File: SpoonModelLibrary.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static <T> CtLocalVariable<T> newLocalVariable(Factory factory, Class<T> aClass, String variableName, CtExpression<T> defaultValue) {
    CtLocalVariable<T> variable = newLocalVariable(factory, aClass, variableName);
    variable.setDefaultExpression(defaultValue);
    return variable;
}
 
Example 29
Source Project: nopol   Source File: SpoonModelLibrary.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static <T> CtLocalVariable<T> newLocalVariable(Factory factory, Class<T> aClass, String variableName) {
    CtLocalVariable<T> variable = factory.Core().createLocalVariable();
    variable.setType((CtTypeReference<T>) newTypeReference(factory, aClass).unbox());
    variable.setSimpleName(variableName);
    return variable;
}
 
Example 30
Source Project: nopol   Source File: PatchGenerator.java    License: GNU General Public License v2.0 4 votes vote down vote up
private String generateStringPatch() {
	String classContent = getClassContent();

	Factory factory = target.getFactory();
	factory.getEnvironment().setAutoImports(true);

	String line = getLine();
	CtStatement parentLine = getParentLine(target);
	String currentIndentation = "";

	for (int i = 0; i < line.length(); i++) {
		char s = line.charAt(i);
		if (s == ' ' || s == '\t') {
			currentIndentation += s;
			continue;
		}
		break;
	}
	line = line.trim();

	final String indentation = getIndentation();
	Writer writer = new Writer(currentIndentation, indentation);

	if (patch.getType() == RepairType.PRECONDITION) {
		if (isElseIf(parentLine)) {
			writer.write("} else {").tab();
			line = getSubstring(classContent, parentLine);
		}
		if (parentLine instanceof CtLocalVariable && patch.getType() == RepairType.PRECONDITION) {
			int variableNamePosition = line.indexOf(((CtLocalVariable) parentLine).getSimpleName());
			writer.write(line.substring(0, variableNamePosition));
			writer.write(((CtLocalVariable) parentLine).getSimpleName());
			writer.write(";").line();
		}
		writer.write("if (");
		writer.write(patch.asString());
		writer.write(") {").tab();
		writer.write(writer.addIndentationToString(line)).untab();
		writer.write("}");
		if (isElseIf(parentLine)) {
			writer.untab();
			writer.write("}");
		}
	} else {
		if (isElseIf(parentLine)) {
			writer.write("} else ");
		}
		writer.write(classContent.substring(parentLine.getPosition().getSourceStart(), target.getPosition().getSourceStart()));
		writer.write(patch.asString());
		writer.write(writer.addIndentationToString(classContent.substring(target.getPosition().getSourceEnd() + 1, parentLine.getPosition().getSourceEnd() + 1)));
	}


	return writer.toString();
}