use of org.eclipse.jdt.core.IPackageFragment 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.IPackageFragment in project che by eclipse.
the class Util method getCompilationUnit.
private static ICompilationUnit getCompilationUnit(char[] fileName, WorkingCopyOwner workingCopyOwner) {
char[] slashSeparatedFileName = CharOperation.replaceOnCopy(fileName, File.separatorChar, '/');
// pkgEnd is exclusive
int pkgEnd = CharOperation.lastIndexOf('/', slashSeparatedFileName);
if (pkgEnd == -1)
return null;
IPackageFragment pkg = getPackageFragment(slashSeparatedFileName, pkgEnd, -1);
if (pkg == null)
return null;
int start;
ICompilationUnit cu = pkg.getCompilationUnit(new String(slashSeparatedFileName, start = pkgEnd + 1, slashSeparatedFileName.length - start));
if (workingCopyOwner != null) {
ICompilationUnit workingCopy = cu.findWorkingCopy(workingCopyOwner);
if (workingCopy != null)
return workingCopy;
}
return cu;
}
use of org.eclipse.jdt.core.IPackageFragment in project che by eclipse.
the class Util method getClassFile.
private static IClassFile getClassFile(char[] fileName) {
int jarSeparator = CharOperation.indexOf(IDependent.JAR_FILE_ENTRY_SEPARATOR, fileName);
// pkgEnd is exclusive
int pkgEnd = CharOperation.lastIndexOf('/', fileName);
if (pkgEnd == -1)
pkgEnd = CharOperation.lastIndexOf(File.separatorChar, fileName);
if (// if in a jar and no slash, it is a default package -> pkgEnd should be equal to jarSeparator
jarSeparator != -1 && pkgEnd < jarSeparator)
pkgEnd = jarSeparator;
if (pkgEnd == -1)
return null;
IPackageFragment pkg = getPackageFragment(fileName, pkgEnd, jarSeparator);
if (pkg == null)
return null;
int start;
return pkg.getClassFile(new String(fileName, start = pkgEnd + 1, fileName.length - start));
}
use of org.eclipse.jdt.core.IPackageFragment 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.IPackageFragment in project che by eclipse.
the class RenameModifications method addAllResourceModifications.
private void addAllResourceModifications(IPackageFragment rootPackage, RenameArguments args, boolean renameSubPackages, IPackageFragment[] allSubPackages) throws CoreException {
IFolder target = addResourceModifications(rootPackage, args, rootPackage, renameSubPackages);
if (renameSubPackages) {
IContainer container = (IContainer) rootPackage.getResource();
if (container == null)
return;
boolean removeContainer = !container.contains(target);
for (int i = 0; i < allSubPackages.length; i++) {
IPackageFragment pack = allSubPackages[i];
IFolder subTarget = addResourceModifications(rootPackage, args, pack, renameSubPackages);
if (container.contains(subTarget))
removeContainer = false;
}
if (removeContainer) {
getResourceModifications().addDelete(container);
}
}
}
Aggregations