Example usage for org.eclipse.jdt.core IType getAncestor

List of usage examples for org.eclipse.jdt.core IType getAncestor

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IType getAncestor.

Prototype

IJavaElement getAncestor(int ancestorType);

Source Link

Document

Returns this Java element or the first ancestor of this element that has the given type.

Usage

From source file:com.android.ide.eclipse.adt.internal.refactoring.core.AndroidTypeMoveParticipant.java

License:Open Source License

@Override
protected boolean initialize(Object element) {

    if (element instanceof IType) {
        IType type = (IType) element;
        IJavaProject javaProject = (IJavaProject) type.getAncestor(IJavaElement.JAVA_PROJECT);
        IProject project = javaProject.getProject();
        IResource manifestResource = project
                .findMember(AdtConstants.WS_SEP + SdkConstants.FN_ANDROID_MANIFEST_XML);

        if (manifestResource == null || !manifestResource.exists() || !(manifestResource instanceof IFile)) {
            RefactoringUtil.logInfo("Invalid or missing the " + SdkConstants.FN_ANDROID_MANIFEST_XML
                    + " in the " + project.getName() + " project.");
            return false;
        }//from   w  ww  .  j  av a2 s  .  co m
        mAndroidManifest = (IFile) manifestResource;
        ManifestData manifestData;
        manifestData = AndroidManifestHelper.parseForData(mAndroidManifest);
        if (manifestData == null) {
            return false;
        }
        mJavaPackage = manifestData.getPackage();
        mOldName = type.getFullyQualifiedName();
        Object destination = getArguments().getDestination();
        if (destination instanceof IPackageFragment) {
            IPackageFragment packageFragment = (IPackageFragment) destination;
            mNewName = packageFragment.getElementName() + "." + type.getElementName();
        }
        if (mOldName == null || mNewName == null) {
            return false;
        }
        mAndroidElements = addAndroidElements();
        try {
            ITypeHierarchy typeHierarchy = type.newSupertypeHierarchy(null);
            if (typeHierarchy == null) {
                return false;
            }
            IType[] superTypes = typeHierarchy.getAllSuperclasses(type);
            for (int i = 0; i < superTypes.length; i++) {
                IType superType = superTypes[i];
                String className = superType.getFullyQualifiedName();
                if (className.equals(SdkConstants.CLASS_VIEW)) {
                    addLayoutChanges(project, type.getFullyQualifiedName());
                    break;
                }
            }
        } catch (JavaModelException ignore) {
        }
        return mAndroidElements.size() > 0 || mFileChanges.size() > 0;
    }
    return false;
}

From source file:com.android.ide.eclipse.adt.internal.refactoring.core.AndroidTypeRenameParticipant.java

License:Open Source License

@Override
protected boolean initialize(Object element) {

    if (element instanceof IType) {
        IType type = (IType) element;
        IJavaProject javaProject = (IJavaProject) type.getAncestor(IJavaElement.JAVA_PROJECT);
        IProject project = javaProject.getProject();
        IResource manifestResource = project
                .findMember(AdtConstants.WS_SEP + SdkConstants.FN_ANDROID_MANIFEST_XML);

        if (manifestResource == null || !manifestResource.exists() || !(manifestResource instanceof IFile)) {
            RefactoringUtil.logInfo("Invalid or missing the " + SdkConstants.FN_ANDROID_MANIFEST_XML
                    + " in the " + project.getName() + " project.");
            return false;
        }//from   w  w w  .ja  v a 2  s  .c  om
        mAndroidManifest = (IFile) manifestResource;
        ManifestData manifestData;
        manifestData = AndroidManifestHelper.parseForData(mAndroidManifest);
        if (manifestData == null) {
            return false;
        }
        mAppPackage = manifestData.getPackage();
        mOldName = type.getFullyQualifiedName();
        String packageName = type.getPackageFragment().getElementName();
        mNewName = getArguments().getNewName();
        if (packageName != null) {
            mLayoutNewName = packageName + "." + getArguments().getNewName(); //$NON-NLS-1$
        } else {
            mLayoutNewName = getArguments().getNewName();
        }
        if (mOldName == null || mNewName == null) {
            return false;
        }
        if (!RefactoringUtil.isRefactorAppPackage() && mNewName.indexOf(".") == -1) { //$NON-NLS-1$
            mNewName = packageName + "." + mNewName; //$NON-NLS-1$
        }
        mAndroidElements = addAndroidElements();
        try {
            ITypeHierarchy typeHierarchy = type.newSupertypeHierarchy(null);
            if (typeHierarchy == null) {
                return false;
            }
            IType[] superTypes = typeHierarchy.getAllSuperclasses(type);
            for (int i = 0; i < superTypes.length; i++) {
                IType superType = superTypes[i];
                String className = superType.getFullyQualifiedName();
                if (className.equals(SdkConstants.CLASS_VIEW)) {
                    addLayoutChanges(project, type.getFullyQualifiedName());
                    break;
                }
            }
        } catch (JavaModelException ignore) {
        }

        return mAndroidElements.size() > 0 || mFileChanges.size() > 0;
    }
    return false;
}

From source file:com.android.ide.eclipse.adt.internal.refactorings.core.AndroidTypeMoveParticipant.java

License:Open Source License

@Override
protected boolean initialize(Object element) {
    if (element instanceof IType) {
        IType type = (IType) element;
        IJavaProject javaProject = (IJavaProject) type.getAncestor(IJavaElement.JAVA_PROJECT);
        mProject = javaProject.getProject();
        IResource manifestResource = mProject
                .findMember(AdtConstants.WS_SEP + SdkConstants.FN_ANDROID_MANIFEST_XML);

        if (manifestResource == null || !manifestResource.exists() || !(manifestResource instanceof IFile)) {
            RefactoringUtil.logInfo("Invalid or missing the " + SdkConstants.FN_ANDROID_MANIFEST_XML
                    + " in the " + mProject.getName() + " project.");
            return false;
        }// w w  w.  jav a  2 s. c o m
        mManifestFile = (IFile) manifestResource;
        ManifestData manifestData;
        manifestData = AndroidManifestHelper.parseForData(mManifestFile);
        if (manifestData == null) {
            return false;
        }
        mAppPackage = manifestData.getPackage();
        mOldFqcn = type.getFullyQualifiedName();
        Object destination = getArguments().getDestination();
        if (destination instanceof IPackageFragment) {
            IPackageFragment packageFragment = (IPackageFragment) destination;
            mNewFqcn = packageFragment.getElementName() + "." + type.getElementName();
        } else if (destination instanceof IResource) {
            try {
                IPackageFragment[] fragments = javaProject.getPackageFragments();
                for (IPackageFragment fragment : fragments) {
                    IResource resource = fragment.getResource();
                    if (resource.equals(destination)) {
                        mNewFqcn = fragment.getElementName() + '.' + type.getElementName();
                        break;
                    }
                }
            } catch (JavaModelException e) {
                // pass
            }
        }
        return mOldFqcn != null && mNewFqcn != null;
    }

    return false;
}

From source file:com.android.ide.eclipse.adt.internal.refactorings.core.AndroidTypeRenameParticipant.java

License:Open Source License

@Override
protected boolean initialize(Object element) {
    if (sIgnore) {
        return false;
    }// w ww .j a  v a2s .c  o  m

    if (element instanceof IType) {
        IType type = (IType) element;
        IJavaProject javaProject = (IJavaProject) type.getAncestor(IJavaElement.JAVA_PROJECT);
        mProject = javaProject.getProject();
        IResource manifestResource = mProject
                .findMember(AdtConstants.WS_SEP + SdkConstants.FN_ANDROID_MANIFEST_XML);

        if (manifestResource == null || !manifestResource.exists() || !(manifestResource instanceof IFile)) {
            RefactoringUtil.logInfo(String.format("Invalid or missing file %1$s in project %2$s",
                    SdkConstants.FN_ANDROID_MANIFEST_XML, mProject.getName()));
            return false;
        }

        try {
            IType classView = javaProject.findType(CLASS_VIEW);
            if (classView != null) {
                ITypeHierarchy hierarchy = type.newSupertypeHierarchy(new NullProgressMonitor());
                if (hierarchy.contains(classView)) {
                    mIsCustomView = true;
                }
            }
        } catch (CoreException e) {
            AdtPlugin.log(e, null);
        }

        mManifestFile = (IFile) manifestResource;
        ManifestData manifestData;
        manifestData = AndroidManifestHelper.parseForData(mManifestFile);
        if (manifestData == null) {
            return false;
        }
        mOldSimpleName = type.getElementName();
        mOldDottedName = '.' + mOldSimpleName;
        mOldFqcn = type.getFullyQualifiedName();
        String packageName = type.getPackageFragment().getElementName();
        mNewSimpleName = getArguments().getNewName();
        mNewDottedName = '.' + mNewSimpleName;
        if (packageName != null) {
            mNewFqcn = packageName + mNewDottedName;
        } else {
            mNewFqcn = mNewSimpleName;
        }
        if (mOldFqcn == null || mNewFqcn == null) {
            return false;
        }
        if (!RefactoringUtil.isRefactorAppPackage() && mNewFqcn.indexOf('.') == -1) {
            mNewFqcn = packageName + mNewDottedName;
        }
        return true;
    }
    return false;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.ClassFileInfo.java

License:Open Source License

/**
 * Creates the handles for the inner types of the given binary type.
 * Adds new handles to the given vector.
 *//*from  ww w .  j  a v a 2 s .  c  o  m*/
private void generateInnerClassHandles(IType type, IBinaryType typeInfo, ArrayList childrenHandles) {
    // Add inner types
    // If the current type is an inner type, innerClasses returns
    // an extra entry for the current type.  This entry must be removed.
    // Can also return an entry for the enclosing type of an inner type.
    IBinaryNestedType[] innerTypes = typeInfo.getMemberTypes();
    if (innerTypes != null) {
        IPackageFragment pkg = (IPackageFragment) type.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
        for (int i = 0, typeCount = innerTypes.length; i < typeCount; i++) {
            IBinaryNestedType binaryType = innerTypes[i];
            IClassFile parentClassFile = pkg.getClassFile(
                    new String(ClassFile.unqualifiedName(binaryType.getName())) + SUFFIX_STRING_class);
            IType innerType = new BinaryType((JavaElement) parentClassFile,
                    ((JavaElement) parentClassFile).manager, ClassFile.simpleName(binaryType.getName()));
            childrenHandles.add(innerType);
        }
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.NamedMember.java

License:Open Source License

protected String getKey(IType type, boolean forceOpen) throws JavaModelException {
    StringBuffer key = new StringBuffer();
    key.append('L');
    String packageName = type.getPackageFragment().getElementName();
    key.append(packageName.replace('.', '/'));
    if (packageName.length() > 0)
        key.append('/');
    String typeQualifiedName = type.getTypeQualifiedName('$');
    ICompilationUnit cu = (ICompilationUnit) type.getAncestor(IJavaElement.COMPILATION_UNIT);
    if (cu != null) {
        String cuName = cu.getElementName();
        String mainTypeName = cuName.substring(0, cuName.lastIndexOf('.'));
        int end = typeQualifiedName.indexOf('$');
        if (end == -1)
            end = typeQualifiedName.length();
        String topLevelTypeName = typeQualifiedName.substring(0, end);
        if (!mainTypeName.equals(topLevelTypeName)) {
            key.append(mainTypeName);/*from  ww  w  .  jav a  2 s .  com*/
            key.append('~');
        }
    }
    key.append(typeQualifiedName);
    key.append(';');
    return key.toString();
}

From source file:com.codenvy.ide.ext.java.server.internal.core.NameLookup.java

License:Open Source License

private AccessRestriction getViolatedRestriction(String typeName, String packageName, IType type,
        AccessRestriction accessRestriction) {
    PackageFragmentRoot root = (PackageFragmentRoot) type.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    ClasspathEntry entry = (ClasspathEntry) this.rootToResolvedEntries.get(root);
    if (entry != null) { // reverse map always contains resolved CP entry
        AccessRuleSet accessRuleSet = entry.getAccessRuleSet();
        if (accessRuleSet != null) {
            // TODO (philippe) improve char[] <-> String conversions to avoid performing them on the fly
            char[][] packageChars = CharOperation.splitOn('.', packageName.toCharArray());
            char[] typeChars = typeName.toCharArray();
            accessRestriction = accessRuleSet
                    .getViolatedRestriction(CharOperation.concatWith(packageChars, typeChars, '/'));
        }//from w  ww  . ja v a  2 s .c  o  m
    }
    return accessRestriction;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.SearchableEnvironmentRequestor.java

License:Open Source License

/**
 * @see org.eclipse.jdt.internal.core.IJavaElementRequestor
 *///from  w w w  .j av  a2s  . c o  m
public void acceptType(IType type) {
    try {
        if (this.unitToSkip != null && this.unitToSkip.equals(type.getCompilationUnit())) {
            return;
        }
        char[] packageName = type.getPackageFragment().getElementName().toCharArray();
        boolean isBinary = type instanceof BinaryType;

        // determine associated access restriction
        AccessRestriction accessRestriction = null;

        if (this.checkAccessRestrictions && (isBinary || !type.getJavaProject().equals(this.project))) {
            PackageFragmentRoot root = (PackageFragmentRoot) type
                    .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            ClasspathEntry entry = (ClasspathEntry) this.nameLookup.rootToResolvedEntries.get(root);
            if (entry != null) { // reverse map always contains resolved CP entry
                AccessRuleSet accessRuleSet = entry.getAccessRuleSet();
                if (accessRuleSet != null) {
                    // TODO (philippe) improve char[] <-> String conversions to avoid performing them on the fly
                    char[][] packageChars = CharOperation.splitOn('.', packageName);
                    char[] fileWithoutExtension = type.getElementName().toCharArray();
                    accessRestriction = accessRuleSet.getViolatedRestriction(
                            CharOperation.concatWith(packageChars, fileWithoutExtension, '/'));
                }
            }
        }
        this.requestor.acceptType(packageName, type.getElementName().toCharArray(), null, type.getFlags(),
                accessRestriction);
    } catch (JavaModelException jme) {
        // ignore
    }
}

From source file:com.google.gdt.eclipse.core.ClasspathUtilities.java

License:Open Source License

/**
 * Return the raw classpath entry on the project's classpath that contributes
 * the given type to the project.//from w  ww  . j a v  a2  s  . c  o  m
 * 
 * @param javaProject The java project
 * @param fullyQualifiedName The fully-qualified type name
 * @return The raw classpath entry that contributes the type to the project,
 *         or <code>null</code> if no such classpath entry can be found.
 * @throws JavaModelException
 */
public static IClasspathEntry findRawClasspathEntryFor(IJavaProject javaProject, String fullyQualifiedName)
        throws JavaModelException {
    IType type = javaProject.findType(fullyQualifiedName);
    if (type != null) {
        IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) type
                .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);

        JavaProject jProject = (JavaProject) javaProject;

        IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
        for (IClasspathEntry rawClasspathEntry : rawClasspath) {
            IClasspathEntry[] resolvedClasspath = jProject
                    .resolveClasspath(new IClasspathEntry[] { rawClasspathEntry });
            IPackageFragmentRoot[] computePackageFragmentRoots = jProject
                    .computePackageFragmentRoots(resolvedClasspath, true, null);
            if (Arrays.asList(computePackageFragmentRoots).contains(packageFragmentRoot)) {
                return rawClasspathEntry;
            }
        }

        return packageFragmentRoot.getRawClasspathEntry();
    }

    return null;
}

From source file:com.google.gdt.eclipse.maven.sdk.GWTMavenRuntime.java

License:Open Source License

/**
 * Find the classpath for get-dev.jar which is used to run super dev mode.
 *
 * @return IClasspathEntry for the path to gwt-dev.jar
 * @throws JavaModelException//from www  .  j  av a2s  .com
 */
private IClasspathEntry findGwtCodeServerClasspathEntry() throws JavaModelException {
    IType type = javaProject
            .findType(GwtLaunchConfigurationProcessorUtilities.SUPERDEVMODE_CODESERVER_MAIN_TYPE);
    if (type == null) {
        return null;
    }

    IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) type
            .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_BINARY) {
        return JavaCore.newLibraryEntry(packageFragmentRoot.getPath(), null, null);
    }

    return null;
}