Search in sources :

Example 6 with IJavaModel

use of org.eclipse.jdt.core.IJavaModel in project liferay-ide by liferay.

the class ProjectSelectionDialog method createDialogArea.

/**
 * (non-Javadoc) Method declared on Dialog.
 */
protected Control createDialogArea(Composite parent) {
    // page group
    Composite composite = (Composite) super.createDialogArea(parent);
    Font font = parent.getFont();
    composite.setFont(font);
    createMessageArea(composite);
    fTableViewer = CheckboxTableViewer.newCheckList(composite, SWT.BORDER);
    fTableViewer.addPostSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            updateOKButtonState(event);
        }
    });
    addSelectionButtons(composite);
    GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
    data.heightHint = _sizingSelectionWidgetHeight;
    data.widthHint = _sizingSelectionWidgetWidth;
    fTableViewer.getTable().setLayoutData(data);
    fTableViewer.setLabelProvider(new JavaElementLabelProvider());
    fTableViewer.setContentProvider(getContentProvider());
    fTableViewer.setComparator(new JavaElementComparator());
    fTableViewer.getControl().setFont(font);
    updateFilter(true);
    IJavaModel input = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
    fTableViewer.setInput(input);
    initialize();
    _selectionChanged(new Object[0]);
    Dialog.applyDialogFont(composite);
    return composite;
}
Also used : Composite(org.eclipse.swt.widgets.Composite) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) GridData(org.eclipse.swt.layout.GridData) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) JavaElementComparator(org.eclipse.jdt.ui.JavaElementComparator) Font(org.eclipse.swt.graphics.Font) JavaElementLabelProvider(org.eclipse.jdt.ui.JavaElementLabelProvider) IJavaModel(org.eclipse.jdt.core.IJavaModel)

Example 7 with IJavaModel

use of org.eclipse.jdt.core.IJavaModel in project mdw-designer by CenturyLinkCloud.

the class JavaSourceHyperlink method getJavaElement.

/**
 * find the source element in the project
 *
 * @param type
 * @return the source element
 * @throws JavaModelException
 * @throws CoreException
 */
protected Object getJavaElement(String type) throws JavaModelException, CoreException {
    Object element = null;
    if (project == null) {
        cantFindSourceWarning();
        return null;
    }
    if (element == null) {
        QualifiedName qName = new QualifiedName(MdwPlugin.getPluginId(), "MdwSourceLookupAllJavaProjects");
        String setting = project.getProject().getPersistentProperty(qName);
        boolean check = (setting != null && Boolean.valueOf(setting));
        if (!check) {
            String msg = "Can't locate source code for:\n" + getTypeName() + (project == null ? "" : " in project " + project.getProject().getName());
            String toggleMessage = "Look in all workspace Java projects for this deployment.";
            MessageDialogWithToggle dialog = MessageDialogWithToggle.openInformation(MdwPlugin.getShell(), "Java Source Lookup", msg, toggleMessage, false, null, null);
            check = dialog.getToggleState();
            project.getProject().setPersistentProperty(qName, String.valueOf(check));
        }
        if (check) {
            IJavaModel javaModel = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
            for (IJavaProject javaProj : javaModel.getJavaProjects()) {
                Object javaElem = getJavaElement(javaProj, type);
                if (javaElem != null) {
                    element = javaElem;
                    break;
                }
            }
        }
    }
    return element;
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) QualifiedName(org.eclipse.core.runtime.QualifiedName) MessageDialogWithToggle(org.eclipse.jface.dialogs.MessageDialogWithToggle) IJavaModel(org.eclipse.jdt.core.IJavaModel)

Example 8 with IJavaModel

use of org.eclipse.jdt.core.IJavaModel in project eclipse-integration-commons by spring-projects.

the class JdtUtils method getAllDependingJavaProjects.

/**
 * Checks if the given <code>type</code> implements/extends
 * <code>className</code>.
 */
// public static boolean doesImplement(IResource resource, IType type,
// String className) {
// if (resource == null || type == null || className == null) {
// return false;
// }
// if (className.startsWith("java.") || className.startsWith("javax.")) {
// try {
// ClassLoader cls = getClassLoader(resource.getProject(), null);
// Class<?> typeClass = cls.loadClass(type.getFullyQualifiedName('$'));
// Class<?> interfaceClass = cls.loadClass(className);
// return typeClass.equals(interfaceClass) ||
// interfaceClass.isAssignableFrom(typeClass);
// }
// catch (Throwable e) {
// // ignore this and fall back to JDT does implement checks
// }
// }
// return doesImplementWithJdt(resource, type, className);
// }
// public static IType getAjdtType(IProject project, String className) {
// IJavaProject javaProject = getJavaProject(project);
// if (IS_AJDT_PRESENT && javaProject != null && className != null) {
// 
// try {
// IType type = null;
// 
// // First look for the type in the project
// if (isAjdtProject(project)) {
// type = AjdtUtils.getAjdtType(project, className);
// if (type != null) {
// return type;
// }
// }
// 
// // Then look for the type in the referenced Java projects
// for (IProject refProject : project.getReferencedProjects()) {
// if (isAjdtProject(refProject)) {
// type = AjdtUtils.getAjdtType(refProject, className);
// if (type != null) {
// return type;
// }
// }
// }
// }
// catch (CoreException e) {
// StatusHandler.log(new Status(IStatus.ERROR, CorePlugin.PLUGIN_ID,
// "Error getting Java type '"
// + className + "'", e));
// }
// }
// return null;
// }
public static List<IJavaProject> getAllDependingJavaProjects(IJavaProject project) {
    List<IJavaProject> javaProjects = new ArrayList<IJavaProject>();
    IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
    if (model != null) {
        try {
            String[] names = project.getRequiredProjectNames();
            IJavaProject[] projects = model.getJavaProjects();
            for (IJavaProject project2 : projects) {
                for (String name2 : names) {
                    String name = project2.getProject().getName();
                    if (name.equals(name2)) {
                        javaProjects.add(project2);
                    }
                }
            }
        } catch (JavaModelException exception) {
        }
    }
    return javaProjects;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) ArrayList(java.util.ArrayList) IJavaModel(org.eclipse.jdt.core.IJavaModel)

Example 9 with IJavaModel

use of org.eclipse.jdt.core.IJavaModel in project che by eclipse.

the class DeltaProcessingState method getRootInfos.

private HashMap[] getRootInfos(boolean usePreviousSession) {
    HashMap newRoots = new HashMap();
    HashMap newOtherRoots = new HashMap();
    HashMap newSourceAttachments = new HashMap();
    HashMap newProjectDependencies = new HashMap();
    IJavaModel model = manager.getJavaModel();
    IJavaProject[] projects;
    try {
        projects = model.getJavaProjects();
    } catch (JavaModelException e) {
        // nothing can be done
        return null;
    }
    for (int i = 0, length = projects.length; i < length; i++) {
        JavaProject project = (JavaProject) projects[i];
        IClasspathEntry[] classpath;
        try {
            //				if (usePreviousSession) {
            //					PerProjectInfo perProjectInfo = project.getPerProjectInfo();
            //					project.resolveClasspath(perProjectInfo, true/*use previous session values*/, false/*don't add classpath change*/);
            //					classpath = perProjectInfo.resolvedClasspath;
            //				} else {
            classpath = project.getResolvedClasspath();
        //				}
        } catch (JavaModelException e) {
            // continue with next project
            continue;
        }
        for (int j = 0, classpathLength = classpath.length; j < classpathLength; j++) {
            IClasspathEntry entry = classpath[j];
            if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                // TODO (jerome) reuse handle
                IJavaProject key = model.getJavaProject(entry.getPath().segment(0));
                IJavaProject[] dependents = (IJavaProject[]) newProjectDependencies.get(key);
                if (dependents == null) {
                    dependents = new IJavaProject[] { project };
                } else {
                    int dependentsLength = dependents.length;
                    System.arraycopy(dependents, 0, dependents = new IJavaProject[dependentsLength + 1], 0, dependentsLength);
                    dependents[dependentsLength] = project;
                }
                newProjectDependencies.put(key, dependents);
                continue;
            }
            // root path
            IPath path = entry.getPath();
            if (newRoots.get(path) == null) {
                newRoots.put(path, new DeltaProcessor.RootInfo(project, path, ((ClasspathEntry) entry).fullInclusionPatternChars(), ((ClasspathEntry) entry).fullExclusionPatternChars(), entry.getEntryKind()));
            } else {
                ArrayList rootList = (ArrayList) newOtherRoots.get(path);
                if (rootList == null) {
                    rootList = new ArrayList();
                    newOtherRoots.put(path, rootList);
                }
                rootList.add(new DeltaProcessor.RootInfo(project, path, ((ClasspathEntry) entry).fullInclusionPatternChars(), ((ClasspathEntry) entry).fullExclusionPatternChars(), entry.getEntryKind()));
            }
            // source attachment path
            if (entry.getEntryKind() != IClasspathEntry.CPE_LIBRARY)
                continue;
            String propertyString = null;
            //				try {
            //					propertyString = Util.getSourceAttachmentProperty(path);
            //				} catch (JavaModelException e) {
            //					e.printStackTrace();
            //				}
            IPath sourceAttachmentPath;
            if (propertyString != null) {
                int index = propertyString.lastIndexOf(PackageFragmentRoot.ATTACHMENT_PROPERTY_DELIMITER);
                sourceAttachmentPath = (index < 0) ? new Path(propertyString) : new Path(propertyString.substring(0, index));
            } else {
                sourceAttachmentPath = entry.getSourceAttachmentPath();
            }
            if (sourceAttachmentPath != null) {
                newSourceAttachments.put(sourceAttachmentPath, path);
            }
        }
    }
    return new HashMap[] { newRoots, newOtherRoots, newSourceAttachments, newProjectDependencies };
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList) IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IJavaModel(org.eclipse.jdt.core.IJavaModel)

Example 10 with IJavaModel

use of org.eclipse.jdt.core.IJavaModel in project che by eclipse.

the class JavaSearchScope method add.

/**
     * Add a path to current java search scope or all project fragment roots if null.
     * Use project resolved classpath to retrieve and store access restriction on each classpath entry.
     * Recurse if dependent projects are found.
     * @param javaProject Project used to get resolved classpath entries
     * @param pathToAdd Path to add in case of single element or null if user want to add all project package fragment roots
     * @param includeMask Mask to apply on classpath entries
     * @param projectsToBeAdded Set to avoid infinite recursion
     * @param visitedProjects Set to avoid adding twice the same project
     * @param referringEntry Project raw entry in referring project classpath
     * @throws JavaModelException May happen while getting java model info
     */
void add(JavaProject javaProject, IPath pathToAdd, int includeMask, HashSet projectsToBeAdded, HashSet visitedProjects, IClasspathEntry referringEntry) throws JavaModelException {
    IProject project = javaProject.getProject();
    if (!project.isAccessible() || !visitedProjects.add(project))
        return;
    IPath projectPath = project.getFullPath();
    String projectPathString = projectPath.toString();
    addEnclosingProjectOrJar(projectPath);
    IClasspathEntry[] entries = javaProject.getResolvedClasspath();
    IJavaModel model = javaProject.getJavaModel();
    JavaModelManager.PerProjectInfo perProjectInfo = javaProject.getPerProjectInfo();
    for (int i = 0, length = entries.length; i < length; i++) {
        IClasspathEntry entry = entries[i];
        AccessRuleSet access = null;
        ClasspathEntry cpEntry = (ClasspathEntry) entry;
        if (referringEntry != null) {
            // Source folder are implicitly exported.
            if (!entry.isExported() && entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
                continue;
            }
            cpEntry = cpEntry.combineWith((ClasspathEntry) referringEntry);
        //				cpEntry = ((ClasspathEntry)referringEntry).combineWith(cpEntry);
        }
        access = cpEntry.getAccessRuleSet();
        switch(entry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:
                IClasspathEntry rawEntry = null;
                Map rootPathToRawEntries = perProjectInfo.rootPathToRawEntries;
                if (rootPathToRawEntries != null) {
                    rawEntry = (IClasspathEntry) rootPathToRawEntries.get(entry.getPath());
                }
                if (rawEntry == null)
                    break;
                rawKind: switch(rawEntry.getEntryKind()) {
                    case IClasspathEntry.CPE_LIBRARY:
                    case IClasspathEntry.CPE_VARIABLE:
                        if ((includeMask & APPLICATION_LIBRARIES) != 0) {
                            IPath path = entry.getPath();
                            if (pathToAdd == null || pathToAdd.equals(path)) {
                                Object target = JavaModel.getTarget(path, false);
                                if (// case of an external folder
                                target instanceof IFolder)
                                    path = ((IFolder) target).getFullPath();
                                String pathToString = path.getDevice() == null ? path.toString() : path.toOSString();
                                //$NON-NLS-1$
                                add(projectPath.toString(), "", pathToString, false, /*not a package*/
                                access);
                                addEnclosingProjectOrJar(entry.getPath());
                            }
                        }
                        break;
                    case IClasspathEntry.CPE_CONTAINER:
                        IClasspathContainer container = JavaCore.getClasspathContainer(rawEntry.getPath(), javaProject);
                        if (container == null)
                            break;
                        switch(container.getKind()) {
                            case IClasspathContainer.K_APPLICATION:
                                if ((includeMask & APPLICATION_LIBRARIES) == 0)
                                    break rawKind;
                                break;
                            case IClasspathContainer.K_SYSTEM:
                            case IClasspathContainer.K_DEFAULT_SYSTEM:
                                if ((includeMask & SYSTEM_LIBRARIES) == 0)
                                    break rawKind;
                                break;
                            default:
                                break rawKind;
                        }
                        IPath path = entry.getPath();
                        if (pathToAdd == null || pathToAdd.equals(path)) {
                            Object target = JavaModel.getTarget(path, false);
                            if (// case of an external folder
                            target instanceof IFolder)
                                path = ((IFolder) target).getFullPath();
                            String pathToString = path.getDevice() == null ? path.toString() : path.toOSString();
                            //$NON-NLS-1$
                            add(projectPath.toString(), "", pathToString, false, /*not a package*/
                            access);
                            addEnclosingProjectOrJar(entry.getPath());
                        }
                        break;
                }
                break;
            case IClasspathEntry.CPE_PROJECT:
                if ((includeMask & REFERENCED_PROJECTS) != 0) {
                    IPath path = entry.getPath();
                    if (pathToAdd == null || pathToAdd.equals(path)) {
                        JavaProject referencedProject = (JavaProject) model.getJavaProject(path.toOSString());
                        if (!projectsToBeAdded.contains(referencedProject)) {
                            // do not recurse if depending project was used to create the scope
                            add(referencedProject, null, includeMask, projectsToBeAdded, visitedProjects, cpEntry);
                        }
                    }
                }
                break;
            case IClasspathEntry.CPE_SOURCE:
                if ((includeMask & SOURCES) != 0) {
                    IPath path = entry.getPath();
                    if (pathToAdd == null || pathToAdd.equals(path)) {
                        add(projectPath.toString(), Util.relativePath(path, projectPath.segmentCount()), projectPathString, false, /*not a package*/
                        access);
                    }
                }
                break;
        }
    }
}
Also used : JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) AccessRuleSet(org.eclipse.jdt.internal.compiler.env.AccessRuleSet) IProject(org.eclipse.core.resources.IProject) JavaModelManager(org.eclipse.jdt.internal.core.JavaModelManager) ClasspathEntry(org.eclipse.jdt.internal.core.ClasspathEntry) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IClasspathContainer(org.eclipse.jdt.core.IClasspathContainer) Map(java.util.Map) IJavaModel(org.eclipse.jdt.core.IJavaModel) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

IJavaModel (org.eclipse.jdt.core.IJavaModel)12 IJavaProject (org.eclipse.jdt.core.IJavaProject)9 JavaModelException (org.eclipse.jdt.core.JavaModelException)5 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)4 IProject (org.eclipse.core.resources.IProject)3 IPath (org.eclipse.core.runtime.IPath)3 IJavaElement (org.eclipse.jdt.core.IJavaElement)3 JavaProject (org.eclipse.jdt.internal.core.JavaProject)3 ArrayList (java.util.ArrayList)2 IFolder (org.eclipse.core.resources.IFolder)2 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 IFile (org.eclipse.core.resources.IFile)1 Path (org.eclipse.core.runtime.Path)1 QualifiedName (org.eclipse.core.runtime.QualifiedName)1 IClasspathContainer (org.eclipse.jdt.core.IClasspathContainer)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IField (org.eclipse.jdt.core.IField)1 IJavaElementDelta (org.eclipse.jdt.core.IJavaElementDelta)1