List of usage examples for org.eclipse.jdt.core IBuffer getContents
public String getContents();
String
. From source file:org.eclipse.wb.internal.rcp.wizards.rcp.editor.MultiPageEditorPartWizardPage.java
License:Open Source License
private void addPageInvocations(IType newType, ImportsManager imports) throws JavaModelException { IBuffer buffer = newType.getCompilationUnit().getBuffer(); // prepare addPage() invocations String addPagesSource = ""; for (IType page : getSelectedPages()) { String simpleName = imports.addImport(page.getFullyQualifiedName()); addPagesSource += "addPage(new " + simpleName + "(), (org.eclipse.ui.IEditorInput) null);"; }/* w w w. j a v a 2s.c om*/ // insert addPage() invocations, don't care about formatting String pattern = "int pages;"; int pagesOffset = buffer.getContents().indexOf(pattern); buffer.replace(pagesOffset, pattern.length(), addPagesSource); // if no pages, remove "try" block if (addPagesSource.length() == 0) { String contents = buffer.getContents(); int tryBegin = contents.indexOf("try {"); int tryEnd = contents.indexOf("}", tryBegin + 1); tryEnd = contents.indexOf("}", tryEnd + 1); buffer.replace(tryBegin, tryEnd - tryBegin + 1, ""); } }
From source file:org.eclipse.wb.tests.designer.editor.SplitModeTest.java
License:Open Source License
public void test_reparse_afterDelay() throws Exception { IPreferenceStore preferences = DesignerPlugin.getPreferences(); preferences.setValue(IPreferenceConstants.P_EDITOR_LAYOUT, IPreferenceConstants.V_EDITOR_LAYOUT_SPLIT_VERTICAL_DESIGN); preferences.setValue(IPreferenceConstants.P_EDITOR_LAYOUT_SYNC_DELAY, 100); openContainer("// filler filler filler", "public class Test extends JPanel {", " public Test() {", " } // marker", "}"); openSourcePage();/*from w w w . jav a 2 s. c o m*/ // initially no setEnabled(false) invocation check_isEnabled(true); // set focus to Source, as if user does this { MultiMode multiMode = (MultiMode) m_designerEditor.getMultiMode(); multiMode.getSourcePage().setFocus(); } // insert setEnabled(false) into buffer { IBuffer buffer = m_lastEditor.getModelUnit().getBuffer(); int position = buffer.getContents().indexOf("} // marker"); buffer.replace(position, 0, "setEnabled(false);"); } // still not re-parsed check_isEnabled(true); // wait for re-parse waitEventLoop(1000); fetchContentFields(); check_isEnabled(false); }
From source file:org.eclipse.wb.tests.designer.editor.SplitModeTest.java
License:Open Source License
public void test_reparse_afterSave() throws Exception { IPreferenceStore preferences = DesignerPlugin.getPreferences(); preferences.setValue(IPreferenceConstants.P_EDITOR_LAYOUT, IPreferenceConstants.V_EDITOR_LAYOUT_SPLIT_VERTICAL_DESIGN); preferences.setValue(IPreferenceConstants.P_EDITOR_LAYOUT_SYNC_DELAY, -1); openContainer("// filler filler filler", "public class Test extends JPanel {", " public Test() {", " } // marker", "}"); openSourcePage();/* w w w.j a v a 2 s. c o m*/ // initially no setEnabled(false) invocation check_isEnabled(true); // insert setEnabled(false) into buffer { IBuffer buffer = m_lastEditor.getModelUnit().getBuffer(); int position = buffer.getContents().indexOf("} // marker"); buffer.replace(position, 0, "setEnabled(false);"); } // still not re-parsed check_isEnabled(true); // wait, but still not re-parsed waitEventLoop(300); check_isEnabled(true); // do save m_designerEditor.doSave(null); check_isEnabled(false); }
From source file:org.grails.ide.eclipse.test.GrailsSourceCodeTest.java
License:Open Source License
private void doTestType(GrailsVersion version, String typeName, String expectedSnippet) throws Exception { ensureDefaultGrailsVersion(version); project = ensureProject(emptyProjectName(version)); IJavaProject javaProject = JavaCore.create(project); IType type = javaProject.findType(typeName); assertNotNull("Type not found on classpath: " + typeName, type); IClassFile classFile = type.getClassFile(); assertNotNull("Couldn't obtain .class file for type: " + type, classFile); IBuffer sourceCode = classFile.getBuffer(); assertNotNull("Couldn't obtain buffer (sourceCode) for .class file: " + classFile, sourceCode); try {//from ww w . j ava 2s .c o m System.out.println(sourceCode.getContents()); assertContains(expectedSnippet, sourceCode.getContents()); } finally { sourceCode.close(); } }
From source file:org.gw4e.eclipse.fwk.source.SourceHelper.java
License:Open Source License
public static void updatePathGenerator(IFile ifile, String oldPathGenerator, String newPathGenerator) throws CoreException { ICompilationUnit cu = JavaCore.createCompilationUnitFrom(ifile); ICompilationUnit workingCopy = cu.getWorkingCopy(new NullProgressMonitor()); IBuffer buffer = ((IOpenable) workingCopy).getBuffer(); String source = buffer.getContents(); int start = source.indexOf(oldPathGenerator); buffer.replace(start, oldPathGenerator.length(), newPathGenerator); workingCopy.reconcile(ICompilationUnit.NO_AST, false, workingCopy.getOwner(), new NullProgressMonitor()); workingCopy.commitWorkingCopy(true, null); workingCopy.discardWorkingCopy();/* www. j av a 2s .co m*/ ifile.touch(new NullProgressMonitor()); }
From source file:org.jboss.tools.cdi.ui.wizard.NewAnnotationLiteralWizardPage.java
License:Open Source License
protected boolean modifyMethodContent(IType type, ImportsManager imports, IProgressMonitor monitor, String lineDelimiter) throws CoreException { IMethod[] ms = type.getMethods();//from ww w. ja va 2s .c o m IMethod sibling = null; List<String[]> fields = new ArrayList<String[]>(); IMethod constructor = null; for (int i = 0; i < ms.length; i++) { if (ms[i].isConstructor()) { constructor = ms[i]; continue; } if (sibling == null) { sibling = ms[i]; } ICompilationUnit cu = type.getCompilationUnit(); synchronized (cu) { cu.reconcile(ICompilationUnit.NO_AST, true, null, null); } IBuffer buf = cu.getBuffer(); ISourceRange range = ms[i].getSourceRange(); int start = -1; int end = -1; StringBuffer sb = new StringBuffer(); if ("void".equals(ms[i].getReturnType()) || "V".equals(ms[i].getReturnType())) { end = buf.getContents().indexOf("}", range.getOffset()); if (end < 0) continue; end = buf.getContents().lastIndexOf(lineDelimiter, end); if (end < 0 || end < range.getOffset()) continue; // end += lineDelimiter.length(); start = end; } else { start = buf.getContents().indexOf("return", range.getOffset()); if (start < 0 || start > range.getOffset() + range.getLength()) continue; start += 7; end = buf.getContents().indexOf(";", start); if (end < 0) continue; end++; } String methodName = ms[i].getElementName(); String fieldName = "" + methodName; sb.append(fieldName).append(";"); buf.replace(start, end - start, sb.toString()); fields.add(new String[] { ms[i].getReturnType(), fieldName }); } for (int i = 0; i < fields.size(); i++) { String[] data = fields.get(i); String fieldType = data[0]; String fieldName = data[1]; IParametedType t = NewCDIAnnotationWizardPage.getCDIProject(getPackageFragmentRoot().getJavaProject()) .getNature().getTypeFactory().getParametedType(type, fieldType); if (t != null) { data[0] = t.getSimpleName(); createField(type, constructor, imports, fieldName, t.getSimpleName(), "private final", null, monitor, lineDelimiter); } } if (constructor != null) { constructor.delete(true, monitor); } if (!fields.isEmpty()) { String constructorContents = "public " + type.getElementName() + "("; String comments = "/**" + lineDelimiter; for (int i = 0; i < fields.size(); i++) { String[] data = fields.get(i); String fieldType = data[0]; String fieldName = data[1]; comments += " * @param " + fieldName + lineDelimiter; if (i > 0) constructorContents += ", "; constructorContents += fieldType + " " + fieldName; } comments += "*/" + lineDelimiter; constructorContents += ") {" + lineDelimiter; for (int i = 0; i < fields.size(); i++) { String[] data = fields.get(i); String fieldName = data[1]; constructorContents += "this." + fieldName + " = " + fieldName + ";" + lineDelimiter; } constructorContents += "}" + lineDelimiter; if (isAddComments()) { constructorContents = comments + constructorContents; } IMethod m = type.createMethod(constructorContents, sibling, true, monitor); } return fields.isEmpty(); }
From source file:org.jboss.tools.cdi.ui.wizard.NewDecoratorWizardPage.java
License:Open Source License
protected void modifyMethodContent(IType type, ImportsManager imports, IProgressMonitor monitor, String lineDelimiter) throws CoreException { IMethod[] ms = type.getMethods();/* w w w . j av a2 s. c om*/ for (int i = 0; i < ms.length; i++) { if (ms[i].isConstructor()) continue; ICompilationUnit cu = type.getCompilationUnit(); synchronized (cu) { cu.reconcile(ICompilationUnit.NO_AST, true, null, null); } IBuffer buf = cu.getBuffer(); ISourceRange range = ms[i].getSourceRange(); int start = -1; int end = -1; StringBuffer sb = new StringBuffer(); if ("void".equals(ms[i].getReturnType()) || "V".equals(ms[i].getReturnType())) { end = buf.getContents().indexOf("}", range.getOffset()); if (end < 0) continue; end = buf.getContents().lastIndexOf(lineDelimiter, end); if (end < 0 || end < range.getOffset()) continue; // end += lineDelimiter.length(); start = end; } else { start = buf.getContents().indexOf("return", range.getOffset()); if (start < 0 || start > range.getOffset() + range.getLength()) continue; start += 7; end = buf.getContents().indexOf(";", start); if (end < 0) continue; end++; } String fieldName = "" + this.fieldName.getValue(); String methodName = ms[i].getElementName(); String[] ps = ms[i].getParameterNames(); sb.append(fieldName).append('.').append(methodName).append('('); for (int k = 0; k < ps.length; k++) { if (k > 0) sb.append(", "); sb.append(ps[k]); } sb.append(");"); buf.replace(start, end - start, sb.toString()); } }
From source file:org.jboss.tools.esb.ui.wizard.NewActionWizardPage.java
License:Open Source License
void modifyJavaSourceForAbstractImplementation(IType type, ImportsManager imports) { try {/*from w w w.j a va2s . c o 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.vscode.java.internal.handlers.ClassfileContentHandler.java
License:Open Source License
@Override public String handle(TextDocumentIdentifier param) { try {// w w w. j a va2s .com IClassFile cf = JDTUtils.resolveClassFile(param.getUri()); if (cf != null) { IBuffer buffer = cf.getBuffer(); if (buffer != null) { JavaLanguageServerPlugin.logInfo("ClassFile contents request completed"); return buffer.getContents(); } } } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Exception getting java element ", e); } return null; }
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 w ww. j av a2 s. c om * @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); }