use of org.eclipse.xtend.core.javaconverter.JavaConverter in project xtext-xtend by eclipse.
the class ConvertJavaCode method doConvert.
private void doConvert(final Set<ICompilationUnit> compilationUnits, IProgressMonitor monitor, Map<ICompilationUnit, ConversionResult> conversionResults) throws ExecutionException, InterruptedException {
for (ICompilationUnit iCompilationUnit : compilationUnits) {
if (monitor.isCanceled()) {
throw new InterruptedException();
}
JavaConverter converter = converterProvider.get();
ConversionResult conversionResult = converter.toXtend(iCompilationUnit);
monitor.subTask("Working with " + iCompilationUnit.getElementName());
if (!conversionResult.getProblems().iterator().hasNext()) {
IFile file = xtendFileToCreate(iCompilationUnit);
Resource resource = createResource(file, conversionResult.getXtendCode());
if (!validateResource(resource)) {
monitor.subTask("Conversion was not successfull. Re-trying with another strategy.");
conversionResult = converter.useRobustSyntax().toXtend(iCompilationUnit);
}
}
conversionResults.put(iCompilationUnit, conversionResult);
monitor.worked(1);
}
}
use of org.eclipse.xtend.core.javaconverter.JavaConverter in project xtext-xtend by eclipse.
the class PasteJavaCodeHandler method doPasteJavaCode.
private void doPasteJavaCode(final XtextEditor activeXtextEditor, final String javaCode, final JavaImportData javaImports) throws ExecutionException {
ISourceViewer sourceViewer = activeXtextEditor.getInternalSourceViewer();
final IXtextDocument xtextDocument = activeXtextEditor.getDocument();
IJavaProject project = null;
IEditorInput editorInput = activeXtextEditor.getEditorInput();
if (editorInput instanceof IFileEditorInput) {
IProject iProject = ((IFileEditorInput) editorInput).getFile().getProject();
project = JavaCore.create(iProject);
}
final int selectionOffset = sourceViewer.getSelectedRange().x - 1;
EObject targetElement = xtextDocument.readOnly(new IUnitOfWork<EObject, XtextResource>() {
@Override
public EObject exec(XtextResource state) throws Exception {
IParseResult parseResult = state.getParseResult();
if (parseResult == null) {
return null;
}
ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), selectionOffset);
return leafNode.getSemanticElement();
}
});
JavaConverter javaConverter = javaConverterProvider.get();
final String xtendCode = javaConverter.toXtend(javaCode, javaImports != null ? javaImports.getImports() : null, targetElement, project);
if (!Strings.isEmpty(xtendCode)) {
if (javaImports != null) {
importsUtil.addImports(javaImports.getImports(), javaImports.getStaticImports(), new String[] {}, xtextDocument);
}
Point selection = sourceViewer.getSelectedRange();
try {
xtextDocument.replace(selection.x, selection.y, xtendCode);
} catch (BadLocationException e) {
throw new ExecutionException("Failed to replace content.", e);
}
// TODO enable formatting, when performance became better
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=457814
// doFormat(sourceViewer, xtendCode, selection);
}
}
use of org.eclipse.xtend.core.javaconverter.JavaConverter in project xtext-xtend by eclipse.
the class JavaFileConverterTest method converToXtend.
public JavaConverter.ConversionResult converToXtend(final String unitName, final String javaCode) {
try {
final JavaConverter j2x = this.javaConverter.get();
JavaConverter.ConversionResult result = j2x.toXtend(unitName, javaCode);
int _size = IterableExtensions.size(result.getProblems());
boolean _equals = (_size == 0);
if (_equals) {
try {
this.file(result.getXtendCode(), true);
} catch (final Throwable _t) {
if (_t instanceof AssertionError) {
result = j2x.useRobustSyntax().toXtend(unitName, javaCode);
} else {
throw Exceptions.sneakyThrow(_t);
}
}
}
return result;
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
Aggregations