List of usage examples for org.eclipse.jdt.core IBuffer replace
public void replace(int position, int length, String text);
From source file:org.jboss.tools.common.refactoring.MarkerResolutionUtils.java
License:Open Source License
public static void deleteAnnotation(String qualifiedName, ICompilationUnit compilationUnit, IJavaElement element, MultiTextEdit rootEdit) throws JavaModelException { IJavaElement workingCopyElement = findWorkingCopy(compilationUnit, element); if (workingCopyElement == null) { return;//from w ww . jav a2 s. c om } IAnnotation annotation = findAnnotation(workingCopyElement, qualifiedName); if (annotation != null) { IBuffer buffer = compilationUnit.getBuffer(); int numberOfSpaces = getNumberOfSpacesToDelete( annotation.getSourceRange().getOffset() + annotation.getSourceRange().getLength(), buffer); // delete annotation if (rootEdit != null) { TextEdit edit = new DeleteEdit(annotation.getSourceRange().getOffset(), annotation.getSourceRange().getLength() + numberOfSpaces); rootEdit.addChild(edit); } else { buffer.replace(annotation.getSourceRange().getOffset(), annotation.getSourceRange().getLength() + numberOfSpaces, ""); } // check and delete import deleteImportForAnnotation(qualifiedName, annotation, compilationUnit, buffer, rootEdit); } }
From source file:org.jboss.tools.esb.ui.wizard.NewActionWizardPage.java
License:Open Source License
void modifyJavaSourceForAbstractImplementation(IType type, ImportsManager imports) { try {/*w w w . j a v a 2 s . co m*/ String name = type.getElementName(); String sc = type.getSuperclassTypeSignature(); if (sc != null) { sc = EclipseJavaUtil.resolveTypeAsString(type, sc); } if (type != null && PROCESSOR_CLASS.equals(sc)) { ICompilationUnit w = type.getCompilationUnit(); IBuffer b = w.getBuffer(); String s = b.getContents(); String lineDelimiter = StubUtility.getLineDelimiterUsed(type.getJavaProject()); imports.addImport("org.jboss.soa.esb.actions.AbstractActionPipelineProcessor"); imports.addImport("org.jboss.soa.esb.actions.ActionProcessingException"); imports.addImport("org.jboss.soa.esb.helpers.ConfigTree"); imports.addImport("org.jboss.soa.esb.message.Message"); // imports.addImport(""); s = b.getContents(); boolean hasOverrideAnnotation = s.indexOf("@Override") > 0; int i = s.indexOf('{'); int j = s.lastIndexOf('}'); if (i > 0 && j > i) { String tab = "\t"; String content = lineDelimiter + tab + "protected ConfigTree _config;" + lineDelimiter + lineDelimiter + tab + "public " + name + "(ConfigTree config) {" + lineDelimiter + tab + tab + "_config = config;" + lineDelimiter + tab + "}" + lineDelimiter + lineDelimiter + (hasOverrideAnnotation ? tab + "@Override" + lineDelimiter : "") + tab + "public Message process(Message message) throws ActionProcessingException {" + lineDelimiter + tab + tab + "//ADD CUSTOM ACTION CODE HERE" + lineDelimiter + tab + tab + "return message;" + lineDelimiter + tab + "}" + lineDelimiter; b.replace(i + 1, j - i - 1, content); } } } catch (CoreException e) { ESBCorePlugin.log(e); } }
From source file:org.jboss.tools.esb.ui.wizard.NewActionWizardPage.java
License:Open Source License
void modifyJavaSourceForPOJO(IType type, ImportsManager imports) { StringBuffer buf = new StringBuffer(); IMethod process = null;//from w w w . ja va 2s. co m try { IMethod[] ms = type.getMethods(); for (int i = 0; i < ms.length; i++) { if (ms[i].getElementName().equals("process")) { //$NON-NLS-1$ process = ms[i]; } } } catch (JavaModelException e) { // } if (process != null) { try { ICompilationUnit w = type.getCompilationUnit(); IBuffer b = w.getBuffer(); String lineDelimiter = StubUtility.getLineDelimiterUsed(type.getJavaProject()); int offset = process.getSourceRange().getOffset(); String tab = "\t"; //$NON-NLS-1$ imports.addImport(PROCESS); //$NON-NLS-1$ buf.append(tab).append("@Process").append(lineDelimiter); //$NON-NLS-1$ b.replace(offset, 0, buf.toString()); } catch (JavaModelException e) { ESBCorePlugin.log(e); } return; } try { final String lineDelim = "\n"; // OK, since content is formatted afterwards //$NON-NLS-1$ String comment = CodeGeneration.getMethodComment(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "process", new String[] { "message" }, new String[0], //$NON-NLS-1$//$NON-NLS-2$ Signature.createTypeSignature("void", true), null, lineDelim); //$NON-NLS-1$ if (comment != null) { buf.append(comment); buf.append(lineDelim); } imports.addImport(PROCESS); //$NON-NLS-1$ buf.append("@Process").append(lineDelim); //$NON-NLS-1$ buf.append("public Message process("); //$NON-NLS-1$ buf.append(imports.addImport("org.jboss.soa.esb.message.Message")); //$NON-NLS-1$ buf.append(" message) {"); //$NON-NLS-1$ buf.append(lineDelim); String content = "//ADD CUSTOM ACTION CODE HERE" + lineDelim + "return message;"; // CodeGeneration.getMethodBodyContent(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "process", false, "//ADD CUSTOM ACTION CODE HERE", lineDelim); //$NON-NLS-1$ //$NON-NLS-2$ if (content != null && content.length() != 0) buf.append(content); buf.append(lineDelim); buf.append("}"); //$NON-NLS-1$ type.createMethod(buf.toString(), null, false, null); } catch (CoreException e) { ESBCorePlugin.log(e); } }
From source file:org.jboss.tools.seam.ui.marker.AddSetterMarkerResolution.java
License:Open Source License
public void run(IMarker marker) { IFile file = (IFile) javaDeclaration.getResource(); try {//from www . j a va2s . c om ICompilationUnit original = EclipseUtil.getCompilationUnit(file); if (original == null) { return; } ICompilationUnit compilationUnit = original.getWorkingCopy(new NullProgressMonitor()); String lineDelim = JavaPropertyGenerator.getLineDelimiterUsed(compilationUnit); Hashtable<String, String> options = JavaCore.getOptions(); int tabSize = new Integer(options.get("org.eclipse.jdt.core.formatter.tabulation.size")); StringBuffer tabBuf = new StringBuffer(); for (int i = 0; i < tabSize; i++) tabBuf.append(" "); String tab = tabBuf.toString(); IType type = compilationUnit.findPrimaryType(); IField field = type.getField(property.getName()); String propertyType = ""; if (field != null && field.exists()) { propertyType = field.getTypeSignature(); } else { propertyType = "String"; //$NON-NLS-1$ StringBuffer buf = new StringBuffer(); buf.append(tab + "private " + propertyType + " " + property.getName() + ";"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ buf.append(lineDelim); field = type.createField(buf.toString(), null, false, new NullProgressMonitor()); if (field != null) { IBuffer buffer = compilationUnit.getBuffer(); buffer.replace(field.getSourceRange().getOffset(), field.getSourceRange().getLength(), buf.toString()); synchronized (compilationUnit) { compilationUnit.reconcile(ICompilationUnit.NO_AST, true, null, null); } } } IMethod oldMethod = GetterSetterUtil.getSetter(field); if (oldMethod == null || !oldMethod.exists()) { String setterName = GetterSetterUtil.getSetterName(field, null); //JavaPropertyGenerator.createSetter(compilationUnit, type, "public", field.getTypeSignature(), setterName, lineDelim); String stub = GetterSetterUtil.getSetterStub(field, setterName, true, Flags.AccPublic); IMethod newMethod = type.createMethod(stub, null, false, new NullProgressMonitor()); if (newMethod != null) { IBuffer buffer = compilationUnit.getBuffer(); // format StringBuffer buf = new StringBuffer(); buf.append(lineDelim); buf.append(tab + "public void " + setterName + "(" + propertyType + " " + property.getName() //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ + "){"); buf.append(lineDelim); buf.append(tab + tab + "this." + property.getName() + " = " + property.getName() + ";"); buf.append(lineDelim); buf.append(tab + "}"); buf.append(lineDelim); buffer.replace(newMethod.getSourceRange().getOffset(), newMethod.getSourceRange().getLength(), buf.toString()); } } compilationUnit.commitWorkingCopy(false, new NullProgressMonitor()); compilationUnit.discardWorkingCopy(); } catch (CoreException ex) { SeamGuiPlugin.getPluginLog().logError(ex); } }
From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java
License:Open Source License
private static void insertCodeAtLocation(ICompilationUnit compilationUnit, String content, int offset, boolean useWorkingCopy) throws CoreException { ICompilationUnit unit = getCompilationUnit(compilationUnit, useWorkingCopy); IBuffer buffer = ((IOpenable) unit).getBuffer(); buffer.replace(offset, 0, content + "\n"); // append a new line at the // same time/*from w ww. ja v a 2 s . c om*/ saveAndClose(unit); String subSource = compilationUnit.getSource().substring(offset, offset + content.length()); Assert.assertEquals("Content was not inserted", content, subSource); }
From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java
License:Open Source License
/** * Replace the first occurrence of the given old content with the new content. Fails if the old content is not found * (avoids weird side effects in the rest of the test). * //from ww w. java 2 s .com * @param compilationUnit * @param oldContent * @param newContent * @param useWorkingCopy * @throws JavaModelException */ public static void replaceFirstOccurrenceOfCode(ICompilationUnit compilationUnit, String oldContent, String newContent, boolean useWorkingCopy) throws JavaModelException { ICompilationUnit unit = getCompilationUnit(compilationUnit, useWorkingCopy); IBuffer buffer = ((IOpenable) unit).getBuffer(); int offset = buffer.getContents().indexOf(oldContent); Assert.assertTrue("Old content '" + oldContent + "' not found", offset != -1); buffer.replace(offset, oldContent.length(), newContent); saveAndClose(unit); }
From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java
License:Open Source License
public static <T extends IMember> T replaceFirstOccurrenceOfCode(T member, String oldContent, String newContent, boolean useWorkingCopy) throws JavaModelException { ICompilationUnit compilationUnit = member.getCompilationUnit(); ICompilationUnit unit = useWorkingCopy ? createWorkingCopy(compilationUnit) : member.getCompilationUnit(); ISourceRange sourceRange = member.getSourceRange(); IBuffer buffer = ((IOpenable) unit).getBuffer(); int offset = buffer.getContents().indexOf(oldContent, sourceRange.getOffset()); Assert.assertTrue("Old content not found: '" + oldContent + "'", offset != -1); buffer.replace(offset, oldContent.length(), newContent); // IJavaElement modifiedMethod = // workingCopy.getElementAt(sourceRange.getOffset()); saveAndClose(unit);//from www. j ava2 s . c o m @SuppressWarnings("unchecked") T modifiedElement = (T) compilationUnit.getElementAt(sourceRange.getOffset()); return modifiedElement; }
From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java
License:Open Source License
public static void replaceAllOccurrencesOfCode(ICompilationUnit compilationUnit, String oldContent, String newContent, boolean useWorkingCopy) throws JavaModelException { ICompilationUnit unit = getCompilationUnit(compilationUnit, useWorkingCopy); IBuffer buffer = ((IOpenable) unit).getBuffer(); int offset = 0; while ((offset = buffer.getContents().indexOf(oldContent, offset)) != -1) { buffer.replace(offset, oldContent.length(), newContent); offset = offset + newContent.length(); }/* ww w . ja v a2 s. com*/ saveAndClose(unit); }
From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java
License:Open Source License
public static void modifyImport(ICompilationUnit compilationUnit, String oldImport, String newImport) throws JavaModelException { LOGGER.debug("Modifying import " + oldImport + " -> " + newImport); String oldImportStmt = "import " + oldImport; String newImportStmt = "import " + newImport; ICompilationUnit workingCopy = createWorkingCopy(compilationUnit); IBuffer buffer = ((IOpenable) workingCopy).getBuffer(); int offset = buffer.getContents().indexOf(oldImportStmt); buffer.replace(offset, oldImportStmt.length(), newImportStmt); saveAndClose(workingCopy);//from w w w. j av a 2s . co m }
From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java
License:Open Source License
public static IMethod removeMethod(ICompilationUnit compilationUnit, String methodName, boolean useWorkingCopy) throws JavaModelException { LOGGER.debug("Removing method " + methodName); ICompilationUnit unit = getCompilationUnit(compilationUnit, useWorkingCopy); for (IMethod method : unit.findPrimaryType().getMethods()) { if (method.getElementName().equals(methodName)) { ISourceRange sourceRange = method.getSourceRange(); IBuffer buffer = ((IOpenable) unit).getBuffer(); buffer.replace(sourceRange.getOffset(), sourceRange.getLength(), ""); saveAndClose(unit);// ww w . j a v a 2s . co m return method; } } Assert.fail("Method not found."); return null; }