use of org.eclipse.jdt.core.JavaModelException 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.JavaModelException in project che by eclipse.
the class HandleFactory method getJarPkgFragmentRoot.
private PackageFragmentRoot getJarPkgFragmentRoot(IPath jarPath, Object target, IJavaProject[] projects) {
for (int i = 0, projectCount = projects.length; i < projectCount; i++) {
try {
JavaProject javaProject = (JavaProject) projects[i];
IClasspathEntry classpathEnty = javaProject.getClasspathEntryFor(jarPath);
if (classpathEnty != null) {
if (target instanceof IFile) {
// internal jar
return (PackageFragmentRoot) javaProject.getPackageFragmentRoot((IFile) target);
} else {
// external jar
return (PackageFragmentRoot) javaProject.getPackageFragmentRoot0(jarPath);
}
}
} catch (JavaModelException e) {
// JavaModelException from getResolvedClasspath - a problem occurred while accessing project: nothing we can do, ignore
}
}
return null;
}
use of org.eclipse.jdt.core.JavaModelException 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.JavaModelException in project che by eclipse.
the class RenameMethodProcessor method createChange.
@Override
public Change createChange(IProgressMonitor monitor) throws CoreException {
try {
final TextChange[] changes = fChangeManager.getAllChanges();
final List<TextChange> list = new ArrayList<TextChange>(changes.length);
list.addAll(Arrays.asList(changes));
String project = null;
IJavaProject javaProject = fMethod.getJavaProject();
if (javaProject != null)
project = javaProject.getElementName();
int flags = JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE;
try {
if (!Flags.isPrivate(fMethod.getFlags()))
flags |= RefactoringDescriptor.MULTI_CHANGE;
} catch (JavaModelException exception) {
JavaPlugin.log(exception);
}
final IType declaring = fMethod.getDeclaringType();
try {
if (declaring.isAnonymous() || declaring.isLocal())
flags |= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
} catch (JavaModelException exception) {
JavaPlugin.log(exception);
}
final String description = Messages.format(RefactoringCoreMessages.RenameMethodProcessor_descriptor_description_short, BasicElementLabels.getJavaElementName(fMethod.getElementName()));
final String header = Messages.format(RefactoringCoreMessages.RenameMethodProcessor_descriptor_description, new String[] { JavaElementLabels.getTextLabel(fMethod, JavaElementLabels.ALL_FULLY_QUALIFIED), BasicElementLabels.getJavaElementName(getNewElementName()) });
final String comment = new JDTRefactoringDescriptorComment(project, this, header).asString();
final RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_METHOD);
descriptor.setProject(project);
descriptor.setDescription(description);
descriptor.setComment(comment);
descriptor.setFlags(flags);
descriptor.setJavaElement(fMethod);
descriptor.setNewName(getNewElementName());
descriptor.setUpdateReferences(fUpdateReferences);
descriptor.setKeepOriginal(fDelegateUpdating);
descriptor.setDeprecateDelegate(fDelegateDeprecation);
return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.RenameMethodProcessor_change_name, list.toArray(new Change[list.size()]));
} finally {
monitor.done();
}
}
use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.
the class RenameTypeProcessor method createChange.
@Override
public Change createChange(IProgressMonitor monitor) throws CoreException {
try {
monitor.beginTask(RefactoringCoreMessages.RenameTypeRefactoring_creating_change, 4);
String project = null;
IJavaProject javaProject = fType.getJavaProject();
if (javaProject != null)
project = javaProject.getElementName();
int flags = JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE;
try {
if (!Flags.isPrivate(fType.getFlags()))
flags |= RefactoringDescriptor.MULTI_CHANGE;
if (fType.isAnonymous() || fType.isLocal())
flags |= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
} catch (JavaModelException exception) {
JavaPlugin.log(exception);
}
final String description = Messages.format(RefactoringCoreMessages.RenameTypeProcessor_descriptor_description_short, BasicElementLabels.getJavaElementName(fType.getElementName()));
final String header = Messages.format(RefactoringCoreMessages.RenameTypeProcessor_descriptor_description, new String[] { JavaElementLabels.getElementLabel(fType, JavaElementLabels.ALL_FULLY_QUALIFIED), getNewElementLabel() });
final String comment = new JDTRefactoringDescriptorComment(project, this, header).asString();
final RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_TYPE);
descriptor.setProject(project);
descriptor.setDescription(description);
descriptor.setComment(comment);
descriptor.setFlags(flags);
descriptor.setJavaElement(fType);
descriptor.setNewName(getNewElementName());
descriptor.setUpdateQualifiedNames(fUpdateQualifiedNames);
descriptor.setUpdateTextualOccurrences(fUpdateTextualMatches);
descriptor.setUpdateReferences(fUpdateReferences);
if (//$NON-NLS-1$
fUpdateQualifiedNames && fFilePatterns != null && !"".equals(fFilePatterns))
descriptor.setFileNamePatterns(fFilePatterns);
descriptor.setUpdateSimilarDeclarations(fUpdateSimilarElements);
descriptor.setMatchStrategy(fRenamingStrategy);
final DynamicValidationRefactoringChange result = new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.RenameTypeProcessor_change_name);
if (fChangeManager.containsChangesIn(fType.getCompilationUnit())) {
TextChange textChange = fChangeManager.get(fType.getCompilationUnit());
if (textChange instanceof TextFileChange) {
((TextFileChange) textChange).setSaveMode(TextFileChange.FORCE_SAVE);
}
}
result.addAll(fChangeManager.getAllChanges());
if (willRenameCU()) {
IResource resource = fType.getCompilationUnit().getResource();
if (resource != null && resource.isLinked()) {
String ext = resource.getFileExtension();
String renamedResourceName;
if (ext == null)
renamedResourceName = getNewElementName();
else
renamedResourceName = getNewElementName() + '.' + ext;
result.add(new RenameResourceChange(fType.getCompilationUnit().getPath(), renamedResourceName));
} else {
String renamedCUName = JavaModelUtil.getRenamedCUName(fType.getCompilationUnit(), getNewElementName());
result.add(new RenameCompilationUnitChange(fType.getCompilationUnit(), renamedCUName));
}
}
monitor.worked(1);
return result;
} finally {
fChangeManager = null;
}
}
Aggregations