Java Code Examples for org.eclipse.jdt.core.IBuffer#getContents()

The following examples show how to use org.eclipse.jdt.core.IBuffer#getContents() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JdtSourceLookUpProvider.java    From java-debug with Eclipse Public License 1.0 6 votes vote down vote up
private String getContents(IClassFile cf) {
    String source = null;
    if (cf != null) {
        try {
            IBuffer buffer = cf.getBuffer();
            if (buffer != null) {
                source = buffer.getContents();
            }
        } catch (JavaModelException e) {
            logger.log(Level.SEVERE, String.format("Failed to parse the source contents of the class file: %s", e.toString()), e);
        }
        if (source == null) {
            source = "";
        }
    }
    return source;
}
 
Example 2
Source File: SourceContentProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String getSource(IClassFile classFile, IProgressMonitor monitor) throws CoreException {
	String source = null;
	try {
		IBuffer buffer = classFile.getBuffer();
		if (buffer == null) {
			ProjectsManager projectsManager = JavaLanguageServerPlugin.getProjectsManager();
			if (projectsManager != null) {
				Optional<IBuildSupport> bs = projectsManager.getBuildSupport(classFile.getJavaProject().getProject());
				if (bs.isPresent()) {
					bs.get().discoverSource(classFile, monitor);
				}
			}
			buffer = classFile.getBuffer();
		}
		if (buffer != null) {
			source = buffer.getContents();
			JavaLanguageServerPlugin.logInfo("ClassFile contents request completed");
		}
	} catch (JavaModelException e) {
		JavaLanguageServerPlugin.logException("Exception getting java element ", e);
	}
	return source;
}
 
Example 3
Source File: CallLocation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void initCallTextAndLineNumber() {
	if (fCallText != null)
		return;

    IBuffer buffer= getBufferForMember();
    if (buffer == null || buffer.getLength() < fEnd) { //binary, without source attachment || buffer contents out of sync (bug 121900)
    	fCallText= ""; //$NON-NLS-1$
    	fLineNumber= UNKNOWN_LINE_NUMBER;
    	return;
    }

    fCallText= buffer.getText(fStart, (fEnd - fStart));

    if (fLineNumber == UNKNOWN_LINE_NUMBER) {
        Document document= new Document(buffer.getContents());
        try {
            fLineNumber= document.getLineOfOffset(fStart) + 1;
        } catch (BadLocationException e) {
            JavaPlugin.log(e);
        }
    }
}
 
Example 4
Source File: UpdateCopyrightFix.java    From saneclipse with Eclipse Public License 1.0 5 votes vote down vote up
public static ICleanUpFix createCleanUp(ICompilationUnit compilationUnit, boolean updateIbmCopyright) throws CoreException {
	if (!updateIbmCopyright)
		return null;
	
	IBuffer buffer= compilationUnit.getBuffer();
	String contents= buffer.getContents();
	
	for (SearchReplacePattern pattern : SEARCH_REPLACE_PATTERNS) {
		Matcher matcher= pattern.getSearchPattern().matcher(contents);
		if (matcher.find(0)) {
			int start= matcher.start();
			int end= matcher.end();
			String substring= contents.substring(start, end);
			
			matcher= pattern.getSearchPattern().matcher(substring);
			String replacement= matcher.replaceFirst(pattern.getReplaceString());
			if (substring.equals(replacement))
				return null;
			
			ReplaceEdit edit= new ReplaceEdit(start, substring.length(), replacement);
			
			CompilationUnitChange change= new CompilationUnitChange("Update Copyright", compilationUnit); //$NON-NLS-1$
			change.setEdit(edit);
			
			return new UpdateCopyrightFix(change);
		}
	}
	
	return null;
}
 
Example 5
Source File: DocumentAdapter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public DocumentAdapter(IBuffer buffer) {
	super(buffer.getContents());
	this.buffer = buffer;
}