use of org.eclipse.jdt.core.dom.CatchClause in project evosuite by EvoSuite.
the class ResolutionMarkerTryBlock method run.
@Override
public void run(IMarker marker) {
// TODO Auto-generated method stub
IResource res = marker.getResource();
try {
String markerMessage = (String) marker.getAttribute(IMarker.MESSAGE);
String exception = markerMessage.split(" ")[0];
String[] exceptionPackageArray = exception.split("\\.");
String exceptionPackage = "";
for (int i = 0; i < exceptionPackageArray.length - 1; i++) {
exceptionPackage += exceptionPackageArray[i] + ".";
}
exception = exception.substring(exceptionPackage.length());
System.out.println("Package: " + exceptionPackage + ", Exception: " + exception);
ICompilationUnit icomp = CompilationUnitManager.getICompilationUnit(res);
CompilationUnit compunit = CompilationUnitManager.getCompilationUnit(res);
int position = marker.getAttribute(IMarker.CHAR_START, 0) + 1;
int line = marker.getAttribute(IMarker.LINE_NUMBER, 0);
if (position == 1) {
position = compunit.getPosition(line, 0);
}
AST ast = compunit.getAST();
ASTRewrite rewriter = ASTRewrite.create(ast);
IJavaElement element = icomp.getElementAt(position);
IJavaElement method = getMethod(element);
// TypeDeclaration td = (TypeDeclaration) compunit.types().get(0);
TypeDeclaration td = (TypeDeclaration) compunit.types().get(0);
MethodDeclaration md = td.getMethods()[0];
int counter = 1;
while (!md.getName().getFullyQualifiedName().equals(method.getElementName()) && counter < td.getMethods().length) {
md = td.getMethods()[counter];
System.out.println(md.getName().getFullyQualifiedName() + " " + method.getElementName());
counter++;
}
Block block = md.getBody();
ListRewrite lr = rewriter.getListRewrite(block, Block.STATEMENTS_PROPERTY);
ImportDeclaration id = ast.newImportDeclaration();
id.setName(ast.newName(exceptionPackage + exception));
ListRewrite lrClass = rewriter.getListRewrite(compunit, CompilationUnit.TYPES_PROPERTY);
lrClass.insertAt(id, 0, null);
ASTNode currentNode = null;
List<ASTNode> list = new ArrayList<ASTNode>();
for (Object o : lr.getOriginalList()) {
if (o instanceof ASTNode) {
list.add((ASTNode) o);
}
}
for (int i = 0; i < list.size(); i++) {
ASTNode node = list.get(i);
int nodeLine = compunit.getLineNumber(node.getStartPosition());
System.out.println(line + " " + nodeLine);
if (line == nodeLine) {
currentNode = node;
break;
}
List childrenList = node.structuralPropertiesForType();
for (int j = 0; j < childrenList.size(); j++) {
StructuralPropertyDescriptor curr = (StructuralPropertyDescriptor) childrenList.get(j);
Object child = node.getStructuralProperty(curr);
if (child instanceof List) {
for (Object ob : (List) child) {
if (ob instanceof ASTNode) {
list.add((ASTNode) ob);
}
}
} else if (child instanceof ASTNode) {
list.add((ASTNode) child);
}
}
}
TryStatement ts = ast.newTryStatement();
Statement emptyStatement = (Statement) rewriter.createStringPlaceholder("\n", ASTNode.EMPTY_STATEMENT);
Statement emptyStatement2 = (Statement) rewriter.createStringPlaceholder("\n\t", ASTNode.EMPTY_STATEMENT);
Block b2 = ast.newBlock();
b2.statements().add(0, ASTNode.copySubtree(b2.getAST(), currentNode));
b2.statements().add(1, emptyStatement);
b2.statements().add(0, emptyStatement2);
ts.setBody(b2);
CatchClause cc = ast.newCatchClause();
SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
svd.setName(ast.newSimpleName("e"));
Type type = ast.newSimpleType(ast.newName(exception));
svd.setType(type);
cc.setException(svd);
Block b3 = ast.newBlock();
Statement printStatement = (Statement) rewriter.createStringPlaceholder("e.printStackTrace();", ASTNode.EMPTY_STATEMENT);
b3.statements().add(0, printStatement);
cc.setBody(b3);
ListRewrite parentList = rewriter.getListRewrite(currentNode.getParent(), Block.STATEMENTS_PROPERTY);
parentList.replace(currentNode, ts, null);
parentList.insertAfter(cc, ts, null);
ITextFileBufferManager bm = FileBuffers.getTextFileBufferManager();
IPath path = compunit.getJavaElement().getPath();
try {
bm.connect(path, null, null);
ITextFileBuffer textFileBuffer = bm.getTextFileBuffer(path, null);
IDocument document = textFileBuffer.getDocument();
TextEdit edits = rewriter.rewriteAST(document, null);
edits.apply(document);
textFileBuffer.commit(null, false);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedTreeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bm.disconnect(path, null, null);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// (4)
}
marker.delete();
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
use of org.eclipse.jdt.core.dom.CatchClause in project eclipse.jdt.ls by eclipse.
the class QuickAssistProcessor method getCatchClauseToThrowsProposals.
public static boolean getCatchClauseToThrowsProposals(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> resultingCollections) {
if (resultingCollections == null) {
return true;
}
CatchClause catchClause = (CatchClause) ASTResolving.findAncestor(node, ASTNode.CATCH_CLAUSE);
if (catchClause == null) {
return false;
}
Statement statement = ASTResolving.findParentStatement(node);
if (statement != catchClause.getParent() && statement != catchClause.getBody()) {
// selection is in a statement inside the body
return false;
}
Type type = catchClause.getException().getType();
if (!type.isSimpleType() && !type.isUnionType() && !type.isNameQualifiedType()) {
return false;
}
BodyDeclaration bodyDeclaration = ASTResolving.findParentBodyDeclaration(catchClause);
if (!(bodyDeclaration instanceof MethodDeclaration) && !(bodyDeclaration instanceof Initializer)) {
return false;
}
AST ast = bodyDeclaration.getAST();
Type selectedMultiCatchType = null;
if (type.isUnionType() && node instanceof Name) {
Name topMostName = ASTNodes.getTopMostName((Name) node);
ASTNode parent = topMostName.getParent();
if (parent instanceof SimpleType) {
selectedMultiCatchType = (SimpleType) parent;
} else if (parent instanceof NameQualifiedType) {
selectedMultiCatchType = (NameQualifiedType) parent;
}
}
if (bodyDeclaration instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
ASTRewrite rewrite = ASTRewrite.create(ast);
if (selectedMultiCatchType != null) {
removeException(rewrite, (UnionType) type, selectedMultiCatchType);
addExceptionToThrows(ast, methodDeclaration, rewrite, selectedMultiCatchType);
String label = CorrectionMessages.QuickAssistProcessor_exceptiontothrows_description;
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_EXCEPTION_WITH_THROWS);
resultingCollections.add(proposal);
} else {
removeCatchBlock(rewrite, catchClause);
if (type.isUnionType()) {
UnionType unionType = (UnionType) type;
List<Type> types = unionType.types();
for (Type elementType : types) {
if (!(elementType instanceof SimpleType || elementType instanceof NameQualifiedType)) {
return false;
}
addExceptionToThrows(ast, methodDeclaration, rewrite, elementType);
}
} else {
addExceptionToThrows(ast, methodDeclaration, rewrite, type);
}
String label = CorrectionMessages.QuickAssistProcessor_catchclausetothrows_description;
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_CATCH_CLAUSE_WITH_THROWS);
resultingCollections.add(proposal);
}
}
{
// for initializers or method declarations
ASTRewrite rewrite = ASTRewrite.create(ast);
if (selectedMultiCatchType != null) {
removeException(rewrite, (UnionType) type, selectedMultiCatchType);
String label = CorrectionMessages.QuickAssistProcessor_removeexception_description;
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_EXCEPTION);
resultingCollections.add(proposal);
} else {
removeCatchBlock(rewrite, catchClause);
String label = CorrectionMessages.QuickAssistProcessor_removecatchclause_description;
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_CATCH_CLAUSE);
resultingCollections.add(proposal);
}
}
return true;
}
Aggregations