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.batch.ui.internal.wizard.NewBatchArtifactWizardPage.java
License:Open Source License
@Override protected void createTypeMembers(IType newType, final ImportsManager imports, IProgressMonitor monitor) throws CoreException { createInheritedMethods(newType, true, true, imports, new SubProgressMonitor(monitor, 1)); ISourceRange range = newType.getSourceRange(); IBuffer buf = newType.getCompilationUnit().getBuffer(); String lineDelimiter = StubUtility.getLineDelimiterUsed(newType.getJavaProject()); StringBuffer sb = new StringBuffer(); addAnnotations(imports, sb, lineDelimiter); buf.replace(range.getOffset(), 0, sb.toString()); createFields(newType, imports, monitor, lineDelimiter); }
From source file:org.jboss.tools.batch.ui.internal.wizard.NewBatchArtifactWizardPage.java
License:Open Source License
void editField(ICompilationUnit cu, IField m, String javatype, String fieldHeader, String lineDelimiter) throws CoreException { synchronized (cu) { cu.reconcile(ICompilationUnit.NO_AST, true, null, null); }//from ww w . ja v a2s . c o m ISourceRange range = m.getSourceRange(); IBuffer buf = cu.getBuffer(); StringBuffer sb = new StringBuffer(lineDelimiter); if (isAddComments()) { String fieldComment = CodeGeneration.getFieldComment(cu, javatype, m.getElementName(), lineDelimiter); sb.append(fieldComment).append(lineDelimiter); } sb.append(fieldHeader); String formattedContent = codeFormat2(CodeFormatter.K_CLASS_BODY_DECLARATIONS, sb.toString(), 1, lineDelimiter, cu.getJavaProject()); if (formattedContent != null && formattedContent.startsWith("\t")) { //$NON-NLS-1$ formattedContent = formattedContent.substring(1); } buf.replace(range.getOffset(), range.getLength(), formattedContent); }
From source file:org.jboss.tools.cdi.internal.core.refactoring.CDIMarkerResolutionUtils.java
License:Open Source License
public static void addQualifier(String qualifiedName, String value, ICompilationUnit compilationUnit, IJavaElement element, MultiTextEdit rootEdit) throws JavaModelException { if (!(element instanceof ISourceReference)) return;/* w ww . ja v a 2s . c o m*/ IAnnotation annotation = findAnnotation(element, qualifiedName); if (annotation != null && annotation.exists()) return; boolean duplicateShortName = addImport(qualifiedName, compilationUnit, rootEdit); String lineDelim = SPACE; IBuffer buffer = compilationUnit.getBuffer(); String shortName = getShortName(qualifiedName); if (!value.isEmpty()) value = "(" + value + ")"; if (duplicateShortName) shortName = qualifiedName; annotation = findAnnotation(element, CDIConstants.INJECT_ANNOTATION_TYPE_NAME); if (rootEdit != null) { if (annotation != null && annotation.exists()) { TextEdit edit = new InsertEdit( annotation.getSourceRange().getOffset() + annotation.getSourceRange().getLength(), lineDelim + AT + shortName + value); rootEdit.addChild(edit); } else { TextEdit edit = new InsertEdit(((ISourceReference) element).getSourceRange().getOffset(), AT + shortName + value + lineDelim); rootEdit.addChild(edit); } } else { if (annotation != null && annotation.exists()) { buffer.replace(annotation.getSourceRange().getOffset() + annotation.getSourceRange().getLength(), 0, lineDelim + AT + shortName + value); } else { buffer.replace(((ISourceReference) element).getSourceRange().getOffset(), 0, AT + shortName + value + lineDelim); } synchronized (compilationUnit) { compilationUnit.reconcile(ICompilationUnit.NO_AST, true, null, null); } } }
From source file:org.jboss.tools.cdi.internal.core.refactoring.CDIMarkerResolutionUtils.java
License:Open Source License
public static void updateQualifier(String qualifiedName, String value, ICompilationUnit compilationUnit, IJavaElement element, MultiTextEdit rootEdit) throws JavaModelException { if (!(element instanceof ISourceReference)) return;//from w w w . ja v a 2 s . c om IAnnotation annotation = findAnnotation(element, qualifiedName); if (annotation == null || !annotation.exists()) return; boolean duplicateShortName = addImport(qualifiedName, compilationUnit, rootEdit); IBuffer buffer = compilationUnit.getBuffer(); String shortName = getShortName(qualifiedName); if (!value.isEmpty()) value = "(" + value + ")"; if (duplicateShortName) shortName = qualifiedName; String newValue = AT + shortName + value; if (!annotation.getSource().equals(newValue)) { if (rootEdit != null) { TextEdit edit = new ReplaceEdit(annotation.getSourceRange().getOffset(), annotation.getSourceRange().getLength(), newValue); rootEdit.addChild(edit); } else { buffer.replace(annotation.getSourceRange().getOffset(), annotation.getSourceRange().getLength(), newValue); synchronized (compilationUnit) { compilationUnit.reconcile(ICompilationUnit.NO_AST, true, null, null); } } } }
From source file:org.jboss.tools.cdi.ui.marker.MakeMethodBusinessMarkerResolution.java
License:Open Source License
private void internal_run() { try {//from w ww. j a v a 2 s. co m ICompilationUnit original = EclipseUtil.getCompilationUnit(file); if (original == null) { return; } ICompilationUnit compilationUnit = original.getWorkingCopy(new NullProgressMonitor()); IBuffer buffer = compilationUnit.getBuffer(); int flag = method.getFlags(); String text = buffer.getText(method.getSourceRange().getOffset(), method.getSourceRange().getLength()); // make method public int position = method.getSourceRange().getOffset(); if (!Flags.isPublic(flag)) { if (Flags.isPrivate(flag)) { position += text.indexOf(PRIVATE); buffer.replace(position, PRIVATE.length(), PUBLIC); } else if (Flags.isProtected(flag)) { position += text.indexOf(PROTECTED); buffer.replace(position, PROTECTED.length(), PUBLIC); } else { String type = Signature.getSignatureSimpleName(method.getReturnType()); position += text.indexOf(type); buffer.replace(position, 0, PUBLIC + SPACE); } } compilationUnit.commitWorkingCopy(false, new NullProgressMonitor()); compilationUnit.discardWorkingCopy(); // add method to interface original = localInterface.getCompilationUnit(); compilationUnit = original.getWorkingCopy(new NullProgressMonitor()); IType interfaceType = compilationUnit.getType(localInterface.getElementName()); StringBuffer content = new StringBuffer(); content.append(PUBLIC + SPACE); String simpleType = Signature.getSignatureSimpleName(method.getReturnType()); content.append(simpleType); content.append(SPACE); content.append(method.getElementName()); content.append("("); //$NON-NLS-1$ IType originalType = method.getDeclaringType(); addImport(originalType, simpleType, compilationUnit); String[] types = method.getParameterTypes(); String[] names = method.getParameterNames(); for (int i = 0; i < method.getNumberOfParameters(); i++) { if (i > 0) content.append(", "); //$NON-NLS-1$ simpleType = Signature.getSignatureSimpleName(types[i]); addImport(originalType, simpleType, compilationUnit); content.append(simpleType); content.append(SPACE); content.append(names[i]); } content.append(");"); //$NON-NLS-1$ interfaceType.createMethod(content.toString(), null, false, new NullProgressMonitor()); compilationUnit.commitWorkingCopy(false, new NullProgressMonitor()); compilationUnit.discardWorkingCopy(); } catch (CoreException ex) { CDIUIPlugin.getDefault().logError(ex); } }
From source file:org.jboss.tools.cdi.ui.wizard.NewAnnotationLiteralWizardPage.java
License:Open Source License
protected void createTypeMembers(IType newType, final ImportsManager imports, IProgressMonitor monitor) throws CoreException { createInheritedMethods(newType, true, true, imports, new SubProgressMonitor(monitor, 1)); ISourceRange range = newType.getSourceRange(); IBuffer buf = newType.getCompilationUnit().getBuffer(); String lineDelimiter = StubUtility.getLineDelimiterUsed(newType.getJavaProject()); StringBuffer sb = new StringBuffer(); buf.replace(range.getOffset(), 0, sb.toString()); boolean isDefault = modifyMethodContent(newType, imports, monitor, lineDelimiter); if (isDefault) { createInstanceField(newType, imports, monitor, lineDelimiter); }/*from w w w .j a v a 2 s . com*/ }
From source file:org.jboss.tools.cdi.ui.wizard.NewAnnotationLiteralWizardPage.java
License:Open Source License
void editField(ICompilationUnit cu, IField m, String javatype, String fieldHeader, String lineDelimiter) throws CoreException { synchronized (cu) { cu.reconcile(ICompilationUnit.NO_AST, true, null, null); }/*from w ww. j a v a 2 s .c o m*/ ISourceRange range = m.getSourceRange(); IBuffer buf = cu.getBuffer(); StringBuffer sb = new StringBuffer(lineDelimiter); if (isAddComments()) { String fieldComment = CodeGeneration.getFieldComment(cu, javatype, m.getElementName(), lineDelimiter); sb.append(fieldComment).append(lineDelimiter); } sb.append(fieldHeader); String formattedContent = JavaBeanGenerator.codeFormat2(CodeFormatter.K_CLASS_BODY_DECLARATIONS, sb.toString(), 1, lineDelimiter, cu.getJavaProject()); if (formattedContent != null && formattedContent.startsWith("\t")) { //$NON-NLS-1$ formattedContent = formattedContent.substring(1); } buf.replace(range.getOffset(), range.getLength(), formattedContent); }
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 w ww .ja v a 2 s.com*/ 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.NewBeanWizardPage.java
License:Open Source License
protected void createTypeMembers(IType newType, final ImportsManager imports, IProgressMonitor monitor) throws CoreException { createInheritedMethods(newType, true, true, imports, new SubProgressMonitor(monitor, 1)); ISourceRange range = newType.getSourceRange(); IBuffer buf = newType.getCompilationUnit().getBuffer(); String lineDelimiter = StubUtility.getLineDelimiterUsed(newType.getJavaProject()); StringBuffer sb = new StringBuffer(); addAnnotations(imports, sb, lineDelimiter); buf.replace(range.getOffset(), 0, sb.toString()); }
From source file:org.jboss.tools.cdi.ui.wizard.NewCDIAnnotationWizardPage.java
License:Open Source License
protected void createTypeMembers(IType newType, final ImportsManager imports, IProgressMonitor monitor) throws CoreException { ISourceRange range = newType.getSourceRange(); IBuffer buf = newType.getCompilationUnit().getBuffer(); String lineDelimiter = StubUtility.getLineDelimiterUsed(newType.getJavaProject()); StringBuffer sb = new StringBuffer(); addAnnotations(imports, sb, lineDelimiter); buf.replace(range.getOffset(), 0, sb.toString()); }