List of usage examples for org.eclipse.jdt.core ICompilationUnit commitWorkingCopy
void commitWorkingCopy(boolean force, IProgressMonitor monitor) throws JavaModelException;
From source file:com.ecfeed.ui.common.EclipseClassImplementHelper.java
License:Open Source License
@Override public void implementClass(final String thePackage, final String classNameWithoutExtension, final String content) { try {/*from w w w . j a va2s. co m*/ final IPackageFragment packageFragment = EclipsePackageFragmentGetter.getPackageFragment(thePackage, fFileInfoProvider); final String unitName = classNameWithoutExtension + ".java"; final boolean FORCE_CREATION = true; final ICompilationUnit unit = packageFragment.createCompilationUnit(unitName, content, FORCE_CREATION, null); unit.becomeWorkingCopy(null); unit.commitWorkingCopy(true, null); } catch (CoreException e) { ExceptionHelper.reportRuntimeException(e.getMessage()); } }
From source file:com.ecfeed.ui.common.EclipseMethodImplementHelper.java
License:Open Source License
@Override public void commitChanges() { IType classType = getClassTypeNotNull(); final ICompilationUnit unit = classType.getCompilationUnit(); try {/*from w w w.j av a 2 s . c om*/ unit.becomeWorkingCopy(null); unit.commitWorkingCopy(true, null); } catch (JavaModelException e) { ExceptionHelper.reportRuntimeException(e.getMessage()); } }
From source file:com.ecfeed.ui.common.EclipseModelImplementer.java
License:Open Source License
protected void implementParameterDefinition(AbstractParameterNode node, Set<String> fields) throws CoreException, EcException { String typeName = node.getType(); if (JavaUtils.isPrimitive(typeName)) { return;//from ww w .j av a 2s . c o m } if (JavaUtils.isValidTypeName(typeName) == false) { return; } String packageName = JavaUtils.getPackageName(typeName); String localName = JavaUtils.getLocalName(typeName); String unitName = localName + ".java"; // IPackageFragment packageFragment = getPackageFragment(packageName); IPackageFragment packageFragment = EclipsePackageFragmentGetter.getPackageFragment(packageName, fFileInfoProvider); ICompilationUnit unit = packageFragment.getCompilationUnit(unitName); unit.createType(enumDefinitionContent(node, fields), null, false, null); unit.becomeWorkingCopy(null); unit.commitWorkingCopy(true, null); }
From source file:com.ecfeed.ui.common.EclipseModelImplementer.java
License:Open Source License
protected void implementChoicesDefinitions(List<ChoiceNode> nodes) throws CoreException, EcException { refreshWorkspace();/*from w w w . j a v a 2s . c om*/ AbstractParameterNode parent = getParameter(nodes); if (parent == null) { return; } String typeName = parent.getType(); if (parameterDefinitionImplemented(parent) == false) { implementParameterDefinition(parent); } IType enumType = getJavaProject().findType(typeName); ICompilationUnit iUnit = enumType.getCompilationUnit(); CompilationUnit unit = getCompilationUnit(enumType); addEnumItems(unit, typeName, nodes, enumType); enumType.getResource().refreshLocal(IResource.DEPTH_ONE, null); iUnit.becomeWorkingCopy(null); iUnit.commitWorkingCopy(true, null); refreshWorkspace(); }
From source file:com.ecfeed.ui.common.JavaDocSupport.java
License:Open Source License
private static void exportJavadoc(IMember member, String comments) throws JavaModelException { if (member != null) { if (comments == null || comments.length() == 0) { removeJavadoc(member);//from ww w . j a va2s. c o m } else { ISourceRange currentJavaDocRange = member.getJavadocRange(); TextEdit edit = null; String indent = getIndent(member); if (currentJavaDocRange != null) { String javadoc = addJavadocFormatting(comments, indent); edit = new ReplaceEdit(currentJavaDocRange.getOffset(), currentJavaDocRange.getLength(), javadoc); } else if (member.getSourceRange().getOffset() >= 0) { boolean moveToNewLine = false; if (indent.matches("\\s*") == false) { indent = trimIndent(indent); moveToNewLine = true; } String javadoc = addJavadocFormatting(comments, indent); String comment = javadoc + "\n" + indent; if (moveToNewLine) { comment = "\n" + indent + comment; } edit = new InsertEdit(member.getSourceRange().getOffset(), comment); } if (edit != null) { ICompilationUnit unit = member.getCompilationUnit(); if (unit != null) { unit.becomeWorkingCopy(null); unit.applyTextEdit(edit, null); unit.commitWorkingCopy(true, null); } } } } }
From source file:com.ecfeed.ui.common.JavaDocSupport.java
License:Open Source License
private static void removeJavadoc(IMember member) throws JavaModelException { ISourceRange currentJavaDocRange = member.getJavadocRange(); if (currentJavaDocRange != null) { ICompilationUnit unit = member.getCompilationUnit(); String source = unit.getSource(); int offset = currentJavaDocRange.getOffset(); int length = currentJavaDocRange.getLength(); while (((Character) source.charAt(offset + length)).toString().matches("\\s")) { ++length;// ww w . j a v a2s. c om } unit.applyTextEdit(new DeleteEdit(offset, length), null); unit.commitWorkingCopy(false, null); } }
From source file:com.github.parzonka.ccms.handler.BatchProcessingHandler.java
License:Open Source License
/** * Sorts a single CompilationUnit.//from w w w.j a v a 2 s.c om * * @param cu * @throws ExecutionException */ private void sort(ICompilationUnit cu) throws ExecutionException { try { cu.becomeWorkingCopy(null); fMethodSorter.sort(cu); sortedClassesCount++; cu.commitWorkingCopy(true, null); cu.discardWorkingCopy(); } catch (JavaModelException e) { throw new ExecutionException(e.getMessage()); } }
From source file:com.google.gdt.eclipse.appengine.rpc.util.CompilationUnitCreator.java
License:Open Source License
/** * /*from ww w .j a v a 2s . co m*/ * @param type IType - entity to generate request factory code * @param pack IPackageFragment - package for the file * @param name String - name of the file * @param rpcType int - whether proxy, locator, service, request, * requestfactory * @param monitor IProgressMonitor * @return IJavaElement - the created element * @throws CoreException */ public IJavaElement create(IType type, IPackageFragment pack, String name, RpcType rpcType, IProgressMonitor monitor) throws CoreException { IJavaElement element = null; IType createdType = null; ImportsManager imports; ICompilationUnit connectedCU = null; current = type; lineDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-N$ try { ICompilationUnit parentCU = pack.createCompilationUnit(name + ".java", //$NON-NLS-N$ "", true, new SubProgressMonitor(monitor, 1)); parentCU.becomeWorkingCopy(new SubProgressMonitor(monitor, 1)); connectedCU = parentCU; IBuffer buffer = parentCU.getBuffer(); String simpleTypeStub = constructSimpleTypeStub(name); String content = CodeGeneration.getCompilationUnitContent(parentCU, null, null, simpleTypeStub, lineDelimiter); buffer.setContents(content); CompilationUnit astRoot = createASTForImports(parentCU); imports = new ImportsManager(astRoot); String typeContent; String annotation = ""; List<String> interfaces = new ArrayList<String>(); boolean isInterface = true; switch (rpcType) { case SERVICE: isInterface = false; break; case LOCATOR: isInterface = false; interfaces.add("com.google.web.bindery.requestfactory.shared.Locator"); //$NON-NLS-N$ if (RequestFactoryUtils.shouldBeProxiedAsAnEntity(type)) { for (IMethod method : type.getMethods()) { if (method.getElementName().equals("getId")) { //$NON-NLS-N$ entityIdType = Signature.toString(method.getReturnType()); } } } else { entityIdType = "Void"; //$NON-NLS-N$ } break; case PROXY: if (RequestFactoryUtils.shouldBeProxiedAsAnEntity(current)) { interfaces.add("com.google.web.bindery.requestfactory.shared.EntityProxy"); //$NON-NLS-N$ } else { interfaces.add("com.google.web.bindery.requestfactory.shared.ValueProxy");//$NON-NLS-N$ } annotation = "@ProxyForName(value=\"" + current.getFullyQualifiedName() //$NON-NLS-N$ + "\",\nlocator = \"" + current.getFullyQualifiedName() + "Locator\")"; break; case REQUEST: interfaces.add("com.google.web.bindery.requestfactory.shared.RequestContext");//$NON-NLS-N$ annotation = "@ServiceName(\"" + serviceName //$NON-NLS-N$ + "\")"; break; case REQ_FACTORY: interfaces.add("com.google.web.bindery.requestfactory.shared.RequestFactory"); //$NON-NLS-N$ break; } typeContent = constructTypeStub(parentCU, name, isInterface, interfaces, annotation, imports); int index = content.lastIndexOf(simpleTypeStub); if (index == -1) { AbstractTypeDeclaration typeNode = (AbstractTypeDeclaration) astRoot.types().get(0); int start = ((ASTNode) typeNode.modifiers().get(0)).getStartPosition(); int end = typeNode.getStartPosition() + typeNode.getLength(); buffer.replace(start, end - start, typeContent); } else { buffer.replace(index, simpleTypeStub.length(), typeContent); } createdType = parentCU.getType(name); ICompilationUnit cu = createdType.getCompilationUnit(); imports.create(false, new SubProgressMonitor(monitor, 1)); cu.reconcile(ICompilationUnit.NO_AST, false, null, null); astRoot = createASTForImports(cu); imports = new ImportsManager(astRoot); switch (rpcType) { case SERVICE: constructServiceBody(createdType, imports, new SubProgressMonitor(monitor, 1)); break; case LOCATOR: constructLocatorBody(createdType, imports, new SubProgressMonitor(monitor, 1)); break; case PROXY: constructProxyBody(createdType, imports, new SubProgressMonitor(monitor, 1)); break; case REQUEST: requestTypes.add(createdType); constructRequestBody(createdType, imports, monitor); break; case REQ_FACTORY: constructReqFactoryBody(createdType, imports, new SubProgressMonitor(monitor, 1)); break; } imports.create(false, new SubProgressMonitor(monitor, 1)); removeUnusedImports(cu, getExistingImports(astRoot), false); cu.reconcile(ICompilationUnit.NO_AST, false, null, null); ISourceRange range = createdType.getSourceRange(); IBuffer buf = cu.getBuffer(); String originalContent = buf.getText(range.getOffset(), range.getLength()); String formattedContent = CodegenUtils.format(originalContent, CodeFormatter.K_CLASS_BODY_DECLARATIONS); buf.replace(range.getOffset(), range.getLength(), formattedContent); cu.commitWorkingCopy(true, new SubProgressMonitor(monitor, 1)); element = cu.getPrimaryElement(); } finally { if (connectedCU != null) { connectedCU.discardWorkingCopy(); } } monitor.done(); return element; }
From source file:com.google.gdt.eclipse.appengine.rpc.wizards.helpers.RpcServiceLayerCreator.java
License:Open Source License
private void addReqFactoryBody(IType type, IProgressMonitor monitor) throws MalformedTreeException, BadLocationException, CoreException { ICompilationUnit cu = type.getCompilationUnit(); cu.becomeWorkingCopy(monitor);/*from w w w . j av a 2 s . co m*/ String source = cu.getSource(); Document document = new Document(source); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(cu); CompilationUnit astRoot = (CompilationUnit) parser.createAST(null); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); ListRewrite listRewriter = null; AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) astRoot.types().get(0); if (declaration != null) { listRewriter = rewrite.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()); } ImportRewrite importRewrite = CodeStyleConfiguration.createImportRewrite(astRoot, true); StringBuffer buf = new StringBuffer(); for (IType request : requestTypes) { importRewrite.addImport(request.getFullyQualifiedName()); buf.append(request.getElementName()); buf.append(" "); //$NON-NLS-N$ String name = request.getElementName(); buf.append(name.substring(0, 1).toLowerCase() + name.substring(1)); buf.append("();"); //$NON-NLS-N$ buf.append(lineDelimiter); } MethodDeclaration methodDecl = (MethodDeclaration) listRewriter.getASTRewrite() .createStringPlaceholder(buf.toString(), ASTNode.METHOD_DECLARATION); listRewriter.insertLast(methodDecl, null); TextEdit edits = rewrite.rewriteAST(document, cu.getJavaProject().getOptions(true)); edits.addChild(importRewrite.rewriteImports(monitor)); edits.apply(document); cu.getBuffer().setContents(document.get()); cu.reconcile(ICompilationUnit.NO_AST, false, null, null); ISourceRange range = type.getSourceRange(); IBuffer buffer = cu.getBuffer(); String originalContent = buffer.getText(range.getOffset(), range.getLength()); String formattedContent = format(originalContent, CodeFormatter.K_CLASS_BODY_DECLARATIONS); buffer.replace(range.getOffset(), range.getLength(), formattedContent); cu.commitWorkingCopy(true, monitor); cu.discardWorkingCopy(); }
From source file:com.google.gdt.eclipse.appengine.rpc.wizards.helpers.RpcServiceLayerCreator.java
License:Open Source License
private void createCompilationUnit(IPackageFragment pack, String name, RpcType rpcType, IProgressMonitor monitor) throws CoreException { IType createdType = null;/*from w ww .jav a2 s . c om*/ ImportsManager imports; ICompilationUnit connectedCU = null; try { ICompilationUnit parentCU = pack.createCompilationUnit(name + ".java", //$NON-NLS-N$ "", true, new SubProgressMonitor(monitor, 1)); parentCU.becomeWorkingCopy(new SubProgressMonitor(monitor, 1)); connectedCU = parentCU; IBuffer buffer = parentCU.getBuffer(); String simpleTypeStub = constructSimpleTypeStub(name); String typeComment = null; switch (rpcType) { case ANNOTATION: typeComment = "/**" + lineDelimiter + " * Annotation on method specifying that the method is a service method" + lineDelimiter + "* and needs to have the corresponding request factory code " + lineDelimiter + "*/"; break; } String content = CodeGeneration.getCompilationUnitContent(parentCU, null, typeComment, simpleTypeStub, lineDelimiter); buffer.setContents(content); CompilationUnit astRoot = createASTForImports(parentCU); // Set<String> existingImports = getExistingImports(astRoot); imports = new ImportsManager(astRoot); String typeContent; String annotation = ""; List<String> interfaces = new ArrayList<String>(); switch (rpcType) { case ANNOTATION: annotation = "@Target(ElementType.METHOD)" + lineDelimiter + "@Retention(RetentionPolicy.CLASS)"; imports.addImport("java.lang.annotation.ElementType"); imports.addImport("java.lang.annotation.Retention"); imports.addImport("java.lang.annotation.RetentionPolicy"); imports.addImport("java.lang.annotation.Target"); break; case LOCATOR: interfaces.add("com.google.web.bindery.requestfactory.shared.Locator"); break; case PROXY: if (RequestFactoryUtils.shouldBeProxiedAsAnEntity(current)) { interfaces.add("com.google.web.bindery.requestfactory.shared.EntityProxy"); //$NON-NLS-N$ } else { interfaces.add("com.google.web.bindery.requestfactory.shared.ValueProxy");//$NON-NLS-N$ } annotation = "@ProxyForName(value=\"" + current.getFullyQualifiedName() //$NON-NLS-N$ + "\",\nlocator = \"" + current.getFullyQualifiedName() + "Locator\")"; break; case REQUEST: interfaces.add("com.google.web.bindery.requestfactory.shared.RequestContext");//$NON-NLS-N$ annotation = "@ServiceName(value=\"" + packageName + "." + serviceName //$NON-NLS-N$ + "\", locator=\"" + packageName + "." + serviceName + "Locator\")"; break; case REQ_FACTORY: interfaces.add("com.google.web.bindery.requestfactory.shared.RequestFactory"); //$NON-NLS-N$ break; case SERVICE_LOCATOR: interfaces.add("com.google.web.bindery.requestfactory.shared.ServiceLocator"); break; } typeContent = constructTypeStub(parentCU, name, rpcType, interfaces, annotation, imports); int index = content.lastIndexOf(simpleTypeStub); if (index == -1) { AbstractTypeDeclaration typeNode = (AbstractTypeDeclaration) astRoot.types().get(0); int start = ((ASTNode) typeNode.modifiers().get(0)).getStartPosition(); int end = typeNode.getStartPosition() + typeNode.getLength(); buffer.replace(start, end - start, typeContent); } else { buffer.replace(index, simpleTypeStub.length(), typeContent); } createdType = parentCU.getType(name); ICompilationUnit cu = createdType.getCompilationUnit(); imports.create(false, new SubProgressMonitor(monitor, 1)); JavaModelUtil.reconcile(cu); astRoot = createASTForImports(cu); imports = new ImportsManager(astRoot); switch (rpcType) { case SERVICE: constructServiceBody(createdType, imports, new SubProgressMonitor(monitor, 1)); break; case LOCATOR: constructLocatorBody(createdType, imports, new SubProgressMonitor(monitor, 1)); break; case PROXY: constructProxyBody(createdType, imports, new SubProgressMonitor(monitor, 1)); break; case REQUEST: requestTypes.add(createdType); constructRequestBody(createdType, imports, monitor); break; case REQ_FACTORY: constructReqFactoryBody(createdType, imports, new SubProgressMonitor(monitor, 1)); break; case SERVICE_LOCATOR: constructServiceLocatorBody(createdType, imports, new SubProgressMonitor(monitor, 1)); break; } imports.create(false, new SubProgressMonitor(monitor, 1)); removeUnusedImports(cu, getExistingImports(astRoot), false); JavaModelUtil.reconcile(cu); ISourceRange range = createdType.getSourceRange(); IBuffer buf = cu.getBuffer(); String originalContent = buf.getText(range.getOffset(), range.getLength()); String formattedContent = format(originalContent, CodeFormatter.K_CLASS_BODY_DECLARATIONS); buf.replace(range.getOffset(), range.getLength(), formattedContent); cu.commitWorkingCopy(true, new SubProgressMonitor(monitor, 1)); if (rpcType == RpcType.SERVICE) { serviceJavaElement = cu.getPrimaryElement(); } } finally { if (connectedCU != null) { connectedCU.discardWorkingCopy(); } monitor.done(); } }