use of org.eclipse.jdt.core.WorkingCopyOwner in project che by eclipse.
the class JavaModelManager method getPerWorkingCopyInfo.
/*
* Returns the per-working copy info for the given working copy at the given path.
* If it doesn't exist and if create, add a new per-working copy info with the given problem requestor.
* If recordUsage, increment the per-working copy info's use count.
* Returns null if it doesn't exist and not create.
*/
public PerWorkingCopyInfo getPerWorkingCopyInfo(CompilationUnit workingCopy, boolean create, boolean recordUsage, IProblemRequestor problemRequestor) {
synchronized (this.perWorkingCopyInfos) {
// use the perWorkingCopyInfo collection as its own lock
WorkingCopyOwner owner = workingCopy.owner;
Map workingCopyToInfos = (Map) this.perWorkingCopyInfos.get(owner);
if (workingCopyToInfos == null && create) {
workingCopyToInfos = new HashMap();
this.perWorkingCopyInfos.put(owner, workingCopyToInfos);
}
PerWorkingCopyInfo info = workingCopyToInfos == null ? null : (PerWorkingCopyInfo) workingCopyToInfos.get(workingCopy);
if (info == null && create) {
info = new PerWorkingCopyInfo(workingCopy, problemRequestor);
workingCopyToInfos.put(workingCopy, info);
}
if (info != null && recordUsage)
info.useCount++;
return info;
}
}
use of org.eclipse.jdt.core.WorkingCopyOwner in project che by eclipse.
the class TypeContextChecker method resolveSuperClass.
public static ITypeBinding resolveSuperClass(String superclass, IType typeHandle, StubTypeContext superClassContext) {
StringBuffer cuString = new StringBuffer();
cuString.append(superClassContext.getBeforeString());
cuString.append(superclass);
cuString.append(superClassContext.getAfterString());
try {
ICompilationUnit wc = typeHandle.getCompilationUnit().getWorkingCopy(new WorkingCopyOwner() {
}, new NullProgressMonitor());
try {
wc.getBuffer().setContents(cuString.toString());
CompilationUnit compilationUnit = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(wc, true);
ASTNode type = NodeFinder.perform(compilationUnit, superClassContext.getBeforeString().length(), superclass.length());
if (type instanceof Type) {
return handleBug84585(((Type) type).resolveBinding());
} else if (type instanceof Name) {
ASTNode parent = type.getParent();
if (parent instanceof Type)
return handleBug84585(((Type) parent).resolveBinding());
}
throw new IllegalStateException();
} finally {
wc.discardWorkingCopy();
}
} catch (JavaModelException e) {
return null;
}
}
use of org.eclipse.jdt.core.WorkingCopyOwner in project che by eclipse.
the class CheASTParser method internalCreateAST.
private ASTNode internalCreateAST(IProgressMonitor monitor) {
boolean needToResolveBindings = (this.bits & CompilationUnitResolver.RESOLVE_BINDING) != 0;
switch(this.astKind) {
case K_CLASS_BODY_DECLARATIONS:
case K_EXPRESSION:
case K_STATEMENTS:
if (this.rawSource == null) {
if (this.typeRoot != null) {
// get the source from the type root
if (this.typeRoot instanceof ICompilationUnit) {
org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) this.typeRoot;
this.rawSource = sourceUnit.getContents();
} else if (this.typeRoot instanceof IClassFile) {
try {
String sourceString = this.typeRoot.getSource();
if (sourceString != null) {
this.rawSource = sourceString.toCharArray();
}
} catch (JavaModelException e) {
// an error occured accessing the java element
StringWriter stringWriter = new StringWriter();
PrintWriter writer = null;
try {
writer = new PrintWriter(stringWriter);
e.printStackTrace(writer);
} finally {
if (writer != null)
writer.close();
}
throw new IllegalStateException(String.valueOf(stringWriter.getBuffer()));
}
}
}
}
if (this.rawSource != null) {
if (this.sourceOffset + this.sourceLength > this.rawSource.length) {
throw new IllegalStateException();
}
return internalCreateASTForKind();
}
break;
case K_COMPILATION_UNIT:
CompilationUnitDeclaration compilationUnitDeclaration = null;
try {
NodeSearcher searcher = null;
org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = null;
WorkingCopyOwner wcOwner = this.workingCopyOwner;
if (this.typeRoot instanceof ICompilationUnit) {
/*
* this.compilationUnitSource is an instance of org.eclipse.jdt.internal.core.CompilationUnit that implements
* both org.eclipse.jdt.core.ICompilationUnit and org.eclipse.jdt.internal.compiler.env.ICompilationUnit
*/
sourceUnit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) this.typeRoot;
/*
* use a BasicCompilation that caches the source instead of using the compilationUnitSource directly
* (if it is a working copy, the source can change between the parse and the AST convertion)
* (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=75632)
*/
sourceUnit = new BasicCompilationUnit(sourceUnit.getContents(), sourceUnit.getPackageName(), new String(sourceUnit.getFileName()), this.project);
wcOwner = ((ICompilationUnit) this.typeRoot).getOwner();
} else if (this.typeRoot instanceof IClassFile) {
try {
String sourceString = this.typeRoot.getSource();
if (sourceString == null) {
throw new IllegalStateException();
}
PackageFragment packageFragment = (PackageFragment) this.typeRoot.getParent();
BinaryType type = (BinaryType) this.typeRoot.findPrimaryType();
IBinaryType binaryType = (IBinaryType) type.getElementInfo();
// file name is used to recreate the Java element, so it has to be the toplevel .class file name
char[] fileName = binaryType.getFileName();
int firstDollar = CharOperation.indexOf('$', fileName);
if (firstDollar != -1) {
char[] suffix = SuffixConstants.SUFFIX_class;
int suffixLength = suffix.length;
char[] newFileName = new char[firstDollar + suffixLength];
System.arraycopy(fileName, 0, newFileName, 0, firstDollar);
System.arraycopy(suffix, 0, newFileName, firstDollar, suffixLength);
fileName = newFileName;
}
sourceUnit = new BasicCompilationUnit(sourceString.toCharArray(), Util.toCharArrays(packageFragment.names), new String(fileName), this.project);
} catch (JavaModelException e) {
// an error occured accessing the java element
StringWriter stringWriter = new StringWriter();
PrintWriter writer = null;
try {
writer = new PrintWriter(stringWriter);
e.printStackTrace(writer);
} finally {
if (writer != null)
writer.close();
}
throw new IllegalStateException(String.valueOf(stringWriter.getBuffer()));
}
} else if (this.rawSource != null) {
needToResolveBindings = ((this.bits & CompilationUnitResolver.RESOLVE_BINDING) != 0) && this.unitName != null && (this.project != null || this.classpaths != null || this.sourcepaths != null || ((this.bits & CompilationUnitResolver.INCLUDE_RUNNING_VM_BOOTCLASSPATH) != 0)) && this.compilerOptions != null;
//$NON-NLS-1$
sourceUnit = new BasicCompilationUnit(this.rawSource, null, this.unitName == null ? "" : this.unitName, this.project);
} else {
throw new IllegalStateException();
}
if ((this.bits & CompilationUnitResolver.PARTIAL) != 0) {
searcher = new NodeSearcher(this.focalPointPosition);
}
int flags = 0;
if ((this.bits & CompilationUnitResolver.STATEMENT_RECOVERY) != 0) {
flags |= ICompilationUnit.ENABLE_STATEMENTS_RECOVERY;
}
if (searcher == null && ((this.bits & CompilationUnitResolver.IGNORE_METHOD_BODIES) != 0)) {
flags |= ICompilationUnit.IGNORE_METHOD_BODIES;
}
if (needToResolveBindings) {
if ((this.bits & CompilationUnitResolver.BINDING_RECOVERY) != 0) {
flags |= ICompilationUnit.ENABLE_BINDINGS_RECOVERY;
}
try {
// parse and resolve
compilationUnitDeclaration = CheCompilationUnitResolver.resolve(sourceUnit, this.project, getClasspath(), searcher, this.compilerOptions, this.workingCopyOwner, flags, monitor);
} catch (JavaModelException e) {
flags &= ~ICompilationUnit.ENABLE_BINDINGS_RECOVERY;
compilationUnitDeclaration = CompilationUnitResolver.parse(sourceUnit, searcher, this.compilerOptions, flags);
needToResolveBindings = false;
}
} else {
compilationUnitDeclaration = CompilationUnitResolver.parse(sourceUnit, searcher, this.compilerOptions, flags);
needToResolveBindings = false;
}
CompilationUnit result = CompilationUnitResolver.convert(compilationUnitDeclaration, sourceUnit.getContents(), this.apiLevel, this.compilerOptions, needToResolveBindings, wcOwner, needToResolveBindings ? new DefaultBindingResolver.BindingTables() : null, flags, monitor, this.project != null);
result.setTypeRoot(this.typeRoot);
return result;
} finally {
if (compilationUnitDeclaration != null && ((this.bits & CompilationUnitResolver.RESOLVE_BINDING) != 0)) {
compilationUnitDeclaration.cleanUp();
}
}
}
throw new IllegalStateException();
}
use of org.eclipse.jdt.core.WorkingCopyOwner in project che by eclipse.
the class JavaReconciler method reconcile.
public ReconcileResult reconcile(IJavaProject javaProject, String fqn) throws JavaModelException {
final ProblemRequestor requestor = new ProblemRequestor();
WorkingCopyOwner wcOwner = new WorkingCopyOwner() {
public IProblemRequestor getProblemRequestor(ICompilationUnit unit) {
return requestor;
}
@Override
public IBuffer createBuffer(ICompilationUnit workingCopy) {
// ?????
return new org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter(workingCopy, (IFile) workingCopy.getResource());
}
};
List<HighlightedPosition> positions = null;
ICompilationUnit compilationUnit = null;
try {
IType type = javaProject.findType(fqn);
if (type == null) {
return null;
}
if (type.isBinary()) {
throw new IllegalArgumentException("Can't reconcile binary type: " + fqn);
} else {
compilationUnit = type.getCompilationUnit().getWorkingCopy(wcOwner, null);
}
requestor.reset();
CompilationUnit unit = compilationUnit.reconcile(AST.JLS8, true, wcOwner, null);
positions = semanticHighlighting.reconcileSemanticHighlight(unit);
if (compilationUnit instanceof ClassFileWorkingCopy) {
//we don't wont to show any errors from ".class" files
requestor.reset();
}
} catch (JavaModelException e) {
LOG.error("Can't reconcile class: " + fqn + " in project:" + javaProject.getPath().toOSString(), e);
throw e;
} finally {
if (compilationUnit != null && compilationUnit.isWorkingCopy()) {
try {
//todo close buffer
compilationUnit.getBuffer().close();
compilationUnit.discardWorkingCopy();
} catch (JavaModelException e) {
//ignore
}
}
}
ReconcileResult result = DtoFactory.getInstance().createDto(ReconcileResult.class);
result.setProblems(convertProblems(requestor.problems));
result.setHighlightedPositions(positions);
return result;
}
use of org.eclipse.jdt.core.WorkingCopyOwner in project che by eclipse.
the class CodeAssist method computeProposals.
public Proposals computeProposals(IJavaProject project, String fqn, int offset, final String content) throws JavaModelException {
WorkingCopyOwner copyOwner = new WorkingCopyOwner() {
@Override
public IBuffer createBuffer(ICompilationUnit workingCopy) {
return new org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter(workingCopy, workingCopy.getPath(), content);
}
};
ICompilationUnit compilationUnit;
IType type = project.findType(fqn);
if (type == null) {
return null;
}
if (type.isBinary()) {
compilationUnit = type.getClassFile().getWorkingCopy(copyOwner, null);
} else {
compilationUnit = type.getCompilationUnit().getWorkingCopy(copyOwner, null);
}
IBuffer buffer = compilationUnit.getBuffer();
IDocument document;
if (buffer instanceof org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) {
document = ((org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) buffer).getDocument();
} else {
document = new DocumentAdapter(buffer);
}
TextViewer viewer = new TextViewer(document, new Point(offset, 0));
JavaContentAssistInvocationContext context = new JavaContentAssistInvocationContext(viewer, offset, compilationUnit);
List<ICompletionProposal> proposals = new ArrayList<>();
proposals.addAll(new JavaAllCompletionProposalComputer().computeCompletionProposals(context, null));
proposals.addAll(new TemplateCompletionProposalComputer().computeCompletionProposals(context, null));
Collections.sort(proposals, new RelevanceSorter());
return convertProposals(offset, compilationUnit, viewer, proposals);
}
Aggregations