Search in sources :

Example 1 with IPackageFragment

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;
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IJavaElement(org.eclipse.jdt.core.IJavaElement) PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IContainer(org.eclipse.core.resources.IContainer)

Example 2 with IPackageFragment

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment)

Example 3 with IPackageFragment

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));
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment)

Example 4 with IPackageFragment

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);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) IType(org.eclipse.jdt.core.IType) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 5 with IPackageFragment

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);
        }
    }
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IContainer(org.eclipse.core.resources.IContainer) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1638 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1456 Test (org.junit.Test)1439 ArrayList (java.util.ArrayList)767 List (java.util.List)454 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)436 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)384 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)284 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)165 Hashtable (java.util.Hashtable)136 IJavaElement (org.eclipse.jdt.core.IJavaElement)72 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)67 IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)67 IJavaProject (org.eclipse.jdt.core.IJavaProject)59 Ignore (org.junit.Ignore)56 IType (org.eclipse.jdt.core.IType)55 JavaModelException (org.eclipse.jdt.core.JavaModelException)38 IResource (org.eclipse.core.resources.IResource)34 IFile (org.eclipse.core.resources.IFile)33 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)33