Example usage for org.eclipse.jdt.core IBuffer replace

List of usage examples for org.eclipse.jdt.core IBuffer replace

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IBuffer replace.

Prototype

public void replace(int position, int length, String text);

Source Link

Document

Replaces the given range of characters in this buffer with the given text.

Usage

From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java

License:Open Source License

public static IMethod createMethod(IType javaType, String contents, boolean useWorkingCopy)
        throws JavaModelException {
    LOGGER.info("Adding method into type " + javaType.getElementName());
    ICompilationUnit unit = javaType.getCompilationUnit();
    if (useWorkingCopy) {
        unit = createWorkingCopy(unit);//from   www .java2s  .  c  om
    }
    ISourceRange sourceRange = javaType.getMethods()[0].getSourceRange();
    IBuffer buffer = ((IOpenable) unit).getBuffer();
    // insert before 1 method
    buffer.replace(sourceRange.getOffset(), 0, contents);
    saveAndClose(unit);
    // return the last method of the java type, assuming it is the one given
    // in parameter
    return javaType.getMethods()[0];
}

From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java

License:Open Source License

public static IField createField(IType type, String contents, boolean useWorkingCopy)
        throws JavaModelException {
    LOGGER.info("Adding type into type " + type.getElementName());
    ICompilationUnit unit = useWorkingCopy ? createWorkingCopy(type.getCompilationUnit())
            : type.getCompilationUnit();
    ISourceRange sourceRange = type.getFields()[0].getSourceRange();
    IBuffer buffer = ((IOpenable) unit).getBuffer();
    // insert before 1 method
    buffer.replace(sourceRange.getOffset(), 0, contents);
    saveAndClose(unit);/*from   www  .j ava2s .co m*/
    // return the last method of the java type, assuming it is the one given
    // in parameter
    return type.getFields()[0];
}

From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java

License:Open Source License

public static IField removeField(IField field, boolean useWorkingCopy) throws JavaModelException {
    LOGGER.info("Removing field " + field.getElementName());
    ICompilationUnit unit = useWorkingCopy ? createWorkingCopy(field.getCompilationUnit())
            : field.getCompilationUnit();
    ISourceRange sourceRange = field.getSourceRange();
    IBuffer buffer = ((IOpenable) unit).getBuffer();
    // remove/*from w  w w .ja  v a 2  s  .c o  m*/
    buffer.replace(sourceRange.getOffset(), sourceRange.getLength(), "");
    saveAndClose(unit);
    // return the last method of the java type, assuming it is the one given
    // in parameter
    return field;

}

From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java

License:Open Source License

public static IAnnotation addMethodAnnotation(IMethod method, String annotationStmt, boolean useWorkingCopy)
        throws JavaModelException {
    ICompilationUnit compilationUnit = method.getCompilationUnit();
    ICompilationUnit unit = getCompilationUnit(compilationUnit, useWorkingCopy);
    ISourceRange sourceRange = method.getSourceRange();
    IBuffer buffer = ((IOpenable) unit).getBuffer();
    buffer.replace(sourceRange.getOffset(), 0, annotationStmt + "\n");
    saveAndClose(unit);/*from   w  w w. jav a  2 s . c  o  m*/
    method = (IMethod) compilationUnit.getElementAt(method.getSourceRange().getOffset());
    String annotationName = StringUtils.substringBetween(annotationStmt, "@", "(");
    for (IAnnotation annotation : method.getAnnotations()) {
        if (annotation.getElementName().equals(annotationName)) {
            return annotation;
        }
    }
    Assert.fail("SimpleAnnotation '" + annotationName + "'not found on method " + method.getSource());
    return null;
}

From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java

License:Open Source License

public static void removeMethodAnnotation(IType javaType, String methodName, String annotationStmt)
        throws JavaModelException {
    LOGGER.info("Removing annotation " + annotationStmt + " on " + javaType.getElementName() + "." + methodName
            + "(...)");
    ICompilationUnit compilationUnit = javaType.getCompilationUnit();
    ICompilationUnit workingCopy = createWorkingCopy(compilationUnit);
    for (IMethod method : compilationUnit.findPrimaryType().getMethods()) {
        if (method.getElementName().equals(methodName)) {
            ISourceRange sourceRange = method.getSourceRange();
            IBuffer buffer = ((IOpenable) workingCopy).getBuffer();
            int index = buffer.getContents().indexOf(annotationStmt, sourceRange.getOffset());
            Assert.assertTrue("SimpleAnnotation not found", (index >= sourceRange.getOffset())
                    && (index <= sourceRange.getOffset() + sourceRange.getLength()));
            buffer.replace(index, annotationStmt.length(), "");
            saveAndClose(workingCopy);//from   w  w w  . j a va 2s . c  o  m
            return;
        }
    }
    Assert.fail("Method not found.");
}

From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java

License:Open Source License

public static void removeMethodAnnotation(IMethod method, String annotationStmt) throws JavaModelException {
    ICompilationUnit compilationUnit = method.getCompilationUnit();
    ICompilationUnit workingCopy = createWorkingCopy(compilationUnit);
    ISourceRange sourceRange = method.getSourceRange();
    IBuffer buffer = ((IOpenable) workingCopy).getBuffer();
    int index = buffer.getContents().indexOf(annotationStmt, sourceRange.getOffset());
    Assert.assertTrue("SimpleAnnotation not found: '" + annotationStmt + "'",
            (index >= sourceRange.getOffset()) && (index <= sourceRange.getOffset() + sourceRange.getLength()));
    buffer.replace(index, annotationStmt.length(), "");
    saveAndClose(workingCopy);/* ww  w .  j  a  v a2  s. co  m*/
}

From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java

License:Open Source License

public static IAnnotation removeMethodAnnotation(IMethod method, IAnnotation annotation, boolean useWorkingCopy)
        throws JavaModelException {
    ICompilationUnit compilationUnit = method.getCompilationUnit();
    ICompilationUnit unit = getCompilationUnit(compilationUnit, useWorkingCopy);
    ISourceRange sourceRange = annotation.getSourceRange();
    IBuffer buffer = ((IOpenable) unit).getBuffer();
    buffer.replace(sourceRange.getOffset(), sourceRange.getLength(), "");
    saveAndClose(unit);/*from   www  .j av  a2  s.c  o  m*/
    return annotation;
}

From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java

License:Open Source License

public static void removeFieldAnnotation(IField field, String annotationStmt, boolean useWorkingCopy)
        throws JavaModelException {
    ICompilationUnit compilationUnit = field.getCompilationUnit();
    ICompilationUnit unit = getCompilationUnit(compilationUnit, useWorkingCopy);
    ISourceRange sourceRange = field.getSourceRange();
    IBuffer buffer = ((IOpenable) unit).getBuffer();
    int index = buffer.getContents().indexOf(annotationStmt, sourceRange.getOffset());
    Assert.assertTrue("SimpleAnnotation not found: '" + annotationStmt + "'",
            (index >= sourceRange.getOffset()) && (index <= sourceRange.getOffset() + sourceRange.getLength()));
    buffer.replace(index, annotationStmt.length(), "");
    saveAndClose(unit);//  ww w  . j  ava 2  s. c o m
}

From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java

License:Open Source License

public static IMethod addMethodParameter(IMethod method, String parameter, boolean useWorkingCopy)
        throws JavaModelException {
    ICompilationUnit unit = getCompilationUnit(method.getCompilationUnit(), useWorkingCopy);
    ISourceRange sourceRange = method.getSourceRange();
    IBuffer buffer = ((IOpenable) unit).getBuffer();
    String[] parameterNames = method.getParameterNames();
    int offset = buffer.getContents().indexOf("public", sourceRange.getOffset());
    int index = buffer.getContents().indexOf("(", offset);
    if (parameterNames.length == 0) {
        buffer.replace(index + 1, 0, parameter);
    } else {//from  w ww .j a  v a 2 s  . c o m
        buffer.replace(index + 1, 0, parameter + ",");
    }
    saveAndClose(unit);
    return (IMethod) method.getCompilationUnit().getElementAt(sourceRange.getOffset());
}

From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java

License:Open Source License

public static void delete(IAnnotation annotation, boolean useWorkingCopy) throws CoreException {
    final IMember parent = (IMember) annotation.getParent();
    ICompilationUnit unit = getCompilationUnit(parent.getCompilationUnit(), useWorkingCopy);
    IBuffer buffer = ((IOpenable) unit).getBuffer();
    final ISourceRange sourceRange = annotation.getSourceRange();
    buffer.replace(sourceRange.getOffset(), sourceRange.getLength(), "");
    saveAndClose(unit);/*ww w  . j  a  v  a  2s. c om*/
}