use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.
the class Util method getPackageFragment.
public static IPackageFragment getPackageFragment(char[] fileName, int pkgEnd, int jarSeparator) {
if (jarSeparator != -1) {
String jarMemento = new String(fileName, 0, jarSeparator);
PackageFragmentRoot root = (PackageFragmentRoot) JavaCore.create(jarMemento);
if (pkgEnd == jarSeparator)
return root.getPackageFragment(CharOperation.NO_STRINGS);
char[] pkgName = CharOperation.subarray(fileName, jarSeparator + 1, pkgEnd);
char[][] compoundName = CharOperation.splitOn('/', pkgName);
return root.getPackageFragment(CharOperation.toStrings(compoundName));
} else {
Path path = new Path(new String(fileName, 0, pkgEnd));
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
IContainer folder = path.segmentCount() == 1 ? workspaceRoot.getProject(path.lastSegment()) : (IContainer) workspaceRoot.getFolder(path);
IJavaElement element = JavaCore.create(folder);
if (element == null)
return null;
switch(element.getElementType()) {
case IJavaElement.PACKAGE_FRAGMENT:
return (IPackageFragment) element;
case IJavaElement.PACKAGE_FRAGMENT_ROOT:
return ((PackageFragmentRoot) element).getPackageFragment(CharOperation.NO_STRINGS);
case IJavaElement.JAVA_PROJECT:
PackageFragmentRoot root = (PackageFragmentRoot) ((IJavaProject) element).getPackageFragmentRoot(folder);
if (root == null)
return null;
return root.getPackageFragment(CharOperation.NO_STRINGS);
}
return null;
}
}
use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.
the class Util method isExcluded.
/*
* Returns whether the given java element is exluded from its root's classpath.
* It doesn't check whether the root itself is on the classpath or not
*/
public static final boolean isExcluded(IJavaElement element) {
int elementType = element.getElementType();
switch(elementType) {
case IJavaElement.JAVA_MODEL:
case IJavaElement.JAVA_PROJECT:
case IJavaElement.PACKAGE_FRAGMENT_ROOT:
return false;
case IJavaElement.PACKAGE_FRAGMENT:
PackageFragmentRoot root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
IResource resource = ((PackageFragment) element).resource();
return resource != null && isExcluded(resource, root.fullInclusionPatternChars(), root.fullExclusionPatternChars());
case IJavaElement.COMPILATION_UNIT:
root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
resource = element.getResource();
if (resource == null)
return false;
if (isExcluded(resource, root.fullInclusionPatternChars(), root.fullExclusionPatternChars()))
return true;
return isExcluded(element.getParent());
default:
IJavaElement cu = element.getAncestor(IJavaElement.COMPILATION_UNIT);
return cu != null && isExcluded(cu);
}
}
use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.
the class StubUtility method getBaseNameFromLocationInParent.
private static String getBaseNameFromLocationInParent(Expression assignedExpression, List<Expression> arguments, IMethodBinding binding) {
if (binding == null)
return null;
ITypeBinding[] parameterTypes = binding.getParameterTypes();
if (// beware of guessed method bindings
parameterTypes.length != arguments.size())
return null;
int index = arguments.indexOf(assignedExpression);
if (index == -1)
return null;
ITypeBinding expressionBinding = assignedExpression.resolveTypeBinding();
if (expressionBinding != null && !expressionBinding.isAssignmentCompatible(parameterTypes[index]))
return null;
try {
IJavaElement javaElement = binding.getJavaElement();
if (javaElement instanceof IMethod) {
IMethod method = (IMethod) javaElement;
if (method.getOpenable().getBuffer() != null) {
// avoid dummy names and lookup from Javadoc
String[] parameterNames = method.getParameterNames();
if (index < parameterNames.length) {
return NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, parameterNames[index], method.getJavaProject());
}
}
}
} catch (JavaModelException e) {
// ignore
}
return null;
}
use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.
the class ContextSensitiveImportRewriteContext method getImportedNames.
private Name[] getImportedNames() {
if (fImportedNames == null) {
IJavaProject project = null;
IJavaElement javaElement = fCompilationUnit.getJavaElement();
if (javaElement != null)
project = javaElement.getJavaProject();
List<SimpleName> imports = new ArrayList<SimpleName>();
ImportReferencesCollector.collect(fCompilationUnit, project, null, imports, null);
fImportedNames = imports.toArray(new Name[imports.size()]);
}
return fImportedNames;
}
use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.
the class HandleFactory method createElement.
/**
* Create handle by adding child to parent obtained by recursing into parent scopes.
*/
public IJavaElement createElement(Scope scope, int elementPosition, ICompilationUnit unit, HashSet existingElements, HashMap knownScopes) {
IJavaElement newElement = (IJavaElement) knownScopes.get(scope);
if (newElement != null)
return newElement;
switch(scope.kind) {
case Scope.COMPILATION_UNIT_SCOPE:
newElement = unit;
break;
case Scope.CLASS_SCOPE:
IJavaElement parentElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
switch(parentElement.getElementType()) {
case IJavaElement.COMPILATION_UNIT:
newElement = ((ICompilationUnit) parentElement).getType(new String(scope.enclosingSourceType().sourceName));
break;
case IJavaElement.TYPE:
newElement = ((IType) parentElement).getType(new String(scope.enclosingSourceType().sourceName));
break;
case IJavaElement.FIELD:
case IJavaElement.INITIALIZER:
case IJavaElement.METHOD:
IMember member = (IMember) parentElement;
if (member.isBinary()) {
return null;
} else {
newElement = member.getType(new String(scope.enclosingSourceType().sourceName), 1);
// increment occurrence count if collision is detected
if (newElement != null) {
while (!existingElements.add(newElement)) ((SourceRefElement) newElement).occurrenceCount++;
}
}
break;
}
if (newElement != null) {
knownScopes.put(scope, newElement);
}
break;
case Scope.METHOD_SCOPE:
if (scope.isLambdaScope()) {
parentElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
LambdaExpression expression = (LambdaExpression) scope.originalReferenceContext();
if (expression.resolvedType != null && expression.resolvedType.isValidBinding() && !(expression.descriptor instanceof ProblemMethodBinding)) {
// chain in lambda element only if resolved properly.
//newElement = new org.eclipse.jdt.internal.core.SourceLambdaExpression((JavaElement) parentElement, expression).getMethod();
newElement = LambdaFactory.createLambdaExpression((JavaElement) parentElement, expression).getMethod();
knownScopes.put(scope, newElement);
return newElement;
}
return parentElement;
}
IType parentType = (IType) createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
MethodScope methodScope = (MethodScope) scope;
if (methodScope.isInsideInitializer()) {
// inside field or initializer, must find proper one
TypeDeclaration type = methodScope.referenceType();
int occurenceCount = 1;
int length = type.fields == null ? 0 : type.fields.length;
for (int i = 0; i < length; i++) {
FieldDeclaration field = type.fields[i];
if (field.declarationSourceStart <= elementPosition && elementPosition <= field.declarationSourceEnd) {
switch(field.getKind()) {
case AbstractVariableDeclaration.FIELD:
case AbstractVariableDeclaration.ENUM_CONSTANT:
newElement = parentType.getField(new String(field.name));
break;
case AbstractVariableDeclaration.INITIALIZER:
newElement = parentType.getInitializer(occurenceCount);
break;
}
break;
} else if (field.getKind() == AbstractVariableDeclaration.INITIALIZER) {
occurenceCount++;
}
}
} else {
// method element
AbstractMethodDeclaration method = methodScope.referenceMethod();
newElement = parentType.getMethod(new String(method.selector), Util.typeParameterSignatures(method));
if (newElement != null) {
knownScopes.put(scope, newElement);
}
}
break;
case Scope.BLOCK_SCOPE:
// standard block, no element per se
newElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
break;
}
return newElement;
}
Aggregations