Java Code Examples for spoon.reflect.declaration.CtMethod#getBody()
The following examples show how to use
spoon.reflect.declaration.CtMethod#getBody() .
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: IfCountingInstrumentingProcessor.java From nopol with GNU General Public License v2.0 | 5 votes |
@Override public void process(final CtMethod<?> method) { if (method != null) { if (isTestCase(method)) { instrumentMethod(method); } else { if (method.getBody() != null) { List<CtIf> ifList = method.getBody().getElements( new Filter<CtIf>() { @Override public boolean matches(CtIf arg0) { if (!(arg0 instanceof CtIf)) { return false; } return true; } }); for (CtIf tmp : ifList) { instrumentIfInsideMethod(tmp); } } } String s = method.getDeclaringType().getQualifiedName(); if (this.ifMetric != null && !this.ifMetric.modifyClass.contains(s)) { this.ifMetric.modifyClass.add(s); } } }
Example 2
Source File: APICheckingProcessor.java From spoon-examples with GNU General Public License v2.0 | 5 votes |
@Override public void process(CtMethod method) { final Factory factory = method.getFactory(); CtBlock methodBody = method.getBody(); List<CtComment> bodyComments = new ArrayList<>(); ArrayList<CtStatement> ctStatements = new ArrayList<>(methodBody.getStatements()); for (CtStatement ctStatement : ctStatements) { String statement = ctStatement.toString(); bodyComments.add(factory.createInlineComment(statement)); methodBody.removeStatement(ctStatement); } CtClass<? extends Throwable> myExceptionClass = factory.Class().get(EXCEPTION_FQN); CtConstructorCall<? extends Throwable> myNewException = factory.createConstructorCall(myExceptionClass.getReference()); CtThrow throwMyException = factory.createThrow(); throwMyException.setThrownExpression(myNewException); methodBody.addStatement(throwMyException); bodyComments.add(factory.createInlineComment("FIXME: The private API type should never be return in a public API.")); for (CtComment bodyComment : bodyComments) { throwMyException.addComment(bodyComment); } }
Example 3
Source File: RetryProcessor.java From spoon-examples with GNU General Public License v2.0 | 5 votes |
@Override public void process(RetryOnFailure retryOnFailure, CtMethod<?> ctMethod) { RetryTemplate template = new RetryTemplate( ctMethod.getBody(), retryOnFailure.attempts(), retryOnFailure.delay(), retryOnFailure.verbose() ); CtBlock newBody = template.apply(ctMethod.getDeclaringType()); ctMethod.setBody(newBody); }
Example 4
Source File: SpoonMethodLibrary.java From nopol with GNU General Public License v2.0 | 4 votes |
public static boolean hasBody(CtMethod<?> method) { return method.getBody() != null; }
Example 5
Source File: IfCountingInstrumentingProcessor.java From nopol with GNU General Public License v2.0 | 4 votes |
private void instrumentMethod(CtMethod<?> method) { String className = method.getPosition().getCompilationUnit().getMainType().getSimpleName(); if (method.getBody() != null) { StringBuilder snippet_compute = new StringBuilder(); snippet_compute.append(IfMetric.COMPUTE_METRIC_CALL) .append("\"" + className + "\"").append("+") .append("\"").append(".") .append(method.getSimpleName()).append("\")"); CtStatement call_compute = getFactory().Code() .createCodeSnippetStatement(snippet_compute.toString()); StringBuilder snippet_reset = new StringBuilder(); snippet_reset.append(IfMetric.RESET_METRIC_CALL); CtStatement call_reset = getFactory().Code() .createCodeSnippetStatement(snippet_reset.toString()); CtStatementList list_call = new CtStatementListImpl<>(); list_call.addStatement(call_compute); list_call.addStatement(call_reset); /* * Workaround, getLastStatement throw * ArrayIndexOutOfBoundException when the method is empty */ try { CtStatement lastStatement = method.getBody() .getLastStatement(); if (lastStatement instanceof CtReturn) { lastStatement.insertBefore(list_call); } else { lastStatement.insertAfter(list_call); } } catch (ArrayIndexOutOfBoundsException aeoob) { /* * Do nothing because of empty method */ } } }
Example 6
Source File: BigTransfoScenarioTest.java From spoon-examples with GNU General Public License v2.0 | 4 votes |
@SuppressWarnings("all") @Test public void main() { MavenLauncher launcher = new MavenLauncher( "./src/test/resources/project/", MavenLauncher.SOURCE_TYPE.APP_SOURCE); CtModel model = launcher.buildModel(); List<CtMethod> methodList = model. filterChildren(new NamedElementFilter<CtPackage>(CtPackage.class, "ow2con")). filterChildren(new NamedElementFilter<CtPackage>(CtPackage.class, "publicapi")). filterChildren(new TypeFilter<CtMethod>(CtMethod.class)). filterChildren(new Filter<CtMethod>() { @Override public boolean matches(CtMethod element) { boolean isPublic = element.isPublic(); CtTypeReference returnType = element.getType(); String privateApiPackage = "ow2con.privateapi"; boolean isTypeFromPrivateApi = returnType.getQualifiedName().contains(privateApiPackage); return isPublic && isTypeFromPrivateApi; } }).list(); Factory factory = launcher.getFactory(); CtClass<? extends Throwable> exceptionClass = factory.createClass("ow2con.PrivateAPIException"); CtConstructorCall<? extends Throwable> exceptionInstance = factory.createConstructorCall(exceptionClass.getReference()); for (CtMethod method : methodList) { CtBlock methodBody = method.getBody(); List<CtComment> bodyComments = new ArrayList<>(); ArrayList<CtStatement> ctStatements = new ArrayList<>(methodBody.getStatements()); for (CtStatement ctStatement : ctStatements) { String statement = ctStatement.toString(); CtComment statementAsComment = factory.createInlineComment(statement); bodyComments.add(statementAsComment); methodBody.removeStatement(ctStatement); } CtThrow throwMyException = factory.createThrow(); CtConstructorCall<? extends Throwable> constructorCall = exceptionInstance.clone(); throwMyException.setThrownExpression(constructorCall); methodBody.addStatement(throwMyException); bodyComments.add( factory.createInlineComment( "FIXME: The private API type should never be return in a public API." ) ); for (CtComment bodyComment : bodyComments) { throwMyException.addComment(bodyComment); } } Environment environment = launcher.getEnvironment(); environment.setCommentEnabled(true); environment.setAutoImports(true); // the transformation must produce compilable code environment.setShouldCompile(true); launcher.prettyprint(); // look in folder spooned/ow2con/publicapi/ the transformed code }