use of org.eclipse.jdt.core.ITypeRoot in project che by eclipse.
the class StubUtility method getLineDelimiterUsed.
/**
* @param elem a Java element (doesn't have to exist)
* @return the existing or default line delimiter for the element
*/
public static String getLineDelimiterUsed(IJavaElement elem) {
IOpenable openable = elem.getOpenable();
if (openable instanceof ITypeRoot) {
try {
return openable.findRecommendedLineSeparator();
} catch (JavaModelException exception) {
// Use project setting
}
}
IJavaProject project = elem.getJavaProject();
return getProjectLineDelimiter(project.exists() ? project : null);
}
use of org.eclipse.jdt.core.ITypeRoot in project che by eclipse.
the class ContextSensitiveImportRewriteContext method findInContext.
@Override
public int findInContext(String qualifier, String name, int kind) {
IBinding[] declarationsInScope = getDeclarationsInScope();
for (int i = 0; i < declarationsInScope.length; i++) {
if (declarationsInScope[i] instanceof ITypeBinding) {
ITypeBinding typeBinding = (ITypeBinding) declarationsInScope[i];
if (isSameType(typeBinding, qualifier, name)) {
return RES_NAME_FOUND;
} else if (isConflicting(typeBinding, name)) {
return RES_NAME_CONFLICT;
}
} else if (declarationsInScope[i] != null) {
if (isConflicting(declarationsInScope[i], name)) {
return RES_NAME_CONFLICT;
}
}
}
Name[] names = getImportedNames();
for (int i = 0; i < names.length; i++) {
IBinding binding = names[i].resolveBinding();
if (binding instanceof ITypeBinding && !binding.isRecovered()) {
ITypeBinding typeBinding = (ITypeBinding) binding;
if (isConflictingType(typeBinding, qualifier, name)) {
return RES_NAME_CONFLICT;
}
}
}
List<AbstractTypeDeclaration> list = fCompilationUnit.types();
for (Iterator<AbstractTypeDeclaration> iter = list.iterator(); iter.hasNext(); ) {
AbstractTypeDeclaration type = iter.next();
ITypeBinding binding = type.resolveBinding();
if (binding != null) {
if (isSameType(binding, qualifier, name)) {
return RES_NAME_FOUND;
} else {
ITypeBinding decl = containingDeclaration(binding, qualifier, name);
while (decl != null && !decl.equals(binding)) {
int modifiers = decl.getModifiers();
if (Modifier.isPrivate(modifiers))
return RES_NAME_CONFLICT;
decl = decl.getDeclaringClass();
}
}
}
}
String[] addedImports = fImportRewrite.getAddedImports();
String qualifiedName = JavaModelUtil.concatenateName(qualifier, name);
for (int i = 0; i < addedImports.length; i++) {
String addedImport = addedImports[i];
if (qualifiedName.equals(addedImport)) {
return RES_NAME_FOUND;
} else {
if (isConflicting(name, addedImport))
return RES_NAME_CONFLICT;
}
}
if (qualifier.equals("java.lang")) {
//$NON-NLS-1$
//No explicit import statement required
ITypeRoot typeRoot = fCompilationUnit.getTypeRoot();
if (typeRoot != null) {
IPackageFragment packageFragment = (IPackageFragment) typeRoot.getParent();
try {
ICompilationUnit[] compilationUnits = packageFragment.getCompilationUnits();
for (int i = 0; i < compilationUnits.length; i++) {
ICompilationUnit cu = compilationUnits[i];
IType[] allTypes = cu.getAllTypes();
for (int j = 0; j < allTypes.length; j++) {
IType type = allTypes[j];
String packageTypeName = type.getFullyQualifiedName();
if (isConflicting(name, packageTypeName))
return RES_NAME_CONFLICT;
}
}
} catch (JavaModelException e) {
}
}
}
return fImportRewrite.getDefaultImportRewriteContext().findInContext(qualifier, name, kind);
}
use of org.eclipse.jdt.core.ITypeRoot in project che by eclipse.
the class ASTNodes method getNodeSource.
/**
* Returns the source of the given node from the location where it was parsed.
* @param node the node to get the source from
* @param extendedRange if set, the extended ranges of the nodes should ne used
* @param removeIndent if set, the indentation is removed.
* @return return the source for the given node or null if accessing the source failed.
*/
public static String getNodeSource(ASTNode node, boolean extendedRange, boolean removeIndent) {
ASTNode root = node.getRoot();
if (root instanceof CompilationUnit) {
CompilationUnit astRoot = (CompilationUnit) root;
ITypeRoot typeRoot = astRoot.getTypeRoot();
try {
if (typeRoot != null && typeRoot.getBuffer() != null) {
IBuffer buffer = typeRoot.getBuffer();
int offset = extendedRange ? astRoot.getExtendedStartPosition(node) : node.getStartPosition();
int length = extendedRange ? astRoot.getExtendedLength(node) : node.getLength();
String str = buffer.getText(offset, length);
if (removeIndent) {
IJavaProject project = typeRoot.getJavaProject();
int indent = StubUtility.getIndentUsed(buffer, node.getStartPosition(), project);
str = Strings.changeIndent(str, indent, project, new String(), typeRoot.findRecommendedLineSeparator());
}
return str;
}
} catch (JavaModelException e) {
// ignore
}
}
return null;
}
use of org.eclipse.jdt.core.ITypeRoot in project che by eclipse.
the class UnresolvedElementsSubProcessor method getExpressionBaseName.
private static String getExpressionBaseName(Expression expr) {
IBinding argBinding = Bindings.resolveExpressionBinding(expr, true);
if (argBinding instanceof IVariableBinding) {
IJavaProject project = null;
ASTNode root = expr.getRoot();
if (root instanceof CompilationUnit) {
ITypeRoot typeRoot = ((CompilationUnit) root).getTypeRoot();
if (typeRoot != null)
project = typeRoot.getJavaProject();
}
return StubUtility.getBaseName((IVariableBinding) argBinding, project);
}
if (expr instanceof SimpleName)
return ((SimpleName) expr).getIdentifier();
return null;
}
use of org.eclipse.jdt.core.ITypeRoot in project che by eclipse.
the class ASTProvider method getAST.
/**
* Returns a shared compilation unit AST for the given Java element.
* <p>
* Clients are not allowed to modify the AST and must synchronize all access to its nodes.
* </p>
*
* @param input the Java element, must not be <code>null</code>
* @param waitFlag {@link SharedASTProvider#WAIT_YES}, {@link SharedASTProvider#WAIT_NO} or
* {@link SharedASTProvider#WAIT_ACTIVE_ONLY}
* @param progressMonitor the progress monitor or <code>null</code>
* @return the AST or <code>null</code> if the AST is not available
*/
public CompilationUnit getAST(final ITypeRoot input, SharedASTProvider.WAIT_FLAG waitFlag, IProgressMonitor progressMonitor) {
if (input == null || waitFlag == null)
//$NON-NLS-1$
throw new IllegalArgumentException("input or wait flag are null");
if (progressMonitor != null && progressMonitor.isCanceled())
return null;
boolean isActiveElement;
synchronized (this) {
//input.equals(fActiveJavaElement);
isActiveElement = false;
if (isActiveElement) {
if (fAST != null) {
if (DEBUG)
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
System.out.println(getThreadName() + " - " + DEBUG_PREFIX + "returning cached AST:" + toString(fAST) + " for: " + input.getElementName());
return fAST;
}
if (waitFlag == SharedASTProvider.WAIT_NO) {
if (DEBUG)
//$NON-NLS-1$ //$NON-NLS-2$
System.out.println(getThreadName() + " - " + DEBUG_PREFIX + "returning null (WAIT_NO) for: " + input.getElementName());
return null;
}
}
}
final boolean canReturnNull = waitFlag == SharedASTProvider.WAIT_NO || (waitFlag == SharedASTProvider.WAIT_ACTIVE_ONLY && !(isActiveElement && fAST == null));
boolean isReconciling = false;
final ITypeRoot activeElement;
if (isActiveElement) {
// synchronized (fReconcileLock) {
// activeElement= fReconcilingJavaElement;
// isReconciling= isReconciling(input);
// if (!isReconciling && !canReturnNull)
// aboutToBeReconciled(input);
// }
} else
activeElement = null;
if (isReconciling) {
// }
return getAST(input, waitFlag, progressMonitor);
}
/*else if (canReturnNull)
return null;*/
CompilationUnit ast = null;
try {
ast = createAST(input, progressMonitor);
if (progressMonitor != null && progressMonitor.isCanceled()) {
ast = null;
if (DEBUG)
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
System.out.println(getThreadName() + " - " + DEBUG_PREFIX + "Ignore created AST for: " + input.getElementName() + " - operation has been cancelled");
}
} finally {
if (isActiveElement) {
if (fAST != null) {
// in the meantime, reconcile created a new AST. Return that one
if (DEBUG)
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
System.out.println(getThreadName() + " - " + DEBUG_PREFIX + "Ignore created AST for " + input.getElementName() + " - AST from reconciler is newer");
reconciled(fAST, input, null);
return fAST;
} else
reconciled(ast, input, null);
}
}
return ast;
}
Aggregations