Java Code Examples for spoon.reflect.code.CtCodeSnippetStatement

The following examples show how to use spoon.reflect.code.CtCodeSnippetStatement. 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
@Override
public void process(CtParameter<?> element) {
	// we declare a new snippet of code to be inserted.
	CtCodeSnippetStatement snippet = getFactory().Core().createCodeSnippetStatement();

	// this snippet contains an if check.
	final String value = String.format("if (%s == null) "
			+ "throw new IllegalArgumentException(\"[Spoon inserted check] null passed as parameter\");",
			element.getSimpleName());
	snippet.setValue(value);

	// we insert the snippet at the beginning of the method body.
	if (element.getParent(CtExecutable.class).getBody() != null) {
		element.getParent(CtExecutable.class).getBody().insertBegin(snippet);
	}
}
 
Example 2
/**
 * Stop Do loop on 3 or 100 Rounds
 */
@Override
public void process(CtLoop candidate) {
	thisIndex++;
	String constanteName = "_doExpressionMetaMutator"+thisIndex+"_Constante";
	String expression = "int "+ constanteName +" = 1";
	CtCodeSnippetStatement DeclareRoundStatement = getFactory().Core()
			.createCodeSnippetStatement();
	DeclareRoundStatement.setValue(expression);
	
	
	
	
	String expression2 = "if((" + PREFIX + thisIndex + ".is("+  NbRound.Rounds3.getClass().getCanonicalName() + '.' + NbRound.Rounds3.toString() + ")) && "+ constanteName +" == 3) "
						+ "{" + this.breakOrReturn(candidate) + "}"
						+ "else if((" + PREFIX + thisIndex + ".is("+NbRound.NoRound.getClass().getCanonicalName() + '.' + NbRound.NoRound.toString() + "))) "
						+ "{" + this.breakOrReturn(candidate) + "}"
						+ "else if("+ constanteName +" == 100)"
						+ "{" + this.breakOrReturn(candidate) + "}"
						+ " "+ constanteName +"++";
	CtCodeSnippetStatement ifRoundStatement = getFactory().Core()
			.createCodeSnippetStatement();
	ifRoundStatement.setValue(expression2);
	
	candidate.insertBefore(DeclareRoundStatement);
	candidate.getBody().insertAfter(ifRoundStatement);
	Selector.generateSelector(candidate, NbRound.NoRound, thisIndex, roundsSet, PREFIX);
}
 
Example 3
private CtCodeSnippetStatement createTestSnippet(CtClass element, Class<? extends Runner> runner) {
	CtCodeSnippetStatement e = getFactory().Core()
			.createCodeSnippetStatement();
	String val = "	new "+runner.getCanonicalName()+"("
			+ element.getQualifiedName()
			+ ".class).run(new org.junit.runner.notification.RunNotifier() {\n"
			+ "		@Override\n"
			+ "		public void fireTestFailure(org.junit.runner.notification.Failure failure) {\n"
			+ "			if (failure.getException() instanceof RuntimeException) throw (RuntimeException)failure.getException(); \n"
			+ "			if (failure.getException() instanceof Error) throw (Error)failure.getException(); \n"
			+ "         throw new RuntimeException(failure.getException());\n"
			+ "		}\n" + "	})";
	e.setValue(val);
	return e;
}
 
Example 4
Source Project: spoon-examples   Source File: LogProcessor.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void process(CtExecutable element) {
	CtCodeSnippetStatement snippet = getFactory().Core().createCodeSnippetStatement();

	// Snippet which contains the log.
	final String value = String.format("System.out.println(\"Enter in the method %s from class %s\");",
			element.getSimpleName(),
			element.getParent(CtClass.class).getSimpleName());
	snippet.setValue(value);

	// Inserts the snippet at the beginning of the method body.
	if (element.getBody() != null) {
		element.getBody().insertBegin(snippet);
	}
}
 
Example 5
@Override
  public void visitCtCodeSnippetStatement(CtCodeSnippetStatement statement) {
/* ignore any code snippet, only work with what was originally in the code */
  }