Java源码示例:spoon.reflect.code.CtCodeSnippetStatement

示例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);
	}
}
 
示例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);
}
 
示例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;
}
 
示例4
@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);
	}
}
 
示例5
@Override
  public void visitCtCodeSnippetStatement(CtCodeSnippetStatement statement) {
/* ignore any code snippet, only work with what was originally in the code */
  }