Search in sources :

Example 1 with NameLookup

use of org.eclipse.jdt.internal.core.NameLookup in project xtext-eclipse by eclipse.

the class JdtTypeProvider method findPrimaryType.

/**
 * @since 2.9
 */
protected IType findPrimaryType(String packageName, String typeName) throws JavaModelException {
    JavaProject casted = (JavaProject) javaProject;
    NameLookup nameLookup = getNameLookup(casted);
    NameLookup.Answer answer = nameLookup.findType(typeName, packageName, false, NameLookup.ACCEPT_ALL, // do not consider secondary types
    false, // wait for indexes (in case we need to consider secondary types)
    true, false, /*don't check restrictions*/
    null);
    return answer == null ? null : answer.type;
}
Also used : NameLookup(org.eclipse.jdt.internal.core.NameLookup) IJavaProject(org.eclipse.jdt.core.IJavaProject) JavaProject(org.eclipse.jdt.internal.core.JavaProject)

Example 2 with NameLookup

use of org.eclipse.jdt.internal.core.NameLookup in project eclipse.pde by eclipse-pde.

the class Util method findSourceTypeinJavaProject.

/**
 * Tries to find SourceType of name typeName in the project. It returns null
 * if it cannot find SourceType of name typeName
 *
 * @param javaProject
 * @param typeName
 * @return
 */
public static IType findSourceTypeinJavaProject(IJavaProject javaProject, String typeName) {
    IType type = null;
    try {
        String pkgName = typeName.substring(0, typeName.lastIndexOf('.'));
        if (javaProject instanceof JavaProject) {
            JavaProject jp = (JavaProject) javaProject;
            NameLookup newNameLookup = null;
            try {
                newNameLookup = jp.newNameLookup(DefaultWorkingCopyOwner.PRIMARY);
            } catch (JavaModelException e) {
                ApiPlugin.log(e);
            }
            IPackageFragment[] findPackageFragment = newNameLookup.findPackageFragments(pkgName, false);
            for (IPackageFragment iJavaElement : findPackageFragment) {
                type = newNameLookup.findType(typeName.substring(typeName.lastIndexOf('.') + 1, typeName.length()), iJavaElement, false, NameLookup.ACCEPT_ALL);
                if (type instanceof SourceType) {
                    break;
                }
            }
        }
    } catch (Exception e) {
    // return null
    }
    return type;
}
Also used : NameLookup(org.eclipse.jdt.internal.core.NameLookup) JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) SourceType(org.eclipse.jdt.internal.core.SourceType) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) AssertionFailedException(org.eclipse.core.runtime.AssertionFailedException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) TransformerException(javax.xml.transform.TransformerException) JavaModelException(org.eclipse.jdt.core.JavaModelException) IOException(java.io.IOException) PatternSyntaxException(java.util.regex.PatternSyntaxException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IType(org.eclipse.jdt.core.IType)

Aggregations

IJavaProject (org.eclipse.jdt.core.IJavaProject)2 JavaProject (org.eclipse.jdt.internal.core.JavaProject)2 NameLookup (org.eclipse.jdt.internal.core.NameLookup)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 AssertionFailedException (org.eclipse.core.runtime.AssertionFailedException)1 CoreException (org.eclipse.core.runtime.CoreException)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 IType (org.eclipse.jdt.core.IType)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 SourceType (org.eclipse.jdt.internal.core.SourceType)1 SAXException (org.xml.sax.SAXException)1