Example usage for org.eclipse.jdt.core IJavaProject findType

List of usage examples for org.eclipse.jdt.core IJavaProject findType

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject findType.

Prototype

IType findType(String packageName, String typeQualifiedName) throws JavaModelException;

Source Link

Document

Returns the first type (excluding secondary types) found following this project's classpath with the given package name and type qualified name or null if none is found.

Usage

From source file:bndtools.editor.components.MethodProposalProvider.java

License:Open Source License

@Override
public List<IContentProposal> doGenerateProposals(String contents, int position) {
    final String prefix = contents.substring(0, position);
    final List<IContentProposal> result = new ArrayList<IContentProposal>();

    try {/*  ww w .  j  av a2  s  .  c om*/
        IRunnableWithProgress runnable = new IRunnableWithProgress() {
            public void run(IProgressMonitor monitor) throws InvocationTargetException {
                SubMonitor progress = SubMonitor.convert(monitor, 10);

                try {
                    IJavaProject project = searchContext.getJavaProject();
                    String targetTypeName = searchContext.getTargetTypeName();
                    IType targetType = project.findType(targetTypeName, progress.newChild(1));

                    if (targetType == null)
                        return;

                    ITypeHierarchy hierarchy = targetType.newSupertypeHierarchy(progress.newChild(5));
                    IType[] classes = hierarchy.getAllClasses();
                    progress.setWorkRemaining(classes.length);
                    for (IType clazz : classes) {
                        IMethod[] methods = clazz.getMethods();
                        for (IMethod method : methods) {
                            if (method.getElementName().toLowerCase().startsWith(prefix)) {
                                // String[] parameterTypes = method.getParameterTypes();
                                // TODO check parameter type
                                result.add(new MethodContentProposal(method));
                            }
                        }
                        progress.worked(1);
                    }
                } catch (JavaModelException e) {
                    throw new InvocationTargetException(e);
                }
            }
        };
        IRunnableContext runContext = searchContext.getRunContext();
        if (runContext != null) {
            runContext.run(false, false, runnable);
        } else {
            runnable.run(new NullProgressMonitor());
        }
        return result;
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return Collections.emptyList();
}

From source file:ca.ecliptical.pde.ds.search.DescriptorQueryParticipant.java

License:Open Source License

private void searchModel(DSModel dsModel, IJavaProject javaProject, Object matchElement,
        IProgressMonitor monitor) throws CoreException {
    IDSComponent component = dsModel.getDSComponent();
    if (component == null) {
        if (debug.isDebugging())
            debug.trace(String.format("No component definition found in file: %s", //$NON-NLS-1$
                    dsModel.getUnderlyingResource() == null ? dsModel.getInstallLocation()
                            : dsModel.getUnderlyingResource().getFullPath())); // TODO de-uglify!

        return;// w  ww  .  ja v a  2  s  .  c om
    }

    IDSImplementation impl = component.getImplementation();
    if (impl == null) {
        if (debug.isDebugging())
            debug.trace(String.format("No component implementation found in file: %s", //$NON-NLS-1$
                    dsModel.getUnderlyingResource() == null ? dsModel.getInstallLocation()
                            : dsModel.getUnderlyingResource().getFullPath())); // TODO de-uglify!

        return;
    }

    IType implClassType = null;
    String implClassName = impl.getClassName();
    if (implClassName != null)
        implClassType = javaProject.findType(implClassName, monitor);

    if ((searchElement != null && searchElement.getElementType() == IJavaElement.TYPE)
            || searchFor == IJavaSearchConstants.TYPE || searchFor == IJavaSearchConstants.CLASS
            || searchFor == IJavaSearchConstants.CLASS_AND_INTERFACE
            || searchFor == IJavaSearchConstants.CLASS_AND_ENUM || searchFor == IJavaSearchConstants.UNKNOWN) {
        // match specific type references
        if (matches(searchElement, searchPattern, implClassType))
            reportMatch(requestor, impl.getDocumentAttribute(IDSConstants.ATTRIBUTE_IMPLEMENTATION_CLASS),
                    matchElement);
    }

    if ((searchElement != null && searchElement.getElementType() == IJavaElement.TYPE)
            || searchFor == IJavaSearchConstants.TYPE || searchFor == IJavaSearchConstants.CLASS
            || searchFor == IJavaSearchConstants.CLASS_AND_INTERFACE
            || searchFor == IJavaSearchConstants.CLASS_AND_ENUM || searchFor == IJavaSearchConstants.INTERFACE
            || searchFor == IJavaSearchConstants.INTERFACE_AND_ANNOTATION
            || searchFor == IJavaSearchConstants.UNKNOWN) {
        IDSService service = component.getService();
        if (service != null) {
            IDSProvide[] provides = service.getProvidedServices();
            if (provides != null) {
                for (IDSProvide provide : provides) {
                    String ifaceName = provide.getInterface();
                    IType ifaceType = javaProject.findType(ifaceName, monitor);
                    if (matches(searchElement, searchPattern, ifaceType))
                        reportMatch(requestor,
                                provide.getDocumentAttribute(IDSConstants.ATTRIBUTE_PROVIDE_INTERFACE),
                                matchElement);
                }
            }
        }

        IDSReference[] references = component.getReferences();
        if (references != null) {
            for (IDSReference reference : references) {
                String ifaceName = reference.getReferenceInterface();
                IType ifaceType = javaProject.findType(ifaceName, monitor);
                if (matches(searchElement, searchPattern, ifaceType))
                    reportMatch(requestor,
                            reference.getDocumentAttribute(IDSConstants.ATTRIBUTE_REFERENCE_INTERFACE),
                            matchElement);
            }
        }
    }

    if ((searchElement != null && searchElement.getElementType() == IJavaElement.METHOD)
            || searchFor == IJavaSearchConstants.METHOD || searchFor == IJavaSearchConstants.UNKNOWN) {
        // match specific method references
        String activate = component.getActivateMethod();
        if (activate == null)
            activate = "activate"; //$NON-NLS-1$

        IMethod activateMethod = findActivateMethod(implClassType, activate, monitor);
        if (matches(searchElement, searchPattern, activateMethod)) {
            if (component.getActivateMethod() == null)
                reportMatch(requestor, component, matchElement);
            else
                reportMatch(requestor,
                        component.getDocumentAttribute(IDSConstants.ATTRIBUTE_COMPONENT_ACTIVATE),
                        matchElement);
        }

        String modified = component.getModifiedMethod();
        if (modified != null) {
            IMethod modifiedMethod = findActivateMethod(implClassType, modified, monitor);
            if (matches(searchElement, searchPattern, modifiedMethod))
                reportMatch(requestor,
                        component.getDocumentAttribute(IDSConstants.ATTRIBUTE_COMPONENT_MODIFIED),
                        matchElement);
        }

        String deactivate = component.getDeactivateMethod();
        if (deactivate == null)
            deactivate = "deactivate"; //$NON-NLS-1$

        IMethod deactivateMethod = findDeactivateMethod(implClassType, deactivate, monitor);
        if (matches(searchElement, searchPattern, deactivateMethod)) {
            if (component.getDeactivateMethod() == null)
                reportMatch(requestor, component, matchElement);
            else
                reportMatch(requestor,
                        component.getDocumentAttribute(IDSConstants.ATTRIBUTE_COMPONENT_DEACTIVATE),
                        matchElement);
        }

        IDSReference[] references = component.getReferences();
        if (references != null) {
            for (IDSReference reference : references) {
                String refIface = reference.getReferenceInterface();
                if (refIface == null) {
                    if (debug.isDebugging())
                        debug.trace(String.format("No reference interface specified: %s", reference)); //$NON-NLS-1$

                    continue;
                }

                String bind = reference.getReferenceBind();
                if (bind != null) {
                    IMethod bindMethod = findBindMethod(implClassType, bind, refIface, monitor);
                    if (matches(searchElement, searchPattern, bindMethod))
                        reportMatch(requestor,
                                reference.getDocumentAttribute(IDSConstants.ATTRIBUTE_REFERENCE_BIND),
                                matchElement);
                }

                String unbind = reference.getReferenceUnbind();
                if (unbind != null) {
                    IMethod unbindMethod = findBindMethod(implClassType, unbind, refIface, monitor);
                    if (matches(searchElement, searchPattern, unbindMethod))
                        reportMatch(requestor,
                                reference.getDocumentAttribute(IDSConstants.ATTRIBUTE_REFERENCE_UNBIND),
                                matchElement);
                }

                String updated = reference.getXMLAttributeValue("updated"); //$NON-NLS-1$
                if (updated != null) {
                    IMethod updatedMethod = findUpdatedMethod(implClassType, updated, monitor);
                    if (matches(searchElement, searchPattern, updatedMethod))
                        reportMatch(requestor, reference.getDocumentAttribute("updated"), matchElement); //$NON-NLS-1$
                }
            }
        }
    }
}

From source file:ca.ecliptical.pde.ds.search.DescriptorQueryParticipant.java

License:Open Source License

private IType findSuperclassType(IType type, IJavaProject project, IProgressMonitor monitor)
        throws JavaModelException, IllegalArgumentException {
    String superSig = type.getSuperclassTypeSignature();
    if (superSig == null)
        return null;

    String superName = Signature.toString(Signature.getTypeErasure(superSig));
    if (type.isResolved())
        return project.findType(superName, monitor);

    String[][] resolvedNames = type.resolveType(superName);
    if (resolvedNames == null || resolvedNames.length == 0)
        return null;

    return project.findType(resolvedNames[0][0], resolvedNames[0][1], monitor);
}

From source file:cc.kave.eclipse.namefactory.astparser.PluginAstParser.java

License:Apache License

private ICompilationUnit getCompilationunit(String project, String qualifiedName) {
    String[] split = qualifiedName.split(";");

    for (IJavaProject iJavaProject : javaProjects) {
        if (iJavaProject.getElementName().equals(project)) {
            try {
                return iJavaProject.findType(split[0], split[1]).getCompilationUnit();
            } catch (JavaModelException e) {
                e.printStackTrace();//from w  w  w  . j a  va  2s .c o m
            }
        }
    }
    return null;
}

From source file:com.astamuse.asta4d.ide.eclipse.util.JdtUtils.java

License:Open Source License

/**
 * Returns the corresponding Java type for given full-qualified class name.
 * //from  w ww.j  a  v a 2s.c  om
 * @param project
 *            the JDT project the class belongs to
 * @param className
 *            the full qualified class name of the requested Java type
 * @return the requested Java type or null if the class is not defined or the project is not accessible
 */
public static IType getJavaType(IProject project, String className) {
    IJavaProject javaProject = JdtUtils.getJavaProject(project);

    if (className != null) {

        // For inner classes replace '$' by '.'
        String unchangedClassName = null;
        int pos = className.lastIndexOf('$');
        if (pos > 0) {
            unchangedClassName = className;
            className = className.replace('$', '.');
        }

        try {
            IType type = null;
            // First look for the type in the Java project
            if (javaProject != null) {
                type = javaProject.findType(className, new NullProgressMonitor());

                if (type == null && unchangedClassName != null) {
                    type = javaProject.findType(unchangedClassName, new NullProgressMonitor());
                }

                if (type != null) {
                    return type;
                }
            }

            // Then look for the type in the referenced Java projects
            for (IProject refProject : project.getReferencedProjects()) {
                IJavaProject refJavaProject = JdtUtils.getJavaProject(refProject);
                if (refJavaProject != null) {
                    type = refJavaProject.findType(className);

                    if (type == null && unchangedClassName != null) {
                        type = javaProject.findType(unchangedClassName, new NullProgressMonitor());
                    }

                    if (type != null) {
                        return type;
                    }
                }
            }

            // fall back and try to locate the class using AJDT
            return getAjdtType(project, className);
        } catch (CoreException e) {
            // SpringCore.log("Error getting Java type '" + className + "'", e);
        }
    }

    return null;
}

From source file:com.google.gdt.eclipse.designer.util.type.YesNoFilter.java

License:Open Source License

/**
 * Expects {"pkg1","class1", "pkg2", "class2"}
 *///w ww.j  av  a 2 s  .co  m
private List<String> getSubTypes(String[] types) {
    if (types == null) {
        return Collections.emptyList();
    }
    //
    IJavaProject javaProject = m_package.getJavaProject();
    List<String> subTypesList = new ArrayList<String>();
    for (int baseIndex = 0; baseIndex < types.length; baseIndex += 2) {
        try {
            IType baseType = javaProject.findType(types[baseIndex], types[baseIndex + 1]);
            if (baseType != null) {
                subTypesList.add(baseType.getFullyQualifiedName());
                // prepare sub-types
                IType[] subTypes;
                {
                    m_monitor.subTask(baseType.getFullyQualifiedName());
                    ITypeHierarchy th = baseType.newTypeHierarchy(javaProject, new NullProgressMonitor());
                    subTypes = th.getAllSubtypes(baseType);
                    m_monitor.worked(1);
                }
                //
                for (int subTypeIndex = 0; subTypeIndex < subTypes.length; subTypeIndex++) {
                    subTypesList.add(subTypes[subTypeIndex].getFullyQualifiedName());
                }
            }
        } catch (JavaModelException e) {
            DesignerPlugin.log(e);
        }
    }
    return subTypesList;
}

From source file:com.google.gwt.eclipse.core.uibinder.UiBinderUtilities.java

License:Open Source License

/**
 * Gets the GWT Widget type./*  w  ww .j a va 2 s  .  com*/
 *
 * @return the GWT Widget type
 */
public static IType getWidgetType(IJavaProject javaProject) throws JavaModelException, UiBinderException {
    IType widgetType = javaProject.findType(UiBinderConstants.GWT_USER_LIBRARY_UI_PACKAGE_NAME,
            UiBinderConstants.GWT_USER_LIBRARY_WIDGET_CLASS_NAME);
    if (widgetType == null) {
        throw new UiBinderException("Could not resolve the Widget type from the GWT user library.");
    }
    return widgetType;
}

From source file:com.iw.plugins.spindle.ui.wizards.fields.TypeDialogField.java

License:Mozilla Public License

protected IType resolveTypeName(IJavaProject jproject, String typeName) throws JavaModelException {
    IType type = null;/*from  w  w  w  .j av a2 s. c  o  m*/
    IPackageFragment currPack = null;
    if (packageChooser != null) {
        packageChooser.getPackageFragment();
    } else {
        // create/get one!
    }
    if (type == null && currPack != null) {
        String packName = currPack.getElementName();
        // search in own package
        if (!currPack.isDefaultPackage()) {
            type = jproject.findType(packName, typeName);
        }
        // search in java.lang
        if (type == null && !"java.lang".equals(packName)) {
            type = jproject.findType("java.lang", typeName);
        }
    }
    // search fully qualified
    if (type == null) {
        type = jproject.findType(typeName);
    }
    //}
    return type;
}

From source file:com.liferay.ide.project.ui.modules.NewLiferayComponentWizard.java

License:Open Source License

@Override
public void performPostFinish() {
    final NewLiferayComponentOp op = element().nearest(NewLiferayComponentOp.class);
    IProject currentProject = CoreUtil.getProject(op.getProjectName().content());

    if (currentProject == null) {
        return;//from   w w w  .ja v  a2 s.  c  o  m
    }

    IJavaProject javaProject = JavaCore.create(currentProject);

    if (javaProject == null) {
        return;
    }

    try {
        String componentClass = op.getComponentClassName().content();
        String packageName = op.getPackageName().text();

        final IType type = javaProject.findType(packageName, componentClass);

        if (type == null) {
            return;
        }

        final IFile classFile = (IFile) type.getResource();

        if (classFile != null) {
            Display.getCurrent().asyncExec(new Runnable() {

                public void run() {
                    try {
                        IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
                                .getActivePage();
                        IDE.openEditor(page, classFile, true);
                    } catch (Exception e) {
                        ProjectUI.logError(e);
                    }
                }
            });
        }
    } catch (Exception e) {
        ProjectUI.logError(e);
    }
}

From source file:com.technophobia.substeps.junit.action.OpenEditorAction.java

License:Open Source License

private IType internalFindType(final IJavaProject project, final String testClassName,
        final Set<IJavaProject> visitedProjects, final IProgressMonitor monitor) throws JavaModelException {
    try {/*w ww . ja  va 2 s  .  co m*/
        if (visitedProjects.contains(project))
            return null;
        monitor.beginTask("", 2); //$NON-NLS-1$
        IType type = project.findType(testClassName, new SubProgressMonitor(monitor, 1));
        if (type != null)
            return type;
        // fix for bug 87492: visit required projects explicitly to also
        // find not exported types
        visitedProjects.add(project);
        final IJavaModel javaModel = project.getJavaModel();
        final String[] requiredProjectNames = project.getRequiredProjectNames();
        final IProgressMonitor reqMonitor = new SubProgressMonitor(monitor, 1);
        reqMonitor.beginTask("", requiredProjectNames.length); //$NON-NLS-1$
        for (final String requiredProjectName : requiredProjectNames) {
            final IJavaProject requiredProject = javaModel.getJavaProject(requiredProjectName);
            if (requiredProject.exists()) {
                type = internalFindType(requiredProject, testClassName, visitedProjects,
                        new SubProgressMonitor(reqMonitor, 1));
                if (type != null)
                    return type;
            }
        }
        return null;
    } finally {
        monitor.done();
    }
}