use of org.eclipse.text.edits.MalformedTreeException in project evosuite by EvoSuite.
the class ResolutionMarkerEvoIgnoreForClass method run.
@Override
public void run(IMarker marker) {
IResource res = marker.getResource();
try {
CompilationUnit compunit = CompilationUnitManager.getCompilationUnit(res);
int position = marker.getAttribute(IMarker.CHAR_START, 0) + 1;
if (position == 0) {
int line = marker.getAttribute(IMarker.LINE_NUMBER, 0);
position = compunit.getPosition(line, 0);
}
AST ast = compunit.getAST();
ASTRewrite rewriter = ASTRewrite.create(ast);
Annotation annotation = ast.newNormalAnnotation();
annotation.setTypeName(ast.newName("EvoIgnore"));
ImportDeclaration id = ast.newImportDeclaration();
id.setName(ast.newName("org.evosuite.quickfixes.annotations.EvoIgnore"));
ListRewrite lr = rewriter.getListRewrite(compunit, CompilationUnit.TYPES_PROPERTY);
// lr.insertFirst(annotation, null);
lr.insertAt(annotation, 0, null);
lr.insertAt(id, 0, 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)
}
System.out.println(lr.getRewrittenList() + "\nPosition: " + position + "\nEdits: " + rewriter.toString());
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
marker.delete();
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of org.eclipse.text.edits.MalformedTreeException in project evosuite by EvoSuite.
the class ResolutionMarkerEvoIgnoreForMethod method run.
@Override
public void run(IMarker marker) {
// TODO Auto-generated method stub
IResource res = marker.getResource();
try {
ICompilationUnit icomp = CompilationUnitManager.getICompilationUnit(res);
CompilationUnit compunit = CompilationUnitManager.getCompilationUnit(res);
int position = marker.getAttribute(IMarker.CHAR_START, 0) + 1;
if (position == 1) {
int line = marker.getAttribute(IMarker.LINE_NUMBER, 0);
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++;
}
Annotation annotation = ast.newNormalAnnotation();
annotation.setTypeName(ast.newName("EvoIgnore"));
ImportDeclaration id = ast.newImportDeclaration();
id.setName(ast.newName("org.evosuite.quickfixes.annotations.EvoIgnore"));
ListRewrite lr = rewriter.getListRewrite(md.getParent(), TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
ListRewrite lr2 = rewriter.getListRewrite(compunit, CompilationUnit.TYPES_PROPERTY);
// lr.insertFirst(annotation, null);
lr.insertBefore(annotation, md, null);
lr2.insertAt(id, 0, 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)
}
System.out.println(lr.getRewrittenList() + "\nPosition: " + position + "\nEdits: " + rewriter.toString());
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
marker.delete();
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of org.eclipse.text.edits.MalformedTreeException 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.text.edits.MalformedTreeException in project eclipse.jdt.ls by eclipse.
the class BaseDocumentLifeCycleHandler method handleChanged.
public ICompilationUnit handleChanged(DidChangeTextDocumentParams params) {
String uri = params.getTextDocument().getUri();
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
if (unit == null || !unit.isWorkingCopy() || params.getContentChanges().isEmpty() || unit.getResource().isDerived()) {
return unit;
}
try {
if (unit.equals(sharedASTProvider.getActiveJavaElement())) {
sharedASTProvider.disposeAST();
}
List<TextDocumentContentChangeEvent> contentChanges = params.getContentChanges();
for (TextDocumentContentChangeEvent changeEvent : contentChanges) {
Range range = changeEvent.getRange();
int length;
IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer());
final int startOffset;
if (range != null) {
Position start = range.getStart();
startOffset = JsonRpcHelpers.toOffset(document, start.getLine(), start.getCharacter());
length = DiagnosticsHelper.getLength(unit, range);
} else {
// range is optional and if not given, the whole file content is replaced
length = unit.getSource().length();
startOffset = 0;
}
TextEdit edit = null;
String text = changeEvent.getText();
if (length == 0) {
edit = new InsertEdit(startOffset, text);
} else if (text.isEmpty()) {
edit = new DeleteEdit(startOffset, length);
} else {
edit = new ReplaceEdit(startOffset, length, text);
}
edit.apply(document, TextEdit.NONE);
}
triggerValidation(unit);
} catch (JavaModelException | MalformedTreeException | BadLocationException e) {
JavaLanguageServerPlugin.logException("Error while handling document change. URI: " + uri, e);
}
return unit;
}
use of org.eclipse.text.edits.MalformedTreeException in project eclipse.jdt.ls by eclipse.
the class TextEditConverter method visit.
/*
* (non-Javadoc)
*
* @see org.eclipse.text.edits.TextEditVisitor#visit(org.eclipse.text.edits.
* MoveTargetEdit)
*/
@Override
public boolean visit(MoveTargetEdit edit) {
try {
if (edit.getSourceEdit() != null) {
org.eclipse.lsp4j.TextEdit te = new org.eclipse.lsp4j.TextEdit();
te.setRange(JDTUtils.toRange(compilationUnit, edit.getOffset(), edit.getLength()));
Document doc = new Document(compilationUnit.getSource());
edit.apply(doc, TextEdit.UPDATE_REGIONS);
String content = doc.get(edit.getSourceEdit().getOffset(), edit.getSourceEdit().getLength());
if (edit.getSourceEdit().getSourceModifier() != null) {
content = applySourceModifier(content, edit.getSourceEdit().getSourceModifier());
}
te.setNewText(content);
converted.add(te);
// do not visit children
return false;
}
} catch (MalformedTreeException | BadLocationException | CoreException e) {
JavaLanguageServerPlugin.logException("Error converting TextEdits", e);
}
return super.visit(edit);
}
Aggregations