spoon.reflect.code.CtThrow Java Examples
The following examples show how to use
spoon.reflect.code.CtThrow.
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: BoundProcessor.java From spoon-examples with GNU General Public License v2.0 | 6 votes |
public void process(Bound annotation, CtParameter<?> element) { final CtMethod parent = element.getParent(CtMethod.class); // Build if check for min. CtIf anIf = getFactory().Core().createIf(); anIf.setCondition(getFactory().Code().<Boolean>createCodeSnippetExpression(element.getSimpleName() + " < " + annotation.min())); CtThrow throwStmt = getFactory().Core().createThrow(); throwStmt.setThrownExpression((CtExpression<? extends Throwable>) getFactory().Core().createCodeSnippetExpression().setValue("new RuntimeException(\"out of min bound (\" + " + element.getSimpleName() + " + \" < " + annotation.min() + "\")")); anIf.setThenStatement(throwStmt); parent.getBody().insertBegin(anIf); anIf.setParent(parent); // Build if check for max. anIf = getFactory().Core().createIf(); anIf.setCondition(getFactory().Code().<Boolean>createCodeSnippetExpression(element.getSimpleName() + " > " + annotation.max())); anIf.setThenStatement(getFactory().Code().createCtThrow("new RuntimeException(\"out of max bound (\" + " + element.getSimpleName() + " + \" > " + annotation.max() + "\")")); parent.getBody().insertBegin(anIf); anIf.setParent(parent); }
Example #2
Source File: SpecialStatementFixSpaceProcessor.java From astor with GNU General Public License v2.0 | 6 votes |
@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 #3
Source File: AstComparatorTest.java From gumtree-spoon-ast-diff with Apache License 2.0 | 6 votes |
@Test public void test_t_214116() throws Exception { AstComparator diff = new AstComparator(); // meld src/test/resources/examples/t_214116/left_Modeller_1.134.java // src/test/resources/examples/t_214116/right_Modeller_1.135.java File fl = new File("src/test/resources/examples/t_214116/left_Modeller_1.134.java"); File fr = new File("src/test/resources/examples/t_214116/right_Modeller_1.135.java"); Diff result = diff.compare(fl, fr); CtElement ancestor = result.commonAncestor(); assertTrue(ancestor instanceof CtBinaryOperator); List<Operation> actions = result.getRootOperations(); // result.debugInformation(); assertEquals(actions.size(), 2); assertTrue(result.containsOperation(OperationKind.Update, "Literal", "\" \"")); // the change is in a throw CtElement elem = actions.get(0).getNode(); assertNotNull(elem); assertNotNull(elem.getParent(CtThrow.class)); }
Example #4
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 #5
Source File: SpoonModelLibrary.java From nopol with GNU General Public License v2.0 | 4 votes |
public static <E extends Throwable> CtThrow newThrow(Factory factory, Class<E> throwableClass, String thrownExpression) { return newThrow(factory, newExpressionFromSnippet(factory, thrownExpression, throwableClass)); }
Example #6
Source File: SpoonModelLibrary.java From nopol with GNU General Public License v2.0 | 4 votes |
public static <E extends Throwable> CtThrow newThrow(Factory factory, CtExpression<E> thrownExpression) { CtThrow aThrow = factory.Core().createThrow(); aThrow.setThrownExpression(thrownExpression); return aThrow; }
Example #7
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 }