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.technophobia.substeps.junit.launcher.SubstepsLaunchConfigurationDelegate.java

License:Open Source License

private final IJavaElement getTestTarget(final ILaunchConfiguration configuration,
        final IJavaProject javaProject) throws CoreException {
    final String containerHandle = configuration
            .getAttribute(SubstepsLaunchConfigurationConstants.ATTR_TEST_CONTAINER, ""); //$NON-NLS-1$
    if (containerHandle.length() != 0) {
        final IJavaElement element = JavaCore.create(containerHandle);
        if (element == null || !element.exists()) {
            abort(SubstepsFeatureMessages.SubstepsLaunchConfigurationDelegate_error_input_element_deosn_not_exist,
                    null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE);
        }/*  w w  w .j a va  2s  .  c  om*/
        return element;
    }
    final IJavaElement element = getMainElementFromProject(configuration, javaProject);
    if (element != null) {
        return element;
    }

    abort(SubstepsFeatureMessages.SubstepsLaunchConfigurationDelegate_input_type_does_not_exist, null,
            IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE);
    return null; // not reachable
}

From source file:com.tsc9526.monalisa.plugin.eclipse.console.HyperLink.java

License:Open Source License

@Override
public void linkActivated() {
    if (url != null) {
        try {//  w w w .j ava 2 s. c om
            String protocol = url.getProtocol();
            if (protocol.equals("file")) {
                String ref = url.getRef();

                IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(url.toURI());
                if (files.length > 0) {
                    for (int i = 0; i < files.length; i++) {
                        IFile curr = files[0];
                        IJavaElement element = JavaCore.create(curr);
                        if (element != null && element.exists()) {
                            if (ref != null && ref.length() > 0 && element instanceof ICompilationUnit) {
                                String[] sv = ref.split(",");
                                String name = sv[0];

                                String[] signatures = null;
                                if (sv.length > 1) {
                                    signatures = new String[sv.length - 1];
                                    for (int x = 1; x < sv.length; x++) {
                                        signatures[x - 1] = Signature.createTypeSignature(sv[x], false);
                                    }
                                }

                                IType type = ((ICompilationUnit) element).getTypes()[0];
                                IMethod method = type.getMethod(name, signatures);
                                if (method != null && method.exists()) {
                                    JavaUI.openInEditor(method, true, true);
                                    return;
                                }
                            }

                            JavaUI.openInEditor(element, true, true);
                            return;
                        }
                    }
                }
            }

            PlatformUI.getWorkbench().getBrowserSupport().createBrowser(text).openURL(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:de.devboost.eclipse.jdtutilities.JDTUtility.java

License:Open Source License

/**
 * Returns the compilation unit contained in the given file or
 * <code>null</code> if the file does not contain a compilation unit or if
 * the file does not exist or if the JDT is not running.
 *//* www . j  a v  a2 s.  c  o  m*/
public ICompilationUnit getCompilationUnit(IFile file) {
    // determine compilation unit that is contained in the file
    IJavaElement javaElement = JavaCore.create(file);
    if (javaElement == null) {
        return null;
    }

    if (!javaElement.exists()) {
        return null;
    }

    if (javaElement instanceof ICompilationUnit) {
        return (ICompilationUnit) javaElement;
    }
    return null;
}

From source file:de.devboost.eclipse.jdtutilities.JDTUtility.java

License:Open Source License

/**
 * Returns the type that is contained in the given file. The file must be a
 * Java class file. If the file does not exists or if it is not a class
 * file, <code>null</code> is returned.
 *//*from  w  ww .  j  a v a 2s  .co m*/
public IType getType(IFile file) {
    IJavaElement javaElement = JavaCore.create(file);
    if (!javaElement.exists()) {
        return null;
    }

    if (javaElement instanceof IClassFile) {
        IClassFile classFile = (IClassFile) javaElement;
        return classFile.getType();
    }

    return null;
}

From source file:edu.buffalo.cse.green.editor.DiagramEditor.java

License:Open Source License

/**
 * Finds all relationships that have the given element as their source.
 * //ww  w . java2 s .co m
 * @param element - The element to find relationships for.
 */
private void findRelationships(IJavaElement element) {
    long modified;

    // if the element contains errors, quit
    try {
        if (!element.exists() || !element.isStructureKnown())
            return;
    } catch (JavaModelException e) {
        e.printStackTrace();
        return;
    }

    CompilationUnit cu;
    String id = element.getHandleIdentifier();

    // generate AST if necessary - check modification stamp
    Long modifiedStore = _cuMap.getModificationStamp(id);
    IResource resource = element.getResource();

    if (resource == null) {
        if (_cuMap.getCompilationUnit(id) != null) {
            modifiedStore = new Long(0);
        }

        modified = 0;
    } else {
        modified = resource.getModificationStamp();
    }

    // if there isn't an up-to-date AST, create one
    if ((modifiedStore == null) || (modified != modifiedStore)) {
        ASTParser parser = ASTParser.newParser(AST.JLS3);
        parser.setResolveBindings(true);

        if (element instanceof ICompilationUnit) {
            parser.setSource((ICompilationUnit) element);
        } else if (element instanceof IClassFile) {
            // only search through the class if it has source code attached
            IClassFile classFile = (IClassFile) element;

            try {
                if (classFile.getSource() == null) {
                    return;
                }
            } catch (JavaModelException e) {
                e.printStackTrace();
            }

            parser.setSource(classFile);
        } else {
            GreenException.illegalOperation("Illegal element type: " + element.getClass());
        }

        cu = (CompilationUnit) parser.createAST(null);
        _cuMap.put(element, cu);
    } else {
        cu = _cuMap.getCompilationUnit(id);
    }

    // run the recognizers
    for (Class klass : PlugIn.getRelationships()) {
        RelationshipRecognizer recognizer = PlugIn.getRelationshipGroup(klass).getRecognizer();

        // run the recognizer
        recognizer.run(cu, getRootModel().getRelationshipCache());
    }
}

From source file:edu.illinois.compositerefactorings.refactorings.copymembertosubtype.CopyMemberToSubtypeRefactoringProcessor.java

License:Open Source License

/**
 * {@link org.eclipse.jdt.internal.corext.refactoring.structure.PushDownRefactoringProcessor#initialize(JavaRefactoringArguments)}
 *///from  w ww.ja  v a 2 s  .c o m
private RefactoringStatus initialize(JavaRefactoringArguments extended) {
    String handle = extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
    if (handle != null) {
        final IJavaElement element = JavaRefactoringDescriptorUtil.handleToElement(extended.getProject(),
                handle, false);
        if (element == null || !element.exists() || element.getElementType() != IJavaElement.TYPE) {
            return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getProcessorName(),
                    IJavaRefactorings.PUSH_DOWN);
        } else {
            fCachedDeclaringType = (IType) element;
        }
    }
    final List<IJavaElement> elements = new ArrayList<IJavaElement>();
    final List<MemberActionInfo> infos = new ArrayList<MemberActionInfo>();
    String attribute = JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + 1;
    final RefactoringStatus status = new RefactoringStatus();
    handle = extended.getAttribute(attribute);
    final IJavaElement element = JavaRefactoringDescriptorUtil.handleToElement(extended.getProject(), handle,
            false);
    if (element == null || !element.exists()) {
        status.merge(JavaRefactoringDescriptorUtil.createInputWarningStatus(element, getProcessorName(),
                IJavaRefactorings.PUSH_DOWN));
    } else {
        elements.add(element);
    }
    infos.add(MemberActionInfo.create((IMember) element, MemberActionInfo.PUSH_DOWN_ACTION));
    fMembersToMove = elements.toArray(new IMember[elements.size()]);
    fMemberInfos = infos.toArray(new MemberActionInfo[infos.size()]);
    String subtypeHandle = extended.getAttribute(CopyMemberToSubtypeDescriptor.ATTRIBUTE_SUBTYPE + 1);
    final IJavaElement subtype = JavaRefactoringDescriptorUtil.handleToElement(extended.getProject(),
            subtypeHandle, false);
    if (subtype == null || !subtype.exists()) {
        status.merge(JavaRefactoringDescriptorUtil.createInputWarningStatus(subtype, getProcessorName(),
                IJavaRefactorings.PUSH_DOWN));
    } else {
        fSubtype = (IType) subtype;
    }
    if (!status.isOK()) {
        return status;
    }
    return new RefactoringStatus();
}

From source file:edu.illinois.compositerefactorings.refactorings.usesupertypeininstanceof.UseSuperTypeInInstanceOfRefactoring.java

License:Open Source License

private final RefactoringStatus initialize(JavaRefactoringArguments extended) {
    String handle = extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
    if (handle != null) {
        final IJavaElement element = JavaRefactoringDescriptorUtil.handleToElement(extended.getProject(),
                handle, false);//w  w  w .  jav a 2 s  . c o  m
        if (element == null || !element.exists() || element.getElementType() != IJavaElement.TYPE)
            return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(),
                    UseSuperTypeInInstanceOfDescriptor.ID);
        else
            fSubType = (IType) element;
    } else
        return RefactoringStatus.createFatalErrorStatus(
                Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
                        JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT));
    handle = extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + 1);
    if (handle != null) {
        final IJavaElement element = JavaRefactoringDescriptorUtil.handleToElement(extended.getProject(),
                handle, false);
        if (element == null || !element.exists() || element.getElementType() != IJavaElement.TYPE)
            return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(),
                    UseSuperTypeInInstanceOfDescriptor.ID);
        else
            fSuperType = (IType) element;
    } else
        return RefactoringStatus.createFatalErrorStatus(
                Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
                        JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + 1));
    return new RefactoringStatus();
}

From source file:es.bsc.servicess.ide.actions.BuildServiceAction.java

License:Apache License

/** Get a Java Element from a selection
 * @param selection Selected item//from www .j  a v  a2  s  .  c o  m
 * @return Java Element
 */
private IJavaElement getInitialJavaElement(IStructuredSelection selection) {
    IJavaElement jelem = null;
    if (selection != null && !selection.isEmpty()) {
        Object selectedElement = selection.getFirstElement();
        if (selectedElement instanceof IAdaptable) {
            IAdaptable adaptable = (IAdaptable) selectedElement;

            jelem = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
            if (jelem == null || !jelem.exists()) {
                jelem = null;
                IResource resource = (IResource) adaptable.getAdapter(IResource.class);
                if (resource != null && resource.getType() != IResource.ROOT) {
                    while (jelem == null && resource.getType() != IResource.PROJECT) {
                        resource = resource.getParent();
                        jelem = (IJavaElement) resource.getAdapter(IJavaElement.class);
                    }
                    if (jelem == null) {
                        jelem = JavaCore.create(resource); // java project
                    }
                }
            }
        }
    }
    if (jelem == null) {
        IWorkbenchPart part = JavaPlugin.getActivePage().getActivePart();
        if (part instanceof ContentOutline) {
            part = JavaPlugin.getActivePage().getActiveEditor();
        }

        if (part instanceof IViewPartInputProvider) {
            Object elem = ((IViewPartInputProvider) part).getViewPartInput();
            if (elem instanceof IJavaElement) {
                jelem = (IJavaElement) elem;
            }
        }
    }
    return jelem;
}

From source file:es.bsc.servicess.ide.actions.DeployAction.java

License:Apache License

private IJavaElement getInitialJavaElement(IStructuredSelection selection) {
    IJavaElement jelem = null;
    if (selection != null && !selection.isEmpty()) {
        Object selectedElement = selection.getFirstElement();
        if (selectedElement instanceof IAdaptable) {
            IAdaptable adaptable = (IAdaptable) selectedElement;

            jelem = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
            if (jelem == null || !jelem.exists()) {
                jelem = null;/*from  w w w  .j a va  2 s . c  o  m*/
                IResource resource = (IResource) adaptable.getAdapter(IResource.class);
                if (resource != null && resource.getType() != IResource.ROOT) {
                    while (jelem == null && resource.getType() != IResource.PROJECT) {
                        resource = resource.getParent();
                        jelem = (IJavaElement) resource.getAdapter(IJavaElement.class);
                    }
                    if (jelem == null) {
                        jelem = JavaCore.create(resource); // java project
                    }
                }
            }
        }
    }
    if (jelem == null) {
        IWorkbenchPart part = JavaPlugin.getActivePage().getActivePart();
        if (part instanceof ContentOutline) {
            part = JavaPlugin.getActivePage().getActiveEditor();
        }

        if (part instanceof IViewPartInputProvider) {
            Object elem = ((IViewPartInputProvider) part).getViewPartInput();
            if (elem instanceof IJavaElement) {
                jelem = (IJavaElement) elem;
            }
        }
    }
    return jelem;
}

From source file:navclus.userinterface.monitor.selections.SelectionKeeper.java

License:Open Source License

public void addSelection(IJavaElement element) {
    try {//from w w  w  . ja  va  2 s .co m
        if (element == null) {
            return;
        }
        if (!element.exists()) {
            return;
        }
        if (!checkElementType(element)) {
            return;
        }
        if (selectionList.contain(element)) {
            return;
        }

        boolean bAdded = selectionList.add(element);
        if (bAdded == false)
            return;
        System.out.println("added: " + element.getElementName());
        printSelectionIfTrigger();
        //         printSelectionIfFull();
    } catch (Exception e) {
        System.err.println("Error in SelectionKeeper.addSelection():" + e.getMessage());
        e.printStackTrace();
    }
}