Example usage for org.eclipse.jdt.core IJavaElement exists

List of usage examples for org.eclipse.jdt.core IJavaElement exists

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement exists.

Prototype

boolean exists();

Source Link

Document

Returns whether this Java element exists in the model.

Usage

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

License:Open Source License

/**
 * @see org.eclipse.jdt.core.ICompilationUnit#findElements(org.eclipse.jdt.core.IJavaElement)
 *//*  w ww  .  j  a  v a  2 s. c o  m*/
public IJavaElement[] findElements(IJavaElement element) {
    ArrayList children = new ArrayList();
    while (element != null && element.getElementType() != IJavaElement.COMPILATION_UNIT) {
        children.add(element);
        element = element.getParent();
    }
    if (element == null)
        return null;
    IJavaElement currentElement = this;
    for (int i = children.size() - 1; i >= 0; i--) {
        SourceRefElement child = (SourceRefElement) children.get(i);
        switch (child.getElementType()) {
        case IJavaElement.PACKAGE_DECLARATION:
            currentElement = ((ICompilationUnit) currentElement).getPackageDeclaration(child.getElementName());
            break;
        case IJavaElement.IMPORT_CONTAINER:
            currentElement = ((ICompilationUnit) currentElement).getImportContainer();
            break;
        case IJavaElement.IMPORT_DECLARATION:
            currentElement = ((IImportContainer) currentElement).getImport(child.getElementName());
            break;
        case IJavaElement.TYPE:
            switch (currentElement.getElementType()) {
            case IJavaElement.COMPILATION_UNIT:
                currentElement = ((ICompilationUnit) currentElement).getType(child.getElementName());
                break;
            case IJavaElement.TYPE:
                currentElement = ((IType) currentElement).getType(child.getElementName());
                break;
            case IJavaElement.FIELD:
            case IJavaElement.INITIALIZER:
            case IJavaElement.METHOD:
                currentElement = ((IMember) currentElement).getType(child.getElementName(),
                        child.occurrenceCount);
                break;
            }
            break;
        case IJavaElement.INITIALIZER:
            currentElement = ((IType) currentElement).getInitializer(child.occurrenceCount);
            break;
        case IJavaElement.FIELD:
            currentElement = ((IType) currentElement).getField(child.getElementName());
            break;
        case IJavaElement.METHOD:
            currentElement = ((IType) currentElement).getMethod(child.getElementName(),
                    ((IMethod) child).getParameterTypes());
            break;
        }

    }
    if (currentElement != null && currentElement.exists()) {
        return new IJavaElement[] { currentElement };
    } else {
        return null;
    }
}

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

License:Open Source License

/**
 * This method checks whether the element is on the classpath (to make sure
 * it's not matched by an exclusion filter), which can be expensive, see GWT
 * issue 4793.//from  ww w. jav  a2  s  .  c o m
 * <p>
 * Note: As we discover new meanings for "validity", the checks could change.
 */
public static boolean isValidElement(IJavaElement element) {
    return element != null && element.exists() && element.getJavaProject().isOnClasspath(element);
}

From source file:com.google.gdt.eclipse.designer.common.GwtPropertyTester.java

License:Open Source License

/**
 * Tests property, can throw {@link Exception}.
 *///from   w  w w. j a va2  s .com
private static boolean testEx(Object receiver, String property) throws Exception {
    // prepare IResource
    final IResource resource;
    if (receiver instanceof IAdaptable) {
        resource = (IResource) ((IAdaptable) receiver).getAdapter(IResource.class);
    } else {
        resource = null;
    }
    // prepare IJavaElement
    final IJavaElement element;
    if (receiver instanceof IAdaptable) {
        element = (IJavaElement) ((IAdaptable) receiver).getAdapter(IJavaElement.class);
    } else {
        element = null;
    }
    // resources tests
    {
        if (PROPERTY_IS_GWT_MODULE_ELEMENT.equals(property)) {
            if (resource != null) {
                return Utils.getSingleModule(resource) != null;
            }
            return false;
        }
        if (PROPERTY_IS_RESOURCE.equals(property)) {
            return resource != null;
        }
    }
    // project tests
    {
        if (PROPERTY_IS_GWT_PROJECT_ELEMENT.equals(property)) {
            // resource selected
            if (resource != null && resource.exists()) {
                IProject project = resource.getProject();
                IJavaProject javaProject = JavaCore.create(project);
                return Utils.isGWTProject(javaProject);
            }
            // Java element selected
            if (element != null && element.exists()) {
                IJavaProject javaProject = (IJavaProject) element.getAncestor(IJavaElement.JAVA_PROJECT);
                return Utils.isGWTProject(javaProject);
            }
            // bad selection
            return false;
        }
    }
    // Java tests
    {
        // prepare java element
        if (element == null || !element.exists()) {
            return false;
        }
        // do tests
        if (PROPERTY_IS_CLIENT_PACKAGE.equals(property)) {
            IPackageFragment packageFragment = (IPackageFragment) element
                    .getAncestor(IJavaElement.PACKAGE_FRAGMENT);
            if (packageFragment != null) {
                return Utils.isModuleSourcePackage(packageFragment);
            }
            return false;
        }
        if (PROPERTY_IS_ENTRY_POINT.equals(property)) {
            return Utils.isEntryPoint(element);
        }
        if (PROPERTY_IS_REMOTE_SERVICE.equals(property)) {
            return Utils.isRemoteService(element);
        }
        if (PROPERTY_IS_REMOTE_SERVICE_IMPL.equals(property)) {
            return Utils.isRemoteServiceImpl(element);
        }
    }
    // unknown property
    throw new IllegalArgumentException(
            MessageFormat.format("Illegal property '{0}' for '{1}'.", property, receiver));
}

From source file:com.google.gdt.eclipse.designer.gxt.actions.GxtPropertyTester.java

License:Open Source License

/**
 * Tests property, can throw {@link Exception}.
 *//*from  w w w .  ja v  a2s . co  m*/
private static boolean testEx(Object receiver, String property) throws Exception {
    // prepare IJavaElement
    final IJavaElement element;
    if (receiver instanceof IAdaptable) {
        element = (IJavaElement) ((IAdaptable) receiver).getAdapter(IJavaElement.class);
    } else {
        element = null;
    }
    // Java tests
    {
        // prepare java element
        if (element == null || !element.exists()) {
            return false;
        }
        // do tests
        if (PROPERTY_IS_CONFIGURED.equals(property)) {
            IJavaProject javaProject = element.getJavaProject();
            boolean hasJar = javaProject.findType("com.extjs.gxt.ui.client.widget.Component") != null;
            if (hasJar) {
                IJavaElement pkg = element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
                if (pkg != null) {
                    IResource resource = pkg.getUnderlyingResource();
                    ModuleDescription module = Utils.getSingleModule(resource);
                    return module != null && Utils.inheritsModule(module, "com.extjs.gxt.ui.GXT");
                }
            }
            return false;
        }
    }
    // unknown property
    throw new IllegalArgumentException(
            MessageFormat.format("Illegal property '{0}' for '{1}'.", property, receiver));
}

From source file:com.google.gdt.eclipse.designer.smart.actions.SmartGwtPropertyTester.java

License:Open Source License

/**
 * Tests property, can throw {@link Exception}.
 *///  ww w  .  j a va  2s . co  m
private static boolean testEx(Object receiver, String property) throws Exception {
    // prepare IJavaElement
    final IJavaElement element;
    if (receiver instanceof IAdaptable) {
        element = (IJavaElement) ((IAdaptable) receiver).getAdapter(IJavaElement.class);
    } else {
        element = null;
    }
    // Java tests
    {
        // prepare java element
        if (element == null || !element.exists()) {
            return false;
        }
        // do tests
        if (PROPERTY_IS_CONFIGURED.equals(property)) {
            IJavaProject javaProject = element.getJavaProject();
            boolean hasJar = javaProject.findType("com.smartgwt.client.widgets.BaseWidget") != null;
            if (hasJar) {
                IJavaElement pkg = element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
                if (pkg != null) {
                    IResource resource = pkg.getUnderlyingResource();
                    ModuleDescription module = Utils.getSingleModule(resource);
                    return module != null && Utils.inheritsModule(module, "com.smartgwt.SmartGwt");
                }
            }
            return false;
        }
    }
    // unknown property
    throw new IllegalArgumentException(
            MessageFormat.format("Illegal property '{0}' for '{1}'.", property, receiver));
}

From source file:com.google.gdt.eclipse.designer.wizards.ui.JUnitWizardPage.java

License:Open Source License

private void handleSelectClassUnderTest(IJavaElement element) {
    try {//ww  w  .ja  v  a2  s.  c o m
        if (element == null || !element.exists() || element.getJavaProject() == null) {
            setErrorState();
        } else {
            IJavaProject javaProject = element.getJavaProject();
            if (Utils.isGWTProject(javaProject)) {
                IPackageFragmentRoot testSourceFragmentRoot = handleTestSourceFolder(javaProject);
                IPackageFragment elementPackage = handleTestPackage(element, testSourceFragmentRoot);
                // handle class under test
                IType classUnderTestType = (IType) element.getAncestor(IJavaElement.TYPE);
                if (classUnderTestType == null) {
                    ICompilationUnit compilationUnit = (ICompilationUnit) element
                            .getAncestor(IJavaElement.COMPILATION_UNIT);
                    if (compilationUnit != null) {
                        classUnderTestType = compilationUnit.findPrimaryType();
                    }
                }
                if (classUnderTestType == null) {
                    setErrorState();
                } else {
                    m_classUnderTestField.setText(classUnderTestType.getFullyQualifiedName());
                    setTypeName(classUnderTestType.getElementName() + "Test", true);
                    m_classUnderTestStatus = new Status(IStatus.OK, Activator.PLUGIN_ID, IStatus.OK, null,
                            null);
                    //
                    ModuleDescription module = Utils.getSingleModule(elementPackage);
                    if (module == null) {
                        setErrorState(
                                "GWT module for " + classUnderTestType.getFullyQualifiedName() + " not found.");
                    } else {
                        m_moduleId = module.getId();
                    }
                }
            } else {
                setErrorState();
            }
        }
    } catch (Throwable e) {
        DesignerPlugin.log(e);
        setErrorState("InternalError: " + e.getMessage());
    } finally {
        doStatusUpdate();
    }
}

From source file:com.iw.plugins.spindle.editors.SpindleMultipageEditor.java

License:Mozilla Public License

protected void checkValidLocation(IEditorInput input) throws CoreException {
    if (input instanceof JarEntryEditorInput) {
        return;//  w  ww  .  jav a 2 s  . c  o  m
    }

    IFile file = (IFile) input.getAdapter(IFile.class);

    SpindleStatus status = new SpindleStatus();

    if (file == null) {

        status.setError("Could not open editor...could not adapt the editor input into a file");

        throw new CoreException(status);
    }

    String filePath = file.getFullPath().toString();

    ITapestryProject project = TapestryPlugin.getDefault().getTapestryProjectFor(file);
    if (project == null) {

        status.setError("Could not open editor.. " + filePath + " is not in a Tapestry Project");
        throw new CoreException(status);
    }

    IJavaProject jproject = TapestryPlugin.getDefault().getJavaProjectFor(file);

    if (jproject == null) {

        status.setError("Could not open editor..could not find the Java Project nature for " + filePath);
        throw new CoreException(status);

    }

    IFolder parentFolder = (IFolder) file.getParent();
    IJavaElement element = JavaCore.create(parentFolder);

    if (element == null || !element.exists()) {

        status.setError(
                "Could not open editor..could not locate " + file.getName() + " in the project src path");
        throw new CoreException(status);
    }

}

From source file:com.redhat.ceylon.eclipse.code.explorer.PackageExplorerPart.java

License:Open Source License

private Object findInputElement() {
    if (getRootMode() == WORKING_SETS_AS_ROOTS) {
        return fWorkingSetModel;
    } else {//ww w.j a  va  2  s .  c o m
        Object input = getSite().getPage().getInput();
        if (input instanceof IWorkspace) {
            return JavaCore.create(((IWorkspace) input).getRoot());
        } else if (input instanceof IContainer) {
            IJavaElement element = JavaCore.create((IContainer) input);
            if (element != null && element.exists())
                return element;
            return input;
        }
        //1GERPRT: ITPJUI:ALL - Packages View is empty when shown in Type Hierarchy Perspective
        // we can't handle the input
        // fall back to show the workspace
        return JavaCore.create(JavaPlugin.getWorkspace().getRoot());
    }
}

From source file:com.redhat.ceylon.eclipse.code.explorer.PackageExplorerPart.java

License:Open Source License

private Object convertElement(Object original) {
    if (original instanceof IJavaElement) {
        if (original instanceof ICompilationUnit) {
            ICompilationUnit cu = (ICompilationUnit) original;
            IJavaProject javaProject = cu.getJavaProject();
            if (javaProject != null && javaProject.exists() && !javaProject.isOnClasspath(cu)) {
                // could be a working copy of a .java file that is not on classpath
                IResource resource = cu.getResource();
                if (resource != null)
                    return resource;
            }//ww w  .j  ava  2 s .co m

        }
        return original;

    } else if (original instanceof IResource) {
        IJavaElement je = JavaCore.create((IResource) original);
        if (je != null && je.exists()) {
            IJavaProject javaProject = je.getJavaProject();
            if (javaProject != null && javaProject.exists()) {
                if (javaProject.equals(je) || javaProject.isOnClasspath(je)) {
                    return je;
                } else {
                    // a working copy of a .java file that is not on classpath
                    return original;
                }
            }
        }
    } else if (original instanceof IAdaptable) {
        IAdaptable adaptable = (IAdaptable) original;
        IJavaElement je = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
        if (je != null && je.exists())
            return je;

        IResource r = (IResource) adaptable.getAdapter(IResource.class);
        if (r != null) {
            je = JavaCore.create(r);
            if (je != null && je.exists())
                return je;
            else
                return r;
        }
    }
    return original;
}

From source file:com.redhat.ceylon.eclipse.code.explorer.WorkingSetAwareContentProvider.java

License:Open Source License

private void processResource(IResource resource, Collection<IAdaptable> result) {
    IJavaElement elem = JavaCore.create(resource);
    if (elem != null && elem.exists()) {
        result.add(elem);//  ww  w . j a va2s.c  o  m
    } else {
        result.add(resource);
    }
}