use of org.eclipse.jdt.core.IMember 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;
}
use of org.eclipse.jdt.core.IMember in project che by eclipse.
the class RenameTypeParameterProcessor method checkNewElementName.
public RefactoringStatus checkNewElementName(String name) throws CoreException {
Assert.isNotNull(name);
RefactoringStatus result = Checks.checkTypeParameterName(name, fTypeParameter);
if (Checks.startsWithLowerCase(name))
result.addWarning(RefactoringCoreMessages.RenameTypeParameterRefactoring_should_start_lowercase);
if (Checks.isAlreadyNamed(fTypeParameter, name))
result.addFatalError(RefactoringCoreMessages.RenameTypeParameterRefactoring_another_name);
IMember member = fTypeParameter.getDeclaringMember();
if (member instanceof IType) {
IType type = (IType) member;
if (type.getTypeParameter(name).exists())
result.addFatalError(RefactoringCoreMessages.RenameTypeParameterRefactoring_class_type_parameter_already_defined);
} else if (member instanceof IMethod) {
IMethod method = (IMethod) member;
if (method.getTypeParameter(name).exists())
result.addFatalError(RefactoringCoreMessages.RenameTypeParameterRefactoring_method_type_parameter_already_defined);
} else {
//$NON-NLS-1$
JavaPlugin.logErrorMessage("Unexpected sub-type of IMember: " + member.getClass().getName());
Assert.isTrue(false);
}
return result;
}
use of org.eclipse.jdt.core.IMember in project che by eclipse.
the class JavaSearchScope method add.
/**
* Add an element to the java search scope.
* @param element The element we want to add to current java search scope
* @throws JavaModelException May happen if some Java Model info are not available
*/
public void add(IJavaElement element) throws JavaModelException {
IPath containerPath = null;
String containerPathToString = null;
PackageFragmentRoot root = null;
int includeMask = SOURCES | APPLICATION_LIBRARIES | SYSTEM_LIBRARIES;
switch(element.getElementType()) {
case IJavaElement.JAVA_MODEL:
// a workspace sope should be used
break;
case IJavaElement.JAVA_PROJECT:
add((JavaProject) element, null, includeMask, new HashSet(2), new HashSet(2), null);
break;
case IJavaElement.PACKAGE_FRAGMENT_ROOT:
root = (PackageFragmentRoot) element;
IPath rootPath = root.internalPath();
containerPath = root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : rootPath;
containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
IResource rootResource = root.resource();
String projectPath = root.getJavaProject().getPath().toString();
if (rootResource != null && rootResource.isAccessible()) {
String relativePath = Util.relativePath(rootResource.getFullPath(), containerPath.segmentCount());
add(projectPath, relativePath, containerPathToString, false, /*not a package*/
null);
} else {
//$NON-NLS-1$
add(projectPath, "", containerPathToString, false, /*not a package*/
null);
}
break;
case IJavaElement.PACKAGE_FRAGMENT:
root = (PackageFragmentRoot) element.getParent();
projectPath = root.getJavaProject().getPath().toString();
if (root.isArchive()) {
String relativePath = Util.concatWith(((PackageFragment) element).names, '/');
containerPath = root.getPath();
containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
add(projectPath, relativePath, containerPathToString, true, /*package*/
null);
} else {
IResource resource = ((JavaElement) element).resource();
if (resource != null) {
if (resource.isAccessible()) {
containerPath = root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : root.internalPath();
} else {
// for working copies, get resource container full path
containerPath = resource.getParent().getFullPath();
}
containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
String relativePath = Util.relativePath(resource.getFullPath(), containerPath.segmentCount());
add(projectPath, relativePath, containerPathToString, true, /*package*/
null);
}
}
break;
default:
// remember sub-cu (or sub-class file) java elements
if (element instanceof IMember) {
if (this.elements == null) {
this.elements = new ArrayList();
}
this.elements.add(element);
}
root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
projectPath = root.getJavaProject().getPath().toString();
String relativePath;
if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
containerPath = root.getParent().getPath();
relativePath = Util.relativePath(getPath(element, false), root.getJavaProject().getPath().segmentCount());
} else {
containerPath = root.internalPath();
relativePath = getPath(element, true).toString();
}
containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
add(projectPath, relativePath, containerPathToString, false, /*not a package*/
null);
}
if (root != null)
addEnclosingProjectOrJar(root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : root.getPath());
}
use of org.eclipse.jdt.core.IMember in project che by eclipse.
the class SourceMapper method getNameRange.
/**
* Returns the SourceRange for the name of the given element, or
* {-1, -1} if no source range is known for the name of the element.
*/
public SourceRange getNameRange(IJavaElement element) {
switch(element.getElementType()) {
case IJavaElement.METHOD:
if (((IMember) element).isBinary()) {
IJavaElement[] el = getUnqualifiedMethodHandle((IMethod) element, false);
if (el[1] != null && this.sourceRanges.get(el[0]) == null) {
element = getUnqualifiedMethodHandle((IMethod) element, true)[0];
} else {
element = el[0];
}
}
break;
case IJavaElement.TYPE_PARAMETER:
IJavaElement parent = element.getParent();
if (parent.getElementType() == IJavaElement.METHOD) {
IMethod method = (IMethod) parent;
if (method.isBinary()) {
IJavaElement[] el = getUnqualifiedMethodHandle(method, false);
if (el[1] != null && this.sourceRanges.get(el[0]) == null) {
method = (IMethod) getUnqualifiedMethodHandle(method, true)[0];
} else {
method = (IMethod) el[0];
}
element = method.getTypeParameter(element.getElementName());
}
}
break;
case IJavaElement.LOCAL_VARIABLE:
LocalVariableElementKey key = new LocalVariableElementKey(element.getParent(), element.getElementName());
SourceRange[] ranges = (SourceRange[]) this.parametersRanges.get(key);
if (ranges == null) {
return UNKNOWN_RANGE;
} else {
return ranges[1];
}
}
SourceRange[] ranges = (SourceRange[]) this.sourceRanges.get(element);
if (ranges == null) {
return UNKNOWN_RANGE;
} else {
return ranges[1];
}
}
use of org.eclipse.jdt.core.IMember in project che by eclipse.
the class IntroduceIndirectionRefactoring method adjustVisibility.
private RefactoringStatus adjustVisibility(IMember whoToAdjust, ModifierKeyword neededVisibility, boolean alsoIncreaseEnclosing, IProgressMonitor monitor) throws CoreException {
Map<IMember, IncomingMemberVisibilityAdjustment> adjustments;
if (isRewriteKept(whoToAdjust.getCompilationUnit()))
adjustments = fIntermediaryAdjustments;
else
adjustments = new HashMap<IMember, IncomingMemberVisibilityAdjustment>();
int existingAdjustments = adjustments.size();
addAdjustment(whoToAdjust, neededVisibility, adjustments);
if (alsoIncreaseEnclosing)
while (whoToAdjust.getDeclaringType() != null) {
whoToAdjust = whoToAdjust.getDeclaringType();
addAdjustment(whoToAdjust, neededVisibility, adjustments);
}
boolean hasNewAdjustments = (adjustments.size() - existingAdjustments) > 0;
if (hasNewAdjustments && ((whoToAdjust.isReadOnly() || whoToAdjust.isBinary())))
return RefactoringStatus.createErrorStatus(Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_update_binary_target_visibility, new String[] { JavaElementLabels.getElementLabel(whoToAdjust, JavaElementLabels.ALL_DEFAULT) }), JavaStatusContext.create(whoToAdjust));
RefactoringStatus status = new RefactoringStatus();
// Don't create a rewrite if it is not necessary
if (!hasNewAdjustments)
return status;
try {
monitor.beginTask(RefactoringCoreMessages.MemberVisibilityAdjustor_adjusting, 2);
Map<ICompilationUnit, CompilationUnitRewrite> rewrites;
if (!isRewriteKept(whoToAdjust.getCompilationUnit())) {
CompilationUnitRewrite rewrite = new CompilationUnitRewrite(whoToAdjust.getCompilationUnit());
rewrite.setResolveBindings(false);
rewrites = new HashMap<ICompilationUnit, CompilationUnitRewrite>();
rewrites.put(whoToAdjust.getCompilationUnit(), rewrite);
status.merge(rewriteVisibility(adjustments, rewrites, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)));
rewrite.attachChange((CompilationUnitChange) fTextChangeManager.get(whoToAdjust.getCompilationUnit()), true, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
}
} finally {
monitor.done();
}
return status;
}
Aggregations